/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.gpsboot.controller.fill;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.service.fill.*;
import com.tofly.pojo.fill.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * app填报
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fill")
@Api(tags = "app填报")
@Slf4j
public class FillController {

    private final FillMonthService fillMonthService;

    private final FillEcService fillEcService;

    private final FillSludgeDealService fillSludgeDealService;

    private final FillSludgeTransferService sludgeTransferService;

    private final FillStcService fillStcService;

    private final FillWaterService fillWaterService;

    @GetMapping("/pageFillMonth")
    @ApiOperation(value = "分页查询月运行记录报表")
    public ResultRespone getFillMonthPage(Page page, @ModelAttribute FillMonth fillMonth) {
        return ResultRespone.success(fillMonthService.getFillMonthPage(page,fillMonth));
    }

    @ApiOperation(value = "新增月运行记录填报表")
    @PostMapping(value = "/saveFillMonth")
    public ResultRespone<Object> saveFillMonth(@RequestBody FillMonth fillMonth) {
        ResultRespone<Object> rest = new ResultRespone<>();
        if (StringUtils.isBlank(fillMonth.getLoadRate())) {
            fillMonth.setLoadRate("0");
        }
        if (StringUtils.isBlank(fillMonth.getSpDesignScale())) {
            fillMonth.setSpDesignScale("0");
        }
        if (StringUtils.isBlank(fillMonth.getMonthDirectCost())) {
            fillMonth.setMonthDirectCost("0");
        }
        if (StringUtils.isBlank(fillMonth.getTotalCs())) {
            fillMonth.setTotalCs("0");
        }
        if (StringUtils.isBlank(fillMonth.getActiveCsRate())) {
            fillMonth.setActiveCsRate("0");
        }
        if (StringUtils.isBlank(fillMonth.getTotalDa())) {
            fillMonth.setTotalDa("0");
        }
        if (StringUtils.isBlank(fillMonth.getActiveDaRate())) {
            fillMonth.setActiveDaRate("0");
        }
        if (StringUtils.isBlank(fillMonth.getTotalPr())) {
            fillMonth.setTotalPr("0");
        }
        if (StringUtils.isBlank(fillMonth.getActivePrRate())) {
            fillMonth.setActivePrRate("0");
        }
        List<FillMonth> list = fillMonthService.list(new QueryWrapper<FillMonth>().lambda()
                .eq(FillMonth::getRecordTime,fillMonth.getRecordTime())
                .eq(FillMonth::getBelongStationId,fillMonth.getBelongStationId()));
        if (CollUtil.isNotEmpty(list)) {
            throw new ToflyDeniedException("该记录已上报，请勿重复上报");
        }
        fillMonthService.saveFillMonth(fillMonth);
        return rest;
    }

