package com.hxhq.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxhq.business.domain.StorageLocation;
import com.hxhq.business.domain.StorageLocationJcgj;
import com.hxhq.business.domain.Yq;
import com.hxhq.business.domain.YqJcgj;
import com.hxhq.business.mapper.StorageLocationJcgjMapper;
import com.hxhq.business.mapper.YqJcgjMapper;
import com.hxhq.business.service.IStorageLocationJcgjService;
import com.hxhq.business.service.IStorageLocationService;
import com.hxhq.business.service.ISystemLogService;
import com.hxhq.business.service.IYqJcgjService;
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 java.util.Date;
import java.util.List;

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

    @Autowired
    private ISystemLogService systemLogService;

    /**
     * 查询仪器管理-稽查轨迹列表
     *
     * @param jcgj 仪器管理-稽查轨迹
     * @return 仪器管理-稽查轨迹
     */
    @Override
    public List<StorageLocationJcgj> queryList(StorageLocationJcgj jcgj)
    {
        LambdaQueryWrapper<StorageLocationJcgj> queryWrapper = new LambdaQueryWrapper<>();
        if (jcgj.getStorageLocationId() == null || jcgj.getStorageLocationId().longValue() < 0) {
            throw new SecurityException("存储位置id不能为空");
        }
        queryWrapper.eq(StorageLocationJcgj::getStorageLocationId, jcgj.getStorageLocationId());
        if (jcgj.getJcgjlx() != null && jcgj.getJcgjlx().intValue() > 0) {
            queryWrapper.eq(StorageLocationJcgj::getJcgjlx, jcgj.getJcgjlx());
        }
        queryWrapper.orderByDesc(MpBaseEntity::getId);
        return this.list(queryWrapper);
    }

    @Override
    @Async
    public void saveJcgj(StorageLocation storageLocation, Integer jcgjlx, String jcmc, String jcmcEn, Integer jcmcys, String jcnr, String jcnrEn) {
        Date date = new Date();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        StorageLocationJcgj jcgj = new StorageLocationJcgj();
        jcgj.setStorageLocationId(storageLocation.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(storageLocation.getName(), storageLocation.getName(), jcgj.getJcmc(), jcgj.getJcmcEn(),
                jcgj.getJcnr(), jcgj.getJcnrEn(), jcgj.getQmrId(), jcgj.getQmrMc(), jcgj.getQmrMcEn(), jcgj.getRemark(), date);
    }

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

}
