wang-hao-jie
2021-12-27 0caa1b9f5c708ab60ea06c1430b9c3c79613830c
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
package cn.exrick.xboot.your.controller.wx;
 
import cn.exrick.xboot.core.common.utils.PageUtil;
import cn.exrick.xboot.core.common.utils.ResultUtil;
import cn.exrick.xboot.core.common.vo.PageVo;
import cn.exrick.xboot.core.common.vo.Result;
import cn.exrick.xboot.your.entity.*;
import cn.exrick.xboot.your.service.*;
import cn.exrick.xboot.your.util.HaiKangPost;
import cn.exrick.xboot.your.util.HttpUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
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.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import java.text.DecimalFormat;
import java.util.*;
 
/**
 * @author whj
 */
@Slf4j
@RestController
@Api(tags = "小程序接口")
@RequestMapping("/xboot/wx")
@Transactional
public class IndexController {
 
    @Autowired
    private IOrderTaskService iOrderTaskService;
 
    @Autowired
    private ICustomerService iCustomerService;
 
    @Autowired
    private IOrderDetailService iOrderDetailService;
 
    @Autowired
    private ICarService iCarService;
 
    @Autowired
    private HaiKangPost haiKangPost;
 
    @Autowired
    private ISuggestService iSuggestService;
 
    @Autowired
    private ICustomerReceiveService iCustomerReceiveService;
 
 
    //2.微信登陆
    @RequestMapping("/login")
    @ApiOperation(value = "微信登陆")
    public Object doLogin(String code){
 
        JSONObject SessionKeyOpenId = getSessionKeyOrOpenId( code );
 
        String openid = SessionKeyOpenId.getStr("openid");
 
        if(StrUtil.isEmpty(openid)){
            return ResultUtil.error(SessionKeyOpenId.toString());
        }
 
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.eq("open_id",openid);
        Customer one = iCustomerService.getOne(wrapper);
        if(one==null){
            return ResultUtil.error(openid);//首次登陆需绑定零售许可证
        }else {
            return ResultUtil.data(one,"登录成功");
        }
    }
 
    public static JSONObject getSessionKeyOrOpenId(String code){
        //微信端登录code
        String wxCode = code;
        String requestUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=wx0f10f6d253f3ee6b&secret=4d4cbc8da31a96559114ad693de70631&grant_type=authorization_code&js_code="+code;
        JSONObject jsonObject = JSONUtil.parseObj( HttpUtil.get(requestUrl));
        return jsonObject;
    }
 
    //3.微信绑定零售许可证
    //licence:零售许可证号
    //openId:微信id
    @RequestMapping("/bindWx")
    @ApiOperation(value = "微信绑定零售许可证")
    public Object bindWx(String licence,String openId){
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.eq("licence",licence);
        Customer one = iCustomerService.getOne(wrapper);
        if(one==null){
            return ResultUtil.error("零售许可证无效");//首次登陆需绑定零售许可证
        }else {
            one.setOpenId(openId);
            iCustomerService.saveOrUpdate(one);
            return ResultUtil.data(one,"登录成功");
        }
    }
 
    //4.获取今日配送订单
    @RequestMapping("/getTodayOrder")
    @ApiOperation(value = "获取今日配送订单")
    public Object getTodayOrder(String customerId){
        OrderTask orderTask = getOrder(customerId);
        if(orderTask==null){
            return ResultUtil.data(new ArrayList<>());
        }else{
            QueryWrapper<OrderDetail> wrapper = new QueryWrapper<OrderDetail>();
            wrapper.eq("order_id",orderTask.getId());
            List<OrderDetail> list = iOrderDetailService.list(wrapper);
            return ResultUtil.data(list);
        }
    }
 
    public OrderTask getOrder(String customerId){
        QueryWrapper<OrderTask> wrapper2 = new QueryWrapper<OrderTask>();
        String format = DateUtil.format(new Date(), "yyyy-MM-dd");
        wrapper2.eq("customer_id",customerId);
        wrapper2.eq("send_date",format);
        OrderTask orderTask = iOrderTaskService.getOne(wrapper2);
        return orderTask;
    }
 
