package com.huatek.busi.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.dto.BusiSensorQueryParamDto;
import com.huatek.busi.service.BusiSensorService;
import com.huatek.busi.service.monitor.BusiOnlineMonitorDataService;
import com.huatek.busi.vo.BusiSensorVo;
import com.huatek.busi.vo.BusiTreeNodeVo;
import com.huatek.busi.vo.generator.DeviceTypeVo;
import org.springframework.web.bind.annotation.*;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.constant.ApiConstant.Tags;
import com.huatek.busi.dto.IecPointsConfDto;
import com.huatek.busi.service.IecPointsConfService;
import com.huatek.busi.vo.IecPointsConfVo;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * IEC104-业务点表配置表 控制层
 *
 * @author anker_cheng
 * @date 2019-8-1 16:15:18
 */
@Api(tags = {Tags.IEC_POINTS_CONFIG})
@Slf4j
@RestController
@RequestMapping("/api/IecPointsConf")
public class IecPointsConfController {
    @Reference(check = false)
    private IecPointsConfService iecPointsConfService;

    @Reference(check = false)
    private BusiOnlineMonitorDataService busiOnlineMonitorDataService;

    @Reference
    private BusiSensorService busiSensorService;

    /**
     * 通过id查找 IEC104-业务点表配置表
     *
     * @param id id
     * @return map map
     */
    @ApiOperation(value = "查找", response = IecPointsConfVo.class)
    @GetMapping(value = "/findIecPointsConf/{id}", produces = {"application/json;charset=utf-8"})
    public Map<String, Object>  findIecPointsConf(@PathVariable(value="id")String id) {
        IecPointsConfVo iecPointsConf =iecPointsConfService.findIecPointsConfVo(id);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", iecPointsConf);
    }

     /**
     * IEC104-业务点表配置表 分页列表
     *
     * @param iecPointsConfDto IEC104-业务点表配置表Dto
     * @return map map
     */
    @ApiOperation(value = "分页查询", response = IecPointsConfVo.class)
    @PostMapping(value="/queryList", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> queryList(@RequestBody IecPointsConfDto iecPointsConfDto) {
        PageInfo<IecPointsConfVo> iecPointsConfs = iecPointsConfService.findIecPointsConfPage(iecPointsConfDto);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, iecPointsConfs.getTotal(), "", iecPointsConfs.getList());
    }

    /**
     * 添加 IEC104-业务点表配置表
     * @param iecPointsConfDto IEC104-业务点表配置表Dto
     * @return map map
     */
    @ApiOperation("新增")
    @PostMapping(value = "/addIecPointsConf", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> addIecPointsConf(@RequestBody IecPointsConfDto iecPointsConfDto) {
        int count = iecPointsConfService.addIecPointsConf(iecPointsConfDto);
        log.debug("addIecPointsConf: {}", count);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    /**
     * 修改 IEC104-业务点表配置表
     * @param iecPointsConfDto  IEC104-业务点表配置表Dto
     * @return the map
     */
    @ApiOperation("更新")
    @PostMapping(value = "/updateIecPointsConf", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> updateIecPointsConf(@RequestBody IecPointsConfDto iecPointsConfDto){
        int count = iecPointsConfService.updateIecPointsConf(iecPointsConfDto);
        log.debug("updateIecPointsConf: {}", count);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    /**
     * 删除 IEC104-业务点表配置表
     * @param iecPointsConfDto  IEC104-业务点表配置表Dto
     * @return map map
     */
    @ApiOperation("删除")
    @PostMapping(value = "/deleteIecPointsConf", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> deleteIecPointsConf(@RequestHeader("token") String token,
        @RequestBody List<String> ids){
        int count = iecPointsConfService.deleteIecPointsConf(ids);
        log.debug("deleteIecPointsConf: {}", count);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }



    // 20230414 新增接口
    /**
     * 获取主设可配点表的装置类型(返回如：[{value: "011", name "振动"}] )
     */
    @ApiOperation("获取主设备可配点表的监测装置类型")
    @GetMapping(value = "/getSubEquipmentTypeList", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> getSubEquipmentTypeList(@RequestParam(value = "mainEquipmentType", required = true) String mainType) {
        List<DeviceTypeVo> list = new ArrayList<>();
        if (SiteConstants.SiteTreeEnum.GEN_EQM.getValue().equals(mainType)) {
            DeviceTypeVo zd = new DeviceTypeVo();
            zd.setDeviceValue(SiteConstants.GeneratorTreeEnum.ZD_EQM.getValue());
            zd.setDeviceName(SiteConstants.GeneratorTreeEnum.ZD_EQM.getName());
            list.add(zd);
            DeviceTypeVo yl = new DeviceTypeVo();
            yl.setDeviceValue(SiteConstants.GeneratorTreeEnum.YL_EQM.getValue());
            yl.setDeviceName(SiteConstants.GeneratorTreeEnum.YL_EQM.getName());
            list.add(yl);
            // 20230504 leo 新增局放
            DeviceTypeVo jf = new DeviceTypeVo();
            jf.setDeviceValue(SiteConstants.GeneratorTreeEnum.JF_EQM.getValue());
            jf.setDeviceName(SiteConstants.GeneratorTreeEnum.JF_EQM.getName());
            list.add(jf);
        }
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", list);
    }

    @ApiOperation("获取监测装置列表")
    @GetMapping(value = "/subEquipmentList")
    public Map<String, Object> subEquipmentList(
            @RequestParam(value = "mainEquipmentType", required = true) String mainType,
            @RequestParam(value = "subEquipmentType", required = true) String subType,
            @RequestParam(value = "mainTreeId", required = true) String mainEquipmentId) {
        List<BusiTreeNodeVo> list = busiOnlineMonitorDataService.subEquipmentList(mainType, subType, mainEquipmentId);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", list);
    }

    @ApiOperation(value = "查询传感器列表")
    @GetMapping(value = "/sensorList")
    public Map<String, Object> sensorList(
            @RequestParam(value = "mainEquipmentType", required = true) String mainType,
            @RequestParam(value = "mainTreeId", required = true) String mainEquipmentId,
            @RequestParam(value = "subTreeId", required = true) String subEquipmentId) {

        List<BusiSensorVo> sensorList = new ArrayList<BusiSensorVo>();
        BusiSensorQueryParamDto param = new BusiSensorQueryParamDto();
        SiteConstants.SiteTreeEnum siteTreeEnum = SiteConstants.SiteTreeEnum.fromValue(mainType);
        param.setMainEquipmentType(mainType);
        param.setMainEquipmentId(mainEquipmentId);
        param.setEquipmentId(subEquipmentId);
        sensorList = busiSensorService.findSensorList(siteTreeEnum, param);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", sensorList);
    }
}