package com.hxhq.business.service.impl;

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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hxhq.business.domain.*;
import com.hxhq.business.service.ISystemLogService;
import com.hxhq.business.utils.lang.GyzjJcnrUtil;
import com.hxhq.common.core.domain.MpBaseEntity;
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.GspJcgjMapper;
import com.hxhq.business.service.IGspJcgjService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 供试品管理-稽查轨迹Service业务层处理
 *
 * @author hxhq
 * @date 2025-12-30
 */
@Service
public class GspJcgjServiceImpl extends ServiceImpl<GspJcgjMapper, GspJcgj> implements IGspJcgjService
{

    @Autowired
    private ISystemLogService systemLogService;

    /**
     * 查询供试品管理-稽查轨迹列表
     *
     * @param gspJcgj 供试品管理-稽查轨迹
     * @return 供试品管理-稽查轨迹
     */
    @Override
    public List<GspJcgj> queryList(GspJcgj gspJcgj)
    {
        LambdaQueryWrapper<GspJcgj> queryWrapper = new LambdaQueryWrapper<>();
        if (gspJcgj.getGspId() == null || gspJcgj.getGspId().longValue() < 0) {
            throw new SecurityException("供试品id不能为空");
        }
        queryWrapper.eq(GspJcgj::getGspId, gspJcgj.getGspId());
        if (gspJcgj.getJcgjlx() != null && gspJcgj.getJcgjlx().intValue() > 0) {
            queryWrapper.eq(GspJcgj::getJcgjlx, gspJcgj.getJcgjlx());
        }
        if (StringUtils.isNoneBlank(gspJcgj.getJcmc())) {
            queryWrapper.and(p -> p.like(GspJcgj::getJcmc, gspJcgj.getJcmc())
                    .or().like(GspJcgj::getJcgjlx, gspJcgj.getJcmc()));
        }
        queryWrapper.orderByDesc(MpBaseEntity::getId);
        return this.list(queryWrapper);
    }

    @Override
    public void saveJcgj(Gsp gsp, Integer jcgjlx, String jcmc, String jcmcEn, Integer jcmcys, String jcnr, String jcnrEn, SysUser qmr) {
        Date date = new Date();

        GspJcgj jcgj = new GspJcgj();
        jcgj.setGspId(gsp.getId());
        jcgj.setGspBh(gsp.getBh());
        jcgj.setJcgjlx(jcgjlx);
        jcgj.setJcmc(jcmc);
        jcgj.setJcmcEn(jcmcEn);
        jcgj.setJcmcys(jcmcys);
        jcgj.setJcnr(jcnr);
        jcgj.setJcnr(jcnrEn);
        jcgj.setQmrId(qmr.getUserId());
        jcgj.setQmrMc(qmr.getNickName());
        jcgj.setQmrMcEn(qmr.getUserName());
        jcgj.setCreateTime(date);
        this.save(jcgj);

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

    @Override
    @Async
    public void saveBatchWithLog(Gsp gsp, List<GspJcgj> jcgjList) {
        Date date = new Date();
        for (GspJcgj gspJcgj : jcgjList) {
            gspJcgj.setCreateTime(date);
            gspJcgj.setGspBh(gsp.getBh());
        }
        this.saveBatch(jcgjList);
        for (GspJcgj jcgj : jcgjList) {
            systemLogService.saveInfoWithData(gsp.getBh(), gsp.getBh(), jcgj.getJcmc(), jcgj.getJcmcEn(),
                    jcgj.getJcnr(), jcgj.getJcnrEn(), jcgj.getQmrId(), jcgj.getQmrMc(), jcgj.getQmrMcEn(), jcgj.getRemark(), date);
        }
    }

    @Override
    @Async
    public void saveBatchWithLog(List<GspJcgj> jcgjList) {
        Date date = new Date();
        for (GspJcgj jcgj : jcgjList) {
            systemLogService.saveInfoWithData(jcgj.getGspBh(), jcgj.getGspBh(), jcgj.getJcmc(), jcgj.getJcmcEn(),
                    jcgj.getJcnr(), jcgj.getJcnrEn(), jcgj.getQmrId(), jcgj.getQmrMc(), jcgj.getQmrMcEn(), jcgj.getRemark(), date);
        }
    }

    @Override
    @Async
    public void saveWithLog(Gsp gsp, GspJcgj jcgj) {
        Date date = new Date();
        jcgj.setCreateTime(date);
        jcgj.setGspBh(gsp.getBh());
        this.save(jcgj);

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


    @Override
    public GspJcgj getJcgj(Gsp gsp, Integer jcgjlx, String jcmc, Integer jcmcys, String jcnr, String jcnrEn, SysUser qmr, String remark) {
        GspJcgj gspJcgj = new GspJcgj();
        gspJcgj.setGspId(gsp.getId());
        gspJcgj.setGspBh(gsp.getBh());
        gspJcgj.setJcgjlx(jcgjlx);
        gspJcgj.setJcmc(jcmc);
        gspJcgj.setJcmcEn(GyzjJcnrUtil.getEn(jcmc));
        gspJcgj.setJcmcys(jcmcys);
        gspJcgj.setJcnr(jcnr);
        gspJcgj.setJcnr(jcnrEn);
        if(qmr != null) {
            gspJcgj.setQmrId(qmr.getUserId());
            gspJcgj.setQmrMc(qmr.getNickName());
            gspJcgj.setQmrMcEn(qmr.getUserName());
        }

        return gspJcgj;
    }

}
