package com.mosty.base.manager.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mosty.base.manager.service.TbJcglKhtjService;
import com.mosty.base.manager.task.TbJcglJxkhTask;
import com.mosty.base.model.entity.jcgl.TbJcglKhtj;
import com.mosty.base.model.query.jcgl.TbJcglKhtjQuery;
import com.mosty.base.model.query.jcgl.TbJcglKhtjStatisticsQuery;
import com.mosty.base.model.vo.jcgl.TbJcglKhtjVo;
import com.mosty.base.utils.DecimalUtils;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.config.Excel.ExcelExp;
import com.mosty.common.config.Excel.ExcelUtil;
import com.mosty.common.config.Excel.ExcelUtilManySheet;
import com.mosty.common.token.JwtSysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@AllArgsConstructor
@RequestMapping("/khtj")
@Api(tags = "绩效考核统计相关接口")
public class TbJcglKhtjController {

    private final TbJcglKhtjService tbJcglKhtjService;

    @GetMapping(value = "/autoTimeJxkh")
    @ApiOperation("启动或者修改定时器绩效考核")
    public ResponseResult<Boolean> autoTimeXzjq() {
        TbJcglJxkhTask.jxkh = !TbJcglJxkhTask.jxkh;
        return ResponseResult.success(TbJcglJxkhTask.jxkh);
    }

    @PostMapping("/builder")
    @JwtSysUser
    @ApiOperation(value = "统计某一天的数据")
    public ResponseResult<Void> addKhbm(String time) {
        this.tbJcglKhtjService.builder(time);
        return ResponseResult.success();
    }

    @GetMapping("/getPageList")
    @JwtSysUser
    @ApiOperation(value = "条件查询列表信息")
    public ResponseResult<IPage<TbJcglKhtjVo>> getPageList(TbJcglKhtjQuery dto) {
        return ResponseResult.success(this.tbJcglKhtjService.getPageList(dto));
    }

    @GetMapping("/getStatistics")
    @JwtSysUser
    @ApiOperation(value = "统计数据")
    public ResponseResult<Map<String, Object>> getStatistics(TbJcglKhtjStatisticsQuery dto) {
        return ResponseResult.success(this.tbJcglKhtjService.getStatistics(dto));
    }

