|  |  |  | 
|---|
|  |  |  | package com.ld.igds.inout.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.bstek.dorado.data.entity.EntityUtils; | 
|---|
|  |  |  | import com.ld.igds.common.CoreCommonService; | 
|---|
|  |  |  | import com.ld.igds.constant.BizType; | 
|---|
|  |  |  | import com.ld.igds.constant.Constant; | 
|---|
|  |  |  | import com.ld.igds.constant.RedisConst; | 
|---|
|  |  |  | import com.ld.igds.data.CommonData; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.ld.igds.inout.mapper.InoutCheckMapper; | 
|---|
|  |  |  | import com.ld.igds.inout.mapper.InoutRecordMapper; | 
|---|
|  |  |  | import com.ld.igds.inout.service.InoutService; | 
|---|
|  |  |  | import com.ld.igds.models.Depot; | 
|---|
|  |  |  | import com.ld.igds.models.InoutLossOver; | 
|---|
|  |  |  | import com.ld.igds.models.InoutPrice; | 
|---|
|  |  |  | import com.ld.igds.util.ContextUtil; | 
|---|
|  |  |  | import com.ld.igds.util.DateUtil; | 
|---|
|  |  |  | import com.ld.igds.util.RedisUtil; | 
|---|
|  |  |  | import com.ld.igds.websocket.WebSocketServer; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.apache.commons.lang3.StringUtils; | 
|---|
|  |  |  | import org.apache.commons.lang3.time.DateFormatUtils; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.annotation.Resource; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.util.concurrent.ExecutorService; | 
|---|
|  |  |  | import java.util.concurrent.Executors; | 
|---|
|  |  |  | import java.util.concurrent.FutureTask; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Slf4j | 
|---|
|  |  |  | @Component | 
|---|
|  |  |  | 
|---|
|  |  |  | private InoutCheckMapper inoutCheckMapper; | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private RedisUtil redisUtil; | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private CoreCommonService commonService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public InoutData inoutProgressQuery(InoutParam param) throws Exception { | 
|---|
|  |  |  | 
|---|
|  |  |  | if (null != result) { | 
|---|
|  |  |  | return result; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<InoutData> list = inoutMapper.inoutProgressQuery(param); | 
|---|
|  |  |  | if (null == list || list.size() == 0) | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public String inoutStop(InoutParam param) { | 
|---|
|  |  |  | public String inoutStop(InoutParam param) throws Exception { | 
|---|
|  |  |  | if (StringUtils.isEmpty(param.getCompanyId())) { | 
|---|
|  |  |  | param.setCompanyId(ContextUtil.getCompanyId()); | 
|---|
|  |  |  | param.setCompanyId(ContextUtil.getDefaultCompanyId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | param.setProgress(InoutConstant.PROGRESS_RECORD); | 
|---|
|  |  |  | param.setRecordStatus(InoutConstant.RECORD_STATUS_DEL); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 缓存中删除 | 
|---|
|  |  |  | delFromCache(param.getDeptId(), param.getType(), param.getId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | param.setProgress(InoutConstant.PROGRESS_RECORD); | 
|---|
|  |  |  | param.setRecordStatus(InoutConstant.RECORD_STATUS_ERROR); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | inoutMapper.inoutStop(param); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | data.setUserName(""); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | inoutMapper.insertData(data); | 
|---|
|  |  |  | InoutData newData; | 
|---|
|  |  |  | if (EntityUtils.isEntity(data)) { | 
|---|
|  |  |  | newData = new InoutData(); | 
|---|
|  |  |  | BeanUtils.copyProperties(data, newData, new String[]{"checkItems", "files"}); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | newData = data; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | updateInoutCache(data); | 
|---|
|  |  |  | inoutMapper.insertData(newData); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | updateInoutCache(newData); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | log.error("------------出入库执行保存出错---{}", e); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | inoutMapper.updateDataByHandle(newData); | 
|---|
|  |  |  | InoutParam param = new InoutParam(); | 
|---|
|  |  |  | param.setId(newData.getId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //更新缓存 | 
|---|
|  |  |  | updateInoutCache(newData); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | updateInoutCache(inoutMapper.inoutQueryById(param)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | // 设置身份证号的模糊查询条件 | 
|---|
|  |  |  | if (null != param.getUserId()) { | 
|---|
|  |  |  | param.setUserId("%" + param.getUserId() + "%"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (null != param.getRecordStatus() && InoutConstant.RECORD_STATUS_NORMAL.equals(param.getRecordStatus())) { | 
|---|
|  |  |  | param.setRecordStatus(null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<InoutData> records = inoutMapper.pageRecordData(page, param); | 
|---|
|  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public String toComplete(InoutParam param) throws Exception { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | param.setUpdateTime(new Date()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (null == param.getCompanyId()) { | 
|---|
|  |  |  | param.setCompanyId(ContextUtil.getDefaultCompanyId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 清除缓存 | 
|---|
|  |  |  | this.delFromCache(param.getDeptId(), param.getType(), param.getId()); | 
|---|
|  |  |  | //        this.delFromCache(param.getDeptId(), param.getType(), param.getId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | inoutMapper.toComplete(param); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public String quickComplete(InoutData data) throws Exception { | 
|---|
|  |  |  | if (null == data.getCompanyId()) { | 
|---|
|  |  |  | data.setCompanyId(ContextUtil.getCompanyId()); | 
|---|
|  |  |  | data.setCompanyId(ContextUtil.getDefaultCompanyId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (null == data.getCompleteTime()) { | 
|---|
|  |  |  | data.setCompleteTime(new Date()); | 
|---|
|  |  |  | 
|---|
|  |  |  | if (null == data.getEmptyWeightTime()) { | 
|---|
|  |  |  | data.setEmptyWeightTime(new Date()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //调整时间差 | 
|---|
|  |  |  | if (InoutConstant.TYPE_IN.equals(data.getType())) { | 
|---|
|  |  |  | 
|---|
|  |  |  | // 设置流程结束 | 
|---|
|  |  |  | data.setProgress(InoutConstant.PROGRESS_RECORD); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (null == data.getFoodType()) { | 
|---|
|  |  |  | // 根据仓库信息获取缓存 | 
|---|
|  |  |  | Depot depot = commonService.getCacheDepot(data.getCompanyId(), data.getDepotId()); | 
|---|
|  |  |  | data.setFoodType(depot.getFoodType()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //执行更新 | 
|---|
|  |  |  | inoutMapper.updateData(data); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //更新缓存 | 
|---|
|  |  |  | updateInoutCache(data); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | setInoutCache(data.getDeptId(), data); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //推送大屏 | 
|---|
|  |  |  | notifyToScreen(data.getCompanyId(), data.getDeptId(), data.getProgress()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | public String addInoutDataByLossOver(InoutData data) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 获取上一车的流水数据 | 
|---|
|  |  |  | //    InoutData inoutData = this.getLastRecord(data.getCompanyId(), data.getDepotId()); | 
|---|
|  |  |  | //    InoutData inoutData = this.getLastRecord(data.getCompanyId(), data.getDepotId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //        // 设置流水的库存 | 
|---|
|  |  |  | //        if (InoutConstant.TYPE_IN.equals(inoutData.getType())) { | 
|---|
|  |  |  | 
|---|
|  |  |  | public void delInoutDataByLossOver(InoutParam param) { | 
|---|
|  |  |  | inoutMapper.deleteData(param); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void setCheckCache(InoutData data) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String cacheKey = RedisConst.buildKey(data.getCompanyId(), data.getCheckId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //化验结果存入缓存3天 | 
|---|
|  |  |  | redisUtil.set(cacheKey, data.getCheckItems(), 60 * 60 * 24 * 3); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 出入库流程ID创建 202001030001 202001030001 | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 推送到大屏,判断只有当大屏在线时候才处理当前逻辑,避免页面卡顿,调用子任务完成推送 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param companyId 组织编号 | 
|---|
|  |  |  | * @param deptId    分库编号 | 
|---|
|  |  |  | * @param progress  流转节点 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void notifyToScreen(String companyId, String deptId, String progress) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 推送到大屏 | 
|---|
|  |  |  | List<InoutData> curList = this.getListInoutCache(deptId); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<InoutData> completeList = this.getCompleteListInoutCache(deptId); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (null == WebSocketServer.contextOnLineMap.get(BizType.SCREEN | 
|---|
|  |  |  | .getCode())) { | 
|---|
|  |  |  | WebSocketServer.contextOnLineMap.put(BizType.SCREEN.getCode(), | 
|---|
|  |  |  | false); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (null == WebSocketServer.contextOnLineMap.get(BizType.SCREEN_INOUT | 
|---|
|  |  |  | .getCode())) { | 
|---|
|  |  |  | WebSocketServer.contextOnLineMap.put( | 
|---|
|  |  |  | BizType.SCREEN_INOUT.getCode(), false); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (null == WebSocketServer.contextOnLineMap.get(BizType.SCREEN_CHECK | 
|---|
|  |  |  | .getCode())) { | 
|---|
|  |  |  | WebSocketServer.contextOnLineMap.put( | 
|---|
|  |  |  | BizType.SCREEN_CHECK.getCode(), false); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (WebSocketServer.contextOnLineMap.get(BizType.SCREEN.getCode()) | 
|---|
|  |  |  | || WebSocketServer.contextOnLineMap.get(BizType.SCREEN_INOUT | 
|---|
|  |  |  | .getCode()) | 
|---|
|  |  |  | || WebSocketServer.contextOnLineMap.get(BizType.SCREEN_CHECK | 
|---|
|  |  |  | .getCode())) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 创建一个子任务进行推送信息 | 
|---|
|  |  |  | FutureTask<String> futureTask = new FutureTask<>(new NotifyScreenTask(curList, completeList, deptId)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | ExecutorService executorService = Executors.newCachedThreadPool(); | 
|---|
|  |  |  | executorService.submit(futureTask); | 
|---|
|  |  |  | executorService.shutdown(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 创建出入库key | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param deptId | 
|---|
|  |  |  | 
|---|
|  |  |  | data.setCompanyId(ContextUtil.getCompanyId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (StringUtils.isEmpty(data.getDeptId())) { | 
|---|
|  |  |  | data.setCompanyId(ContextUtil.subDeptId(null)); | 
|---|
|  |  |  | data.setDeptId(ContextUtil.subDeptId(null)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int num = inoutCheckMapper.updateSampleData(data); | 
|---|
|  |  |  | inoutCheckMapper.updateSampleData(data); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public String updateCheckData(InoutCheckData data) { | 
|---|
|  |  |  | if (StringUtils.isEmpty(data.getCompanyId())) { | 
|---|
|  |  |  | data.setCompanyId(ContextUtil.getCompanyId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (StringUtils.isEmpty(data.getDeptId())) { | 
|---|
|  |  |  | data.setCompanyId(ContextUtil.subDeptId(null)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | public String updateCheckData(InoutData data) { | 
|---|
|  |  |  | data.setUpdateTime(new Date()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int num = inoutCheckMapper.updateCheckData(data); | 
|---|
|  |  |  | inoutCheckMapper.updateCheckData(data); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //更新缓存 | 
|---|
|  |  |  | updateInoutCache(data); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<InoutPrice> getPrice(InoutCheckParam param) { | 
|---|
|  |  |  | if(null == param.getStart()){ | 
|---|
|  |  |  | if (null == param.getStart()) { | 
|---|
|  |  |  | param.setStart(new Date()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(null == param.getEnd()){ | 
|---|
|  |  |  | if (null == param.getEnd()) { | 
|---|
|  |  |  | param.setEnd(new Date()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return inoutCheckMapper.getPrice(param); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public InoutCheckData inoutDataByCheckId(InoutCheckParam param) throws Exception { | 
|---|
|  |  |  | List<InoutCheckData> list = inoutCheckMapper.inoutDataByCheckId(param); | 
|---|
|  |  |  | if (null == list || list.size() == 0) | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | if (list.size() == 1) { | 
|---|
|  |  |  | return list.get(0); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | throw new Exception("当前条件下存在多个满足条件的数据,请核查!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<InoutLossOver> listLossOver(InoutParam param) { | 
|---|
|  |  |  | if (StringUtils.isEmpty(param.getCompanyId())) { | 
|---|
|  |  |  | param.setCompanyId(ContextUtil.getCompanyId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<InoutLossOver> list = inoutMapper.listLossOver(param); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return list; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public double sumLossOverWeight(InoutParam param) { | 
|---|
|  |  |  | CommonData result = inoutMapper.sumLossOverWeight(param); | 
|---|
|  |  |  | return result.getNumValue1(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|