package com.hxhq.business.service.impl;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hxhq.business.domain.MjyJcgj;
import com.hxhq.business.enums.zykgl.JcgjlxEnum;
import com.hxhq.business.enums.zykgl.JcmcysEnum;
import com.hxhq.business.form.common.SignForm;
import com.hxhq.common.core.exception.ServiceException;
import com.hxhq.common.core.utils.DateUtils;
import com.hxhq.common.core.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.hxhq.business.mapper.StudyJcgjMapper;
import com.hxhq.business.domain.StudyJcgj;
import com.hxhq.business.service.IStudyJcgjService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 试验-稽查轨迹Service业务层处理
 *
 * @author hxhq
 * @date 2025-12-30
 */
@Service
public class StudyJcgjServiceImpl extends ServiceImpl<StudyJcgjMapper, StudyJcgj> implements IStudyJcgjService
{
    /**
     * 查询试验-稽查轨迹列表
     *
     * @param studyJcgj 试验-稽查轨迹
     * @return 试验-稽查轨迹
     */
    @Override
    public List<StudyJcgj> queryList(StudyJcgj studyJcgj)
    {
        if(studyJcgj.getStudyId()==null||studyJcgj.getStudyId().longValue()<0){
            throw new ServiceException("试验id不能为空");
        }
        QueryWrapper<StudyJcgj> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("study_id",studyJcgj.getStudyId());
        if(studyJcgj.getJcgjlx()!=null&&studyJcgj.getJcgjlx().intValue()>0){
            queryWrapper.eq("jcgjlx",studyJcgj.getJcgjlx());
        }
        if (StringUtils.isNoneBlank(studyJcgj.getJcmc())) {
            queryWrapper.and(p -> p.like("`jcmc`", studyJcgj.getJcmc())
                    .or().like("`jcnr`", studyJcgj.getJcmc()));
        }
        queryWrapper.orderByDesc("id");
        return this.list(queryWrapper);
    }

    @Override
    public void saveInfo(Long studyId, JcgjlxEnum jcgjlx, JcmcysEnum jcmcys, String jcnr,String jcnrEn, SignForm signForm) {
        StudyJcgj info = new StudyJcgj();
        info.setStudyId(studyId);
        info.setJcgjlx(jcgjlx.getValue());
        info.setJcmcys(jcmcys.getValue());

        info.setJcnr(jcnr);
        info.setJcnrEn(jcnrEn);

        info.setJcmc(signForm.getQmyy());
        info.setJcmcEn(signForm.getQmyyEn());
        info.setQmrId(signForm.getQmrId());
        info.setQmrMc(signForm.getQmrMc());
        info.setQmrMcEn(signForm.getQmrMcEn());
        info.setRemark(signForm.getRemark());
        save(info);
    }
}
