package com.hxhq.business.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hxhq.business.domain.StudyFormApply;
import com.hxhq.business.domain.StudyFormApplyJcgj;
import com.hxhq.business.domain.StudyFormFill;
import com.hxhq.business.enums.zykgl.JcgjlxEnum;
import com.hxhq.business.service.ISystemLogService;
import com.hxhq.business.utils.lang.StudyFormUtil;
import com.hxhq.business.utils.JctUtil;
import com.hxhq.common.core.exception.ServiceException;
import com.hxhq.common.core.utils.StringUtils;
import com.hxhq.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.hxhq.business.mapper.StudyFormFillJcgjMapper;
import com.hxhq.business.domain.StudyFormFillJcgj;
import com.hxhq.business.service.IStudyFormFillJcgjService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 试验-填报单-稽查轨迹Service业务层处理
 *
 * @author hxhq
 * @date 2025-12-31
 */
@Service
public class StudyFormFillJcgjServiceImpl extends ServiceImpl<StudyFormFillJcgjMapper, StudyFormFillJcgj> implements IStudyFormFillJcgjService
{
    @Autowired
    private ISystemLogService systemLogService;


    /**
     * 查询稽查轨迹列表
     * @param studyFormFillJcgj
     * @return
     */
    @Override
    public List<StudyFormFillJcgj> queryList(StudyFormFillJcgj studyFormFillJcgj){
        QueryWrapper<StudyFormFillJcgj> queryWrapper = Wrappers.query();
        if(studyFormFillJcgj.getFormId()==null||studyFormFillJcgj.getFormId().longValue()<0){
            throw new ServiceException("表单id不能为空");
        }
        queryWrapper.eq("form_id",studyFormFillJcgj.getFormId());
        if(studyFormFillJcgj.getJcgjlx()!=null&&studyFormFillJcgj.getJcgjlx().intValue()>0){
            Integer jcgjlxExport=999;
            if(studyFormFillJcgj.getJcgjlx().intValue()==jcgjlxExport){
                queryWrapper.notIn("jcgjlx", JcgjlxEnum.xg.getValue());
            }else{
                queryWrapper.eq("jcgjlx",studyFormFillJcgj.getJcgjlx());
            }
        }
        if (StringUtils.isNoneBlank(studyFormFillJcgj.getJcmc())) {
            queryWrapper.and(p -> p.like("`jcmc`", studyFormFillJcgj.getJcmc())
                    .or().like("`jcnr`", studyFormFillJcgj.getJcmc()));
        }
        queryWrapper.orderByDesc("id");
        return this.list(queryWrapper);
    }

    /**
     * 批量保存稽查轨迹-并保留日志
     * @param jcgjList
     */
    @Override
    @Async
    public void saveBatchWithLog( List<StudyFormFillJcgj> jcgjList) {
        this.saveBatch(jcgjList);
        for (StudyFormFillJcgj jcgj : jcgjList) {
            systemLogService.saveInfoWithData(jcgj.getFormBh(), jcgj.getFormBh(), jcgj.getJcmc(), jcgj.getJcmcEn(),
                    jcgj.getJcnr(), jcgj.getJcnrEn(), jcgj.getQmrId(), jcgj.getQmrMc(), jcgj.getQmrMcEn(), jcgj.getRemark(),jcgj.getCreateTime());
        }
    }


    /**
     * 新增稽查轨迹
     * @param studyFormFill 表单
     * @param jcgjlx 稽查轨迹类型:1:流程；3：编辑
     * @param jcmc 稽查名称
     * @param jcmcys 稽查名称颜色：1：蓝色；3：红色；5：绿色；7：橙色
     * @param jcnr 稽查内容
     * @param qmr 签名人
     * @param remark 备注
     * @param time 时间
     */
    @Override
    public  void saveJcgj(StudyFormFill studyFormFill,Integer jcgjlx, String jcmc, Integer jcmcys, Map<String, String> jcnr, SysUser qmr, String remark, Date time){
        StudyFormFillJcgj formJcgj = new StudyFormFillJcgj();
        formJcgj.setFormId(studyFormFill.getId());
        formJcgj.setFormBh(studyFormFill.getBdbh());
        formJcgj.setJcgjlx(jcgjlx);
        formJcgj.setJcmc(jcmc);
        formJcgj.setJcmcEn(StudyFormUtil.getEn(jcmc));
        formJcgj.setJcmcys(jcmcys);
        if(jcnr!=null){
            formJcgj.setJcnr(JctUtil.formatStr(jcnr));
            formJcgj.setJcnrEn(StudyFormUtil.getJcnrEn(jcnr));
        }
        String excludeStr="填写";
        if(qmr!=null&&!jcmc.equals(excludeStr)) {
            formJcgj.setQmrId(qmr.getUserId());
            formJcgj.setQmrMc(qmr.getNickName());
            formJcgj.setQmrMcEn(qmr.getUserName());
        }
        if(time!=null){
            formJcgj.setCreateTime(time);
        }
        formJcgj.setRemark(remark);
        this.save(formJcgj);

        systemLogService.saveInfoWithData(studyFormFill.getBdbh(), studyFormFill.getBdbh(), formJcgj.getJcmc(), formJcgj.getJcmcEn(),
                formJcgj.getJcnr(), formJcgj.getJcnrEn(), formJcgj.getQmrId(), formJcgj.getQmrMc(), formJcgj.getQmrMcEn(), formJcgj.getRemark(),time);
    }


