package com.chinaweal.aiccs.aiccs.test;

import com.chinaweal.aiccs.aiccs.abnormal.entity.TSAbnList;
import com.chinaweal.aiccs.aiccs.abnormal.entity.TSBizSpeList;
import com.chinaweal.aiccs.aiccs.abnormal.entity.TSWrit;
import com.chinaweal.aiccs.aiccs.abnormal.mapper.TSAbnListMapper;
import com.chinaweal.aiccs.aiccs.abnormal.service.TSAttachmentService;
import com.chinaweal.aiccs.aiccs.abnormal.service.TSBizRemListService;
import com.chinaweal.aiccs.aiccs.abnormal.service.TSBizSpeListService;
import com.chinaweal.aiccs.aiccs.abnormal.service.TSWritService;
import com.chinaweal.aiccs.aiccs.business.entity.TSTaskList;
import com.chinaweal.aiccs.aiccs.business.mapper.TSTaskListMapper;
import com.chinaweal.aiccs.aiccs.inspect.entity.TSWorkNo;
import com.chinaweal.aiccs.aiccs.inspect.service.TSWorkNoService;
import com.chinaweal.aiccs.common.util.WorkNoUtil;
import com.chinaweal.aiccs.common.util.filestorage.FileStorageService;
import com.chinaweal.aiccs.crgs.transfor.schedule.DownloadSchedule;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/InitTestController")
public class InitTestController {

    private static final Map<String, String> NO_MAP = new HashMap<>();

    @Resource
    private TSTaskListMapper tsTaskListMapper;
    @Autowired
    private TSBizSpeListService tsBizSpeListService;
    @Resource
    private TSAbnListMapper tsAbnListMapper;
    @Autowired
    private TSWorkNoService tsWorkNoService;
    @Autowired
    private TSWritService tsWritService;
    @Autowired
    private TSBizRemListService tsBizRemListService;
    @Resource
    private DownloadSchedule downloadSchedule;
    @Resource
    private FileStorageService fileStorageService;
    @Autowired
    private TSAttachmentService tsAttachmentService;


    /**
     * 找回列异文书数据接口
     * @return
     */
    @PostMapping("retrieveTswritData")
    public RestResult<?> retrieveTswritData(String date) {
        List<TSTaskList> tsTaskListList =  tsTaskListMapper.queryForNoTswrit(date);
        List<String> pripidlist = new ArrayList<>();
        for (TSTaskList tsTaskListModel : tsTaskListList) {
            pripidlist.add(tsTaskListModel.getBizseqid());
        }
        List<Map<String, String>> list = tsAbnListMapper.statistics(pripidlist);
        for (Map<String, String> map : list) {
            // 获取每个区的初始化文书号
            String decorg = map.get("DECORG");
            String zs = String.valueOf(map.get("sl"));
            TSWorkNo paramModel = new TSWorkNo();
            paramModel.setAicid(decorg);
            paramModel.setWorkNoType("11");
            TSWorkNo tsWorkNoModels = tsWorkNoService.selectTSWorkNoListByTypeComment(paramModel);
            int num = tsWorkNoModels.getCurrentNo().intValue() - Integer.parseInt(zs);
            NO_MAP.put(decorg, num + "");

        }

        for (TSTaskList tsTaskListModel : tsTaskListList) {
            TSBizSpeList paramModel = new TSBizSpeList();
            paramModel.setBizseq(tsTaskListModel.getBizseqid());
            paramModel.setIsRemove("0");
            //批量，业务主体有多个
            List<TSBizSpeList> tsBizSpeLists = tsBizSpeListService.selectBSPexptlist(paramModel);
            for (TSBizSpeList tsBizSpeList : tsBizSpeLists) {
                TSAbnList tsAbnList = tsAbnListMapper.queryByPripidAndDate(tsBizSpeList.getPripid(), date);
                LocalDateTime now = LocalDateTime.now();
                TSWrit tsWritModel = new TSWrit();
                tsWritModel.setWritsid(tsAbnList.getIntowritid());
                String workNoType = WorkNoUtil.WORKNOTYPE_11; //企业列入经营异常名录决定书文号
                tsWritModel.setBizseq(tsBizSpeList.getBizseq());
                //获取用户所在部门辖区码值
                tsWritModel.setAreaCode(tsAbnList.getDecorg());
                String curNo = NO_MAP.get(tsWritModel.getAreaCode());
                tsWritModel.setWritsno(tsWorkNoService.getWriteNo(workNoType, tsWritModel.getAreaCode(), curNo));
                tsWritModel.setWritstype(workNoType);
                tsWritModel.setPripid(tsBizSpeList.getPripid());
                tsWritModel.setWritsname(WorkNoUtil.getWorkNoTypeName(workNoType));
                tsWritModel.setCreatetime(now);
                tsWritModel.setContent(tsBizSpeList.getDecisionContent());
                tsWritService.save(tsWritModel);
                // 文书号加一
                NO_MAP.put(tsWritModel.getAreaCode(), (Integer.parseInt(curNo) + 1 ) + "" );
            }
        }
        return RestResult.ok(NO_MAP);
    }

    @PostMapping("checkWritNoWthSleep")
     public RestResult<?> checkWritNoWthSleep (String type, String code){
        return RestResult.ok(tsBizRemListService.checkWritNoWithSleep(type,code));
    }

    @PostMapping("checkWritNo")
    public RestResult<?> checkWritNo(String type, String code) {
        return RestResult.ok(tsBizRemListService.checkWritNo(type,code));
    }

    @PostMapping("caseDownload")
    public RestResult<?> caseDownload(String start, String end) {
        return RestResult.ok(downloadSchedule.caseDownload(start, end));
    }
    @PostMapping("permitDownload")
    public RestResult<?> permitDownload(String start, String end) {
        return RestResult.ok(downloadSchedule.permitDownload(start, end));
    }

    @PostMapping("getfileList")
    public RestResult<?> getfileList(String path) {
        return RestResult.ok(fileStorageService.getFtpFileNames(path));
    }

    /**
     * 下载双随机文件
     * @param files
     * @param bizseq
     * @return
     */
    @PostMapping("saveClueFile")
    public RestResult<?> saveClueFile(String files, String bizseq) {
        Boolean rs = tsAttachmentService.saveClueFile(files,bizseq);
        return RestResult.ok(rs);
    }

}
