package com.zksy.web.controller.manhole;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.domain.EquipmentBase;
import com.zksy.base.domain.EquipmentStatus;
import com.zksy.base.domain.EquipmentType;
import com.zksy.base.manhole.domain.ManholeData;
import com.zksy.base.manhole.service.ManholeDataService;
import com.zksy.base.service.EquipmentBaseService;
import com.zksy.base.service.EquipmentStatusService;
import com.zksy.base.service.EquipmentTypeService;
import com.zksy.common.annotation.Anonymous;
import com.zksy.common.core.controller.BaseController;
import com.zksy.common.core.domain.AjaxResult;
import com.zksy.common.core.domain.AjaxResult2;
import com.zksy.manhole.dto.in.EquipmentStatusInDTO;
import com.zksy.manhole.dto.in.ManholeDeviceListInDTO;
import com.zksy.manhole.dto.in.ManholeDevicePageInDTO;
import com.zksy.manhole.dto.out.EquipmentBaseOutDTO;
import com.zksy.manhole.dto.out.EquipmentStatusOutDTO;
import com.zksy.manhole.dto.out.ManholeDataOutDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 窨井盖监测系统-监测设备信息管理Controller
 * 
 * @author xianggx
 * @date 2026-05-06
 */
@RestController
@RequestMapping("/manhole/device")
@Api(tags = "窨井盖监测系统-监测设备信息管理")
public class ManholeDeviceController extends BaseController
{
    @Autowired
    private EquipmentBaseService equipmentBaseService;

    @Autowired
    private EquipmentStatusService equipmentStatusService;

    @Autowired
    private EquipmentTypeService equipmentTypeService;

    @Autowired
    private ManholeDataService manholeDataService;

    /**
     * 分页查询监测设备台账列表
     */
    @Anonymous
    @GetMapping(value = "/findByPage")
    @ApiOperation(value = "分页查询监测设备台账列表")
    public AjaxResult2<Page<EquipmentBaseOutDTO>> findByPage(@RequestBody ManholeDevicePageInDTO pageInDTO)
    {
        return AjaxResult2.success(equipmentBaseService.findManholeDeviceByPage(pageInDTO));
    }

    /**
     * 查询井盖监测设备列表
     */
    @Anonymous
    @GetMapping(value = "/getManholeDataList")
    @ApiOperation(value = "查询井盖监测设备列表")
    public AjaxResult2<List<EquipmentBaseOutDTO>> getManholeDataList(@RequestBody ManholeDeviceListInDTO listInDTO)
    {
        return AjaxResult2.success(equipmentBaseService.getManholeDataList(listInDTO));
    }

    /**
     * 获取监测设备台账详情
     */
    @Anonymous
    @GetMapping(value = "/getById/{id}")
    @ApiOperation(value = "获取监测设备台账详情")
    public AjaxResult2<EquipmentBaseOutDTO> getById(@PathVariable("id") Long id)
    {
        EquipmentBase equipmentBase = equipmentBaseService.getById(id);
        if(equipmentBase == null){
            return AjaxResult2.error("设备ID不存在");
        }
        EquipmentBaseOutDTO outDTO = BeanUtil.copyProperties(equipmentBase, EquipmentBaseOutDTO.class);
        // 查询设备状态
        EquipmentStatus equipmentStatus = equipmentStatusService.lambdaQuery()
                .eq(EquipmentStatus::getEquipmentId, outDTO.getEquipmentId())
                .one();
        if(equipmentStatus != null){
            EquipmentStatusOutDTO equipmentStatusOutDTO = BeanUtil.copyProperties(equipmentStatus, EquipmentStatusOutDTO.class);
            outDTO.setEquipmentStatus(equipmentStatusOutDTO);
        }
        // 查询设备类别名称
        EquipmentType equipmentType = equipmentTypeService.getById(outDTO.getEquipmentTypeId());
        if(equipmentType != null){
            outDTO.setEquipmentTypeName(equipmentType.getTypeName());
        }
        // 查询设备监测状态
        List<ManholeData> manholeDataList = manholeDataService.list(new LambdaQueryWrapper<ManholeData>()
                .eq(ManholeData::getImeiCardNumber, outDTO.getEquipmentCode())
                .orderByDesc(ManholeData::getCreateTime));
        if(CollUtil.isNotEmpty(manholeDataList)){
            ManholeData manholeData = manholeDataList.get(0);
            ManholeDataOutDTO manholeDataOutDTO = BeanUtil.copyProperties(manholeData, ManholeDataOutDTO.class);
            outDTO.setManholeData(manholeDataOutDTO);
        }
        return AjaxResult2.success(outDTO);
    }

    /**
     * 修改设备状态
     * @param inDTO
     * @return
     */
    @Anonymous
    @PostMapping("/updateDeviceStatus")
    @ApiOperation(value = "修改设备状态")
    public AjaxResult updateDeviceStatus(@RequestBody EquipmentStatusInDTO inDTO) {
        boolean updated = equipmentStatusService.updateDeviceStatus(inDTO);
        return updated ? AjaxResult.success("修改成功",updated)
                : AjaxResult.error("修改失败",updated);
    }
}