    //5.获取车辆位置
    @RequestMapping("/getCarInfo")
    @ApiOperation(value = "获取车辆位置")
    public Object getCarInfo(String customerId){
        QueryWrapper<OrderTask> wrapper2 = new QueryWrapper<OrderTask>();
        String format = DateUtil.format(new Date(), "yyyy-MM-dd");
        wrapper2.eq("customer_id",customerId);
        wrapper2.eq("send_date",format);
        OrderTask orderTask = iOrderTaskService.getOne(wrapper2);
        String carId = orderTask.getCarId();
        Car car = iCarService.getById(carId);
        String[] carids = new String[1];
        carids[0]= car.getCode();
        String latestGps = haiKangPost.findLatestGps(carids);
        JSONObject jsonObject = JSONUtil.parseObj(latestGps);
        String data = jsonObject.getStr("data");
        JSONObject jsonObject2 = JSONUtil.parseObj(data);
        Integer lng = jsonObject2.getInt("longitude");
        Integer lat = jsonObject2.getInt("latitude");
 
        DecimalFormat df = new DecimalFormat("#.000000");
        Map<String,Object> map = new HashMap<>();
        map.put("lng",df.format(lng/360000.0));
        map.put("lat",df.format(lat/360000.0));
        map.put("content","还有3单,预计10:22送达");
        return ResultUtil.data(map);
    }
 
    //6.加油助力
    @RequestMapping("/likes")
    @ApiOperation(value = "加油助力")
    public Object likes(String customerId){
        OrderTask order = getOrder(customerId);
        order.setLikes(1);
        iOrderTaskService.saveOrUpdate(order);
        return ResultUtil.success("助力成功");
    }
 
    //7.获取订单列表
    @RequestMapping("/getOrderList")
    @ApiOperation(value = "获取订单列表")
    public Object getOrderList(String customerId, String startTime, String endTime, PageVo page){
        page.setSort("sendDate");
        page.setOrder("desc");
        QueryWrapper<OrderTask> wrapper = new QueryWrapper<>();
        wrapper.eq("customer_id",customerId);
        if(StrUtil.isNotEmpty(startTime)){
            wrapper.ge("send_date",startTime);
        }
        if(StrUtil.isNotEmpty(endTime)){
            wrapper.le("send_date",endTime);
        }
        IPage<OrderTask> page1 = iOrderTaskService.page(PageUtil.initMpPage(page), wrapper);
        return ResultUtil.data(page1.getRecords());
    }
 
    //8.获取订单详情列表
    @RequestMapping("/getOrderDetail")
    @ApiOperation(value = "获取订单详情列表")
    public Object getOrderDetail(String orderId){
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",orderId);
        List<OrderDetail> list1 = iOrderDetailService.list(wrapper);
        return ResultUtil.data(list1);
    }
 
    //9.评价
    @RequestMapping("/remark")
    @ApiOperation(value = "评价")
    public Object remark(int level,String orderId){
        OrderTask byId = iOrderTaskService.getById(orderId);
        byId.setLevel(level);
        iOrderTaskService.saveOrUpdate(byId);
        return ResultUtil.success("评价成功");
    }
 
 
    //10.获取意见建议列表
    @RequestMapping(value = "/getSuggestByPage", method = RequestMethod.GET)
    @ApiOperation(value = "获取意见建议列表")
    public Result<IPage<Suggest>> getByPage(String customerId,PageVo page) {
        QueryWrapper<Suggest> wrapper = new QueryWrapper<>();
        wrapper.eq("customer_id",customerId);
        IPage<Suggest> data = iSuggestService.page2(PageUtil.initMpPage(page),wrapper);
        return new ResultUtil<IPage<Suggest>>().setData(data);
    }
 
    //12.新增意见建议
    @RequestMapping(value = "/insertSuggest", method = RequestMethod.POST)
    @ApiOperation(value = "新增意见建议")
    public Result<Suggest> insertSuggest(String customerId,String content) {
        Suggest suggest = new Suggest();
        suggest.setContent(content);
        suggest.setCustomerId(customerId);
        if (iSuggestService.saveOrUpdate(suggest)) {
            return new ResultUtil<Suggest>().setSuccessMsg("操作成功");
        }
        return new ResultUtil<Suggest>().setErrorMsg("操作失败");
    }
 
    //13.新增签收人
    @RequestMapping(value = "/insertReceive", method = RequestMethod.POST)
    @ApiOperation(value = "新增签收人")
    public Result<CustomerReceive> insertReceive(String customerId,String name,String phone) {
        CustomerReceive customerReceive = new CustomerReceive();
        customerReceive.setCustomerId(customerId);
        customerReceive.setPhone(phone);
        customerReceive.setName(name);
        customerReceive.setFstatus(0);
        if (iCustomerReceiveService.saveOrUpdate(customerReceive)) {
            return new ResultUtil<CustomerReceive>().setSuccessMsg("操作成功");
        }
        return new ResultUtil<CustomerReceive>().setErrorMsg("操作失败");
    }
}