xiaogang
2022-08-18 146bd184f86648f21603d7d6698cdbe0a2df7cff
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package com.java110.store.cmd.purchase;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.factory.CommunitySettingFactory;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.allocationStorehouse.AllocationStorehouseDto;
import com.java110.dto.allocationStorehouseApply.AllocationStorehouseApplyDto;
import com.java110.dto.purchaseApply.PurchaseApplyDto;
import com.java110.dto.resourceStore.ResourceStoreDto;
import com.java110.dto.user.UserDto;
import com.java110.intf.store.*;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.po.purchase.PurchaseApplyDetailPo;
import com.java110.po.purchase.PurchaseApplyPo;
import com.java110.po.purchase.ResourceStorePo;
import com.java110.store.bmo.purchase.IPurchaseApplyBMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
@Java110Cmd(serviceCode = "/purchase/urgentPurchaseApply")
public class UrgentPurchaseApplyCmd extends Cmd {
 
    //域
    public static final String DOMAIN_COMMON = "DOMAIN.COMMON";
 
    //键
    public static final String URGRNT_NUMBER = "URGRNT_NUMBER";
 
    @Autowired
    private IAllocationStorehouseInnerServiceSMO allocationStorehouseInnerServiceSMOImpl;
 
    @Autowired
    private IAllocationStorehouseApplyInnerServiceSMO allocationStorehouseApplyInnerServiceSMOImpl;
 
    @Autowired
    private IPurchaseApplyInnerServiceSMO purchaseApplyInnerServiceSMOImpl;
 
    @Autowired
    private IResourceStoreInnerServiceSMO resourceStoreInnerServiceSMOImpl;
 
    @Autowired
    private IStorehouseInnerServiceSMO storehouseInnerServiceSMOImpl;
 
    @Autowired
    private IPurchaseApplyBMO purchaseApplyBMOImpl;
 
