fix
Helius
2021-09-15 7ab7943de217a9e5d4a489c22c7ae27a29692d55
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
package com.xzx.gc.system.controller;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzx.gc.common.Result;
import com.xzx.gc.common.annotations.PassToken;
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.request.BaseController;
import com.xzx.gc.common.utils.BusinessUtil;
import com.xzx.gc.common.utils.LocationUtils;
import com.xzx.gc.common.utils.StringUtils;
import com.xzx.gc.entity.ConfigInfo;
import com.xzx.gc.entity.OrderInfo;
import com.xzx.gc.entity.OtherUserInfo;
import com.xzx.gc.entity.StoreInfo;
import com.xzx.gc.model.JsonResult;
import com.xzx.gc.model.user.AddressVo;
import com.xzx.gc.system.dto.StoreInfoUpDto;
import com.xzx.gc.system.mapper.*;
import com.xzx.gc.system.service.AddressService;
import com.xzx.gc.system.service.OtherUserService;
import com.xzx.gc.system.service.StoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;
 
import javax.servlet.http.HttpServletRequest;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
 
/**
 * @author :zz
 */
@RestController
@Api(tags = {"门店管理"})
@Validated
public class StoreInfoController extends BaseController {
 
    @Autowired
    private StoreInfoMapper storeInfoMapper;
 
    @Autowired
    private ConfigMapper configMapper;
 
    @Autowired
    private OtherUserMapper otherUserMapper;
 
    @Autowired
    private OrderMapper orderMapper;
 
 
    @Autowired
    private StoreService storeService;
 
    @Autowired
    private AddressService addressService;
 
