package com.hxhq.business.service.impl;

import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hxhq.business.domain.GspRkjl;
import com.hxhq.business.service.ISystemLogService;
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.GspRkjlJcgjMapper;
import com.hxhq.business.domain.GspRkjlJcgj;
import com.hxhq.business.service.IGspRkjlJcgjService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 供试品入库记录-稽查轨迹Service业务层处理
 *
 * @author hxhq
 * @date 2025-12-30
 */
@Service
public class GspRkjlJcgjServiceImpl extends ServiceImpl<GspRkjlJcgjMapper, GspRkjlJcgj> implements IGspRkjlJcgjService
{
    @Autowired
    private ISystemLogService systemLogService;

    /**
     * 查询供试品入库记录-稽查轨迹列表
     *
     * @param gspRkjlJcgj 供试品入库记录-稽查轨迹
     * @return 供试品入库记录-稽查轨迹
     */
    @Override
    public List<GspRkjlJcgj> queryList(GspRkjlJcgj gspRkjlJcgj)
    {
        QueryWrapper<GspRkjlJcgj> queryWrapper = Wrappers.query();
        if(gspRkjlJcgj.getGspRkjlId() != null) {
            queryWrapper.eq("gsp_rkjl_id", gspRkjlJcgj.getGspRkjlId());
        }
        queryWrapper.orderByDesc("id");
        return this.list(queryWrapper);
    }

    @Override
    @Async
    public void saveJcgj(GspRkjl gspRkjl, Integer jcgjlx, String jcmc, String jcmcEn, Integer jcmcys, String jcnr, String jcnrEn, SysUser qmr) {
        Date date = new Date();
        GspRkjlJcgj jcgj = new GspRkjlJcgj();
        jcgj.setGspRkjlId(gspRkjl.getId());
        jcgj.setJcgjlx(jcgjlx);
        jcgj.setJcmc(jcmc);
        jcgj.setJcmcEn(jcmcEn);
        jcgj.setJcmcys(jcmcys);
        jcgj.setJcnr(jcnr);
        jcgj.setJcnrEn(jcnrEn);
        jcgj.setQmrId(qmr.getUserId());
        jcgj.setQmrMc(qmr.getNickName());
        jcgj.setQmrMcEn(qmr.getUserName());
        jcgj.setCreateTime(date);
        this.save(jcgj);

        systemLogService.saveInfoWithData(gspRkjl.getMc(), gspRkjl.getMc(), jcgj.getJcmc(), jcgj.getJcmcEn(),
                jcgj.getJcnr(), jcgj.getJcnrEn(), jcgj.getQmrId(), jcgj.getQmrMc(), jcgj.getQmrMcEn(), jcgj.getRemark(), date);
    }

    @Override
    @Async
    public void saveBatchWithLog(GspRkjl gspRkjl, List<GspRkjlJcgj> jcgjList) {
        Date date = new Date();
        for (GspRkjlJcgj gspRkjlJcgj : jcgjList) {
            gspRkjlJcgj.setCreateTime(date);
        }
        this.saveBatch(jcgjList);
        for (GspRkjlJcgj jcgj : jcgjList) {
            systemLogService.saveInfoWithData(gspRkjl.getMc(), gspRkjl.getMc(), jcgj.getJcmc(), jcgj.getJcmcEn(),
                    jcgj.getJcnr(), jcgj.getJcnrEn(), jcgj.getQmrId(), jcgj.getQmrMc(), jcgj.getQmrMcEn(), jcgj.getRemark(), date);
        }
    }

    @Override
    @Async
    public void saveWithLog(GspRkjl gspRkjl, GspRkjlJcgj jcgj) {
        Date date = new Date();
        jcgj.setCreateTime(date);
        this.save(jcgj);
        systemLogService.saveInfoWithData(gspRkjl.getMc(), gspRkjl.getMc(), jcgj.getJcmc(), jcgj.getJcmcEn(),
                jcgj.getJcnr(), jcgj.getJcnrEn(), jcgj.getQmrId(), jcgj.getQmrMc(), jcgj.getQmrMcEn(), jcgj.getRemark(), date);
    }

    @Override
    public GspRkjlJcgj genJcgj(Long gspRkjlId, Integer jcgjlx, String jcmc, String jcmcEn, Integer jcmcys, String jcnr, String jcnrEn, SysUser qmr) {
        GspRkjlJcgj gspRkjlJcgj = new GspRkjlJcgj();
        gspRkjlJcgj.setGspRkjlId(gspRkjlId);
        gspRkjlJcgj.setJcgjlx(jcgjlx);
        gspRkjlJcgj.setJcmc(jcmc);
        gspRkjlJcgj.setJcmcEn(jcmcEn);
        gspRkjlJcgj.setJcmcys(jcmcys);
        gspRkjlJcgj.setJcnr(jcnr);
        gspRkjlJcgj.setJcnrEn(jcnrEn);
        if(qmr != null) {
            gspRkjlJcgj.setQmrId(qmr.getUserId());
            gspRkjlJcgj.setQmrMc(qmr.getNickName());
            gspRkjlJcgj.setQmrMcEn(qmr.getUserName());
        }

        return gspRkjlJcgj;
    }

}
