package com.gyee.runeconomy.controller.bmk;/*
@author   谢生杰
@date   2022/11/23-13:16
*/

import com.gyee.common.model.StringUtils;
import com.gyee.common.util.DateUtils;
import com.gyee.common.util.SortUtils;
import com.gyee.common.vo.benchmark.FjjxbVo;
import com.gyee.common.vo.benchmark.FjjxbmxVo;
import com.gyee.common.vo.benchmark.WxsslVo;
import com.gyee.runeconomy.dto.R;
import com.gyee.runeconomy.dto.ResultMsg;
import com.gyee.runeconomy.model.auto.*;
import com.gyee.runeconomy.service.bmk.BenchmarkingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("//benchmarking")
@Api(value = "对标管理", tags = "对标管理")
public class BenchmarkingController {

    @Resource
    private BenchmarkingService benchmarkingService;


    @GetMapping(value = "/companys")
    @ApiOperation(value = "公司列表", notes = "公司列表")
    public R companys() {
        List<ProBasicCompany> resultList = benchmarkingService.companys();
        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }

    @GetMapping(value = "/companysByRg")
    @ApiOperation(value = "根据区域查询公司列表", notes = "根据区域查询公司列表")
    public R companysByRg(@RequestParam(value = "regionid", required = true) String regionid) {
        List<ProBasicCompany> resultList = benchmarkingService.companys().stream().filter(i -> i.getRegionId().equals(regionid)).collect(Collectors.toList());
        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }

    @GetMapping(value = "/wpByCplist")
    @ApiOperation(value = "场站列表", notes = "场站列表")
    public R wpByCplist(@RequestParam(value = "companyids") String companyids,
                        @RequestParam(value = "type", required = false) String type) {

        List<ProBasicPowerstation> resultList = benchmarkingService.wpByCplist(companyids, type);
        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }


    @GetMapping(value = "/subByCplist")
    @ApiOperation(value = "升压站列表", notes = "升压站列表")
    public R subByCplist(@RequestParam(value = "companyids", required = true) String companyids,
                         @RequestParam(value = "type", required = false) String type) {

        List<ProBasicSubStation> resultList = benchmarkingService.subByCplist(companyids, type);
        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }

    @GetMapping(value = "/wtByWplist")
    @ApiOperation(value = "设备列表", notes = "设备列表")
    public R wtByWplist(@RequestParam(value = "wpId", required = true) String wpId) {

        List<ProBasicEquipment> resultList = benchmarkingService.wtByWplist(wpId);
        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }


    @GetMapping(value = "/pjByWplist")
    @ApiOperation(value = "项目列表", notes = "项目列表")
    public R pjByWplist(@RequestParam(value = "wpids", required = true) String wpids) {

        List<ProBasicProject> resultList = benchmarkingService.pjByWplist(wpids);
        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }

    @GetMapping(value = "/lnByPjlist")
    @ApiOperation(value = "线路列表", notes = "线路列表")
    public R lnByPjlist(@RequestParam(value = "pjids", required = true) String pjids) {

        List<ProBasicLine> resultList = benchmarkingService.lnByPjlist(pjids);
        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }

    @GetMapping(value = "/performance2")
    @ApiOperation(value = "风机绩效榜", notes = "风机绩效榜")
    public R performance2(@RequestParam(value = "companyid") String companyid,
                         @RequestParam(value = "getype") String getype,
                         @RequestParam(value = "sttype") String sttype,
                         @RequestParam(value = "beginDate") String beginDate,
                         @RequestParam(value = "endDate") String endDate,
                         @RequestParam(value = "wpids") String wpids,
                         @RequestParam(value = "projectids") String projectids,
                         @RequestParam(value = "lineids") String lineids,
                         @RequestParam(value = "target", required = false) String target,
                         @RequestParam(value = "sort", required = false) String sort) {
        List<FjjxbVo> resultList = benchmarkingService.performance2(companyid, getype, sttype, beginDate, endDate, wpids, projectids, lineids, target, sort);
        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }


