/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2023/10/26
 */
package org.springblade.modules.business.count.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.constant.BusinessConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.baseinfo.monitorpoint.dto.MonitorPointInfoDTO;
import org.springblade.modules.baseinfo.monitorpoint.entity.MonitorPointInfoEntity;
import org.springblade.modules.baseinfo.monitorpoint.service.IMonitorPointService;
import org.springblade.modules.baseinfo.monitorsite.dto.MonitorSiteInfoDTO;
import org.springblade.modules.baseinfo.monitorsite.entity.MonitorSiteInfoEntity;
import org.springblade.modules.baseinfo.monitorsite.service.IMonitorSiteService;
import org.springblade.modules.baseinfo.wateruseconsumer.dto.WaterUseConsumerInfoDTO;
import org.springblade.modules.baseinfo.wateruseconsumer.entity.WaterUseConsumerInfoEntity;
import org.springblade.modules.baseinfo.wateruseconsumer.service.IWaterUseConsumerService;
import org.springblade.modules.business.count.dto.OrgWaterUseCountDTO;
import org.springblade.modules.business.count.dto.WaterUseConsumerCountDTO;
import org.springblade.modules.business.count.entity.MonitorSiteCountEntity;
import org.springblade.modules.business.count.entity.OrgWaterUseCountEntity;
import org.springblade.modules.business.count.entity.WaterUseConsumerCountEntity;
import org.springblade.modules.business.count.entity.WaterUseConsumerMonthCountEntity;
import org.springblade.modules.business.home.VO.WarnCountInfoVO;
import org.springblade.modules.business.monitor.dto.WaterFlowRateHistoryInfoDTO;
import org.springblade.modules.business.monitor.service.IWaterFlowRateHistoryService;
import org.springblade.modules.business.monitor.vo.ConsumerWaterFlowCountInfoVO;
import org.springblade.modules.business.monitor.vo.OrgWaterFlowCountInfoVO;
import org.springblade.modules.business.monitor.vo.WaterFlowRateHistoryInfoVO;
import org.springblade.modules.business.monitor.wrapper.WaterFlowRateHistoryWrapper;
import org.springblade.modules.business.warning.dto.RtuWarningInfoDTO;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.service.IDeptService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;

