package com.fzzy.igds.sys; import com.fzzy.igds.dzhwk.constant.RedisConst; import com.fzzy.igds.dzhwk.domain.DepotStatusConfirm; import com.fzzy.igds.sys.repository.DepotStatusConfirmRepository; import com.fzzy.igds.util.ContextUtil; 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.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.Date; import java.util.List; /** * @Description * @Author CZT * @Date 2024/11/23 10:48 */ @Slf4j @Service("sys.depotStatusConfirmService") public class DepotStatusConfirmService { public static final String CACHE_DEPOT_STATUS_CONFIRM_ID = "DEPOT_STATUS_CONFIRM_ID"; @Resource private DepotStatusConfirmRepository DepotStatusConfirmRepository; @Resource private RedisCache redisCache; /** * JPA分页查询数据 * * @param specification * @param pageable * @return */ public Page findAll(Specification specification, Pageable pageable) { return DepotStatusConfirmRepository.findAll(specification, pageable); } /** * JPA更新保存 * @param data */ public void saveOrUpdate(DepotStatusConfirm data) { if (StringUtils.isEmpty(data.getCompanyId())) { data.setCompanyId(ContextUtil.getCompanyId()); } if (StringUtils.isEmpty(data.getDeptId())) { data.setDeptId(ContextUtil.subDeptId(null)); } data.setUpdateTime(new Date()); if (StringUtils.isEmpty(data.getFcqrdh())) { data.setFcqrdh(buildId(data.getFcrq())); } DepotStatusConfirmRepository.save(data); } /** * JPA删除 * @param data */ public void delData(DepotStatusConfirm data) { DepotStatusConfirmRepository.delete(data); } /** * 获取封仓单号 * @param time * @return */ public String buildId(Date time) { String timeKey = "21" + DateFormatUtils.format(time, "yyyyMMdd"); // 从缓存中获取已有的组织编码 String cacheKey = RedisConst.buildKey(ContextUtil.getCompanyId(), CACHE_DEPOT_STATUS_CONFIRM_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); cacheId = timeKey + String.format("%04d", ++i); } else { List list = DepotStatusConfirmRepository.getDataByFcqrdh(timeKey + "%"); if (null == list || list.isEmpty()) { cacheId = timeKey + "0001"; } else { String temp = list.get(0).getFcqrdh(); String tempNum = temp.substring(temp.length() - 4); Integer i = Integer.valueOf(tempNum); cacheId = timeKey + String.format("%04d", ++i); } } // 更新缓存 redisCache.setCacheObject(cacheKey, cacheId); return cacheId; } }