package com.chinaweal.aiccs.aiccs.revoke.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chinaweal.aiccs.aiccs.abnormal.entity.EBaseinfo;
import com.chinaweal.aiccs.aiccs.abnormal.entity.TSWrit;
import com.chinaweal.aiccs.aiccs.abnormal.service.EBaseinfoService;
import com.chinaweal.aiccs.aiccs.abnormal.service.TSWritService;
import com.chinaweal.aiccs.aiccs.inspect.service.TSWorkNoService;
import com.chinaweal.aiccs.aiccs.revoke.entity.TsHearingReport;
import com.chinaweal.aiccs.aiccs.revoke.entity.Tsbizrevlist;
import com.chinaweal.aiccs.aiccs.revoke.entity.dto.DecisionDto;
import com.chinaweal.aiccs.aiccs.revoke.entity.dto.HearingReportDto;
import com.chinaweal.aiccs.aiccs.revoke.service.TsbizrevlistService;
import com.chinaweal.aiccs.aiccs.revoke.service.TshearingreportService;
import com.chinaweal.aiccs.common.base.controller.BaseController;
import com.chinaweal.aiccs.common.util.WorkNoUtil;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.rest.ResultCode;
import com.chinaweal.youfool.framework.springboot.util.AssertUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lee
 * @since 2021-04-15
 */
@RestController
@RequestMapping("/tshearingreport")
public class TsHearingReportController extends BaseController {

    @Autowired
    private TshearingreportService tshearingreportService;
    @Autowired
    private TSWorkNoService tsWorkNoService;
    @Autowired
    private TSWritService tsWritService;
    @Autowired
    private TsbizrevlistService tsbizrevlistService;
    @Autowired
    private EBaseinfoService eBaseinfoService;

    /**
     * 生成听证告知书 单个主体操作
     *
     * @param pripid    主体ID
     * @param bizseq    业务号
     * @return
     */
    @PostMapping("insertHearReport")
    public RestResult<?> insertHearReport(String pripid, String bizseq, HttpServletRequest request){
        AssertUtils.isNotBlank(pripid,bizseq);
        AICUser aicUser = getLoginUser(request);
        //生成文书号
        TSWrit tsWritModel = new TSWrit();
        tsWritModel.setWritsid(UUID.randomUUID().toString());
        String workNoType = WorkNoUtil.WORKNOTYPE_32; //批量吊销听证告知书
        tsWritModel.setBizseq(bizseq);
        tsWritModel.setWritsno(tsWorkNoService.getOperatingWrirsno(workNoType, aicUser.getRegionID())); //当前用户的regionID
        tsWritModel.setWritstype(workNoType);
        tsWritModel.setPripid(pripid);
        tsWritModel.setWritsname(WorkNoUtil.getWorkNoTypeName(workNoType));
        tsWritModel.setCreatetime(LocalDateTime.now());
        tsWritService.save(tsWritModel);
        //附件操作
        //生成听证报告表
        TsHearingReport report = new TsHearingReport();
        report.setBizseq(bizseq);
        report.setPripid(pripid);
        tshearingreportService.save(report);
        //修改生成报告状态
        Tsbizrevlist tsbizrevlistModel = new Tsbizrevlist();
        tsbizrevlistModel.setIsHearing("1");//已生成听证报告
        tsbizrevlistService.update(tsbizrevlistModel,new LambdaQueryWrapper<Tsbizrevlist>().eq(Tsbizrevlist::getBizseq,bizseq).eq(Tsbizrevlist::getPripid,pripid));

        return OK;
    }

