package cn.cslg.report.service.business.InvalidReReport;

import cn.cslg.report.common.model.dto.UploadFileDTO;
import cn.cslg.report.common.model.dto.invalidReReport.InvalidProcessDTO;
import cn.cslg.report.common.model.vo.PersonnelVO;
import cn.cslg.report.common.utils.CacheUtils;
import cn.cslg.report.common.utils.FileUtils;
import cn.cslg.report.common.utils.Response;
import cn.cslg.report.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.report.entity.Template;
import cn.cslg.report.entity.invalidReReport.InvalidProcess;
import cn.cslg.report.entity.invalidReReport.OralExam;
import cn.cslg.report.exception.XiaoShiException;
import cn.cslg.report.mapper.InvalidReReport.InvalidProcessMapper;
import cn.cslg.report.mapper.TemplateMapper;
import cn.cslg.report.service.business.OralExamService;
import cn.cslg.report.service.business.ReportFileService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class InvalidProcessService extends ServiceImpl<InvalidProcessMapper, InvalidProcess> {
    private final CacheUtils cacheUtils;
    private final LoginUtils loginUtils;
    private final FileUtils fileUtils;
    private final ReportFileService reportFileService;
    private final OralExamService oralExamService;
    /**
     * 无效事务添加
     * @param invalidProcessStr
     * @param file
     * @return
     */
    public void addInvalidProcess(String invalidProcessStr, MultipartFile file) {
        InvalidProcessDTO invalidProcessDTO = JSONObject.parseObject(invalidProcessStr, InvalidProcessDTO.class);
        //判断无效事务时间是否为空，无论是文件类型或者口审类型都要时间
        if(invalidProcessDTO.getOccuredTime() != null) {
            //判断无效事务的类型
            //获取当前登陆人信息
            PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
            //添加无效事务
            InvalidProcess invalidProcess = new InvalidProcess();
            //报告id
            invalidProcess.setReportId(invalidProcessDTO.getReportId());
            //发生时间
            invalidProcess.setOccuredTime(invalidProcessDTO.getOccuredTime());
            //创建人
            invalidProcess.setCreateId(personnelVO.getId());
//            invalidProcess.setCreateId(1);
            List<Integer> typeIds = new ArrayList<>();
            //1为无效请求书，2为陈述意见书，3为无效决定书，4为行政诉讼书
            typeIds.add(1);
            typeIds.add(2);
            typeIds.add(3);
            typeIds.add(4);
            //如果类型为0，则为口审记录
            if(invalidProcessDTO.getProcessType().equals(0)){
                //口审记录要判断 参与人,地点是否为空
                if(invalidProcessDTO.getAddress() != null && invalidProcessDTO.getAddress() != "" && invalidProcessDTO.getParticipants() != null && invalidProcessDTO.getParticipants() != ""){
                    //若是口审记录则添加口审记录接着添加无效事务
                    //添加口审记录
                    OralExam oralExam = new OralExam();
                    //地点
                    oralExam.setAddress(invalidProcessDTO.getAddress());
                    //参与人
                    oralExam.setParticipants(invalidProcessDTO.getParticipants());
                    //创建人
                    oralExam.setCreateId(personnelVO.getId());
//                    oralExam.setCreateId(1);
                    oralExam.insert();
                    //流程类型
                    invalidProcess.setProcessType(invalidProcessDTO.getProcessType());
                    //口审记录id
                    invalidProcess.setOralExamId(oralExam.getId());
                    invalidProcess.insert();
                } else {
                    throw new XiaoShiException("参与人，地点不能为空");
                }
            } else if(typeIds.contains(invalidProcessDTO.getProcessType())) {
                //流程类型不为0
                //若是文件则上传文件并添加无效事务
                List<MultipartFile> files = new ArrayList<>();
                files.add(file);
                List<Integer> ids = reportFileService.uploadFiles(files);
                int fileId = ids.get(0);
                invalidProcess.setFileId(fileId);
                invalidProcess.setProcessType(invalidProcessDTO.getProcessType());
                invalidProcess.insert();
            } else {
                throw new XiaoShiException("流程类型错误，不存在该类型！");
            }
        } else {
            throw new XiaoShiException("无效事务时间不能为空！");
        }
    }

    /**
     * 删除无效事务
     * @param id
     */
    public void removeInvalidProcess(Integer id) {
        //根据无效事务id删除无效事务
        LambdaQueryWrapper<InvalidProcess> lw = new LambdaQueryWrapper<>();
        lw.eq(InvalidProcess::getId, id);
        InvalidProcess invalidProcess = this.getOne(lw);
        if(invalidProcess != null){
            //如果流程类型为0，则为口审记录
            if(invalidProcess.getProcessType().equals(0)) {
                oralExamService.removeById(id);
            }
            invalidProcess.deleteById();
        } else {
            throw new XiaoShiException("没有此条数据");
        }
    }



    /**
     * 修改无效事务
     * @param invalidProcessStr
     * @param file
     */
    public void updateInvalidProcess(String invalidProcessStr, MultipartFile file) {
        InvalidProcessDTO iPDTO = JSONObject.parseObject(invalidProcessStr, InvalidProcessDTO.class);
        //判断无效事务时间是否为空，无论是文件类型或者口审类型都要时间
        if(iPDTO.getOccuredTime() != null) {
            //判断无效事务的类型
            //获取当前登陆人信息
            PersonnelVO pVO = cacheUtils.getLoginUser(loginUtils.getId());
            //无效事务需要先查出来
            InvalidProcess invalidProcess = this.getById(iPDTO.getId());
            invalidProcess.setReportId(iPDTO.getReportId());
            invalidProcess.setOccuredTime(iPDTO.getOccuredTime());
            invalidProcess.setCreateId(pVO.getId());
            //创建类型集合
            //1为无效请求书，2为陈述意见书，3为无效决定书，4为行政诉讼书
            List<Integer> typeIds = new ArrayList<>();
            typeIds.add(1);
            typeIds.add(2);
            typeIds.add(3);
            typeIds.add(4);
            //如果类型为0，则为口审记录
            if(iPDTO.getProcessType().equals(0)){
                //口审记录要判断 参与人,地点是否为空
                if(iPDTO.getAddress() != null && iPDTO.getAddress() != "" && iPDTO.getParticipants() != null && iPDTO.getParticipants() != ""){
                    //若是口审记录则添加口审记录接着添加无效事务
                    //口审记录也需要先查出来
                    OralExam oralExam = oralExamService.getById(invalidProcess.getOralExamId());
                    //地点
                    oralExam.setAddress(iPDTO.getAddress());
                    //参与人
                    oralExam.setParticipants(iPDTO.getParticipants());
                    //创建人
                    oralExam.setCreateId(pVO.getId());
//                    oralExam.setCreateId(1);
                    oralExam.updateById();
                    //流程类型
                    invalidProcess.setProcessType(iPDTO.getProcessType());
                    //口审记录id
                    invalidProcess.setOralExamId(oralExam.getId());
                    invalidProcess.updateById();
                } else {
                    throw new XiaoShiException("参与人，地点不能为空");
                }
            } else if(typeIds.contains(iPDTO.getProcessType())) {
                //流程类型不为0
                //若是文件则上传文件并添加无效事务
                List<MultipartFile> files = new ArrayList<>();
                files.add(file);
                List<Integer> ids = reportFileService.uploadFiles(files);
                int fileId = ids.get(0);
                invalidProcess.setFileId(fileId);
                invalidProcess.setProcessType(iPDTO.getProcessType());
                invalidProcess.updateById();
            } else {
                throw new XiaoShiException("流程类型错误，不存在该类型！");
            }
        } else {
            throw new XiaoShiException("无效事务时间不能为空！");
        }
    }

    /**
     * 查找无效事务
     *
     * @param reportId
     * @param orderBy
     * @return
     */
    public List<InvalidProcess> queryInvalidProcess(Integer reportId, Integer orderBy) {
        //根据报告id，根据时间排序返回所有的无效事务。
        LambdaQueryWrapper<InvalidProcess> LW = new LambdaQueryWrapper<>();
        LW.eq(InvalidProcess::getReportId, reportId);
        //根据时间排序
        if(orderBy==1) {
            LW.orderByAsc(InvalidProcess::getOccuredTime);
        } else {
            LW.orderByDesc(InvalidProcess::getOccuredTime);
        }
        List<InvalidProcess> invalidProcessList = this.list(LW);
        return invalidProcessList;
    }
}