package com.hxhq.business.service.impl;

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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hxhq.business.domain.Yq;
import com.hxhq.business.service.ISystemLogService;
import com.hxhq.common.core.domain.MpBaseEntity;
import com.hxhq.common.core.utils.StringUtils;
import com.hxhq.common.security.utils.SecurityUtils;
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.YqJcgjMapper;
import com.hxhq.business.domain.YqJcgj;
import com.hxhq.business.service.IYqJcgjService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 仪器管理-稽查轨迹Service业务层处理
 *
 * @author HanLong
 * @date 2025-12-22
 */
@Service
public class YqJcgjServiceImpl extends ServiceImpl<YqJcgjMapper, YqJcgj> implements IYqJcgjService
{

    @Autowired
    private ISystemLogService systemLogService;

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

    @Override
    @Async
    public void saveJcgj(Yq yq, Integer jcgjlx, String jcmc, String jcmcEn, Integer jcmcys, String jcnr, String jcnrEn) {
        Date date = new Date();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        YqJcgj jcgj = new YqJcgj();
        jcgj.setYqId(yq.getId());
        jcgj.setJcgjlx(jcgjlx);
        jcgj.setJcmc(jcmc);
        jcgj.setJcmcEn(jcmcEn);
        jcgj.setJcmcys(jcmcys);
        jcgj.setJcnr(jcnr);
        jcgj.setJcnrEn(jcnrEn);
        jcgj.setQmrId(sysUser.getUserId());
        jcgj.setQmrMc(sysUser.getNickName());
        jcgj.setQmrMcEn(sysUser.getUserName());
        jcgj.setCreateTime(date);
        this.save(jcgj);

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

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

}