    @PostMapping("/export")
    @JwtSysUser
    @ApiOperation(value = "导出统计")
    public void getStatistics(HttpServletResponse response, TbJcglKhtjQuery dto) throws Exception {
        List<TbJcglKhtjVo> pageList = this.tbJcglKhtjService.getFxbm(dto);
        if (!CollectionUtils.isEmpty(pageList)) {
            List<ExcelExp> sheet = new ArrayList<>();
            for (TbJcglKhtjVo vo : pageList) {
                dto.setSsbmdm(vo.getSsbmdm());
                List<TbJcglKhtj> list = this.tbJcglKhtjService.getList(dto);
                List<TbJcglKhtjVo> voList = new ArrayList<>();
                if (!CollectionUtils.isEmpty(list)) {
                    TbJcglKhtjVo tj = new TbJcglKhtjVo();
                    tj.setSsbm("统计");
                    tj.setTjrq(list.get(0).getTjrq());
                    for (TbJcglKhtj khtj : list) {
                        TbJcglKhtjVo khtjVo = new TbJcglKhtjVo();
                        BeanUtils.copyProperties(khtj, khtjVo);
                        khtjVo.setWbbl(khtjVo.getSjbb() == 0 ? "0" : getPercent(khtjVo.getSjbb(), khtjVo.getYbb()));
                        khtjVo.setXlbl(khtjVo.getSjxl() == 0 ? "0" : getPercent(khtjVo.getSjxl(), khtjVo.getYxl()));
                        khtjVo.setXllcbl(khtjVo.getSjxllc() == 0 ? "0" : getPercent(khtjVo.getSjxllc(), khtjVo.getYxllc()));
                        khtjVo.setXlscbl(khtjVo.getSjxlsc() == 0 ? "0" : getPercent(khtjVo.getSjxlsc(), khtjVo.getYxlsc()));
                        double kf = 0;
                        //计算三项扣分 得分
                        if ((ObjectUtils.isNotEmpty(khtjVo.getXlkf()) && khtjVo.getXlkf().compareTo(BigDecimal.ZERO) > 0)
                                || (ObjectUtils.isNotEmpty(khtjVo.getXlsckf()) && khtjVo.getXlsckf().compareTo(BigDecimal.ZERO) > 0)
                                || (ObjectUtils.isNotEmpty(khtjVo.getXllckf()) && khtjVo.getXllckf().compareTo(BigDecimal.ZERO) > 0)
                        ) {
                            kf = 0.15;
                        }
                        khtjVo.setSxdf(10 - kf);
                        khtjVo.setSxkf(kf);

//                        khtjVo.setQsbl(khtjVo.getSjqs() == 0 ? "0" : getPercent(khtjVo.getSjqs(), khtjVo.getYqs()));
//                        khtjVo.setCrbl(khtjVo.getSjcr() == 0 ? "0" : getPercent(khtjVo.getSjcr(), khtjVo.getZscr()));
//                        khtjVo.setCcbl(khtjVo.getSjcc() == 0 ? "0" : getPercent(khtjVo.getSjcc(), khtjVo.getzscc()));


                        voList.add(khtjVo);

                        tj.setYbb(khtj.getYbb() + tj.getYbb());
                        tj.setZsbb(khtj.getZsbb() + tj.getZsbb());
                        tj.setSjbb(khtj.getSjbb() + tj.getSjbb());
                        tj.setWbb(khtj.getWbb() + tj.getWbb());
                        tj.setBbkf(DecimalUtils.add(khtj.getBbkf(), tj.getBbkf()));


                        tj.setYxl(khtj.getYxl() + tj.getYxl());
                        tj.setZsxl(khtj.getZsxl() + tj.getZsxl());
                        tj.setSjxl(khtj.getSjxl() + tj.getSjxl());
                        tj.setWxl(khtj.getWxl() + tj.getWxl());
                        tj.setXlkf(DecimalUtils.add(khtj.getXlkf(), tj.getXlkf()));


//
//                        tj.setYqs(khtj.getYqs() + tj.getYqs());
//                        tj.setSjqs(khtj.getSjqs() + tj.getSjqs());
//                        tj.setWqs(khtj.getWqs() + tj.getWqs());

//
//                        tj.setYcr(khtj.getYcr() + tj.getYcr());
//                        tj.setSjcr(khtj.getSjcr() + tj.getSjcr());
//                        tj.setWcr(khtj.getWcr() + tj.getWcr());


//                        tj.setYcc(khtj.getYcc() + tj.getYcc());
//                        tj.setSjcc(khtj.getSjcc() + tj.getSjcc());
//                        tj.setWcc(khtj.getWcc() + tj.getWcc());


                        tj.setYxllc(khtj.getYxllc() + tj.getYxllc());
                        tj.setZsxllc(khtj.getZsxllc() + tj.getZsxllc());
                        tj.setSjxllc(khtj.getSjxllc() + tj.getSjxllc());
                        tj.setWxllc(khtj.getWxllc() + tj.getWxllc());
                        tj.setXllckf(DecimalUtils.add(khtj.getXllckf(), tj.getXllckf()));

                        tj.setYxlsc(khtj.getYxlsc() + tj.getYxlsc());
                        tj.setZsxlsc(khtj.getZsxlsc() + tj.getZsxlsc());
                        tj.setSjxlsc(khtj.getSjxlsc() + tj.getSjxlsc());
                        tj.setWxlsc(khtj.getWxlsc() + tj.getWxlsc());
                        tj.setXlsckf(DecimalUtils.add(khtj.getXlsckf(), tj.getXlsckf()));
                        tj.setSxkf(kf + tj.getSxkf());
                        tj.setSxdf(10 - tj.getSxkf());


                    }
                    tj.setWbbl(tj.getSjbb() == 0 ? "0" : getPercent(tj.getSjbb(), tj.getYbb()));
                    tj.setXlbl(tj.getSjxl() == 0 ? "0" : getPercent(tj.getSjxl(), tj.getYxl()));
//                    tj.setQsbl(tj.getSjqs() == 0 ? "0" : getPercent(tj.getSjqs(), tj.getYqs()));
//                    tj.setCrbl(tj.getSjcr() == 0 ? "0" : getPercent(tj.getSjcr(), tj.getYcr()));
//                    tj.setCcbl(tj.getSjcc() == 0 ? "0" : getPercent(tj.getSjcc(), tj.getYcc()));
                    tj.setXllcbl(tj.getSjxllc() == 0 ? "0" : getPercent(tj.getSjxllc(), tj.getYxllc()));
                    tj.setXlscbl(tj.getSjxlsc() == 0 ? "0" : getPercent(tj.getSjxlsc(), tj.getYxlsc()));
                    voList.add(tj);
                    ExcelExp excelExp = new ExcelExp(vo.getSsbm(), voList, TbJcglKhtjVo.class);
                    sheet.add(excelExp);
                }
            }
            ExcelUtilManySheet<List<ExcelExp>> util2 = new ExcelUtilManySheet<List<ExcelExp>>(sheet);
            util2.exportExcelManySheet(response, sheet);
        }


/*        List<TbJcglKhtj> list = this.tbJcglKhtjService.getList(dto);
        ExcelUtil<TbJcglKhtj> util = new ExcelUtil<>(TbJcglKhtj.class);
        util.exportExcel(response, list, "考核统计", "考核统计");*/
    }

    public String getPercent(int x, int y) {
        double d1 = x * 1.0;
        double d2 = y * 1.0;
        NumberFormat per = NumberFormat.getPercentInstance();
        per.setMaximumFractionDigits(2);
        return per.format(d1 / d2);
    }


}