    @ApiOperation(value = "修改月运行记录填报表")
    @PutMapping("/updateFillMonth")
    public ResultRespone<Object> updateFillMonth(@RequestBody FillMonth fillMonth) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 判断当前内容是否已上报
        FillMonth byId = fillMonthService.getById(fillMonth.getId());
        String report1 = byId.getReport();
        if ("1".equals(report1)) {
            rest.setCode(-1);
            rest.setMessage("该信息已上报,不可再次上报,请等待审核");
        } else {
            boolean b = fillMonthService.updateFillMonth(fillMonth);
            rest.setResult(b);
        }
        return rest;
    }

    @ApiOperation(value = "审核月运行记录填报表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "审核月运行记录填报id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "auditUserId", value = "审核人id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "auditNote", value = "审核说明", required = true, dataType = "String"),
            @ApiImplicitParam(name = "auditState", value = "审核状态 1通过,2未通过", required = true, dataType = "String")
    })
    @PostMapping(value = "/auditFillMonth")
    public ResultRespone<Object> auditFillMonth(Long id,Long auditUserId,String auditNote,String auditState) {
        ResultRespone<Object> rest = new ResultRespone<>();
        fillMonthService.auditFillMonth(id, auditUserId, auditNote, auditState);
        return rest;
    }

    @ApiOperation(value = "通过ID批量删除月运行记录填报表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIdsFillMonth")
    public ResultRespone<Object> deleteByIdsFillMonth(String ids) {
        ResultRespone<Object> rest = new ResultRespone<>();
        List<Long> removeId = Arrays.stream(ids.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                .collect(Collectors.toList());
        // 判断当前删除列表是否包含已上报数据
        Set<String> collect = fillMonthService.list(new QueryWrapper<FillMonth>().lambda().in(FillMonth::getId, removeId)).stream().map(FillMonth::getReport).collect(Collectors.toSet());
        if (collect.contains("1")) {
            rest.setCode(-1);
            rest.setMessage("当前删除列表中存在已上报数据,请检查");
        } else {
            fillMonthService.removeByIds(removeId);
        }
        return rest;
    }

    @GetMapping("/pageFillEc")
    @ApiOperation(value = "分页查询能耗填报登记记录表")
    public ResultRespone getFillEcPage(Page page, @ModelAttribute FillEc fillEc) {
        return ResultRespone.success(fillEcService.page(page,new QueryWrapper<FillEc>().lambda().setEntity(fillEc)
                .ge(null!=fillEc.getStartTime(),FillEc::getRecordTime,fillEc.getStartTime())
                .le(null!=fillEc.getEndTime(),FillEc::getRecordTime,fillEc.getEndTime())));
    }

    @ApiOperation(value = "新增能耗填报登记记录表")
    @PostMapping(value = "/saveFillEc")
    public ResultRespone<Object> saveFillEc(@RequestBody FillEc fillEc) {
        ResultRespone<Object> rest = new ResultRespone<>();
        List<FillEc> list = fillEcService.list(new QueryWrapper<FillEc>().lambda()
                .eq(FillEc::getRecordTime,fillEc.getRecordTime())
                .eq(FillEc::getBelongStationId,fillEc.getBelongStationId()));
        if (CollUtil.isNotEmpty(list)) {
            throw new ToflyDeniedException("该记录已上报，请勿重复上报");
        }
        fillEcService.save(fillEc);
        return rest;
    }

    @ApiOperation(value = "修改能耗填报登记记录表")
    @PutMapping("/updateFillEc")
    public ResultRespone<Object> updateFillEc(@RequestBody FillEc fillEc) {
        ResultRespone<Object> rest = new ResultRespone<>();
        fillEcService.updateById(fillEc);
        return rest;
    }

    @ApiOperation(value = "通过ID批量删除能耗填报登记记录表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIdsFillEc")
    public ResultRespone<Object> deleteByIdsFillEc(String ids) {
        ResultRespone<Object> rest = new ResultRespone<>();
        List<Long> removeId = Arrays.stream(ids.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                .collect(Collectors.toList());
        fillEcService.removeByIds(removeId);
        return rest;
    }

    @GetMapping("/pageFillSludgeDeal")
    @ApiOperation(value = "分页查询污泥处理量填报登记记录表")
    public ResultRespone getFillSludgeDealPage(Page page, @ModelAttribute FillSludgeDeal fillSludgeDeal) {
        return ResultRespone.success(fillSludgeDealService.page(page,new QueryWrapper<FillSludgeDeal>().lambda().setEntity(fillSludgeDeal)
                .ge(null!=fillSludgeDeal.getStartTime(),FillSludgeDeal::getRecordTime,fillSludgeDeal.getStartTime())
                .le(null!=fillSludgeDeal.getEndTime(),FillSludgeDeal::getRecordTime,fillSludgeDeal.getEndTime())));
    }

    @ApiOperation(value = "新增污泥处理量填报登记记录表")
    @PostMapping(value = "/saveFillSludgeDeal")
    public ResultRespone<Object> saveFillSludgeDeal(@RequestBody FillSludgeDeal fillSludgeDeal) {
        ResultRespone<Object> rest = new ResultRespone<>();
        List<FillSludgeDeal> list = fillSludgeDealService.list(new QueryWrapper<FillSludgeDeal>().lambda()
                .eq(FillSludgeDeal::getRecordTime,fillSludgeDeal.getRecordTime())
                .eq(FillSludgeDeal::getBelongStationId,fillSludgeDeal.getBelongStationId()));
        if (CollUtil.isNotEmpty(list)) {
            throw new ToflyDeniedException("该记录已上报，请勿重复上报");
        }
        fillSludgeDealService.save(fillSludgeDeal);
        return rest;
    }

    @ApiOperation(value = "修改污泥处理量填报登记记录表")
    @PutMapping("/updateFillSludgeDeal")
    public ResultRespone<Object> updateFillSludgeDeal(@RequestBody FillSludgeDeal fillSludgeDeal) {
        ResultRespone<Object> rest = new ResultRespone<>();
        fillSludgeDealService.updateById(fillSludgeDeal);
        return rest;
    }

    @ApiOperation(value = "通过ID批量删除污泥处理量填报登记记录表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIdsFillSludgeDeal")
    public ResultRespone<Object> deleteByIdsFillSludgeDeal(String ids) {
        ResultRespone<Object> rest = new ResultRespone<>();
        List<Long> removeId = Arrays.stream(ids.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                .collect(Collectors.toList());
        fillSludgeDealService.removeByIds(removeId);
        return rest;
    }

    @GetMapping("/pageFillSludgeTransfer")
    @ApiOperation(value = "分页查询污泥转运填报登记记录表")
    public ResultRespone getFillSludgeTransferPage(Page page, @ModelAttribute FillSludgeTransfer fillSludgeTransfer) {
        return ResultRespone.success(sludgeTransferService.page(page,new QueryWrapper<FillSludgeTransfer>().lambda().setEntity(fillSludgeTransfer)
                .ge(null!=fillSludgeTransfer.getStartTime(),FillSludgeTransfer::getRecordTime,fillSludgeTransfer.getStartTime())
                .le(null!=fillSludgeTransfer.getEndTime(),FillSludgeTransfer::getRecordTime,fillSludgeTransfer.getEndTime())));
    }

    @ApiOperation(value = "新增污泥转运填报登记记录表")
    @PostMapping(value = "/saveFillSludgeTransfer")
    public ResultRespone<Object> saveFillSludgeTransfer(@RequestBody FillSludgeTransfer fillSludgeTransfer) {
        ResultRespone<Object> rest = new ResultRespone<>();
        List<FillSludgeTransfer> list = sludgeTransferService.list(new QueryWrapper<FillSludgeTransfer>().lambda()
                .eq(FillSludgeTransfer::getRecordTime,fillSludgeTransfer.getRecordTime())
                .eq(FillSludgeTransfer::getBelongStationId,fillSludgeTransfer.getBelongStationId()));
        if (CollUtil.isNotEmpty(list)) {
            throw new ToflyDeniedException("该记录已上报，请勿重复上报");
        }
        sludgeTransferService.save(fillSludgeTransfer);
        return rest;
    }

    @ApiOperation(value = "修改污泥转运填报登记记录表")
    @PutMapping("/updateFillSludgeTransfer")
    public ResultRespone<Object> updateFillSludgeTransfer(@RequestBody FillSludgeTransfer fillSludgeTransfer) {
        ResultRespone<Object> rest = new ResultRespone<>();
        sludgeTransferService.updateById(fillSludgeTransfer);
        return rest;
    }

    @ApiOperation(value = "通过ID批量删除污泥转运填报登记记录表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIdsFillSludgeTransfer")
    public ResultRespone<Object> deleteByIdsFillSludgeTransfer(String ids) {
        ResultRespone<Object> rest = new ResultRespone<>();
        List<Long> removeId = Arrays.stream(ids.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                .collect(Collectors.toList());
        sludgeTransferService.removeByIds(removeId);
        return rest;
    }

    @GetMapping("/pageFillStc")
    @ApiOperation(value = "分页查询污水处理量填报登记表")
    public ResultRespone getFillStcPage(Page page, @ModelAttribute FillStc fillStc) {
        return ResultRespone.success(fillStcService.page(page,new QueryWrapper<FillStc>().lambda().setEntity(fillStc)
                .ge(null!=fillStc.getStartTime(),FillStc::getRecordTime,fillStc.getStartTime())
                .le(null!=fillStc.getEndTime(),FillStc::getRecordTime,fillStc.getEndTime())));
    }

    @ApiOperation(value = "新增污水处理量填报登记表")
    @PostMapping(value = "/saveFillStc")
    public ResultRespone<Object> saveFillStc(@RequestBody FillStc fillStc) {
        ResultRespone<Object> rest = new ResultRespone<>();
        List<FillStc> list = fillStcService.list(new QueryWrapper<FillStc>().lambda()
                .eq(FillStc::getRecordTime,fillStc.getRecordTime())
                .eq(FillStc::getBelongStationId,fillStc.getBelongStationId()));
        if (CollUtil.isNotEmpty(list)) {
            throw new ToflyDeniedException("该记录已上报，请勿重复上报");
        }
        fillStcService.save(fillStc);
        return rest;
    }

    @ApiOperation(value = "修改污水处理量填报登记表")
    @PutMapping("/updateFillStc")
    public ResultRespone<Object> updateFillStc(@RequestBody FillStc fillStc) {
        ResultRespone<Object> rest = new ResultRespone<>();
        fillStcService.updateById(fillStc);
        return rest;
    }

    @ApiOperation(value = "通过ID批量删除污水处理量填报登记表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIdsFillStc")
    public ResultRespone<Object> deleteByIdsFillStc(String ids) {
        ResultRespone<Object> rest = new ResultRespone<>();
        List<Long> removeId = Arrays.stream(ids.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                .collect(Collectors.toList());
        fillStcService.removeByIds(removeId);
        return rest;
    }

    @GetMapping("/pageFillWater")
    @ApiOperation(value = "分页查询水质填报登记记录表")
    public ResultRespone getFillWaterPage(Page page, @ModelAttribute FillWater fillWater) {
        return ResultRespone.success(fillWaterService.page(page,new QueryWrapper<FillWater>().lambda().setEntity(fillWater)
                .ge(null!=fillWater.getStartTime(),FillWater::getRecordTime,fillWater.getStartTime())
                .le(null!=fillWater.getEndTime(),FillWater::getRecordTime,fillWater.getEndTime())));
    }

    @ApiOperation(value = "新增水质填报登记记录表")
    @PostMapping(value = "/saveFillWater")
    public ResultRespone<Object> saveFillWater(@RequestBody FillWater fillWater) {
        ResultRespone<Object> rest = new ResultRespone<>();
        List<FillWater> list = fillWaterService.list(new QueryWrapper<FillWater>().lambda()
                .eq(FillWater::getRecordTime,fillWater.getRecordTime())
                .eq(FillWater::getBelongStationId,fillWater.getBelongStationId()));
        if (CollUtil.isNotEmpty(list)) {
           throw new ToflyDeniedException("该记录已上报，请勿重复上报");
        }
        fillWaterService.save(fillWater);
        return rest;
    }

    @ApiOperation(value = "修改水质填报登记记录表")
    @PutMapping("/updateFillWater")
    public ResultRespone<Object> updateFillWater(@RequestBody FillWater fillWater) {
        ResultRespone<Object> rest = new ResultRespone<>();
        fillWaterService.updateById(fillWater);
        return rest;
    }

    @ApiOperation(value = "通过ID批量删除水质填报登记记录表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIdsFillWater")
    public ResultRespone<Object> deleteByIdsFillWater(String ids) {
        ResultRespone<Object> rest = new ResultRespone<>();
        List<Long> removeId = Arrays.stream(ids.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                .collect(Collectors.toList());
        fillWaterService.removeByIds(removeId);
        return rest;
    }

    @GetMapping("/getSludgeList")
    @ApiOperation(value = "获取污泥厂列表")
    public ResultRespone getSludgeList() {
        return ResultRespone.success(sludgeTransferService.getSludgeList());
    }

    @GetMapping("/getIndexValDay")
    @ApiOperation(value = "获取填报数据日报表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDate", value = "开始时间 yyyy-MM-dd HH:mm:ss", dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "结束时间 yyyy-MM-dd HH:mm:ss", dataType = "String"),
            @ApiImplicitParam(name = "belongStationIds", value = "所属污水厂ID（多个用,分隔）", dataType = "String")
    })
    public ResultRespone<List<FillVo>> getIndexValDay(String startDate, String endDate, String belongStationIds) {
        List<Long> sewageIds = new ArrayList<>();
        if (StringUtils.isNotBlank(belongStationIds)) {
            sewageIds = Arrays.stream(belongStationIds.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                    .collect(Collectors.toList());
        }
        return ResultRespone.success(fillMonthService.getIndexValDay(startDate, endDate, sewageIds));
    }

//    @GetMapping("/getIndexValMonth")
//    @ApiOperation(value = "获取填报数据月报表")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "startMonth", value = "开始月 yyyy-MM", dataType = "String"),
//            @ApiImplicitParam(name = "endMonth", value = "结束时月 yyyy-MM", dataType = "String")
//    })
//    public ResultRespone getIndexValMonth(String startMonth, String endMonth) {
//        return ResultRespone.success(fillMonthService.getIndexValMonth(startMonth, endMonth));
//    }

    @GetMapping("/getIndexValYear")
    @ApiOperation(value = "获取填报数据年报表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startYear", value = "开始年 yyyy", dataType = "String"),
            @ApiImplicitParam(name = "endYear", value = "结束年 yyyy", dataType = "String"),
            @ApiImplicitParam(name = "belongStationIds", value = "所属污水厂ID（多个用,分隔）", dataType = "String")
    })
    public ResultRespone<List<FillMonth>> getIndexValYear(String startYear, String endYear, String belongStationIds) {
        List<Long> sewageIds = new ArrayList<>();
        if (StringUtils.isNotBlank(belongStationIds)) {
            sewageIds = Arrays.stream(belongStationIds.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                    .collect(Collectors.toList());
        }
        return ResultRespone.success(fillMonthService.getIndexValYear(startYear, endYear, sewageIds));
    }

}
