package com.thhy.materials.modules.biz.concret.controller;
|
|
|
import com.google.zxing.qrcode.encoder.QRCode;
|
import com.thhy.general.common.BasicStatus;
|
import com.thhy.general.exception.BasicException;
|
import com.thhy.materials.modules.biz.concret.entity.dto.DicetName;
|
import com.thhy.materials.modules.biz.concret.entity.dto.TMinxingPlant;
|
import com.thhy.materials.modules.biz.concret.entity.dto.WeighDto;
|
import com.thhy.materials.modules.biz.weigh.entity.WeighVo;
|
import com.thhy.materials.modules.biz.weigh.mapper.WeighMapper;
|
import com.thhy.materials.modules.biz.weigh.service.WeighService;
|
import org.redisson.api.RLock;
|
import org.redisson.api.RedissonClient;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.Resource;
|
import java.math.BigDecimal;
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.concurrent.TimeUnit;
|
|
@Component
|
public class MixingPlantScheduled {
|
@Resource
|
private WeighService weighService;
|
@Resource
|
private WeighMapper weighMapper;
|
|
@Resource
|
private RedissonClient redissonclient;
|
|
|
@Scheduled(fixedRate = 1800000) // 每隔3秒执行一次任务
|
public void task() {
|
//查询搅拌 所有类型
|
List<DicetName> dictList = weighService.dictList();
|
System.out.println("-------"+dictList);
|
Integer byId = weighService.mixingPlantId();
|
for (DicetName values : dictList){
|
if(byId!=null){
|
List<WeighDto> weighVos =weighService.findAlltask(byId,values.getDictName());
|
for (WeighDto weighVo : weighVos){
|
HashMap<String, Object> hashMap = new HashMap<>();
|
hashMap.put("byId",weighVo.getById());
|
hashMap.put("carName",weighVo.getCarNumber());
|
hashMap.put("changeStock",weighVo.getGrossWeight()/1000);
|
hashMap.put("dictId",values.getDictId());
|
hashMap.put("createTime",weighVo.getUpdateTime());
|
String forwardingUnit = weighVo.getForwardingUnit();
|
String id = weighService.forwardingUnitSelectBy(forwardingUnit);
|
hashMap.put("supplierId",id);
|
hashMap.put("companyId","02d2fc8b7ea0eff7ec70c649");
|
//数据消耗
|
double v = (weighVo.getGrossWeight() / 1000);
|
//字典表id
|
String dictId = values.getDictId();
|
//查询当前库存
|
Double materialValue = weighMapper.selectMaterialValue(dictId);
|
|
|
//入库记录添加
|
HashMap<String, Object> hashMap1 = new HashMap<>();
|
hashMap1.put("createDate",new Date());
|
hashMap1.put("materialName",dictId);
|
hashMap1.put("type",1);
|
hashMap1.put("opType",1);
|
hashMap1.put("materialValue",v);
|
BigDecimal b1 = new BigDecimal(v);
|
BigDecimal b2 = new BigDecimal(materialValue);
|
hashMap1.put("beforeStock",materialValue);
|
hashMap1.put("afterStock",b1.add(b2).doubleValue());
|
hashMap1.put("companyId","02d2fc8b7ea0eff7ec70c649");
|
RLock lock = redissonclient.getLock("materlock");
|
System.out.println(hashMap1);
|
|
if(!lock.isLocked()) {
|
lock.lock(10, TimeUnit.SECONDS);
|
//插入记录表
|
weighMapper.materialStockRecordIn(hashMap1);
|
//更改记录值
|
weighMapper.materialStockRecordUp(dictId,b1.add(b2).doubleValue());
|
lock.unlock();
|
}
|
weighMapper.minxingIn(hashMap);
|
}
|
}else {
|
List<WeighDto> weighVos =weighService.findAlltaskAll(values.getDictName());
|
for (WeighDto weighVo : weighVos){
|
HashMap<String, Object> hashMap = new HashMap<>();
|
hashMap.put("byId",weighVo.getById());
|
hashMap.put("carName",weighVo.getCarNumber());
|
hashMap.put("changeStock",weighVo.getGrossWeight()/1000);
|
hashMap.put("dictId",values.getDictId());
|
hashMap.put("createTime",weighVo.getUpdateTime());
|
//供应商
|
String forwardingUnit = weighVo.getForwardingUnit();
|
String id = weighService.forwardingUnitSelectBy(forwardingUnit);
|
hashMap.put("supplierId",id);
|
hashMap.put("companyId","02d2fc8b7ea0eff7ec70c649");
|
System.out.println("----------"+hashMap);
|
|
//数据消耗
|
double v = (weighVo.getGrossWeight() / 1000);
|
//字典表id
|
String dictId = values.getDictId();
|
//查询当前库存
|
Double materialValue = weighMapper.selectMaterialValue(dictId);
|
|
//入库记录添加
|
HashMap<String, Object> hashMap1 = new HashMap<>();
|
hashMap1.put("createDate",new Date());
|
hashMap1.put("materialName",dictId);
|
hashMap1.put("type",1);
|
hashMap1.put("opType",1);
|
hashMap1.put("materialValue",v);
|
BigDecimal b1 = new BigDecimal(v);
|
BigDecimal b2 = new BigDecimal(materialValue);
|
hashMap1.put("beforeStock",materialValue);
|
|
double v1 = b1.add(b2).doubleValue();
|
System.out.println("---------====-=--="+v1);
|
System.out.println("---------====-=--="+v1);
|
hashMap1.put("afterStock",v1);
|
hashMap1.put("companyId","02d2fc8b7ea0eff7ec70c649");
|
|
RLock lock = redissonclient.getLock("materlock");
|
try {
|
if (lock.tryLock(5, 10, TimeUnit.SECONDS)) {
|
//更新记录
|
weighMapper.materialStockRecordIn(hashMap1);
|
//更新库存
|
weighMapper.materialStockRecordUp(dictId,b1.add(b2).doubleValue());
|
} else {
|
return;
|
}
|
} catch (InterruptedException e) {
|
return;
|
} finally {
|
if (lock.isLocked()) {
|
lock.unlock();
|
}
|
}
|
weighMapper.minxingIn(hashMap);
|
}
|
}
|
// 定时执行的任务内容
|
}
|
|
}
|
}
|