package com.hxhq.business.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.hxhq.business.mapper.SjkcJcgjMapper;
import com.hxhq.business.domain.SjJcgj;
import com.hxhq.business.service.ISjJcgjService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 试剂库存-稽查轨迹Service业务层处理
 *
 * @author HanLong
 * @date 2025-12-22
 */
@Service
public class SjJcgjServiceImpl extends ServiceImpl<SjkcJcgjMapper, SjJcgj> implements ISjJcgjService {
    /**
     * 查询试剂库存-稽查轨迹列表
     *
     * @param sjJcgj 试剂库存-稽查轨迹
     * @return 试剂库存-稽查轨迹
     */
    @Override
    public List<SjJcgj> queryList(SjJcgj sjJcgj) {
        LambdaQueryWrapper<SjJcgj> queryWrapper = new LambdaQueryWrapper<>();
        if (sjJcgj.getSjId() == null || sjJcgj.getSjId().longValue() < 0) {
            throw new SecurityException("试剂id不能为空");
        }
        queryWrapper.eq(SjJcgj::getSjId, sjJcgj.getSjId());
        if (sjJcgj.getJcgjlx() != null && sjJcgj.getJcgjlx().intValue() > 0) {
            queryWrapper.eq(SjJcgj::getJcgjlx, sjJcgj.getJcgjlx());
        }
        if (StringUtils.isNoneBlank(sjJcgj.getJcmc())) {
            queryWrapper.and(p -> p.like(SjJcgj::getJcmc, sjJcgj.getJcmc())
                    .or().like(SjJcgj::getJcgjlx, sjJcgj.getJcmc()));
        }
        queryWrapper.orderByDesc(MpBaseEntity::getId);
        return this.list(queryWrapper);
    }

    @Override
    @Async
    public void saveJcgj(Long  sjId, Integer jcgjlx, String jcmc, String jcmcEn, Integer jcmcys, String jcnr, String jcnrEn) {
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        SjJcgj sjJcgj = new SjJcgj();
        sjJcgj.setSjId(sjId);
        sjJcgj.setJcgjlx(jcgjlx);
        sjJcgj.setJcmc(jcmc);
        sjJcgj.setJcmcEn(jcmcEn);
        sjJcgj.setJcmcys(jcmcys);
        sjJcgj.setJcnr(jcnr);
        sjJcgj.setJcnrEn(jcnrEn);
        sjJcgj.setQmrId(sysUser.getUserId());
        sjJcgj.setQmrMc(sysUser.getNickName());
        sjJcgj.setQmrMcEn(sysUser.getUserName());
        this.save(sjJcgj);
    }

    @Override
    public SjJcgj genJcgj(Long sjId, Integer jcgjlx, String jcmc, String jcmcEn, Integer jcmcys, String jcnr, String jcnrEn, SysUser qmr) {
        SjJcgj sjJcgj = new SjJcgj();
        sjJcgj.setSjId(sjId);
        sjJcgj.setJcgjlx(jcgjlx);
        sjJcgj.setJcmc(jcmc);
        sjJcgj.setJcmcEn(jcmcEn);
        sjJcgj.setJcmcys(jcmcys);
        sjJcgj.setJcnr(jcnr);
        sjJcgj.setJcnrEn(jcnrEn);
        sjJcgj.setQmrId(qmr.getUserId());
        sjJcgj.setQmrMc(qmr.getNickName());
        sjJcgj.setQmrMcEn(qmr.getUserName());
        return sjJcgj;
    }

}
