package com.tofly.gpsboot.controller.statement;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.es.PdaDeviceUpload;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.common.business.ExcelExportFileTypeEnum;
import com.tofly.gpsboot.service.comm.StatementAssessService;
import com.tofly.gpsboot.util.ExcelUtil;
import com.tofly.pojo.emergency.EmergencyType;
import com.tofly.pojo.statement.StatementAssess;
import com.tofly.pojo.statement.dto.InspectionStatisticsDto;
import com.tofly.pojo.statement.dto.MaintenanceClassificationDto;
import com.tofly.pojo.statement.dto.StatementDto;
import com.tofly.pojo.statement.vo.*;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.service.statement.IStatementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 报表接口信息
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@RestController
@RequestMapping("/statement")
@Api(tags = "报表接口信息")
public class StatementController {
    @Autowired
    private IStatementService iStatementService;

    @Autowired
    private StatementAssessService statementAssessService;
    @ApiOperation(value = "获取管网设施维护情况")
    @PostMapping("/getMaintenanceStatistics")
    @ToFlyAppLog(title = "获取管网设施维护情况")
    public ResultRespone<Object> getMaintenanceStatistics(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<MaintenanceStatisticsVo> maintenanceStatistics = iStatementService.getMaintenanceStatistics(dto);
            rest.setResult(maintenanceStatistics);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }





