xiaoyong931011
2021-06-30 5a855bd4b2c62f8b234d6c983e3131921a378975
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package com.xzx.gc.order.controller;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzx.gc.common.Result;
import com.xzx.gc.common.constant.CommonEnum;
import com.xzx.gc.common.constant.Constants;
import com.xzx.gc.common.constant.OrderEnum;
import com.xzx.gc.common.dto.CommonDto;
import com.xzx.gc.common.dto.SimplePage;
import com.xzx.gc.common.dto.log.OperationAppLog;
import com.xzx.gc.common.exception.RestException;
import com.xzx.gc.common.request.BaseController;
import com.xzx.gc.common.utils.*;
import com.xzx.gc.entity.*;
import com.xzx.gc.model.JsonResult;
import com.xzx.gc.model.admin.PayStorageModel;
import com.xzx.gc.model.admin.StorageUserModel;
import com.xzx.gc.model.pay.MoneyAuditDto;
import com.xzx.gc.model.system.ConfigInfoReq;
import com.xzx.gc.model.system.ConfigInfoVo;
import com.xzx.gc.order.dto.*;
import com.xzx.gc.order.mapper.*;
import com.xzx.gc.order.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;
 
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
/**
 * @author :zz
 */
@RestController
@Api(tags = {"入库管理"})
@Validated
@Slf4j
public class StorageController extends BaseController {
 
    @Autowired
    private OrderClockInService orderClockInService;
 
    @Autowired
    private CityPartnerService cityPartnerService;
 
    @Autowired
    private OrderStorageInfoMapper orderStorageInfoMapper;
 
 
    @Autowired
    private AccountService accountService;
 
 
    @Autowired
    private OrderClockInMapper orderClockInMapper;
 
    @Autowired
    private OrderBatchInfoMapper orderBatchInfoMapper;
 
    @Autowired
    private OrderStorageDetailMapper orderStorageDetailMapper;
 
    @Autowired
    private StorageService storageService;
 
    @Autowired
    private OrderMapper orderMapper;
 
 
    @Autowired
    private ConfigService configService;
 
    @Autowired
    private RedisUtil redisUtil;
 
 
 
    @Autowired
    private OtherUserMapper otherUserMapper;
 