    /**
     * 一键批量生成听证告知书
     *
     * @param bizseq    业务号
     * @return
     */
    @PostMapping("insertHearReportBatch")
    public RestResult<?> insertHearReportBatch(String bizseq, HttpServletRequest request){
        AssertUtils.isNotBlank(bizseq);
        AICUser aicUser = getLoginUser(request);
        // 过滤已移除主体，找出还未生成听证告知书的主体
        List<Tsbizrevlist> tsbizrevlists = tsbizrevlistService.list(new LambdaQueryWrapper<Tsbizrevlist>()
                .eq(Tsbizrevlist::getBizseq, bizseq)
                .eq(Tsbizrevlist::getIsRemove, "0")
                .eq(Tsbizrevlist::getIsHearing,"0"));
        if(tsbizrevlists.size() == 0){
            return RestResult.error(ResultCode.DATA_NONE,"已无主体需要生成听证报告，请勿重复操作！");
        }
        for (Tsbizrevlist tsbizrevlist : tsbizrevlists) {
            String pripid = tsbizrevlist.getPripid();
            //生成文书号
            TSWrit tsWritModel = new TSWrit();
            tsWritModel.setWritsid(UUID.randomUUID().toString());
            String workNoType = WorkNoUtil.WORKNOTYPE_32; //批量吊销听证告知书
            tsWritModel.setBizseq(bizseq);
            tsWritModel.setWritsno(tsWorkNoService.getOperatingWrirsno(workNoType, aicUser.getRegionID())); //当前用户的regionID
            tsWritModel.setWritstype(workNoType);
            tsWritModel.setPripid(pripid);
            tsWritModel.setWritsname(WorkNoUtil.getWorkNoTypeName(workNoType));
            tsWritModel.setCreatetime(LocalDateTime.now());
            tsWritService.save(tsWritModel);
            //附件操作
            //生成听证报告表
            TsHearingReport report = new TsHearingReport();
            report.setBizseq(bizseq);
            report.setPripid(pripid);
            tshearingreportService.save(report);
            //修改生成报告状态
            Tsbizrevlist tsbizrevlistModel = new Tsbizrevlist();
            tsbizrevlistModel.setIsHearing("1");//已生成听证报告
            tsbizrevlistService.update(tsbizrevlistModel,new LambdaQueryWrapper<Tsbizrevlist>().eq(Tsbizrevlist::getBizseq,bizseq).eq(Tsbizrevlist::getPripid,pripid));
        }

        return OK;
    }

    /**
     * 批量生成决定书
     *
     * @param bizseq    业务号
     * @return
     */
    @PostMapping("insertDecision")
    public RestResult<?> insertDecision(String bizseq, HttpServletRequest request){
        AssertUtils.isNotBlank(bizseq);
        AICUser aicUser = getLoginUser(request);
        //查找过滤已移除的主体
        List<Tsbizrevlist> tsbizrevlists = tsbizrevlistService.list(new LambdaQueryWrapper<Tsbizrevlist>().eq(Tsbizrevlist::getBizseq, bizseq).eq(Tsbizrevlist::getIsRemove, "0"));
        if(tsbizrevlists.size() == 0){
            return RestResult.error(ResultCode.DATA_NONE);
        }

        for (Tsbizrevlist tsbizrevlist : tsbizrevlists) {
            //附件操作
            //生成文书号
            TSWrit tsWritModel = new TSWrit();
            tsWritModel.setWritsid(UUID.randomUUID().toString());
            String workNoType = WorkNoUtil.WORKNOTYPE_33; //批量吊销行政处罚决定书
            tsWritModel.setBizseq(bizseq);
            tsWritModel.setWritsno(tsWorkNoService.getOperatingWrirsno(workNoType, aicUser.getRegionID())); //当前用户的regionID
            tsWritModel.setWritstype(workNoType);
            tsWritModel.setPripid(tsbizrevlist.getPripid());
            tsWritModel.setWritsname(WorkNoUtil.getWorkNoTypeName(workNoType));
            tsWritModel.setCreatetime(LocalDateTime.now());
            //文书相对路径
            //保存
            tsWritService.save(tsWritModel);
        }
        return OK;
    }

    /**
     * 获取决定书列表
     *
     * @param bizseq   业务号
     * @param request
     * @return
     */
    @GetMapping("getDecisionList")
    public RestResult<?> getDecisionList(String bizseq,HttpServletRequest request) {
        AssertUtils.isNotBlank(bizseq);
        AICUser aicUser = getLoginUser(request);
        //查找过滤已移除的主体
        List<Tsbizrevlist> tsbizrevlists = tsbizrevlistService.list(new LambdaQueryWrapper<Tsbizrevlist>().eq(Tsbizrevlist::getBizseq, bizseq).eq(Tsbizrevlist::getIsRemove, "0"));
        if(tsbizrevlists.size() == 0){
            return RestResult.error(ResultCode.DATA_NONE);
        }
        List<DecisionDto> decisionDtoList = new ArrayList<>();
        for (Tsbizrevlist tsbizrevlist : tsbizrevlists) {
            //封装主体信息
            String pripid = tsbizrevlist.getPripid();
            EBaseinfo eBaseinfo = eBaseinfoService.getOne(new LambdaQueryWrapper<EBaseinfo>().eq(EBaseinfo::getPripid, pripid));
            DecisionDto decisionDto = new DecisionDto();
            BeanUtils.copyProperties(eBaseinfo,decisionDto);

            TSWrit writQueryModel = new TSWrit();//获取文书号
            writQueryModel.setWritstype(WorkNoUtil.WORKNOTYPE_33);
            writQueryModel.setPripid(pripid);
            writQueryModel.setBizseq(bizseq);
            List<TSWrit> writs = tsWritService.listTswrit(writQueryModel);
            if (writs.size() == 0) {
                return RestResult.ok(decisionDtoList);
            }
            TSWrit writ = writs.get(0);
            decisionDto.setWritsno(writ.getWritsno());
            decisionDto.setWritsname(writ.getWritsname());

            decisionDtoList.add(decisionDto);
        }
        return RestResult.ok(decisionDtoList);
    }