    @ApiOperation(value = "隐患类型分析")
    @PostMapping("/getTroubleTypeAnalysis")
    @ToFlyAppLog(title = "隐患类型分析")
    public ResultRespone<Object> getTroubleTypeAnalysis(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<TroubleTypeAnalysisVo> troubleTypeAnalysis = iStatementService.getTroubleTypeAnalysis(dto);
            rest.setResult(troubleTypeAnalysis);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }
    @ApiOperation(value = "隐患原因分析")
    @PostMapping("/getTroubleCause")
    @ToFlyAppLog(title = "隐患原因分析")
    public ResultRespone<Object> getTroubleCause(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<TroubleCauseVo> troubleCause = iStatementService.getTroubleCause(dto);
            rest.setResult(troubleCause);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }
    @ApiOperation(value = "隐患趋势分析")
    @PostMapping("/getTroubleTrendAnalysis")
    @ToFlyAppLog(title = "隐患趋势分析")
    public ResultRespone<Object> getTroubleTrendAnalysis(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<TroubleTrendAnalysisVo> troubleTrendAnalysis = iStatementService.getTroubleTrendAnalysis(dto);
            rest.setResult(troubleTrendAnalysis);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @ApiOperation(value = "隐患记录台账")
    @PostMapping("/getTroubleRecordLedger")
    @ToFlyAppLog(title = "隐患记录台账")
    public ResultRespone<Page> getTroubleRecordLedger(Page page, @ModelAttribute StatementDto dto) {
        ResultRespone<Page> rest = new ResultRespone<>();
        try {
            Page troubleRecordLedger = iStatementService.getTroubleRecordLedger(page, dto);
            rest.setResult(troubleRecordLedger);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }
    @ApiOperation(value = "获取巡检日报")
    @PostMapping("/getInspectionDay")
    @ToFlyAppLog(title = "获取巡检日报")
    public ResultRespone<Object> getInspectionDay(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<InspectionDayVo> inspectionDay = iStatementService.getInspectionDay(dto);
            rest.setResult(inspectionDay);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }
    @ApiOperation(value = "获取巡检月报")
    @PostMapping("/getInspectionMonth")
    @ToFlyAppLog(title = "获取巡检月报")
    public ResultRespone<Object> getInspectionMonth(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<InspectionMonthVo> inspectionMonth = iStatementService.getInspectionMonth(dto);
            rest.setResult(inspectionMonth);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }
    @ApiOperation(value = "获取巡检统计")
    @PostMapping("/getInspectionStatistics")
    @ToFlyAppLog(title = "获取巡检统计")
    public ResultRespone<Object> getInspectionStatistics(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<InspectionStatisticsVo> inspectionStatistics = iStatementService.getInspectionStatistics(dto);
            rest.setResult(inspectionStatistics);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }
    @ApiOperation(value = "考勤记录")
    @PostMapping("/getAttendanceRecords")
    @ToFlyAppLog(title = "考勤记录")
    public ResultRespone<Object> getAttendanceRecords(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<AttendanceRecordsVo> attendanceRecords = iStatementService.getAttendanceRecords(dto);
            rest.setResult(attendanceRecords);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @ApiOperation(value = "排班计划")
    @PostMapping("/getScheduling")
    @ToFlyAppLog(title = "排班计划")
    public ResultRespone<Object> getScheduling(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<SchedulingVo> scheduling = iStatementService.getScheduling(dto);
            rest.setResult(scheduling);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }
    @ApiOperation(value = "获取终端记录台账")
    @PostMapping("/getTerminalRecord")
    @ToFlyAppLog(title = "获取终端记录台账")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "exportFlag", value = "不导出不传值,导出传1", dataType = "String"),
    })
    public ResultRespone<Page<List<PdaDeviceUpload>>> getTerminalRecord(HttpServletResponse response, Page page, @ModelAttribute StatementDto dto, String exportFlag) {
        ResultRespone<Page<List<PdaDeviceUpload>>> rest = new ResultRespone<>();
        try {
            //导出时采用升序排序导出
            Page<List<PdaDeviceUpload>> terminalRecord = iStatementService.getTerminalRecord(page, dto, exportFlag);
            if(!StringUtils.isEmpty(exportFlag)){
                //将查询的结果转json字符串的格式，因为导出的时候只能传一个字符串
                // 从模型类型枚举根据templateServiceType获取策略对应模板数据
                Map<String, Object> dataMap = new HashMap<>(12);
                dataMap.put("list", terminalRecord.getRecords());
                ExcelExportFileTypeEnum excelExportFileTypeEnum = ExcelExportFileTypeEnum.getExcelExportFileTypeEnum("statementService", "2");
                ExcelUtil.export(dataMap, excelExportFileTypeEnum, Constant.FILE_PATH_TEST_EXPORT_EXCEL, response);
                return null;
            }else {
                rest.setResult(terminalRecord);
            }
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }


    @GetMapping("/assessPage" )
    @ApiOperation(value = "报表考核分页查询")
    public ResultRespone getAssessPage(Page page, @ModelAttribute StatementAssess statementAssess) {
        return ResultRespone.success(statementAssessService.page(page, Wrappers.query(statementAssess)));
    }
    @ApiOperation(value = "报表考核")
    @PostMapping("/addAssess")
    @ToFlyAppLog(title = "报表考核")
    @Transactional(rollbackFor = Exception.class)
    public ResultRespone<Object> addAssess(@RequestBody InspectionStatisticsDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            Long statisticalUserId = dto.getStatisticalUserId();
            String type = dto.getType();
            String year = dto.getYear();
            String quarter = dto.getQuarter();
            String month = dto.getMonth();
            Long user = null == dto.getAssessUserId() ? SecurityUtils.getUserId() : dto.getAssessUserId();
            // 查询是否存在该考核内容, 存在则删除
            List<StatementAssess> list = statementAssessService.list(new QueryWrapper<StatementAssess>().lambda()
                    .eq(StatementAssess::getType, type)
                    .eq(!StringUtils.isEmpty(year), StatementAssess::getYear, year)
                    .eq(!StringUtils.isEmpty(quarter), StatementAssess::getQuarter, quarter)
                    .eq(!StringUtils.isEmpty(month), StatementAssess::getMonth, month)
                    .eq(StatementAssess::getAssessUserId, user)
                    .eq(StatementAssess::getStatisticalUserId, statisticalUserId));
            if(null!=list && list.size()>0){
                Set<Long> collect = list.stream().map(StatementAssess::getId).collect(Collectors.toSet());
                statementAssessService.removeByIds(collect);
            }
            List<InspectionStatisticsVo> voList = dto.getVoList();
            voList.forEach(info->{

                StatementAssess vo = new StatementAssess();
                vo.setStatisticalUserId(statisticalUserId);

                vo.setType(type);
                vo.setYear(year);
                vo.setQuarter(quarter);
                vo.setMonth(month);
                vo.setAssessUserId(user);
                vo.setAssessTime(null==dto.getAssessTime()?new Date():dto.getAssessTime());
                vo.setAssessNote(dto.getAssessNote());
                vo.setAssessScore(dto.getAssessScore());
                vo.setVaildSpeed(BigDecimal.valueOf(info.getVaildSpeed()));
                vo.setFindTroubleNum(info.getFindTroubleNum());
                vo.setBuildingsitefinded(info.getBuildingsitefinded());
                vo.setPlanNum(info.getPlanNum());
                List<WorkCompletionVo> workCompletionVoList = info.getWorkCompletionVoList();
                if(null!=workCompletionVoList && workCompletionVoList.size()>0){
                    List<WorkCompletionVo> collect = workCompletionVoList.stream().filter(i -> statisticalUserId.equals(i.getStatisticalUserId())).collect(Collectors.toList());
                    collect.forEach(base->{
                        vo.setTotalCount(base.getTotalCount());
                        vo.setTotalLenth(base.getTotalLenth());
                        vo.setTotalOverCount(base.getTotalOverCount());
                        vo.setTotalOverLenth(base.getTotalOverLenth());
                        vo.setTotalOverEtCount(base.getTotalOverEtCount());
                        vo.setTotalOverCountRate(base.getTotalOverCountRate());
                        vo.setTotalOverLenthRate(base.getTotalOverLenthRate());
                        vo.setTotalOverEtCountRate(base.getTotalOverEtCountRate());
                        vo.setTotalLossRate(base.getTotalLossRate());
                        vo.setTotalEtLossRate(base.getTotalEtLossRate());
                        vo.setUserOverCount(base.getUserOverCount());
                        vo.setUserOverLenth(base.getUserOverLenth());
                        vo.setUserOverEtCount(base.getUserOverEtCount());
                        vo.setUserOverCountRate(base.getUserOverCountRate());
                        vo.setUserOverLenthRate(base.getUserOverLenthRate());
                        vo.setUserOverEtCountRate(base.getUserOverEtCountRate());

                        statementAssessService.save(vo);
                    });
                }else {
                    statementAssessService.save(vo);
                }

            });

        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

    @ApiOperation(value = "巡检 年/季/月报查询")
    @PostMapping("/getInspectionStatement")
    @ToFlyAppLog(title = "巡检 年/季/月报查询")
    public ResultRespone<Object> getInspectionStatement(@RequestBody StatementDto dto) {
        ResultRespone<Object> rest = new ResultRespone<>();
        try {
            List<InspectionStatisticsVo> inspectionStatement = iStatementService.getInspectionStatement(dto);
            rest.setResult(inspectionStatement);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());

        }
        return rest;
    }

}