    @Autowired
    private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;
 
    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        Assert.hasKeyAndValue(reqJson, "resourceStores", "必填,请填写申请采购的物资");
        Assert.hasKeyAndValue(reqJson, "description", "必填,请填写采购申请说明");
        Assert.hasKeyAndValue(reqJson, "resOrderType", "必填,请填写申请类型");
    }
 
    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        String userId = context.getReqHeaders().get("user-id");
        String userName = context.getReqHeaders().get("user-name");
        UserDto userDto = new UserDto();
        userDto.setUserId(userId);
        userDto.setRow(1);
        userDto.setPage(1);
        List<UserDto> userDtos = userV1InnerServiceSMOImpl.queryUsers(userDto);
 
        Assert.listOnlyOne(userDtos,"用户不存在");
 
        userName = userDtos.get(0).getName();
 
        String storeId = context.getReqHeaders().get("store-id");
        PurchaseApplyDto purchaseApplyDto = new PurchaseApplyDto();
        purchaseApplyDto.setResOrderType(reqJson.getString("resOrderType"));
        //紧急采购入库
        purchaseApplyDto.setWarehousingWay(PurchaseApplyDto.WAREHOUSING_TYPE_URGENT);
        purchaseApplyDto.setCommunityId(reqJson.getString("communityId"));
        purchaseApplyDto.setUserId(userId);
        //查询当月数据标识,当值为1时查询当月数据
        purchaseApplyDto.setUrgentFlag("1");
        //查询当月紧急采购的次数
        int count = purchaseApplyInnerServiceSMOImpl.queryPurchaseApplysCount(purchaseApplyDto);
        //取出开关映射的值
        String value = CommunitySettingFactory.getValue(purchaseApplyDto.getCommunityId(), URGRNT_NUMBER);
        if (StringUtil.isEmpty(value)) {
            value = MappingCache.getValue(DOMAIN_COMMON, URGRNT_NUMBER);
        }
        if (StringUtil.isEmpty(value)) {
            throw new IllegalArgumentException("映射值为空!");
        }
        int number = Integer.parseInt(value);
        if (count >= number) {
            ResponseEntity<String> responseEntity = ResultVo.createResponseEntity(ResultVo.CODE_BUSINESS_VERIFICATION, "本月紧急采购次数已超过" + number + "次,请下月再使用!");
            context.setResponseEntity(responseEntity);
            return;
        }
        PurchaseApplyPo purchaseApplyPo = new PurchaseApplyPo();
        purchaseApplyPo.setApplyOrderId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_applyOrderId));
        purchaseApplyPo.setDescription(reqJson.getString("description"));
        purchaseApplyPo.setUserId(userId);
        purchaseApplyPo.setUserName(userName);
        purchaseApplyPo.setEndUserName(reqJson.getString("endUserName"));
        purchaseApplyPo.setEndUserTel(reqJson.getString("endUserTel"));
        purchaseApplyPo.setStoreId(storeId);
        purchaseApplyPo.setResOrderType(PurchaseApplyDto.RES_ORDER_TYPE_ENTER);
        purchaseApplyPo.setState(PurchaseApplyDto.STATE_END);
        purchaseApplyPo.setCreateTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
        purchaseApplyPo.setDescription(reqJson.getString("description"));
        purchaseApplyPo.setCreateUserId(userId);
        purchaseApplyPo.setCreateUserName(userName);
        purchaseApplyPo.setWarehousingWay(PurchaseApplyDto.WAREHOUSING_TYPE_URGENT);
        purchaseApplyPo.setCommunityId(reqJson.getString("communityId"));
        //获取采购物品信息
        JSONArray resourceStores = reqJson.getJSONArray("resourceStores");
        List<PurchaseApplyDetailPo> purchaseApplyDetailPos = new ArrayList<>();
        for (int resourceStoreIndex = 0; resourceStoreIndex < resourceStores.size(); resourceStoreIndex++) {
            JSONObject resourceStore = resourceStores.getJSONObject(resourceStoreIndex);
            PurchaseApplyDetailPo purchaseApplyDetailPo = BeanConvertUtil.covertBean(resourceStore, PurchaseApplyDetailPo.class);
            purchaseApplyDetailPo.setId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_applyOrderId));
            purchaseApplyDetailPo.setPurchaseQuantity(resourceStore.get("quantity").toString());
            purchaseApplyDetailPo.setPurchaseRemark(resourceStore.get("remark").toString());
            purchaseApplyDetailPo.setOriginalStock(resourceStore.getString("stock"));
            purchaseApplyDetailPo.setPrice(resourceStore.getString("urgentPrice"));
            purchaseApplyDetailPos.add(purchaseApplyDetailPo);
            //查询当前小区仓库下该物品信息
            ResourceStoreDto resourceStoreDto = new ResourceStoreDto();
            resourceStoreDto.setResCode(resourceStore.getString("resCode"));
 
            if (StringUtil.isEmpty(resourceStore.getString("shzId"))) {
                resourceStore.put("shzId", resourceStore.getString("shId"));
            }
            resourceStoreDto.setShId(resourceStore.getString("shzId")); //小区目标仓库
 
            List<ResourceStoreDto> resourceStoreDtos = resourceStoreInnerServiceSMOImpl.queryResourceStores(resourceStoreDto);
            //调整集团仓库物品信息
            ResourceStorePo resourceStorePo = new ResourceStorePo();
            resourceStorePo.setResId(purchaseApplyDetailPo.getResId());
            resourceStorePo.setPurchasePrice(resourceStore.getString("urgentPrice"));
            resourceStorePo.setStock(purchaseApplyDetailPo.getQuantity());
            resourceStorePo.setResOrderType(PurchaseApplyDto.WAREHOUSING_TYPE_URGENT);
            resourceStorePo.setOperationType(PurchaseApplyDto.WEIGHTED_MEAN_TRUE);
            resourceStoreInnerServiceSMOImpl.updateResourceStore(resourceStorePo);
            if (resourceStoreDtos != null && resourceStoreDtos.size() == 1) {
                //生成调拨记录
                AllocationStorehouseDto allocationStorehouseDto = new AllocationStorehouseDto();
                allocationStorehouseDto.setAsId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_allocationStorehouseId));
                allocationStorehouseDto.setbId("-1");
                allocationStorehouseDto.setShIda(resourceStore.getString("shId"));
                allocationStorehouseDto.setShIdz(resourceStore.getString("shzId"));//小区仓库
                allocationStorehouseDto.setResId(resourceStoreDtos.get(0).getResId());
                allocationStorehouseDto.setResName(resourceStoreDtos.get(0).getResName());
                allocationStorehouseDto.setStoreId(storeId);
                allocationStorehouseDto.setStock(purchaseApplyDetailPo.getQuantity());
                allocationStorehouseDto.setStartUserId(userId);
                allocationStorehouseDto.setStartUserName(userName);
                allocationStorehouseDto.setCreateTime(new Date());
                allocationStorehouseDto.setRemark("紧急采购入库并调拨");
                allocationStorehouseDto.setApplyId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_applyId));
                allocationStorehouseDto.setOriginalStock(resourceStoreDtos.get(0).getStock());
                allocationStorehouseInnerServiceSMOImpl.saveAllocationStorehouses(allocationStorehouseDto);
                //生成调拨申请记录
                AllocationStorehouseApplyDto allocationStorehouseApplyDto = new AllocationStorehouseApplyDto();
                allocationStorehouseApplyDto.setApplyId(allocationStorehouseDto.getApplyId());
                allocationStorehouseApplyDto.setbId("-1");
                allocationStorehouseApplyDto.setStartUserId(allocationStorehouseDto.getStartUserId());
                allocationStorehouseApplyDto.setStartUserName(allocationStorehouseDto.getStartUserName());
                allocationStorehouseApplyDto.setRemark("紧急采购入库并调拨");
                allocationStorehouseApplyDto.setApplyCount(purchaseApplyDetailPo.getQuantity());
                allocationStorehouseApplyDto.setStoreId(storeId);
                allocationStorehouseApplyDto.setCreateTime(new Date());
                allocationStorehouseApplyDto.setState(AllocationStorehouseDto.STATE_SUCCESS);
                allocationStorehouseApplyDto.setCommunityId(reqJson.getString("communityId"));
                //调拨
                allocationStorehouseApplyDto.setApplyType("30000");
                allocationStorehouseApplyInnerServiceSMOImpl.saveAllocationStorehouseApplys(allocationStorehouseApplyDto);
 
                //调整小区仓库物品均价、数量
 
                //集团仓库商品信息
                ResourceStoreDto resourceStoreDto2 = new ResourceStoreDto();
                resourceStoreDto2.setResCode(resourceStore.getString("resCode"));
                resourceStoreDto2.setResId(purchaseApplyDetailPo.getResId());
                List<ResourceStoreDto> resourceStoreDtoList = resourceStoreInnerServiceSMOImpl.queryResourceStores(resourceStoreDto2);
 
                ResourceStorePo resourceStorePo1 = new ResourceStorePo();
                resourceStorePo1.setPurchasePrice(resourceStore.getString("urgentPrice"));
                resourceStorePo1.setResId(resourceStoreDtos.get(0).getResId());
                resourceStorePo1.setStock(purchaseApplyDetailPo.getQuantity());
                resourceStorePo1.setAveragePrice(resourceStoreDtoList.get(0).getAveragePrice());
                resourceStorePo1.setPrice(resourceStoreDtoList.get(0).getPrice());
                resourceStorePo1.setResOrderType(PurchaseApplyDto.WAREHOUSING_TYPE_URGENT);
                resourceStorePo1.setOperationType(PurchaseApplyDto.WEIGHTED_MEAN_FALSE);
                //获取紧急采购数量
                BigDecimal purchaseQuantity = new BigDecimal(purchaseApplyDetailPo.getQuantity());
                //获取原有最小计量总数
                if (StringUtil.isEmpty(resourceStoreDtos.get(0).getMiniStock())) {
                    throw new IllegalArgumentException("最小计量总数不能为空!");
                }
                BigDecimal miniStock = new BigDecimal(resourceStoreDtos.get(0).getMiniStock());
                //获取最小计量单位数量
                if (StringUtil.isEmpty(resourceStoreDtos.get(0).getMiniUnitStock())) {
                    throw new IllegalArgumentException("最小计量单位数量不能为空!");
                }
                BigDecimal miniUnitStock = new BigDecimal(resourceStoreDtos.get(0).getMiniUnitStock());
                //计算最小计量总数
                BigDecimal newMiniStock = purchaseQuantity.multiply(miniUnitStock).add(miniStock);
                resourceStorePo1.setMiniStock(String.valueOf(newMiniStock));
                resourceStoreInnerServiceSMOImpl.updateResourceStore(resourceStorePo1);
            } else if (resourceStoreDtos != null && resourceStoreDtos.size() > 1) {
                throw new IllegalArgumentException("查询商品错误!");
            } else {
                //生成调拨记录
                AllocationStorehouseDto allocationStorehouseDto = new AllocationStorehouseDto();
                allocationStorehouseDto.setAsId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_allocationStorehouseId));
                allocationStorehouseDto.setbId("-1");
                allocationStorehouseDto.setShIda(resourceStore.getString("shId"));
                allocationStorehouseDto.setShIdz(resourceStore.getString("shzId"));//小区目标仓库
                allocationStorehouseDto.setResId(resourceStore.getString("resId"));
                allocationStorehouseDto.setResName(resourceStore.getString("resName"));
                allocationStorehouseDto.setStoreId(storeId);
                allocationStorehouseDto.setStock(purchaseApplyDetailPo.getQuantity());
                allocationStorehouseDto.setStartUserId(userId);
                allocationStorehouseDto.setStartUserName(userName);
                allocationStorehouseDto.setCreateTime(new Date());
                allocationStorehouseDto.setRemark("紧急采购入库并调拨");
                allocationStorehouseDto.setApplyId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_applyId));
                allocationStorehouseDto.setOriginalStock("0");
                allocationStorehouseInnerServiceSMOImpl.saveAllocationStorehouses(allocationStorehouseDto);
                //生成调拨申请记录
                AllocationStorehouseApplyDto allocationStorehouseApplyDto = new AllocationStorehouseApplyDto();
                allocationStorehouseApplyDto.setApplyId(allocationStorehouseDto.getApplyId());
                allocationStorehouseApplyDto.setbId("-1");
                allocationStorehouseApplyDto.setStartUserId(allocationStorehouseDto.getStartUserId());
                allocationStorehouseApplyDto.setStartUserName(allocationStorehouseDto.getStartUserName());
                allocationStorehouseApplyDto.setRemark("紧急采购入库并调拨");
                allocationStorehouseApplyDto.setApplyCount(purchaseApplyDetailPo.getQuantity());
                allocationStorehouseApplyDto.setStoreId(storeId);
                allocationStorehouseApplyDto.setCreateTime(new Date());
                allocationStorehouseApplyDto.setState(AllocationStorehouseDto.STATE_SUCCESS);
                allocationStorehouseApplyDto.setCommunityId(reqJson.getString("communityId"));
                //调拨
                allocationStorehouseApplyDto.setApplyType("30000");
                allocationStorehouseApplyInnerServiceSMOImpl.saveAllocationStorehouseApplys(allocationStorehouseApplyDto);
                //小区仓库进行入库插入
                //集团仓库商品信息
                ResourceStoreDto resourceStoreDto2 = new ResourceStoreDto();
                resourceStoreDto2.setResCode(resourceStore.getString("resCode"));
                resourceStoreDto2.setResId(purchaseApplyDetailPo.getResId());
                List<ResourceStoreDto> resourceStoreDtoList = resourceStoreInnerServiceSMOImpl.queryResourceStores(resourceStoreDto2);
                ResourceStoreDto resourceStoreDto1 = BeanConvertUtil.covertBean(resourceStoreDtoList.get(0), ResourceStoreDto.class);
                resourceStoreDto1.setResId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_resId));
                resourceStoreDto1.setShId(resourceStore.getString("shzId"));
                resourceStoreDto1.setbId("-1");
                resourceStoreDto1.setStock(purchaseApplyDetailPo.getQuantity());
                resourceStoreDto1.setCreateTime(new Date());
                //获取紧急采购数量
                BigDecimal purchaseQuantity = new BigDecimal(purchaseApplyDetailPo.getQuantity());
                //获取最小计量单位数量
                if (StringUtil.isEmpty(resourceStore.getString("miniUnitStock"))) {
                    throw new IllegalArgumentException("最小计量单位数量不能为空!");
                }
                BigDecimal miniUnitStock = new BigDecimal(resourceStore.getString("miniUnitStock"));
                //计算最小计量总数
                BigDecimal miniStock = purchaseQuantity.multiply(miniUnitStock);
                resourceStoreDto1.setMiniStock(String.valueOf(miniStock));
                resourceStoreInnerServiceSMOImpl.saveResourceStore(resourceStoreDto1);
            }
        }
        purchaseApplyPo.setPurchaseApplyDetailPos(purchaseApplyDetailPos);
        ResponseEntity responseEntity = purchaseApplyBMOImpl.apply(purchaseApplyPo, reqJson);
        context.setResponseEntity(responseEntity);
    }
}