    @Autowired
    private OtherUserService otherUserService;
 
 
    @PostMapping("/storeInfo/select")
    @ApiOperation(value = "分页查询门店", notes = "extra代表模糊查询字段")
    public Result<PageInfo<StoreInfo>> find(HttpServletRequest request, @RequestBody SimplePage simplePage) {
        ConfigInfo configInfo = new ConfigInfo();
        configInfo.setConfigTypeCode("STORE_STATUS");
        ConfigInfo configInfo1 = configMapper.selectOne(configInfo);
        String configValue = configInfo1.getConfigValue();
        PageHelper.startPage(simplePage.getPageNo(), simplePage.getPageSize());
 
 
        Example example = new Example(StoreInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("id",Constants.DEFAULT_ID);
        if (Constants.CLOSE.equals(configValue)) {
            criteria.andEqualTo("userId", getUserId(request));
        }
        criteria.andEqualTo("delFlag", 0);
//        criteria.andEqualTo("flag", 0);
 
        String extra = simplePage.getExtra();
        if (StrUtil.isNotBlank(extra)) {
            extra="%"+extra+"%";
            example.and().orLike("mobilePhone", extra).orLike("userName", extra).orLike("addressArea", extra).orLike("detailAddress", extra);
        }
        example.setOrderByClause("create_time desc,flag asc");
        List<StoreInfo> storeInfos = storeInfoMapper.selectByExample(example);
        for (StoreInfo storeInfo : storeInfos) {
            String provinceId = storeInfo.getProvinceId();
            String cityId = storeInfo.getCityId();
            String townshipId = storeInfo.getTownshipId();
            if(StrUtil.isNotBlank(provinceId)&&StrUtil.isNotBlank(cityId)&&StrUtil.isNotBlank(townshipId)){
                AddressVo byId = addressService.findByLevelId(provinceId, cityId, townshipId);
                if(byId!=null){
                    storeInfo.setProvinceName(byId.getProvinceName());
                    storeInfo.setTownshipName(byId.getTownshipName());
                    storeInfo.setCityName(byId.getCityName());
                }
            }
        }
        PageInfo<StoreInfo> pageInfo = new PageInfo<>(storeInfos);
        return Result.success(pageInfo);
    }
 
    @PostMapping("/storeInfo/selectUser")
    @ApiOperation(value = "查询门店回收员", notes = "目前只能查出自己,可兼容以后的固定门店")
    public Result<List<OtherUserInfo>> selectUser(HttpServletRequest request) {
        List<OtherUserInfo> list = new ArrayList<>();
        ConfigInfo configInfo = new ConfigInfo();
        configInfo.setConfigTypeCode("STORE_STATUS");
        ConfigInfo configInfo1 = configMapper.selectOne(configInfo);
        String configValue = configInfo1.getConfigValue();
        if (Constants.CLOSE.equals(configValue)) {
            OtherUserInfo otherUserInfo = otherUserMapper.selectByPrimaryKey(getUserId(request));
            list.add(otherUserInfo);
        } else if (Constants.OPEN.equals(configValue)) {
            list=otherUserService.find();
        }
        return Result.success(list);
    }
 
 
    @PostMapping("/storeInfo/add")
    @ApiOperation(value = "添加门店")
    public Result add(HttpServletRequest request, @Validated @RequestBody StoreInfo storeInfo) {
        AddressVo byName = addressService.findByName(storeInfo.getProvinceName(), storeInfo.getCityName(), storeInfo.getTownshipName());
        if(byName!=null){
            storeInfo.setProvinceId(byName.getProvinceId());
            storeInfo.setCityId(byName.getCityId());
            storeInfo.setTownshipId(byName.getTownshipId());
        }
        //真实门店关闭的状态下只能新增一个门店
        StoreInfo storeInfo1 = new StoreInfo();
        storeInfo1.setUserId(storeInfo.getUserId());
        storeInfo1.setDelFlag(Constants.DEL_NOT_FLAG + "");
        int i = storeInfoMapper.selectCount(storeInfo1);
        if (i > 0) {
            return Result.error(-1, "一个用户只能对应一个门店");
        }
        storeInfo.setCreateTime(DateUtil.now());
        storeInfoMapper.insertSelective(storeInfo);
        return Result.success("");
    }
 
 
    @PostMapping("/storeInfo/delete")
    @ApiOperation(value = "删除门店", notes = "id:传列表返回的门店ID")
    public Result delete(HttpServletRequest request, @Validated @RequestBody CommonDto commonDto) {
        String id = commonDto.getId();
        if (isOrder(id)) {
            return Result.error(-1, "用户已经下单,您不能进行删除操作");
        }
        StoreInfo storeInfo = new StoreInfo();
        storeInfo.setId(Long.parseLong(id));
        storeInfo.setDelFlag(Constants.DEL_FLAG + "");
        storeInfoMapper.updateByPrimaryKeySelective(storeInfo);
        return Result.success("");
    }
 
    @PostMapping("/storeInfo/update")
    @ApiOperation(value = "修改门店")
    public Result update(HttpServletRequest request, @Validated @RequestBody StoreInfoUpDto storeInfoUpDto) {
        if (isOrder(storeInfoUpDto.getId().toString())) {
            return Result.error(-1, "用户已经下单,您不能进行修改操作");
        }
 
        StoreInfo storeInfo = new StoreInfo();
        BeanUtil.copyProperties(storeInfoUpDto, storeInfo);
 
        AddressVo byName = addressService.findByName(storeInfo.getProvinceName(), storeInfo.getCityName(), storeInfo.getTownshipName());
        if(byName!=null){
            storeInfo.setProvinceId(byName.getProvinceId());
            storeInfo.setCityId(byName.getCityId());
            storeInfo.setTownshipId(byName.getTownshipId());
        }
 
        storeInfoMapper.updateByPrimaryKeySelective(storeInfo);
        return Result.success("");
    }
 
    @PostMapping("/storeInfo/selectNearby")
    @ApiOperation(value = "查看附近门店", notes = "参数:extra传经纬度以逗号分隔,返回data为空则是无门店")
    @PassToken
    public Result<StoreInfo> selectNearby(HttpServletRequest request, @RequestBody CommonDto commonDto) {
        String longitude = commonDto.getExtra().split(",")[0];
        String latitude = commonDto.getExtra().split(",")[1];
 
        if (StrUtil.isNotBlank(latitude) && StrUtil.isNotBlank(longitude)) {
            List<StoreInfo> storeInfos=storeService.select(true);
            double temp = Double.MAX_VALUE;
            Dict dict = Dict.create();
            if (CollUtil.isNotEmpty(storeInfos)) {
                for (StoreInfo storeInfo : storeInfos) {
                    if (StringUtils.isNotBlank(storeInfo.getLatitude()) && StringUtils.isNotBlank(storeInfo.getLongitude())) {
                        double distance = LocationUtils.getDistance(Convert.toDouble(storeInfo.getLatitude()), Convert.toDouble(storeInfo.getLongitude()), Convert.toDouble(latitude), Convert.toDouble(longitude));
                        if (distance < temp) {
                            temp = distance;
                            dict.set(Convert.toStr(temp), storeInfo);
                        }
                    }
                }
 
                if (NumberUtil.compare(temp, Double.MAX_VALUE) != 0) {
                    StoreInfo storeInfo = (StoreInfo) dict.get(Convert.toStr(temp));
                    double v = temp / 1000;
                    String s = NumberUtil.roundStr(Convert.toStr(v), 3, RoundingMode.DOWN);
                    storeInfo.setDistance(s);
 
                    //价格升高的百分比
                    ConfigInfo configInfo = new ConfigInfo();
                    configInfo.setConfigTypeCode("STORE_PRICE");
                    ConfigInfo configInfo1 = configMapper.selectOne(configInfo);
                    String configValue = configInfo1.getConfigValue();
                    storeInfo.setScale(configValue);
                    return Result.success(storeInfo);
                }
            }
        }
 
        return Result.success(null);
    }
 
 
 
    @PostMapping("/admin/front/storeInfo/queryStoreList.json")
    @ApiOperation(value="订单管理-查询所有门店", notes="test: 仅0有正确返回")
    public JsonResult<List<Map<String,Object>>> queryStoreList(){
        List<Map<String,Object>> list = storeInfoMapper.queryStoreList();
        return JsonResult.success(list);
    }
 
 
    /**
     * 用户是否已下单
     *
     * @param id 门店id
     * @return
     */
    private boolean isOrder(String id) {
        //判断是否已经有用户下单在此门店 否则不允许删除
        Example example = new Example(OrderInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("orderStatus", Arrays.asList(OrderEnum.待接单.getValue(), OrderEnum.服务中.getValue(), OrderEnum.待确认.getValue()));
        criteria.andEqualTo("storeId", id);
        int i = orderMapper.selectCountByExample(example);
        if (i > 0) return true;
        return false;
    }
 
}