张磊磊
2023-11-08 2b2d292c1a274aa95aab8c2ac20530e54a503923
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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);
                }
            }
        // 定时执行的任务内容
        }
 
    }
}