package cn.com.countrygarden.bi.main.controller.ck;

import cn.com.countrygarden.bi.main.controller.BaseController;
import cn.com.countrygarden.bi.main.controller.Time.CkOrgtimedTask;
import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dao.ck.CkCleanerBasicMapper;
import cn.com.countrygarden.bi.main.dao.ck.CkCleanerExpandMapper;
import cn.com.countrygarden.bi.main.dto.ck.CkCleanerQueyr;
import cn.com.countrygarden.bi.main.dto.ck.CkCleanerTableVo;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.ck.CkCleanerBasic;
import cn.com.countrygarden.bi.main.model.ck.CkCleanerFieldConfig;
import cn.com.countrygarden.bi.main.service.ck.CkCleanerExpandService;
import cn.com.countrygarden.bi.main.util.ExcelCopyUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.time.LocalDate;
import java.util.List;


/**
 * 基础数据表-业务逻辑层实现
 *
 * @author 李森林
 * @email www.justintoforest@gamail.com
 * @url
 * @date 2020-04-13
 */
@RestController("CkCleanerBasicController")
@RequestMapping("/api/ckCleaner")
public class CkCleanerController extends BaseController {
    @Autowired
    private CkCleanerExpandService ckCleanerService;
    @Autowired
    private CkCleanerExpandMapper ckCleanerExpandMapper;
    @Autowired
    private CkCleanerBasicMapper ckCleanerBasicMapper;

    @GetMapping(value = "/config", produces = "application/json;charset=utf-8")
    public Result config(CkCleanerFieldConfig ckCleanerFieldConfig,
                         @RequestParam(value = "role", required = false) String role,
                         HttpServletRequest request) {
        return ckCleanerService.getDTableConfig(role,getAccessToken(request));
    }

    @GetMapping(value = "/list", produces = "application/json;charset=utf-8")
    public Result list(CkCleanerQueyr cleanerQueyr,
                       HttpServletRequest request) {
        return ckCleanerService.queryPageList(cleanerQueyr,getAccessToken(request));
    }


    @PostMapping(value = "/addOrEdit")
    public Result addOrEdit(@RequestParam(value = "data", required = true) String data,
                            HttpServletRequest request) {
        return ckCleanerService.addOrEdit(data, getAccessToken(request));
    }


    @DeleteMapping(value = "/del")
    public Result del(@RequestParam(value = "basicIds", required = true) List<Integer> basicIds,
                      @RequestParam(value = "dataClassification", required = false) String dataClassification,
                      HttpServletRequest request) {
        return ckCleanerService.del(basicIds,dataClassification,getAccessToken(request));
    }

    @GetMapping(value = "/getRegionDate", produces = "application/json;charset=utf-8")
    public Result getRegionDate(HttpServletRequest request) {

        return Response.response(ckCleanerService.getRegionDate(getAccessToken(request)));
    }

    @GetMapping(value = "/getPojDate", produces = "application/json;charset=utf-8")
    public Result getPojDate(HttpServletRequest request,
                             @RequestParam(value = "organCode",required = true) String organCode) {

        return Response.response(ckCleanerService.getPojDate(getAccessToken(request),organCode));
    }

    @PostMapping(value = "/inherit", produces = "application/json;charset=utf-8")
    public Result inheritInfo(CkCleanerBasic ckCleanerBasic) {
        return Response.response(ckCleanerService.inheritInfo(ckCleanerBasic));
    }

    //添加对外配置
    @PostMapping(value = "/addckexpand", produces = "application/json;charset=utf-8")
    public Result addckexpand(@RequestParam(value = "basicId",required = true) Integer basicId,HttpServletRequest request
            ,@RequestParam(value = "data",required = false) String data) {
        return Response.response(ckCleanerService.addckexpand(basicId,data,getAccessToken(request)));
    }

    //查询对外配置
    @GetMapping(value = "/ckexpandkedKeys", produces = "application/json;charset=utf-8")
    public Result ckexpandkedKeys(@RequestParam(value = "basicId") Integer basicId) {
        return Response.response(ckCleanerService.ckexpandkedKeys(basicId));
    }

