wang-hao-jie
2022-01-18 d9da603305a2b94bde78483fa8777a43ee352548
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
package cn.exrick.xboot.your.controller;
 
import cn.exrick.xboot.core.common.utils.PageUtil;
import cn.exrick.xboot.core.common.utils.ResultUtil;
import cn.exrick.xboot.core.common.utils.SecurityUtil;
import cn.exrick.xboot.core.common.vo.PageVo;
import cn.exrick.xboot.core.common.vo.Result;
import cn.exrick.xboot.core.entity.Department;
import cn.exrick.xboot.core.entity.User;
import cn.exrick.xboot.core.service.DepartmentService;
import cn.exrick.xboot.core.service.UserService;
import cn.exrick.xboot.your.entity.*;
import cn.exrick.xboot.your.service.*;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @author whj
 */
@Slf4j
@RestController
@Api(tags = "订单接口")
@RequestMapping("/xboot/orderTask")
@Transactional
public class OrderTaskController {
 
    @Autowired
    private IOrderTaskService iOrderTaskService;
 
    @Autowired
    private IAreaService iAreaService;
 
    @Autowired
    private ICustomerService iCustomerService;
 
    @Autowired
    private IOrderDetailService iOrderDetailService;
 
    @Autowired
    private SecurityUtil securityUtil;
 
    @Autowired
    private UserService userService;
 
    @Autowired
    private IAreaSectionService iAreaSectionService;
 
    @Autowired
    private ICarService iCarService;
 
    @Autowired
    private DepartmentService departmentService;
 
    @Autowired
    private IEventLogService iEventLogService;
 
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<OrderTask> get(@PathVariable String id) {
 
        OrderTask orderTask = iOrderTaskService.getById(id);
        return new ResultUtil<OrderTask>().setData(orderTask);
    }
 
    public Area getArea(String userId){
        QueryWrapper<Area> wrapper = new QueryWrapper<Area>();
        wrapper.eq("user_id",userId);
        Area area = iAreaService.getOne(wrapper);
        if(area==null){
            QueryWrapper<Car> carQueryWrapper = new QueryWrapper<Car>();
            carQueryWrapper.eq("user_id",userId);
            Car one = iCarService.getOne(carQueryWrapper);
 
            QueryWrapper<Area> wrapper3 = new QueryWrapper<Area>();
            wrapper3.eq("user_id",one.getFollowUserId());
            area = iAreaService.getOne(wrapper3);
            return area;
        }
        return area;
    }
 
    @RequestMapping(value = "/getByUserId", method = RequestMethod.GET)
    @ApiOperation(value = "获取今日任务统计信息")
    public Result<Object> getAll() {
        Area area = getArea(securityUtil.getCurrUser().getId());
        if(area==null){
            return ResultUtil.error("请联系管理员绑定片区配送员");
        }
 
        QueryWrapper<OrderTask> wrapper2 = new QueryWrapper<OrderTask>();
        String format = DateUtil.format(new Date(), "yyyy-MM-dd");
        wrapper2.eq("area_id",area.getId());
        wrapper2.eq("send_date",format);
        List<OrderTask> list = iOrderTaskService.list(wrapper2);
 
        int sum = iOrderTaskService.sum(area.getId(),format);
 
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("count",list.size());
        map.put("sum",sum);
        map.put("name","");
        if(list.size()>0){
            OrderTask orderTask = list.get(0);
            String areaSectionId = orderTask.getAreaSectionId();
            AreaSection a = iAreaSectionService.getById(areaSectionId);
            Area area2 = iAreaService.getById(orderTask.getAreaId());
            map.put("name",area2.getName()+a.getName());
            list.clear();
        }
        return new ResultUtil<Object>().setData(map);
    }
 
    @RequestMapping(value = "/getTodayOrder", method = RequestMethod.GET)
    @ApiOperation(value = "获取今日任务详情")
    public Result<List<OrderTask>> getTodayOrder() {
        Area area = getArea(securityUtil.getCurrUser().getId());
        if(area==null){
            return ResultUtil.error("请联系管理员绑定片区配送员");
        }
 
        QueryWrapper<OrderTask> wrapper2 = new QueryWrapper<OrderTask>();
        String format = DateUtil.format(new Date(), "yyyy-MM-dd");
        wrapper2.eq("a.area_id",area.getId());
        wrapper2.eq("a.send_date",format);
        wrapper2.orderByAsc("a.seq");
        List<OrderTask> list = iOrderTaskService.list2(wrapper2);
        return new ResultUtil<List<OrderTask>>().setData(list);
    }
 
    @RequestMapping(value = "/getTodayOrderDetail", method = RequestMethod.GET)
    @ApiOperation(value = "获取当前配送商户详情")
    public Result<OrderTask> getTodayOrderDetail(String orderId) {
        OrderTask orderTask = new OrderTask();
        if(!StrUtil.isEmpty(orderId)){
            orderTask = iOrderTaskService.getById(orderId);
            if(orderTask.getStatus()!=0){
                return ResultUtil.error("此商户已配送");
            }
        }else{
            Area area = getArea(securityUtil.getCurrUser().getId());
            if(area==null){
                return ResultUtil.error("请联系管理员绑定片区配送员");
            }
 
            QueryWrapper<OrderTask> wrapper2 = new QueryWrapper<OrderTask>();
            String format = DateUtil.format(new Date(), "yyyy-MM-dd");
            wrapper2.eq("area_id",area.getId());
            wrapper2.eq("send_date",format);
            wrapper2.eq("status",0);
            wrapper2.orderByAsc("seq");
            List<OrderTask> list = iOrderTaskService.list(wrapper2);
            if(list.size()>0){
                orderTask = list.get(0);
            }else{
                return new ResultUtil<OrderTask>().setData(null);
            }
        }
 
        orderTask.setCustomer(iCustomerService.getById(orderTask.getCustomerId()));
        QueryWrapper<OrderDetail> wrapper3 = new QueryWrapper<OrderDetail>();
        wrapper3.eq("order_id",orderTask.getId());
        orderTask.setOrderDetails(iOrderDetailService.list(wrapper3));
 
        return new ResultUtil<OrderTask>().setData(orderTask);
    }
 
