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.OrderRespEnum;
|
import com.fzzy.igds.constant.RedisConst;
|
import com.fzzy.igds.data.GrainData;
|
import com.fzzy.igds.data.GrainParam;
|
import com.fzzy.igds.domain.Depot;
|
import com.fzzy.igds.domain.Grain;
|
import com.fzzy.igds.mapper.GrainMapper;
|
import com.fzzy.igds.utils.ContextUtil;
|
import com.fzzy.igds.utils.DateUtil;
|
import com.fzzy.igds.websocket.WebSocketPacket;
|
import com.fzzy.igds.websocket.WebSocketServer;
|
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.beans.BeanUtils;
|
import org.springframework.stereotype.Service;
|
import javax.annotation.Resource;
|
import java.util.*;
|
|
/**
|
* @Description
|
* @Author CZT
|
* @Date 2025/12/9 10:00
|
*/
|
@Slf4j
|
@Service
|
public class GrainService {
|
|
@Resource
|
private GrainMapper grainMapper;
|
@Resource
|
private RedisCache redisCache;
|
@Resource
|
private DepotService depotService;
|
|
/**
|
* 查询数据
|
* @param param
|
*/
|
public List<Grain> listData(GrainParam param) {
|
if(StringUtils.isBlank(param.getCompanyId())){
|
param.setCompanyId(ContextUtil.getCompanyId());
|
}
|
if(StringUtils.isBlank(param.getDeptId())){
|
param.setDeptId(ContextUtil.subDeptId(null));
|
}
|
|
QueryWrapper<Grain> queryWrapper = new QueryWrapper<>();
|
if (StringUtils.isNotBlank(param.getCompanyId())) {
|
queryWrapper.eq("company_id", param.getCompanyId());
|
}
|
if (StringUtils.isNotBlank(param.getDeptId())) {
|
queryWrapper.eq("dept_id", param.getDeptId());
|
}
|
if (StringUtils.isNotBlank(param.getDepotId())) {
|
queryWrapper.eq("depot_id", param.getDepotId());
|
}
|
if (StringUtils.isNotBlank(param.getBatchId())) {
|
queryWrapper.eq("batch_id", param.getBatchId());
|
}
|
if (null != param.getStart()) {
|
queryWrapper.ge("receive_date", DateUtil.getCurZero(param.getStart()));
|
}
|
if (null != param.getEnd()) {
|
queryWrapper.le("receive_date", DateUtil.getNextZero(param.getEnd()));
|
}
|
queryWrapper.orderByDesc("batch_id");
|
return grainMapper.selectList(queryWrapper);
|
}
|
|
/**
|
* 分页查询数据
|
* @param page
|
* @param param
|
*/
|
public void pageData(Page<Grain> page, Map<String, Object> param) {
|
|
QueryWrapper<Grain> queryWrapper = new QueryWrapper<>();
|
|
queryWrapper.eq("company_id", ContextUtil.getCompanyId());
|
queryWrapper.eq("dept_id", ContextUtil.subDeptId(null));
|
|
String ids = (String) param.get("ids");
|
if (StringUtils.isNotBlank(ids)) {
|
queryWrapper.in("depot_id", ids);
|
}
|
Date date = (Date) param.get(("start"));
|
if (null != date) {
|
queryWrapper.ge("receive_date", DateUtil.getCurZero(date));
|
}
|
date = (Date) param.get(("end"));
|
if (null != date) {
|
queryWrapper.le("receive_date", DateUtil.getNextZero(date));
|
}
|
queryWrapper.orderByDesc("batch_id");
|
grainMapper.selectPage(page, queryWrapper);
|
}
|
|
/**
|
* JPA - 更新数据
|
* @param data
|
*/
|
public void saveOrUpdateGrain(Grain data, String batchTag) {
|
if (StringUtils.isEmpty(data.getCompanyId())) {
|
data.setCompanyId(ContextUtil.getCompanyId());
|
}
|
if (StringUtils.isEmpty(data.getBatchId())) {
|
data.setBatchId(DateFormatUtils.format(data.getReceiveDate(), "yyyyMMddHHmm"));
|
}
|
if(StringUtils.isEmpty(batchTag)){
|
//自动生成
|
batchTag = "01";
|
}
|
|
if(StringUtils.isBlank(data.getCreateBy())){
|
data.setCreateBy(ContextUtil.getLoginUserName());
|
data.setCreateTime(new Date());
|
}
|
|
data.setUpdateBy(ContextUtil.getLoginUserName());
|
data.setUpdateTime(new Date());
|
|
data.setBatchTag(batchTag);
|
int insert = grainMapper.updateById(data);
|
if(insert == 0){
|
grainMapper.insert(data);
|
}
|
|
updateCacheGrainData(data);
|
}
|
|
|
/**
|
* 更新缓存中最新的粮情检测数据
|
*
|
* @param data 当前数据为标准的Grain数据,没有做更新点位信息
|
* @return key=参考编码
|
*/
|
public void updateCacheGrainData(Grain data) {
|
|
GrainData grainData = new GrainData();
|
BeanUtils.copyProperties(data, grainData);
|
|
String key = RedisConst.buildKey(data.getCompanyId(),
|
RedisConst.KEY_GRAIN, grainData.getDepotId());
|
redisCache.setCacheObject(key, grainData);
|
|
|
//推送大屏
|
String deptId = data.getDeptId();
|
if (null == deptId) {
|
Depot depot = depotService.getCacheDepot(data.getCompanyId(), data.getDepotId());
|
if (null == depot) {
|
return;
|
}
|
deptId = depot.getDeptId();
|
}
|
Map<String, GrainData> mapData = this.getCacheGrainDateMap(data.getCompanyId(), deptId);
|
WebSocketPacket packet = new WebSocketPacket();
|
packet.setBizType("screen");
|
packet.setCompanyId(ContextUtil.getCompanyId());
|
packet.setDeptId(deptId);
|
packet.setBizId("grain");
|
packet.setOrderResp(OrderRespEnum.ORDER_INPROGRESS.getCode());
|
packet.setData(mapData);
|
WebSocketServer.sendByPocket(packet);
|
}
|
|
/**
|
* 获取缓存中最新的粮情检测数据
|
*
|
* @param companyId
|
* @return key=参考编码
|
*/
|
public Map<String, GrainData> getCacheGrainDateMap(String companyId, String deptId) {
|
if (null == companyId || null == deptId) return null;
|
|
String pattern = RedisConst.buildKey(companyId, RedisConst.KEY_GRAIN) + "*";
|
|
Collection<String> keys = redisCache.keys(pattern);
|
|
if (null == keys || keys.isEmpty()) return null;
|
|
Map<String, GrainData> result = new HashMap<>();
|
GrainData data;
|
for (String key : keys) {
|
data = (GrainData) redisCache.getCacheObject(key);
|
if (null == data) continue;
|
|
result.put(data.getDepotId(), data);
|
}
|
return result;
|
}
|
|
}
|