    /**
     * 新增稽查轨迹
     * @param time 时间
     * @param studyFormFill 表单
     * @param jcgjlx 稽查轨迹类型:1:流程；3：编辑
     * @param jcmc 稽查名称
     * @param jcmcys 稽查名称颜色：1：蓝色；3：红色；5：绿色；7：橙色
     * @param jcnr 稽查内容
     * @param qmr 签名人
     * @param remark 备注
     */
    @Override
    public StudyFormFillJcgj getJcgj(Date time,StudyFormFill studyFormFill,Integer jcgjlx, String jcmc, Integer jcmcys, Map<String, String> jcnr, SysUser qmr, String remark){
        StudyFormFillJcgj formJcgj = new StudyFormFillJcgj();
        formJcgj.setFormId(studyFormFill.getId());
        formJcgj.setFormBh(studyFormFill.getBdbh());
        formJcgj.setJcgjlx(jcgjlx);
        formJcgj.setJcmc(jcmc);
        formJcgj.setJcmcEn(StudyFormUtil.getEn(jcmc));
        formJcgj.setJcmcys(jcmcys);
        if(jcnr!=null){
            formJcgj.setJcnr(JctUtil.formatStr(jcnr));
            formJcgj.setJcnrEn(StudyFormUtil.getJcnrEn(jcnr));
        }
        if(qmr!=null) {
            formJcgj.setQmrId(qmr.getUserId());
            formJcgj.setQmrMc(qmr.getNickName());
            formJcgj.setQmrMcEn(qmr.getUserName());
        }
        if(time!=null){
            formJcgj.setCreateTime(time);
        }
        formJcgj.setRemark(remark);
        return  formJcgj;
    }

    /**
     * 新增稽查轨迹
     * @param time 时间
     * @param studyFormFill 表单
     * @param jcgjlx 稽查轨迹类型:1:流程；3：编辑
     * @param jcmc 稽查名称
     * @param jcmcys 稽查名称颜色：1：蓝色；3：红色；5：绿色；7：橙色
     * @param jcnr 稽查内容
     * @param jcnrEn 稽查内容英文
     * @param qmr 签名人
     * @param remark 备注
     */
    @Override
    public  StudyFormFillJcgj getJcgj(Date time,StudyFormFill studyFormFill, Integer jcgjlx, String jcmc, Integer jcmcys, String jcnr,String jcnrEn, SysUser qmr, String remark){
        StudyFormFillJcgj formJcgj = new StudyFormFillJcgj();
        formJcgj.setFormId(studyFormFill.getId());
        formJcgj.setFormBh(studyFormFill.getBdbh());
        formJcgj.setJcgjlx(jcgjlx);
        formJcgj.setJcmc(jcmc);
        formJcgj.setJcmcEn(StudyFormUtil.getEn(jcmc));
        formJcgj.setJcmcys(jcmcys);
        formJcgj.setJcnr(jcnr);
        formJcgj.setJcnrEn(jcnrEn);
        if(qmr!=null) {
            formJcgj.setQmrId(qmr.getUserId());
            formJcgj.setQmrMc(qmr.getNickName());
            formJcgj.setQmrMcEn(qmr.getUserName());
        }
        if(time!=null){
            formJcgj.setCreateTime(time);
        }
        formJcgj.setRemark(remark);
        return  formJcgj;
    }

}