    @GetMapping(value = "/performance")
    @ApiOperation(value = "风机绩效榜", notes = "风机绩效榜")
    public R performance(@RequestParam(value = "companyid") String companyid,
                         @RequestParam(value = "getype") String getype,
                         @RequestParam(value = "sttype") String sttype,
                         @RequestParam(value = "beginDate") String beginDate,
                         @RequestParam(value = "endDate") String endDate,
                         @RequestParam(value = "wpids") String wpids,
                         @RequestParam(value = "projectids") String projectids,
                         @RequestParam(value = "lineids") String lineids,
                         @RequestParam(value = "target", required = false) String target,
                         @RequestParam(value = "sort", required = false) String sort) {
        List<FjjxbVo> resultList = benchmarkingService.performance(companyid, getype, sttype, beginDate, endDate, wpids, projectids, lineids, target, sort);
        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }


    @GetMapping(value = "/performanceMX")
    @ApiOperation(value = "风机绩效榜明细", notes = "风机绩效榜明细")
    public R performanceMX(@RequestParam(value = "companyid", required = true) String companyid,
                           @RequestParam(value = "getype", required = true) String getype,
                           @RequestParam(value = "sttype", required = true) String sttype,
                           @RequestParam(value = "beginDate", required = true) String beginDate,
                           @RequestParam(value = "endDate", required = true) String endDate,
                           @RequestParam(value = "wpids", required = true) String wpids,
                           @RequestParam(value = "projectids", required = true) String projectids,
                           @RequestParam(value = "lineids", required = true) String lineids,
                           @RequestParam(value = "target", required = true) String target,
                           @RequestParam(value = "sort", required = true) String sort) {
        List<FjjxbmxVo> resultList = benchmarkingService.performanceMX(companyid, getype, sttype, beginDate, endDate, wpids, projectids, lineids, target, sort);
        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }


    @GetMapping("/wxssl2")
    @ResponseBody
    @ApiOperation(value = "五项损失率", notes = "五项损失率")
    public R wxssl2(@RequestParam(value = "companys") String companys,
                   @RequestParam(value = "type") String type,
                   @RequestParam(value = "wpids") String wpids,
                   @RequestParam(value = "projectids") String projectids,
                   @RequestParam(value = "lineids") String lineids,
                   @RequestParam(value = "beginDate") String beginDate,
                   @RequestParam(value = "endDate") String endDate,
                   @RequestParam(value = "target") String target,
                   @RequestParam(value = "sort") String sort
    ) {
        List<WxsslVo> resultList = benchmarkingService.wxssl2(companys, type, wpids, projectids, lineids, beginDate, endDate, target, sort);

        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }


    @GetMapping("/wxssl")
    @ResponseBody
    @ApiOperation(value = "五项损失率", notes = "五项损失率")
    public R wxssl(@RequestParam(value = "companys") String companys,
                   @RequestParam(value = "type") String type,
                   @RequestParam(value = "wpids") String wpids,
                   @RequestParam(value = "projectids") String projectids,
                   @RequestParam(value = "lineids") String lineids,
                   @RequestParam(value = "beginDate") String beginDate,
                   @RequestParam(value = "endDate") String endDate,
                   @RequestParam(value = "target") String target,
                   @RequestParam(value = "sort") String sort
    ) {
        List<WxsslVo> resultList = benchmarkingService.wxssl(companys, type, wpids, projectids, lineids, beginDate, endDate, target, sort);

        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }


    @GetMapping("/cndb2")
    @ResponseBody
    @ApiOperation(value = "场内对标", notes = "场内对标")
    public R cndb2(@RequestParam(value = "companys", required = true) String companys,
                  @RequestParam(value = "type", required = true) String type,
                  @RequestParam(value = "wpid", required = true) String wpid,
                  @RequestParam(value = "beginDate", required = true) String beginDate,
                  @RequestParam(value = "endDate", required = true) String endDate,
                  @RequestParam(value = "target", required = true) String target,
                  @RequestParam(value = "sort", required = true) String sort,
                  @RequestParam(value = "pageNum", required = true) int pageNum,
                  @RequestParam(value = "pageSize", required = true) int pageSize
    ) {
        Map<String, Object> resultList = benchmarkingService.cndb2(companys, type, wpid, beginDate, endDate, target, sort, pageNum, pageSize);

        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }


    @GetMapping("/cndb")
    @ResponseBody
    @ApiOperation(value = "场内对标", notes = "场内对标")
    public R cndb(@RequestParam(value = "companys") String companys,
                  @RequestParam(value = "type") String type,
                  @RequestParam(value = "wpid") String wpid,
                  @RequestParam(value = "beginDate") String beginDate,
                  @RequestParam(value = "endDate") String endDate,
                  @RequestParam(value = "target") String target,
                  @RequestParam(value = "sort") String sort,
                  @RequestParam(value = "pageNum") int pageNum,
                  @RequestParam(value = "pageSize") int pageSize
    ) {
        Map<String, Object> resultList = benchmarkingService.cndb(companys, type, wpid, beginDate, endDate, target, sort, pageNum, pageSize);

        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }


    @GetMapping("/cndbthb")
    @ResponseBody
    @ApiOperation(value = "场内对标同环比", notes = "场内对标同环比")
    public R cndbthb(@RequestParam(value = "companys", required = true) String companys,
                     @RequestParam(value = "type", required = true) String type,
                     @RequestParam(value = "wpid", required = true) String wpid,
                     @RequestParam(value = "pageNum", required = true) int pageNum,
                     @RequestParam(value = "pageSize", required = true) int pageSize
    ) throws Exception {
        List<WxsslVo> resultList = new ArrayList<>();
        //获取当前时间
        String endDate = DateUtils.toDate1(new Date());
        Date date = DateUtils.parseDate(endDate);
        String beginDate = DateUtils.toDate1(DateUtils.getFirstAndLastDayOfMonth(date));
        //        Map<String, Object> dateList = benchmarkingService.cndbthb(companys,type,wpid,beginDate,endDate,page,pageSize);
        //        if(dateList != null && dateList.size()>0){
        //            dateList.get(0).setName("当月");
        //            resultList.add((dateList != null && dateList.size()>0)?dateList.get(0): null);
        //        }
        Map<String, Object> dateList = benchmarkingService.cndbthb(companys, type, wpid, beginDate, endDate, pageNum, pageSize);
        if (dateList != null && dateList.containsKey("resultList")) {
            List<WxsslVo> currentMonthList = (List<WxsslVo>) dateList.get("resultList");
            if (currentMonthList.size() > 0) {
                WxsslVo firstItem = currentMonthList.get(0);
                firstItem.setName("当月");
                resultList.add(firstItem);
            }
        }

        String previousMonthEndDate = DateUtils.toDate1(DateUtils.getYestmonthLastday(date));
        String previousMonthBeginDate = DateUtils.toDate1(DateUtils.getFirstAndLastDayOfMonth(DateUtils.parseDate(previousMonthEndDate)));

        Map<String, Object> previousMonthList = benchmarkingService.cndbthb(companys, type, wpid, previousMonthBeginDate, previousMonthEndDate, pageNum, pageSize);
        if (previousMonthList != null && previousMonthList.containsKey("resultList")) {
            List<WxsslVo> previousMonthData = (List<WxsslVo>) previousMonthList.get("resultList");
            if (previousMonthData.size() > 0) {
                WxsslVo firstItem = previousMonthData.get(0);
                firstItem.setName("环比");
                resultList.add(firstItem);
            }
        }

        Date previousYearDate = DateUtils.subOneYear(date);
        String previousYearBeginDate = DateUtils.toDate1(DateUtils.getMonthFirstZero(DateUtils.toDate1(previousYearDate)));
        String previousYearEndDate = DateUtils.toDate1(DateUtils.getMonthLast(previousYearDate));

        Map<String, Object> previousYearList = benchmarkingService.cndbthb(companys, type, wpid, previousYearBeginDate, previousYearEndDate, pageNum, pageSize);
        if (previousYearList != null && previousYearList.containsKey("resultList")) {
            List<WxsslVo> previousYearData = (List<WxsslVo>) previousYearList.get("resultList");
            if (previousYearData.size() > 0) {
                WxsslVo firstItem = previousYearData.get(0);
                firstItem.setName("同比");
                resultList.add(firstItem);
            }
        }


        SortUtils.sort(resultList, "llfdl", SortUtils.DESC);
        for (int i = 0; i < resultList.size(); i++) {
            resultList.get(i).setZhpm(i + 1);
        }
        SortUtils.sort(resultList, "fdl", SortUtils.DESC);
        for (int i = 0; i < resultList.size(); i++) {
            resultList.get(i).setFdlpm(i + 1);
        }
        SortUtils.sort(resultList, "fnlyl", SortUtils.DESC);
        for (int i = 0; i < resultList.size(); i++) {
            resultList.get(i).setFnlylpm(i + 1);
        }
        SortUtils.sort(resultList, "gzssdl", SortUtils.ASC);
        for (int i = 0; i < resultList.size(); i++) {
            resultList.get(i).setGzssdlpm(i + 1);
        }
        SortUtils.sort(resultList, "gzssl", SortUtils.ASC);
        for (int i = 0; i < resultList.size(); i++) {
            resultList.get(i).setGzsslpm(i + 1);
        }
        SortUtils.sort(resultList, "jxssdl", SortUtils.ASC);
        for (int i = 0; i < resultList.size(); i++) {
            resultList.get(i).setJxssdlpm(i + 1);
        }
        SortUtils.sort(resultList, "jxssl", SortUtils.ASC);
        for (int i = 0; i < resultList.size(); i++) {
            resultList.get(i).setJxsslpm(i + 1);
        }
        SortUtils.sort(resultList, "xdssdl", SortUtils.ASC);
        for (int i = 0; i < resultList.size(); i++) {
            resultList.get(i).setXdssdlpm(i + 1);
        }
        SortUtils.sort(resultList, "qfl", SortUtils.ASC);
        for (int i = 0; i < resultList.size(); i++) {
            resultList.get(i).setQflpm(i + 1);
        }
        SortUtils.sort(resultList, "xnssdl", SortUtils.ASC);
        for (int i = 0; i < resultList.size(); i++) {
            resultList.get(i).setXnssdlpm(i + 1);
        }
        SortUtils.sort(resultList, "xnssl", SortUtils.ASC);
        for (int i = 0; i < resultList.size(); i++) {
            resultList.get(i).setXnsslpm(i + 1);
        }
        SortUtils.sort(resultList, "slssdl", SortUtils.ASC);
        for (int i = 0; i < resultList.size(); i++) {
            resultList.get(i).setSlssdlpm(i + 1);
        }
        SortUtils.sort(resultList, "slssl", SortUtils.ASC);
        for (int i = 0; i < resultList.size(); i++) {
            resultList.get(i).setSlsslpm(i + 1);
        }

        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }


