package com.fzzy.igds.service; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.fzzy.igds.constant.RedisConst; import com.fzzy.igds.domain.QuantityConf; import com.fzzy.igds.mapper.QuantityConfMapper; import com.fzzy.igds.utils.ContextUtil; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.utils.StringUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.List; /** * @Description * @Author CZT * @Date 2025/11/28 16:55 */ @Slf4j @Service public class QuantityService { @Resource private QuantityConfMapper quantityConfMapper; @Resource private RedisCache redisCache; /** * 查询配置信息,根据库区编码获取 * * @param companyId * @param deptId * @return */ public List getConfList(String companyId, String deptId) { if (StringUtils.isEmpty(companyId)) { companyId = ContextUtil.getCompanyId(); } if (StringUtils.isEmpty(deptId)) { deptId = ContextUtil.subDeptId(null); } QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("company_id", companyId); if(StringUtils.isNotBlank(deptId)){ queryWrapper.eq("dept_id", deptId); } queryWrapper.orderByAsc("order_num"); return quantityConfMapper.selectList(queryWrapper); } /** * - 查询配置信息,根据组织编码获取 * * @param companyId * @return */ public List getConfList(String companyId) { if (StringUtils.isEmpty(companyId)) { companyId = ContextUtil.getCompanyId(); } return this.getConfList(companyId, null); } /** * - 更新保存数据 * * @param conf */ public void saveConf(QuantityConf conf) { if (StringUtils.isEmpty(conf.getDepotId())) { return; } if (StringUtils.isBlank(conf.getCompanyId())) { conf.setCompanyId(ContextUtil.getCompanyId()); } if (StringUtils.isBlank(conf.getDeptId())) { conf.setDeptId(ContextUtil.subDeptId(null)); } if (null == conf.getUpdateBy()) { conf.setCreateBy(ContextUtil.getLoginUserName()); conf.setCreateTime(new Date()); conf.setUpdateBy(ContextUtil.getLoginUserName()); conf.setUpdateTime(new Date()); quantityConfMapper.insert(conf); }else { conf.setUpdateBy(ContextUtil.getLoginUserName()); conf.setUpdateTime(new Date()); quantityConfMapper.updateById(conf); } //刷新缓存 this.setCacheQuantityConf(conf); } /** * 更新信息 * * @param conf */ public void updateQuantityConfBySn(QuantityConf conf) { UpdateWrapper updateWrapper = new UpdateWrapper<>(); updateWrapper.eq("sn", conf.getSn()).set("ip", conf.getIp()).set("port", conf.getPort()).set("status", conf.getStatus()); quantityConfMapper.update(null, updateWrapper); this.setCacheQuantityConf(conf); } /** * - 删除数据 * * @param conf * @return */ public String delQuantityConf(QuantityConf conf) { quantityConfMapper.deleteById(conf); //删除配置信息 flushConfCache(conf.getCompanyId(), conf.getDeptId()); return null; } /** * 刷新缓存 * * @param companyId * @param deptId */ public void flushConfCache(String companyId, String deptId) { if (StringUtils.isEmpty(companyId)) { companyId = ContextUtil.getCompanyId(); } if (StringUtils.isEmpty(deptId)) { deptId = ContextUtil.subDeptId(null); } List list = this.getConfList(companyId, deptId); this.setCacheQuantityConf(list); } /** * 设置缓存 * * @param list */ public void setCacheQuantityConf(List list) { if (null == list || list.isEmpty()) { return; } String key; for (QuantityConf conf : list) { if (StringUtils.isEmpty(conf.getSn())) { conf.setSn(conf.getYtIp()); } key = RedisConst.buildKeyByPrefix(RedisConst.KEY_QUANTITY_CONF, conf.getSn()); redisCache.setCacheObject(key, conf); } } /** * 设置缓存 * * @param conf */ public void setCacheQuantityConf(QuantityConf conf) { if (StringUtils.isEmpty(conf.getSn())) { conf.setSn(conf.getYtIp()); } String key = RedisConst.buildKeyByPrefix(RedisConst.KEY_QUANTITY_CONF, conf.getSn()); redisCache.setCacheObject(key, conf); } /** * 获取所有配置缓存 * * @return */ public List getCacheConfList(String companyId) { String pattern = RedisConst.buildKeyByPrefix(RedisConst.KEY_QUANTITY_CONF, null) + "*"; Collection keys = redisCache.keys(pattern); if (null == keys || keys.isEmpty()) { return null; } List list = new ArrayList<>(); QuantityConf conf; for (String key : keys) { conf = (QuantityConf) redisCache.getCacheObject(key); if (null == conf) { continue; } list.add(conf); } if (list.isEmpty()) { if (StringUtils.isEmpty(companyId)) { companyId = ContextUtil.getCompanyId(); } list = this.getConfList(companyId); this.setCacheQuantityConf(list); } return list; } /** * 根据仓库编码获取获取缓存 * * @param companyId * @param depotId * @return */ public QuantityConf getCacheQuantityConf(String companyId, String depotId) { if (StringUtils.isEmpty(depotId)) { return null; } List list = getCacheConfList(companyId); if (null == list || list.isEmpty()) { return null; } for (QuantityConf conf : list) { if (conf.getCompanyId().equals(companyId) && conf.getDepotId().equals(depotId)) { return conf; } } return null; } /** * 根据SN获取配置信息 * * @param sn * @return */ public QuantityConf getCacheQuantityConfBySn(String sn) { if (StringUtils.isEmpty(sn)) { return null; } List list = getCacheConfList(null); if (null == list || list.isEmpty()) { return null; } for (QuantityConf conf : list) { if (conf.getSn().equals(sn)) { return conf; } } return null; } }