    @RequestMapping(value = "/getTodayOtherInfo", method = RequestMethod.GET)
    @ApiOperation(value = "获取其它信息")
    public Result<Object> getTodayOtherInfo() {
 
        Area area = getArea(securityUtil.getCurrUser().getId());
        if(area==null){
            return ResultUtil.error("请联系管理员绑定片区配送员");
        }
 
        QueryWrapper<OrderTask> wrapper2 = new QueryWrapper<OrderTask>();
        String format = DateUtil.format(new Date(), "yyyy-MM-dd");
        wrapper2.eq("area_id",area.getId());
        wrapper2.eq("send_date",format);
        wrapper2.ne("status",0);
        wrapper2.orderByAsc("seq");
        int count = iOrderTaskService.count(wrapper2);
 
 
        QueryWrapper<OrderTask> wrapper4 = new QueryWrapper<OrderTask>();
        wrapper4.eq("area_id",area.getId());
        wrapper4.eq("send_date",format);
        int count2 = iOrderTaskService.count(wrapper4);
 
 
        Map<String,Object> map = new HashMap<>();
        map.put("num",count+"/"+count2);
 
        QueryWrapper<OrderTask> wrapper5 = new QueryWrapper<OrderTask>();
        wrapper5.ne("status",0);
        wrapper5.eq("send_date",format);
 
        PageVo page = new PageVo();
        page.setSort("updateTime");
        page.setOrder("desc");
        page.setPageSize(1);
        page.setPageNumber(0);
        IPage<OrderTask> data = iOrderTaskService.page(PageUtil.initMpPage(page), wrapper5);
        String content = "";
        if(data.getRecords().size()>0){
            String userId = data.getRecords().get(0).getUserId();
            User user = userService.get(userId);
 
            QueryWrapper<OrderTask> wrapper6 = new QueryWrapper<OrderTask>();
            wrapper6.eq("user_id",userId);
            wrapper6.eq("send_date",format);
            wrapper6.ne("status",0);
            int count6 = iOrderTaskService.count(wrapper6);
            if(count6>0){
                content+=user.getNickname()+"已配送"+count6+"单";
            }
        }
        map.put("content",content);
 
        map.put("lng","");
        map.put("lat","");
        String deptId = securityUtil.getCurrUser().getDepartmentId();
        if(StrUtil.isNotEmpty(deptId)){
            Department department = departmentService.get(deptId);
            map.put("lng",department.getLng());
            map.put("lat",department.getLat());
        }
        return new ResultUtil<Object>().setData(map);
    }
 
    @RequestMapping(value = "/addImg", method = RequestMethod.POST)
    @ApiOperation(value = "上传门头照")
    public Object saveOrUpdate(String orderId,String imgUrl) {
        OrderTask orderTask = iOrderTaskService.getById(orderId);
        orderTask.setImg(imgUrl);
        iOrderTaskService.saveOrUpdate(orderTask);
        return ResultUtil.success("添加成功");
    }
 
    @RequestMapping(value = "/signFor", method = RequestMethod.POST)
    @ApiOperation(value = "签收")
    public Object signFor(String orderId,int status,String content,String customerReceiveId,int time,String carId) {
        if(status==1){
            if(StrUtil.isEmpty(customerReceiveId)){
                return ResultUtil.error("正常签收,接货人id必填");
            }
        }
 
        if(StrUtil.isEmpty(carId)){
            return ResultUtil.error("车辆id不能为空");
        }
        OrderTask orderTask = iOrderTaskService.getById(orderId);
        orderTask.setStatus(status);
        orderTask.setUserId(securityUtil.getCurrUser().getId());
        orderTask.setTime(time);
        orderTask.setCarId(carId);
        if(StrUtil.isNotEmpty(customerReceiveId)){
            orderTask.setCustomerReceiveId(customerReceiveId);
        }
 
        if(!StrUtil.isEmpty(content)){
            orderTask.setRemarks(content);
        }else{
            if(status==2){
                return ResultUtil.error("请填写异常签收原因");
            }
        }
        iOrderTaskService.saveOrUpdate(orderTask);
 
        Car car = iCarService.getById(orderTask.getCarId());
        EventLog eventLog = new EventLog();
        eventLog.setCarNo(car.getCarNo());
        eventLog.setRefId(orderId);
        eventLog.setType(6);//6:配送完成
        iEventLogService.saveOrUpdate(eventLog);
        return ResultUtil.success("添加成功");
    }
 
    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<OrderTask>> getByPage(PageVo page) {
 
        IPage<OrderTask> data = iOrderTaskService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<OrderTask>>().setData(data);
    }
 
    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<OrderTask> saveOrUpdate(OrderTask orderTask) {
 
        if (iOrderTaskService.saveOrUpdate(orderTask)) {
            return new ResultUtil<OrderTask>().setData(orderTask);
        }
        return new ResultUtil<OrderTask>().setErrorMsg("操作失败");
    }
 
    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@RequestParam String[] ids) {
 
        for (String id : ids) {
            iOrderTaskService.removeById(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }
}