package com.zhgd.xmgl.modules.basicdata.controller.government;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.redis.lock.RedisRepository;
import com.zhgd.xmgl.modules.basicdata.constant.CacheConstants;
import com.zhgd.xmgl.modules.basicdata.dto.EngineeringDeviceDto;
import com.zhgd.xmgl.modules.basicdata.dto.EnvironmentDevDto;
import com.zhgd.xmgl.modules.basicdata.dto.EnvironmentDevStatDto;
import com.zhgd.xmgl.modules.basicdata.entity.DustNoiseData;
import com.zhgd.xmgl.modules.basicdata.entity.EnvironmentAlarm;
import com.zhgd.xmgl.modules.basicdata.entity.EnvironmentDev;
import com.zhgd.xmgl.modules.basicdata.service.IEngineeringService;
import com.zhgd.xmgl.modules.basicdata.service.IEnvironmentAlarmService;
import com.zhgd.xmgl.modules.basicdata.service.IEnvironmentDevService;
import com.zhgd.xmgl.modules.basicdata.service.IProjectService;
import com.zhgd.xmgl.security.SecurityUser;
import com.zhgd.xmgl.security.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @Title: Controller
 * @Description: 扬尘设备管理
 * @author： pds
 * @date： 2023-03-01
 * @version： V1.0
 */
@RestController
@RequestMapping("/gov/environmentDev")
@Slf4j
@Api(tags = "扬尘管理")
public class GovEnvironmentDevController {
    @Autowired
    private IEnvironmentDevService environmentDevService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IEngineeringService engineeringService;

    @Autowired
    private IEnvironmentAlarmService environmentAlarmService;

    @Autowired
    private RedisRepository redisRepository;
    /**
     * 分页列表查询
     *
     * @return
     */
    @OperLog(operModul = "扬尘管理", operType = "分页查询", operDesc = "分页列表查询扬尘工程信息")
    @ApiOperation(value = " 分页列表查询扬尘工程信息", notes = "分页列表查询扬尘工程信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "engineeringName", value = "工程名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNo", value = "页数", paramType = "query", required = true, defaultValue = "1", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", required = true, defaultValue = "10", dataType = "Integer")
    })
    @PostMapping(value = "/page")
    public Result<IPage<EngineeringDeviceDto>> queryPageList(@RequestBody Map<String, Object> map) {
        return Result.success(environmentDevService.pageListByGov(map));
    }

    /**
     * 列表查询
     *
     * @return
     */
    @OperLog(operModul = "扬尘管理", operType = "列表查询", operDesc = "列表查询扬尘设备管理信息")
    @ApiOperation(value = " 列表查询扬尘设备管理信息", notes = "列表查询扬尘设备管理信息", httpMethod = "POST")
    @PostMapping(value = "/list")
    public Result<List<EnvironmentDev>> queryList(@RequestBody EnvironmentDev environmentDev) {
        LambdaQueryWrapper<EnvironmentDev> wrapper = Wrappers.<EnvironmentDev>lambdaQuery();
        if (environmentDev.getProjectSn() != null) {
            wrapper.eq(EnvironmentDev::getEngineeringSn, environmentDev.getEngineeringSn());
        }
        List<EnvironmentDev> list = environmentDevService.list(wrapper);
        return Result.success(list);
    }

    /**
     * 数据统计
     *
     * @return
     */
    @OperLog(operModul = "扬尘管理", operType = "统计", operDesc = "统计扬尘设备数据")
    @ApiOperation(value = " 统计扬尘设备数据", notes = "统计扬尘设备数据", httpMethod = "GET")
    @GetMapping(value = "/statistics")
    public Result<EnvironmentDevStatDto> statistics() {
        Result<EnvironmentDevStatDto> result = new Result<EnvironmentDevStatDto>();
        EnvironmentDevStatDto environmentDev = new EnvironmentDevStatDto();
        SecurityUser user = SecurityUtil.getUser();
        List<String> projectSnList = projectService.getSnListForGov(user.getSn());
        int installNum = environmentDevService.count(Wrappers.<EnvironmentDev>lambdaQuery()
                .in(EnvironmentDev::getProjectSn, projectSnList)
                .isNotNull(EnvironmentDev::getEngineeringSn)
                .groupBy(EnvironmentDev::getEngineeringSn));
        Integer engineeringNum = engineeringService.getSnListByGov(user.getSn()).size();
        environmentDev.setInstallNum(installNum);
        environmentDev.setUseRate(engineeringNum == 0 ? 0 : NumberUtil.round(installNum * 100F / engineeringNum, 2).floatValue());
        environmentDev.setTotalAlarm(environmentAlarmService.count(Wrappers.<EnvironmentAlarm>lambdaQuery()
                .in(EnvironmentAlarm::getProjectSn, projectSnList)));
        return Result.success(environmentDev);
    }

    /**
     * 通过设备编码查询设备详细信息
     *
     * @return
     */
    @OperLog(operModul = "扬尘管理", operType = "查询", operDesc = "通过设备ID查询设备详细信息")
    @ApiOperation(value = "通过设备ID查询设备详细信息", notes = "通过设备ID查询设备详细信息", httpMethod = "POST")
    @ApiImplicitParam(name = "id", value = "扬尘设备id", paramType = "query", required = true, dataType = "String")
    @PostMapping(value = "/queryDetailById")
    public Result<EnvironmentDevDto> queryById(@RequestBody EnvironmentDev environmentDev) {
        Result<EnvironmentDevDto> result = new Result<EnvironmentDevDto>();
        environmentDev = environmentDevService.getById(environmentDev.getId());
        if (environmentDev == null) {
            result.error500("未找到对应实体");
        } else {
            EnvironmentDevDto environmentDevDto = new EnvironmentDevDto();
            BeanUtils.copyProperties(environmentDev, environmentDevDto);
			// 最新上报记录
            Object obj = redisRepository.get(CacheConstants.DUST_NOISE_DATA + environmentDev.getCode());
            if (obj != null) {
                environmentDevDto.setDustNoiseData((DustNoiseData) obj);
            }
            Object objList = redisRepository.getList(CacheConstants.ENVIRONMENT_ALARM_LIST + environmentDev.getCode());
            if (objList != null) {
                environmentDevDto.setEnvironmentAlarmList((List<EnvironmentAlarm>) objList);
            }
            result.setResult(environmentDevDto);
            result.setSuccess(true);
        }
        return result;
    }
}