    //对外配置接口
    @GetMapping(value = "/ckexpand", produces = "application/json;charset=utf-8")
    public Result ckexpand(@RequestParam(value = "commId") Integer[] commId,
                           @RequestParam(value = "year") Integer year,
                           @RequestParam(value = "month") Integer month) {
        return Response.response(ckCleanerService.ckexpand(commId,year,month));
    }

    //对外配置数据同步接口
    @GetMapping(value = "/allData", produces = "application/json;charset=utf-8")
    public Result allData(@RequestParam(value = "year", required = false) Integer year,
                          @RequestParam(value = "month", required = false) Integer month,
                          @RequestParam(value="page",required=false,defaultValue="1") int page,
                          @RequestParam(value="size",required=false,defaultValue="10") int size,
                          @RequestParam(value="updateDate",required=false) String updateDate) {
        return ckCleanerService.allData(year,month,page,size,updateDate);
    }

    @GetMapping(value = "/statisticsProject", produces = "application/json;charset=utf-8")
    public Result statisticsProject(@RequestParam(value = "month", required = false) Integer month,
                                    @RequestParam(value = "year", required = false) Integer year,
                                    @RequestParam(value = "orgCode", required = false) String orgCode) {
        return ckCleanerService.statisticsProject(year,month,orgCode);
    }

    @GetMapping(value = "/statisProjDetad", produces = "application/json;charset=utf-8")
    public Result statisProjDetad(@RequestParam(value = "page", required = false, defaultValue = "10") int page,
                                  @RequestParam(value = "size", required = false, defaultValue = "1") int size,
                                  @RequestParam(value = "type",required = false) String type,
                                  @RequestParam(value = "month", required = false) Integer month,
                                  @RequestParam(value = "year", required = false) Integer year,
                                  @RequestParam(value = "orgCode", required = false) String orgCode) {
        return ckCleanerService.statisProjDetad(year,month,orgCode,type,page,size);
    }

   @Autowired
    private CkOrgtimedTask ckOrgtimedTask;

    @RequestMapping(value = "/ckOrgtimed", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public void ckOrgtimed(HttpServletRequest request, HttpServletResponse response){
        ckOrgtimedTask.doResignation();
    }


    //导出录入和未录入数据
    @GetMapping(value = "/exportEntered", produces = "multipart/form-data")
    public void exportEntered(
            @RequestParam(value = "month", required = false) Integer month,
            @RequestParam(value = "year", required = false) Integer year,
            @RequestParam(value = "orgCode", required = false) String orgCode,
            @RequestParam(value = "entered", required = false) String entered,
            @RequestParam(value = "notEntered", required = false) String notEntered, AccessToken accessToken, HttpServletRequest request, HttpServletResponse response) throws ParseException, IOException {
        LocalDate date= LocalDate.now();
        int YesValue=0;
        int MonthValue=0;
        if(date.getMonthValue()-1==0){
            YesValue=date.getYear()-1;
            MonthValue=12;
        }else {
            YesValue=date.getYear();
            MonthValue=date.getMonthValue()-1;
        }

        if(month!=null&&year!=null){
            YesValue=year;
            MonthValue=month;
            MonthValue=MonthValue-1;
            if(MonthValue==0){
                MonthValue=12;
                YesValue=YesValue-1;
            }
        }
        List<CkCleanerTableVo> enList=ckCleanerExpandMapper.statisProjDetad(YesValue,MonthValue,orgCode);
        List<CkCleanerTableVo> notEnList=ckCleanerBasicMapper.statisProjDetadNot(YesValue,MonthValue,orgCode);

        ExportParams params = new ExportParams("上月区域已录入情况", "已录入", ExcelType.XSSF);
        XSSFWorkbook workbook = (XSSFWorkbook) ExcelExportUtil.exportExcel(params, CkCleanerTableVo.class, enList);

        ExportParams params2 = new ExportParams("上月区域未录入情况", "未录入", ExcelType.XSSF);
        XSSFWorkbook workbook2 = (XSSFWorkbook) ExcelExportUtil.exportExcel(params2, CkCleanerTableVo.class, notEnList);

        workbook.createSheet("未录入");
        ExcelCopyUtil.copySheet(workbook,workbook2.getSheetAt(0),workbook.getSheetAt(1),true);

        downloadExcelData(workbook, "上月区域录入情况");

    }

}