/***
 * Date:2023/10/26
 * Title:数据统计管理模块
 * Description：监测站统计、区域取水排名、取水户取水统计、取水户月报、取水户年报、历史同期对比统计
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/count")
@AllArgsConstructor
@Api(value = "数据统计管理模块", tags = "数据统计接口")
public class CountController extends BladeController {
    private final IDeptService deptService;
    private final IWaterUseConsumerService waterUseConsumerService;
    private final IMonitorSiteService monitorSiteService;
    private final IMonitorPointService monitorPointService;
    private final IWaterFlowRateHistoryService waterFlowRateHistoryService;

    /**
     * 监测站统计
     *
     * @return
     */
    @GetMapping("/monitorsite/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页")
    public R<List<MonitorSiteCountEntity>> monitorsitePage(@ApiIgnore MonitorSiteCountEntity dto) {
        List<MonitorSiteCountEntity> list = new LinkedList<>();
        //盟市统计
        LambdaQueryWrapper<Dept> wrapper = Wrappers.<Dept>query().lambda();
        wrapper.eq(Dept::getParentId, BusinessConstant.DEPT_XAM_ID);
        List<Dept> deptList = this.deptService.list(wrapper);
        for (Dept dept : deptList) {
            MonitorSiteCountEntity vo = new MonitorSiteCountEntity();
            //盟市名称
            vo.setDeptId(dept.getId());
            vo.setAdCity(dept.getDeptName());

            //取用水户
            WaterUseConsumerInfoDTO waterUseConsumerInfoDTO = new WaterUseConsumerInfoDTO();
            waterUseConsumerInfoDTO.setOrgId(dept.getId());
            long count = waterUseConsumerService.getWaterUseConsumerCount(waterUseConsumerInfoDTO);
            vo.setWaterUseConsumer(count);
            //监测站
            MonitorSiteInfoDTO monitorSiteInfoDTO = new MonitorSiteInfoDTO();
            monitorSiteInfoDTO.setOrgId(dept.getId());
            count = monitorSiteService.getMonitorSiteCount(monitorSiteInfoDTO);
            vo.setMonitorSite(count);
            //监测点
            MonitorPointInfoDTO monitorPointInfoDTO = new MonitorPointInfoDTO();
            monitorPointInfoDTO.setOrgId(dept.getId());
            count = monitorPointService.getMonitorPointCount(monitorPointInfoDTO);
            vo.setMonitorPoint(count);
            list.add(vo);
        }
        return R.data(list);
    }

    /**
     * 按机构统计用水
     *
     * @param dto
     * @return
     */
    @GetMapping("/org/wateruse/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mpcd", value = "监测点代码", paramType = "query", dataType = "string"),
    })
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页")
    public R<List<OrgWaterUseCountEntity>> orgWaterUsePage(@ApiIgnore OrgWaterUseCountDTO dto) {
        List<OrgWaterUseCountEntity> list = new LinkedList<>();
        //盟市统计
        LambdaQueryWrapper<Dept> wrapper = Wrappers.<Dept>query().lambda();
        wrapper.eq(Dept::getParentId, BusinessConstant.DEPT_XAM_ID);
        List<Dept> deptList = this.deptService.list(wrapper);
        for (Dept dept : deptList) {
            OrgWaterUseCountEntity vo = new OrgWaterUseCountEntity();
            //盟市名称
            vo.setDeptId(dept.getId());
            vo.setAdCity(dept.getDeptName());

            WaterFlowRateHistoryInfoDTO infoDTO = new WaterFlowRateHistoryInfoDTO();
            infoDTO.setOrgId(dept.getId());

            List<OrgWaterFlowCountInfoVO> waterFlowCountInfoVOList = waterFlowRateHistoryService.orgWaterUseCount(infoDTO);
            if (null != waterFlowCountInfoVOList && waterFlowCountInfoVOList.size() > 0) {
                vo.setWaterFlowSum(waterFlowCountInfoVOList.get(0).getMpaccq().longValue());
            } else {
                vo.setWaterFlowSum(0L);
            }

            //取用水户
            WaterUseConsumerInfoDTO waterUseConsumerInfoDTO = new WaterUseConsumerInfoDTO();
            waterUseConsumerInfoDTO.setOrgId(dept.getId());
            long count = waterUseConsumerService.getWaterUseConsumerCount(waterUseConsumerInfoDTO);
            vo.setWaterUseConsumer(count);

            list.add(vo);
        }
        list.sort(new Comparator<OrgWaterUseCountEntity>() {
            @Override
            public int compare(OrgWaterUseCountEntity o1, OrgWaterUseCountEntity o2) {
                if (o1.getWaterFlowSum() > o2.getWaterFlowSum()) {
                    return -1;
                } else if (o1.getWaterFlowSum() < o2.getWaterFlowSum()) {
                    return 1;
                }
                return 0;
            }
        });

        return R.data(list);
    }

    /**
     * 按用水户统计用水
     *
     * @param dto
     * @return
     */
    @GetMapping("/wateruseconsumer/wateruse/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mpcd", value = "监测点代码", paramType = "query", dataType = "string"),
    })
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页")
    public R<List<WaterUseConsumerCountEntity>> waterUseConsumerPage(@ApiIgnore WaterUseConsumerCountDTO dto) {
        WaterFlowRateHistoryInfoDTO infoDTO = new WaterFlowRateHistoryInfoDTO();
        List<ConsumerWaterFlowCountInfoVO> voList = waterFlowRateHistoryService.consumerWaterUseCount(infoDTO);
        voList.sort(new Comparator<ConsumerWaterFlowCountInfoVO>() {
            @Override
            public int compare(ConsumerWaterFlowCountInfoVO o1, ConsumerWaterFlowCountInfoVO o2) {
                if (o1.getMpaccq() > o2.getMpaccq()) {
                    return -1;
                } else if (o1.getMpaccq() < o2.getMpaccq()) {
                    return 1;
                }
                return 0;
            }
        });
        List<WaterUseConsumerCountEntity> list = new ArrayList<>();
        for (ConsumerWaterFlowCountInfoVO vo :voList){
            WaterUseConsumerCountEntity entity = new WaterUseConsumerCountEntity();
            entity.setWiuCd(vo.getWiuCd());
            entity.setWaterFlowSum(vo.getMpaccq().longValue());
            LambdaQueryWrapper<WaterUseConsumerInfoEntity> wrapper = Wrappers.<WaterUseConsumerInfoEntity>query().lambda();
            wrapper.eq(WaterUseConsumerInfoEntity::getIsDeleted, 0);
            wrapper.eq(WaterUseConsumerInfoEntity::getWiuCd,vo.getWiuCd());
            wrapper.last("limit 1");
            WaterUseConsumerInfoEntity infoEntity = waterUseConsumerService.getOne(wrapper);
            entity.setWiuNm(infoEntity.getWiuNm());
            list.add(entity);
        }
        return R.data(list);
    }

    @GetMapping("/wateruseconsumer/month/list")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页")
    public R<List<WaterUseConsumerMonthCountEntity>> consumerWaterUseMonthList(@ApiIgnore WaterFlowRateHistoryInfoDTO dto, Query query) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dto.getCountTime());
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date endTime = calendar.getTime();
        calendar.add(Calendar.HOUR_OF_DAY, -1);
        List<WaterUseConsumerMonthCountEntity> list = new LinkedList<>();

        return R.data(list);
    }

    @GetMapping("/wateruseconsumer/year/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mpcd", value = "监测点代码", paramType = "query", dataType = "string"),
    })
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页")
    public R<IPage<WaterFlowRateHistoryInfoVO>> consumerWaterUseYearPage(@ApiIgnore WaterFlowRateHistoryInfoDTO dto, Query query) {
        IPage<WaterFlowRateHistoryInfoVO> pages = waterFlowRateHistoryService.selectPage(Condition.getPage(query), dto);
        List<WaterFlowRateHistoryInfoVO> list = pages.getRecords();
        for (WaterFlowRateHistoryInfoVO v : list) {
            WaterFlowRateHistoryWrapper.build().entityVO(v);
        }
        return R.data(pages);
    }
}