    @Autowired
    private BusinessUtil businessUtil;
 
 
    @Autowired
    private UserService userService;
 
 
    @PostMapping("/storage/rk/list")
    @ApiOperation(value = "入库员新任务列表")
    public Result<PageInfo<StorageRkResDto>> rkList(HttpServletRequest request, @RequestBody SimplePage simplePage) {
        Result<PageInfo<StorageRkResDto>> result = new Result();
        List<StorageRkResDto> list = new ArrayList<>();
        OrderStorageInfo orderStorageInfo = new OrderStorageInfo();
        orderStorageInfo.setStorageStatus(Convert.toShort(OrderEnum.入库检验中.getValue()));
 
        String userId = getUserId(request);
        OtherUserInfo otherUserInfo = otherUserMapper.selectByPrimaryKey(userId);
        //仓库或打包站id
        Integer sysorageId = null;
        if (otherUserInfo.getUserType().equals(CommonEnum.打包员.getValue())||otherUserInfo.getUserType().equals(CommonEnum.打包站运营员.getValue())) {
            CityPartner byuserId = cityPartnerService.findByuserId(otherUserInfo.getUserId(), OrderEnum.打包站.getValue());
            if (byuserId != null) {
                sysorageId = byuserId.getId();
                orderStorageInfo.setSysStorageId(Convert.toLong(sysorageId));
                orderStorageInfo.setSysStorageType(Convert.toShort(OrderEnum.打包站.getValue()));
            }
        } else if (otherUserInfo.getUserType().equals(CommonEnum.入库员.getValue())) {
            sysorageId = Convert.toInt(otherUserInfo.getStorageId());
            orderStorageInfo.setSysStorageType(Convert.toShort(OrderEnum.仓库.getValue()));
            orderStorageInfo.setSysStorageId(Convert.toLong(sysorageId));
        }
        PageHelper.startPage(simplePage.getPageNo(), simplePage.getPageSize());
 
        List<OrderStorageInfo> select = orderStorageInfoMapper.select(orderStorageInfo);
        if (CollUtil.isNotEmpty(select)) {
            for (OrderStorageInfo info : select) {
                StorageRkResDto storageRkResDto = new StorageRkResDto();
                BeanUtils.copyProperties(info, storageRkResDto);
                //地称信息
                Example example = new Example(OrderBatchInfo.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("orderId", info.getOrderId());
                OrderBatchInfo orderBatchInfo = orderBatchInfoMapper.selectOneByExample(example);
                if (orderBatchInfo != null) {
                    //载货打卡时间
                    storageRkResDto.setCreateTime(DateUtil.parseDateTime(orderBatchInfo.getCreateTime()).toString(DateUtils.DATE_FORMAT_HM));
                    storageRkResDto.setWeight(orderBatchInfo.getWeight());
 
                    Example example1 = new Example(OrderClockIn.class);
                    Example.Criteria criteria1 = example1.createCriteria();
                    criteria1.andEqualTo("userId", orderBatchInfo.getUserId());
                    criteria1.andCondition("substring_index(create_time,' ',1)=", StrUtil.split(orderBatchInfo.getCreateTime(), " ")[0]);
                    OrderClockIn orderClockIn = orderClockInMapper.selectOneByExample(example1);
                    if (orderClockIn != null) {
                        storageRkResDto.setEmptyWeight(orderClockIn.getWeight());
                    }
                }
                if (StrUtil.isBlank(storageRkResDto.getWeight())) {
                    storageRkResDto.setWeight(Convert.toStr(Constants.WEIGHT_INIT));
                }
                if (StrUtil.isBlank(storageRkResDto.getEmptyWeight())) {
                    storageRkResDto.setEmptyWeight(Convert.toStr(Constants.WEIGHT_INIT));
                }
                list.add(storageRkResDto);
            }
        }
        PageInfo<StorageRkResDto> pageInfo = new PageInfo(list);
        int i = storageService.countCompleteOrder(userId);
        pageInfo.setNavigatePages(i);
        result.setData(pageInfo);
        return result;
    }
 
    @PostMapping("/storage/rk/detail")
    @ApiOperation(value = "入库员新任务详情", notes = "id:入库ID")
    public Result<RkDto> rkDetailList(HttpServletRequest request, @RequestBody CommonDto commonDto) {
        Result<RkDto> result = new Result();
        String storageId = commonDto.getId();
        LogUtils.setTraceId(LogUtils.TRACE_ORDER_ID, storageId);
        RkDto rk = new RkDto();
        List<OrderItemDto> orderItemDtos = new ArrayList<>();
        OrderStorageDetailInfo orderStorageDetailInfo = new OrderStorageDetailInfo();
        orderStorageDetailInfo.setStorageId(storageId);
        orderStorageDetailInfo.setFlag(OrderEnum.回收型.getValue());
        List<OrderStorageDetailInfo> select = orderStorageDetailMapper.select(orderStorageDetailInfo);
 
        //1为上门 2为定点
        int type = 1;
        OrderStorageInfo orderStorageInfo = orderStorageInfoMapper.selectByPrimaryKey(storageId);
        String orderId = orderStorageInfo.getOrderId();
        String s = orderId.split(",")[0];
        OrderInfo orderInfo = orderMapper.selectByPrimaryKey(s);
        Long storeId = orderInfo.getStoreId();
        if (storeId != null) {
            type = 2;
        }
 
        int usertype = 1;
        String userId = getUserId(request);
        OtherUserInfo otherUserInfo = otherUserMapper.selectByPrimaryKey(userId);
        String packageId = null;
        if (otherUserInfo.getUserType().equals(CommonEnum.打包员.getValue())||otherUserInfo.getUserType().equals(CommonEnum.打包站运营员.getValue())) {
            usertype = 2;
            packageId = otherUserInfo.getPartnerId();
        }else if(otherUserInfo.getUserType().equals(CommonEnum.入库员.getValue())){
            packageId=otherUserInfo.getStorageId();
        }
        //设置分类信息
        String receiver = orderStorageInfo.getReceiver();
        storageService.setItem(rk, orderItemDtos, select, usertype, packageId);
        rk.setStorageUserId(userId);
        rk.setStorageId(storageId);
 
        //地称信息
        BigDecimal recycleMoney = orderStorageInfo.getRecycleMoney();
        rk.setMoney(Convert.toStr(recycleMoney));
        Example example = new Example(OrderBatchInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId", orderStorageInfo.getOrderId());
        OrderBatchInfo orderBatchInfo = orderBatchInfoMapper.selectOneByExample(example);
        if (orderBatchInfo != null) {
            rk.setWeight(orderBatchInfo.getWeight());
        }
 
        if (StrUtil.isBlank(rk.getWeight())) {
            rk.setWeight(Convert.toStr(Constants.WEIGHT_INIT));
        }
        result.setData(rk);
        return result;
    }
 
    @PostMapping("/storage/rk/code/detail")
    @ApiOperation(value = "手动扫码入库新任务详情")
    public Result<RkDto> rkCodeDetailList(HttpServletRequest request, @RequestBody CommonDto commonDto) {
        JSONObject parse = null;
        //回收员ID
        String userId = "";
        Long timestamp = null;
        try {
            String extra = commonDto.getExtra();
            extra = StrUtil.removePrefix(extra, ",");
            String decrypt = SecurityUtil.decrypt(Constants.PWD_DECRET, extra);
            parse = JSONUtil.parseObj(decrypt);
            userId = parse.getStr("userId");
//            devCode = parse.getStr("devCode");
            timestamp = parse.getLong("timestamp");
        } catch (Exception e) {
            ExceptionUtils.err("解码失败", e);
        }
 
        Date date = new Date(timestamp);
 
        if(SpringUtil.isDev()) {
            if (DateUtil.between(date, new Date(), DateUnit.HOUR) > 30) {
                throw new RestException("二维码已过期");
            }
        }else {
            if (DateUtil.between(date, new Date(), DateUnit.SECOND) > 30) {
                throw new RestException("二维码已过期");
            }
        }
 
        Result<RkDto> rkDtoResult=new Result<>();
 
        //判断扫码开关是否打开  关闭的情况下是否用户入库失败三次
        String userId1 = getUserId(request);
 
        //入库员或打包员
        OtherUserInfo otherUserInfo = otherUserMapper.selectByPrimaryKey(userId1);
        //回收员所属的合伙人
        String partnerId = otherUserMapper.selectByPrimaryKey(userId).getPartnerId();
 
        if (otherUserInfo.getUserType().equals(CommonEnum.打包员.getValue())||otherUserInfo.getUserType().equals(CommonEnum.打包站运营员.getValue())) {
            String packageId =otherUserInfo.getPartnerId();
 
            /*
            判断扫码的入库员所属的合伙人是否与回收员一致
             */
 
            String packingStation = cityPartnerService.findById(Convert.toInt(packageId)).getPackingStation();
            if(!partnerId.equals(packingStation)){
                return Result.error(-1, "禁止扫码");
            }
 
            rkDtoResult = storageService.rkDetail(request, userId, 2, packageId);
        } else {
            ConfigInfoReq configInfoReq = new ConfigInfoReq();
            configInfoReq.setConfigTypeCode("CODE_SCAN_RK");
            List<ConfigInfoVo> configInfoVos = configService.configInfoQuery(configInfoReq);
            if (CollUtil.isNotEmpty(configInfoVos) && Constants.OPEN.equals(configInfoVos.get(0).getConfigValue())) {
 
 
                if(!partnerId.equals(otherUserInfo.getPartnerId())){
                    return Result.error(-1, "禁止扫码");
                }
 
                rkDtoResult = storageService.rkDetail(request, userId, 1, otherUserInfo.getStorageId());
            } else {
                if (orderClockInService.notClock(userId)) {
                    rkDtoResult = storageService.rkDetail(request, userId, 1, otherUserInfo.getStorageId());
                }else {
                    //开关关闭的情况下
                    String errNum = redisUtil.hget(Constants.REDIS_ORDER_KEY + "storage:errcount", userId);
                    if (errNum != null && Convert.toInt(errNum) >= 3) {
                        rkDtoResult = storageService.rkDetail(request, userId, 1, otherUserInfo.getStorageId());
                    } else {
                        return Result.error(-1, "禁止扫码");
                    }
                }
            }
        }
 
        if(rkDtoResult.getCode()==0){
            String mobilePhone = userService.findOtherByUserId(getUserId(request),1);
            OperationAppLog build = OperationAppLog.builder().appPrograme(getFrontClient(request)).opreateName(mobilePhone)
                    .methodName(Constants.ORDER_MODUL_NAME).operateAction("手动扫码入库-"+rkDtoResult.getData().getStorageId()).build();
            mqUtil.sendApp(build);
        }
        return rkDtoResult;
 
    }
 
 
 
 
    @PostMapping("/storage/rk/statistics")
    @ApiOperation(value = "入库统计(含回收员与入库员与打包员)")
    public Result<RkStatisticsDto> rkStatistics(HttpServletRequest request, @RequestBody RkStatisDto rkStatisDto) {
        Result<RkStatisticsDto> res = new Result<>();
        String userType = rkStatisDto.getUserType();
        String userId = getUserId(request);
 
        RkStatisticsDto rkStatisticsDto = storageService.statistics(userId, userType,rkStatisDto.getData(),rkStatisDto.getReceiver());
        res.setData(rkStatisticsDto);
        return res;
    }
 
    @PostMapping("/storage/rk")
    @ApiOperation(value = "入库")
    public Result rk(HttpServletRequest request, @RequestBody RkDto rkDto) {
        rkDto.setVersion(getVersion(request));
        //处理金额和重量
        BigDecimal money = rkDto.getStorageMoney();
        if (money != null && money.compareTo(BigDecimal.ZERO) != 0) {
            String money1 = businessUtil.changeMoney(Convert.toStr(money));
            rkDto.setStorageMoney(Convert.toBigDecimal(money1));
        }
        String weight = rkDto.getStorageWeight();
        if (StrUtil.isNotBlank(weight)) {
            String s = businessUtil.changeWeight(weight);
            rkDto.setStorageWeight(s);
        }
        LogUtils.setTraceId(LogUtils.TRACE_ORDER_ID, rkDto.getStorageId());
        storageService.rk(rkDto);
 
        String mobilePhone = userService.findOtherByUserId(getUserId(request),1);
        OperationAppLog build = OperationAppLog.builder().appPrograme(getFrontClient(request)).opreateName(mobilePhone)
                .methodName(Constants.ORDER_MODUL_NAME).operateAction("入库-"+rkDto.getStorageId()).build();
        mqUtil.sendApp(build);
 
        return Result.success();
    }
 
 
}