    /**
     *  获取听证报告详情
     *
     * @param pripid  主体id
     * @param bizseq  业务号
     * @return
     */
    @GetMapping("getHearingReport")
    public RestResult<?> getHearingReport(String pripid,String bizseq,HttpServletRequest request){
        AICUser aicUser = getLoginUser(request);
        AssertUtils.isNotBlank(pripid,bizseq);
        TsHearingReport hearingReport = tshearingreportService.getOne(new LambdaQueryWrapper<TsHearingReport>().eq(TsHearingReport::getBizseq, bizseq).eq(TsHearingReport::getPripid, pripid));
        // 单个新增
        if(hearingReport == null ){

            //生成文书号
            TSWrit tsWritModel = new TSWrit();
            tsWritModel.setWritsid(UUID.randomUUID().toString());
            String workNoType = WorkNoUtil.WORKNOTYPE_32; //批量吊销听证告知书
            tsWritModel.setBizseq(bizseq);
            tsWritModel.setWritsno(tsWorkNoService.getOperatingWrirsno(workNoType, aicUser.getRegionID())); //当前用户的regionID
            tsWritModel.setWritstype(workNoType);
            tsWritModel.setPripid(pripid);
            tsWritModel.setWritsname(WorkNoUtil.getWorkNoTypeName(workNoType));
            tsWritModel.setCreatetime(LocalDateTime.now());
            tsWritService.save(tsWritModel);
            //附件操作
            //生成听证报告表
            TsHearingReport report = new TsHearingReport();
            report.setBizseq(bizseq);
            report.setPripid(pripid);
            tshearingreportService.save(report);
            //修改生成报告状态
            Tsbizrevlist tsbizrevlistModel = new Tsbizrevlist();
            tsbizrevlistModel.setIsHearing("1");//已生成听证报告
            tsbizrevlistService.update(tsbizrevlistModel,new LambdaQueryWrapper<Tsbizrevlist>().eq(Tsbizrevlist::getBizseq,bizseq).eq(Tsbizrevlist::getPripid,pripid));
            hearingReport = tshearingreportService.getOne(new LambdaQueryWrapper<TsHearingReport>().eq(TsHearingReport::getBizseq, bizseq).eq(TsHearingReport::getPripid, pripid));
        }
        HearingReportDto reportDto = new HearingReportDto();
        BeanUtils.copyProperties(hearingReport,reportDto);
        //主体信息
        EBaseinfo eBaseinfo = eBaseinfoService.getOne(new LambdaQueryWrapper<EBaseinfo>().eq(EBaseinfo::getPripid, pripid));
        reportDto.setName(eBaseinfo.getName());
        reportDto.setPhone(eBaseinfo.getPhone());
        reportDto.setEstdate(eBaseinfo.getEstdate());
        reportDto.setEntname(eBaseinfo.getEntname());

        TSWrit writQueryModel = new TSWrit();//获取文书号
        writQueryModel.setWritstype(WorkNoUtil.WORKNOTYPE_32);
        writQueryModel.setPripid(pripid);
        writQueryModel.setBizseq(bizseq);
        List<TSWrit> writs = tsWritService.listTswrit(writQueryModel);
        if( writs.size() > 0) {
            TSWrit writ = writs.get(0);
            reportDto.setWritsno(writ.getWritsno());
            reportDto.setWritsname(writ.getWritsname());
        }
        return RestResult.ok(reportDto);
    }


    /**
     * 更新听证报告
     *
     * @param tsHearingReportModel 听证报告信息
     * @param bizseq               业务号
     * @param request
     * @return
     */
    @PostMapping("saveHearingReport")
    public RestResult<?> saveHearingReport(TsHearingReport tsHearingReportModel, String bizseq, HttpServletRequest request){
        AICUser aicUser = getLoginUser(request);
        if(tsHearingReportModel == null){
            return RestResult.error(ResultCode.PARAM_IS_BLANK);
        }
        boolean saveOrUpdate = tshearingreportService.saveOrUpdate(tsHearingReportModel);
        return RestResult.ok(saveOrUpdate);
    }
}