    @GetMapping("/cjdb2")
    @ResponseBody
    @ApiOperation(value = "场际对标", notes = "场际对标")
    public R cjdb2(@RequestParam(value = "companys", required = true) String companys,
                  @RequestParam(value = "type", required = true) String type,
                  @RequestParam(value = "wpids", required = true) String wpids,
                  @RequestParam(value = "beginDate", required = true) String beginDate,
                  @RequestParam(value = "endDate", required = true) String endDate,
                  @RequestParam(value = "target", required = true) String target,
                  @RequestParam(value = "sort", required = true) String sort,
                  @RequestParam(value = "pageNum", required = true) int pageNum,
                  @RequestParam(value = "pageSize", required = true) int pageSize
    ) {
        Map<String, Object> resultList = benchmarkingService.cjdb2(companys, type, wpids, beginDate, endDate, target, sort, pageNum, pageSize);

        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }


    @GetMapping("/cjdb")
    @ResponseBody
    @ApiOperation(value = "场际对标", notes = "场际对标")
    public R cjdb(@RequestParam(value = "companys") String companys,
                  @RequestParam(value = "type") String type,
                  @RequestParam(value = "wpids") String wpids,
                  @RequestParam(value = "beginDate") String beginDate,
                  @RequestParam(value = "endDate") String endDate,
                  @RequestParam(value = "target") String target,
                  @RequestParam(value = "sort") String sort,
                  @RequestParam(value = "pageNum") int pageNum,
                  @RequestParam(value = "pageSize") int pageSize
    ) {
        Map<String, Object> resultList = benchmarkingService.cjdb(companys, type, wpids, beginDate, endDate, target, sort, pageNum, pageSize);

        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }


