package com.fzzy.igds.service; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.fzzy.igds.constant.Constant; import com.fzzy.igds.constant.RedisConst; import com.fzzy.igds.data.InoutParam; import com.fzzy.igds.domain.InoutRecord; import com.fzzy.igds.mapper.InoutRecordMapper; import com.fzzy.igds.utils.ContextUtil; import com.fzzy.igds.utils.DateUtil; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.utils.StringUtils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.time.DateFormatUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.Date; import java.util.List; /** * @Description * @Author CZT * @Date 2025/12/2 10:36 */ @Slf4j @Service public class InoutRecordService { @Resource private InoutRecordMapper inoutRecordMapper; @Resource private RedisCache redisCache; /** * 分页查询数据 * @param page * @param param */ public void listPageInout(Page page, InoutParam param) { QueryWrapper queryWrapper = getQueryWrapper(param); inoutRecordMapper.selectPage(page, queryWrapper); } /** * 查询数据集合 * @param param */ public List listInout(InoutParam param) { QueryWrapper queryWrapper = getQueryWrapper(param); return inoutRecordMapper.selectList(queryWrapper); } /** * 封装查询条件 * @param param */ public QueryWrapper getQueryWrapper(InoutParam param) { QueryWrapper queryWrapper = new QueryWrapper<>(); param.setCompanyId(ContextUtil.getCompanyId()); param.setDeptId(ContextUtil.subDeptId(null)); queryWrapper.eq("company_id", param.getCompanyId()); queryWrapper.eq("dept_id", param.getDeptId()); if (StringUtils.isNotBlank(param.getCustomerName())) { queryWrapper.like("customer_name", param.getCustomerName()); } if (StringUtils.isNotBlank(param.getWeightTag())) { queryWrapper.likeRight("progress", param.getWeightTag()); } if (StringUtils.isNotBlank(param.getFoodVariety())) { queryWrapper.eq("food_variety", param.getFoodVariety()); } if (StringUtils.isNotBlank(param.getCheckStatus())) { queryWrapper.eq("check_status", param.getCheckStatus()); } if (StringUtils.isNotBlank(param.getDepotId())) { queryWrapper.eq("depot_id", param.getDepotId()); } if (StringUtils.isNotBlank(param.getType())) { queryWrapper.eq("type", param.getType()); } if (null != param.getStart()) { queryWrapper.ge("create_time", DateUtil.getCurZero(param.getStart())); } if (null != param.getEnd()) { queryWrapper.le("create_time", DateUtil.getNextZero(param.getEnd())); } queryWrapper.eq("record_status", "NORMAL"); //正常状态的单子 queryWrapper.orderByDesc("create_time"); return queryWrapper; } /** * 根据id查询数据 * @param id * @return */ public InoutRecord selectById(String id) { if(StringUtils.isBlank(id)){ return null; } return inoutRecordMapper.selectById(id); } /** * 验证车牌号是否在流程中 * @param companyId * @param plateNum * @return */ public int checkExist(String companyId, String plateNum) { if (StringUtils.isEmpty(companyId)) { companyId = ContextUtil.getCompanyId(); } QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("company_id", companyId); queryWrapper.eq("plate_num", plateNum); queryWrapper.ne("progress", Constant.PROGRESS_RECORD); queryWrapper.eq("record_status", "NORMAL"); return inoutRecordMapper.selectCount(queryWrapper); } /** * 新增出入库记录 * * @param data * @return */ public int addInoutRecord(InoutRecord data) { if (StringUtils.isBlank(data.getId())) { String id = this.createId(data.getRegisterTime(), data.getCompanyId()); if (Constant.TYPE_IN.equals(data.getType())) { data.setId("R_" + id); } else if (Constant.TYPE_OUT.equals(data.getType())) { data.setId("C_" + id); }else { data.setId("M_" + id); } } data.setCreateBy(ContextUtil.getLoginUserName()); data.setCreateTime(new Date()); data.setUpdateBy(ContextUtil.getLoginUserName()); data.setUpdateTime(new Date()); int num = inoutRecordMapper.insert(data); //TODO 更新缓存 //updateInoutCache(data); return num; } /** * 新增出入库记录 * * @param data * @return */ public int updateInoutRecord(InoutRecord data) { data.setUpdateBy(ContextUtil.getLoginUserName()); data.setUpdateTime(new Date()); int num = inoutRecordMapper.updateById(data); //TODO 更新缓存 //updateInoutCache(data); return num; } /** * 出入库流程ID创建 202001030001 202001030001 * @param registerTime * @param companyId * @return */ public String createId(Date registerTime, String companyId) { // 时间戳标签 String timeKey = DateFormatUtils.format(registerTime, "yyyyMMdd"); // 从缓存中获取已有的组织编码 String cacheKey = RedisConst.buildKey(companyId, Constant.CACHE_RECORD_ID); String cacheId = (String) redisCache.getCacheObject(cacheKey); if (null != cacheId && cacheId.indexOf(timeKey) >= 0) { String temp = cacheId.substring(cacheId.length() - 4); Integer i = Integer.valueOf(temp); i++; temp = String.valueOf(i); if (temp.length() == 1) { cacheId = timeKey + "000" + temp; } if (temp.length() == 2) { cacheId = timeKey + "00" + temp; } if (temp.length() == 3) { cacheId = timeKey + "0" + temp; } if (temp.length() == 4) { cacheId = timeKey + temp; } } else { String result = this.getMaxId(companyId, timeKey); if (null == result) { cacheId = timeKey + "0001"; } else { // 获取最后四位 int i = Integer.valueOf(result.substring(result.length() - 4)); i++; String temp = String.valueOf(i); if (temp.length() == 1) { cacheId = timeKey + "000" + temp; } if (temp.length() == 2) { cacheId = timeKey + "00" + temp; } if (temp.length() == 3) { cacheId = timeKey + "0" + temp; } if (temp.length() == 4) { cacheId = timeKey + temp; } } } // 更新缓存 redisCache.setCacheObject(cacheKey, cacheId); return cacheId; } /** * 查询最大id号,为空则返回null * @param companyId * @param timeKey * @return */ public String getMaxId(String companyId, String timeKey) { if (StringUtils.isEmpty(companyId)) { companyId = ContextUtil.getCompanyId(); } QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("company_id", companyId); queryWrapper.like("id", timeKey); queryWrapper.orderByDesc("create_time"); List inoutRecords = inoutRecordMapper.selectList(queryWrapper); if(null == inoutRecords || inoutRecords.isEmpty()){ return null; }else { return inoutRecords.get(0).getId(); } } }