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.StudyFormPlan;
import com.hxhq.business.domain.StudyFormPlanJcgj;
import com.hxhq.business.domain.StudyFormPre;
import com.hxhq.business.domain.StudyFormPreJcgj;
import com.hxhq.business.service.ISystemLogService;
import com.hxhq.business.utils.JctUtil;
import com.hxhq.business.utils.lang.StudyFormUtil;
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.StudyFormPreJcgjMapper;
import com.hxhq.business.service.IStudyFormPreJcgjService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 试验-预填单-稽查轨迹Service业务层处理
 *
 * @author hxhq
 * @date 2025-12-31
 */
@Service
public class StudyFormPreJcgjServiceImpl extends ServiceImpl<StudyFormPreJcgjMapper, StudyFormPreJcgj> implements IStudyFormPreJcgjService
{

    @Autowired
    private ISystemLogService systemLogService;


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

    /**
     * 批量保存稽查轨迹-并保留日志
     * @param jcgjList
     */
    @Override
    @Async
    public void saveBatchWithLog( List<StudyFormPreJcgj> jcgjList) {
        this.saveBatch(jcgjList);
        for (StudyFormPreJcgj 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 studyFormPre 表单
     * @param jcgjlx 稽查轨迹类型:1:流程；3：编辑
     * @param jcmc 稽查名称
     * @param jcmcys 稽查名称颜色：1：蓝色；3：红色；5：绿色；7：橙色
     * @param jcnr 稽查内容
     * @param qmr 签名人
     * @param remark 备注
     * @param time 时间
     */
    @Override
    public  void saveJcgj(StudyFormPre studyFormPre, Integer jcgjlx, String jcmc, Integer jcmcys, Map<String, String> jcnr, SysUser qmr, String remark, Date time){
        StudyFormPreJcgj formJcgj = new StudyFormPreJcgj();
        formJcgj.setFormId(studyFormPre.getId());
        formJcgj.setFormBh(studyFormPre.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);
        this.save(formJcgj);
        systemLogService.saveInfoWithData(studyFormPre.getBdbh(), studyFormPre.getBdbh(), formJcgj.getJcmc(), formJcgj.getJcmcEn(),
                formJcgj.getJcnr(), formJcgj.getJcnrEn(), formJcgj.getQmrId(), formJcgj.getQmrMc(), formJcgj.getQmrMcEn(), formJcgj.getRemark(),time);
    }


    /**
     * 新增稽查轨迹
     * @param studyFormPre 表单
     * @param jcgjlx 稽查轨迹类型:1:流程；3：编辑
     * @param jcmc 稽查名称
     * @param jcmcys 稽查名称颜色：1：蓝色；3：红色；5：绿色；7：橙色
     * @param jcnr 稽查内容
     * @param qmr 签名人
     * @param remark 备注
     */
    @Override
    public StudyFormPreJcgj getJcgj(Date time,StudyFormPre studyFormPre,Integer jcgjlx, String jcmc, Integer jcmcys, Map<String, String> jcnr, SysUser qmr, String remark){
        StudyFormPreJcgj formJcgj = new StudyFormPreJcgj();
        formJcgj.setFormId(studyFormPre.getId());
        formJcgj.setFormBh(studyFormPre.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 studyFormPre 表单
     * @param jcgjlx 稽查轨迹类型:1:流程；3：编辑
     * @param jcmc 稽查名称
     * @param jcmcys 稽查名称颜色：1：蓝色；3：红色；5：绿色；7：橙色
     * @param jcnr 稽查内容
     * @param jcnrEn 稽查内容英文
     * @param qmr 签名人
     * @param remark 备注
     */
    @Override
    public  StudyFormPreJcgj getJcgj(Date time,StudyFormPre studyFormPre,Integer jcgjlx, String jcmc, Integer jcmcys, String jcnr,String jcnrEn, SysUser qmr, String remark){
        StudyFormPreJcgj formJcgj = new StudyFormPreJcgj();
        formJcgj.setFormId(studyFormPre.getId());
        formJcgj.setFormBh(studyFormPre.getBdbh());
        formJcgj.setJcgjlx(jcgjlx);
        formJcgj.setJcmc(jcmc);
        formJcgj.setJcmcEn(StudyFormUtil.getEn(jcmc));
        formJcgj.setJcmcys(jcmcys);
        formJcgj.setJcnr(jcnr);
        formJcgj.setJcnrEn(jcnrEn);
        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);
        return  formJcgj;
    }

}