    @GetMapping("/xmdb2")
    @ResponseBody
    @ApiOperation(value = "项目对标", notes = "项目对标")
    public R xmdb2(@RequestParam(value = "companys", required = true) String companys,
                  @RequestParam(value = "type", required = true) String type,
                  @RequestParam(value = "wpids", required = true) String wpids,
                  @RequestParam(value = "projectids", required = true) String projectids,
                  @RequestParam(value = "beginDate", required = true) String beginDate,
                  @RequestParam(value = "endDate", required = true) String endDate,
                  @RequestParam(value = "target", required = true) String target,
                  @RequestParam(value = "sort", required = true) String sort,
                  @RequestParam(value = "pageNum", required = true) int pageNum,
                  @RequestParam(value = "pageSize", required = true) int pageSize
    ) {
        Map<String, Object> resultList = benchmarkingService.xmdb2(companys, type, wpids, projectids, beginDate, endDate, target, sort, pageNum, pageSize);

        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }


    @GetMapping("/xmdb")
    @ResponseBody
    @ApiOperation(value = "项目对标", notes = "项目对标")
    public R xmdb(@RequestParam(value = "companys") String companys,
                  @RequestParam(value = "type") String type,
                  @RequestParam(value = "wpids") String wpids,
                  @RequestParam(value = "projectids") String projectids,
                  @RequestParam(value = "beginDate") String beginDate,
                  @RequestParam(value = "endDate") String endDate,
                  @RequestParam(value = "target") String target,
                  @RequestParam(value = "sort") String sort,
                  @RequestParam(value = "pageNum") int pageNum,
                  @RequestParam(value = "pageSize") int pageSize
    ) {
        Map<String, Object> resultList = benchmarkingService.xmdb(companys, type, wpids, projectids, beginDate, endDate, target, sort, pageNum, pageSize);

        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }



    @GetMapping("/xldb2")
    @ResponseBody
    @ApiOperation(value = "线路对标", notes = "线路对标")
    public R xldb2(@RequestParam(value = "companys", required = true) String companys,
                  @RequestParam(value = "type", required = true) String type,
                  @RequestParam(value = "wpids", required = true) String wpids,
                  @RequestParam(value = "projectids", required = true) String projectids,
                  @RequestParam(value = "lineids", required = true) String lineids,
                  @RequestParam(value = "beginDate", required = true) String beginDate,
                  @RequestParam(value = "endDate", required = true) String endDate,
                  @RequestParam(value = "target", required = true) String target,
                  @RequestParam(value = "sort", required = true) String sort,
                  @RequestParam(value = "pageNum", required = true) int pageNum,
                  @RequestParam(value = "pageSize", required = true) int pageSize
    ) {
        Map<String, Object> resultList = benchmarkingService.xldb2(companys, type, wpids, projectids, lineids, beginDate, endDate, target, sort, pageNum, pageSize);

        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }


    @GetMapping("/xldb")
    @ResponseBody
    @ApiOperation(value = "线路对标", notes = "线路对标")
    public R xldb(@RequestParam(value = "companys") String companys,
                  @RequestParam(value = "type") String type,
                  @RequestParam(value = "wpids") String wpids,
                  @RequestParam(value = "projectids") String projectids,
                  @RequestParam(value = "lineids") String lineids,
                  @RequestParam(value = "beginDate") String beginDate,
                  @RequestParam(value = "endDate") String endDate,
                  @RequestParam(value = "target") String target,
                  @RequestParam(value = "sort") String sort,
                  @RequestParam(value = "pageNum") int pageNum,
                  @RequestParam(value = "pageSize") int pageSize
    ) {
        Map<String, Object> resultList = benchmarkingService.xldb(companys, type, wpids, projectids, lineids, beginDate, endDate, target, sort, pageNum, pageSize);

        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }


    /**
     * 对标页面详情
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    @GetMapping("/details")
    @ResponseBody
    @ApiOperation(value = "对标详情", notes = "对标详情")
    public R details(@RequestParam(value = "id", required = true) String id,
                     @RequestParam(value = "beginDate", required = true) String beginDate,
                     @RequestParam(value = "endDate", required = true) String endDate,
                     @RequestParam(value = "target", required = true) String target,
                     @RequestParam(value = "sort", required = true) String sort,
                     @RequestParam(value = "pageNum", required = true) int pageNum,
                     @RequestParam(value = "pageSize", required = true) int pageSize
    ) {
        Map<String, Object> resultList = benchmarkingService.details(id, beginDate, endDate, target, sort, pageNum, pageSize);

        if (StringUtils.isNotNull(resultList)) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.error(ResultMsg.error());
        }
    }


}
