package com.chy.borrow.controller;


import com.chy.borrow.entity.BorrowApply;
import com.chy.borrow.entity.BorrowApplyZjl0754;
import com.chy.borrow.entity.TUnit;
import com.chy.borrow.eum.BorrowStatusEnum;
import com.chy.borrow.mapper.BorrowApplyZjl0754Mapper;
import com.chy.borrow.mapper.BorrowExamineZjl0754Mapper;
import com.chy.borrow.service.BorrowApplyService;
import com.chy.borrow.service.BorrowApplyZjl0754Service;
import com.chy.borrow.service.TUnitService;
import com.chy.borrow.util.FileUploadUtil;
import com.shanghaigeography.Util.DateUtils;
import com.shanghaigeography.Util.SnowflakeConfig;
import com.shanghaigeography.entity.ResultMsg;
import com.shanghaigeography.eum.ResultState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import com.chy.borrow.controller.BaseController;

import java.sql.Timestamp;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 沈晔浩
 * @since 2022-03-25
 */
@Controller
@CrossOrigin
@RequestMapping("/borrowApplyZjl0754")
public class BorrowApplyZjl0754Controller extends BaseController {

    @Autowired
    BorrowApplyZjl0754Service borrowApplyZjl0754Service;
    @Autowired
    BorrowApplyZjl0754Mapper borrowApplyZjl0754Mapper;
    @Autowired
    BorrowExamineZjl0754Mapper borrowExamineZjl0754Mapper;
    @Autowired
    TUnitService tUnitService;

    @RequestMapping("/list")
    @ResponseBody
    public String list(@RequestBody Map map) {
        return borrowApplyZjl0754Service.getList(map);
    }

    @RequestMapping("/getInfoById")
    @ResponseBody
    public String getInfoById(@RequestBody BorrowApplyZjl0754 borrowApplyZjl0754) {
        return borrowApplyZjl0754Service.getInfoById(borrowApplyZjl0754);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public String insert(BorrowApplyZjl0754 borrowApplyZjl0754, @RequestHeader String userId) {
        try {
            String borrowStatus = BorrowStatusEnum.get(1)==null?null:BorrowStatusEnum.get(1).getStatusCodeString();
            borrowApplyZjl0754.setBorrowApplyId(new SnowflakeConfig().snowflakeStringId());
            borrowApplyZjl0754.setApplyUserId(userId);
            borrowApplyZjl0754.setApplyTime(new Date());
            borrowApplyZjl0754.setBorrowStatus(borrowStatus);
            borrowApplyZjl0754Service.save(borrowApplyZjl0754);
            return new ResultMsg<String>().setCode(ResultState.SUCCESS).setMsg("插入成功").toJSON();
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMsg<String>().setCode(ResultState.ERROR).setMsg("添加失败").toJSON();
        }
    }

    @RequestMapping("/submit")
    @ResponseBody
    public String submit(@RequestBody BorrowApplyZjl0754 borrowApplyZjl0754) {
        try {
            BorrowApplyZjl0754 borrowApplyZjl07541 = new BorrowApplyZjl0754();
            borrowApplyZjl07541.setBorrowApplyId(borrowApplyZjl0754.getBorrowApplyId());
            borrowApplyZjl07541.setBorrowStatus(BorrowStatusEnum.STATUS_CONFIRM_DOWNLOAD.getStatusCodeString());
            borrowApplyZjl0754Service.updateById(borrowApplyZjl07541);
            return new ResultMsg<String>().setCode(ResultState.SUCCESS).setMsg("确认获取成功").toJSON();
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMsg<String>().setCode(ResultState.ERROR).setMsg("确认获取失败").toJSON();
        }
    }

    @RequestMapping("/getWord")
    @ResponseBody
    public String getWord(@RequestBody BorrowApplyZjl0754 borrowApplyZjl0754) {
        try {
            String fileName = borrowApplyZjl0754.getBorrowApplyId();
            Map borrowApplyZjl0754_1 = borrowApplyZjl0754Mapper.getInfoById(borrowApplyZjl0754.getBorrowApplyId());
            Map map = new HashMap();
            map.put("borrowApplyId",borrowApplyZjl0754_1.get("borrowApplyId"));
            map.put("unit",borrowApplyZjl0754_1.get("unit"));
            map.put("borrowUser",borrowApplyZjl0754_1.get("borrowUser"));
            map.put("applyReason",borrowApplyZjl0754_1.get("applyReason"));
            map.put("applyTime",borrowApplyZjl0754_1.get("applyTime").toString().split(" ")[0]);
            map.put("borrowData",borrowApplyZjl0754_1.get("borrowData"));
            List<Map> check_1 = borrowExamineZjl0754Mapper.getCheckById(borrowApplyZjl0754.getBorrowApplyId(),"1");
            List<Map> check_2 = borrowExamineZjl0754Mapper.getCheckById(borrowApplyZjl0754.getBorrowApplyId(),"2");
            List<Map> check_3 = borrowExamineZjl0754Mapper.getCheckById(borrowApplyZjl0754.getBorrowApplyId(),"3");
            map.put("examineOpinion1",check_1.get(0).get("examineOpinion"));
            map.put("checkTime1",check_1.get(0).get("examineTime"));
            map.put("examineOpinion2",check_2.get(0).get("examineOpinion"));
            map.put("checkTime2",check_2.get(0).get("examineTime"));
            map.put("transferUnit","到"+tUnitService.getById(Integer.valueOf(check_2.get(0).get("transferUnit").toString())).getName()+"获取数据");
            map.put("isNeedMore",(check_2.get(0).get("isNeedMore")==null?"":check_2.get(0).get("isNeedMore")).equals("1")?"需要领导进一步审批":" ");
            map.put("examineOpinion3",check_3.get(0).get("examineOpinion"));
            map.put("checkTime3",check_3.get(0).get("examineTime"));
            FileUploadUtil.exportWord("D:\\demo.docx","D:\\file",fileName+".docx",map);

            List result = new ArrayList();
            Map map1 = new HashMap();
            map1.put("downloadUrl","http://47.101.207.123:8080/file/"+fileName+".docx");
            result.add(map1);
            return new ResultMsg<>().setCode(ResultState.SUCCESS).setMsg("生成申请单成功").setData(result).toJSON();
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMsg<String>().setCode(ResultState.ERROR).setMsg("生成申请单失败").toJSON();
        }
    }


    public String getBorrowApplyId(){
        String borrowApplyId_before = "fcg"+ DateUtils.getNowTimeStringToDD("yyyyMMdd");
        int count = borrowApplyZjl0754Mapper.getCountByCode(borrowApplyId_before);
        if (count<9){
            return borrowApplyId_before+"000"+(count+1);
        }else if (count<99){
            return borrowApplyId_before+"00"+(count+1);
        }else if(count<999){
            return borrowApplyId_before+"0"+(count+1);
        }else {
            return borrowApplyId_before+(count+1);
        }
    }

    @RequestMapping(value = "/listTempCount")
    @ResponseBody
    public String getCountList() {
        return borrowApplyZjl0754Service.getCountListzj();
    }

    @RequestMapping(value = "/listYMDCount")
    @ResponseBody
    public String getYearCountList(Integer start,Integer num) {
        return borrowApplyZjl0754Service.getApplyTimeCountList(start,num);
    }

    @RequestMapping(value = "/listPeriodCount")
    @ResponseBody
    public String getDayCountList(String start, String end) {

        Timestamp startTime = Timestamp.valueOf(start);

        Timestamp endTime = Timestamp.valueOf(end);

        return borrowApplyZjl0754Service.getApplyTimeByPeriod(startTime, endTime);
    }
}