jyy
2021-03-19 382794b20c47d0f74317b2ac5bb7b6849986166f
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
package com.matrix.system.shopXcx.api.action;
 
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.biz.bean.BizUser;
import com.matrix.component.redis.RedisUserLoginUtils;
import com.matrix.system.common.constance.AppConstance;
 
import com.matrix.system.shopXcx.bean.ShopReceiveAddress;
import com.matrix.system.shopXcx.dao.ShopReceiveAddressDao;
import com.matrix.system.shopXcx.api.pojo.AddressUntilsPOJO;
import com.matrix.system.shopXcx.api.tools.AddressUntils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
 
import java.util.Collections;
import java.util.List;
 
/**
 * @description 收货地址接口
 * @author jiangyouyao
 * @date 2019-06-06 10:15
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
@RequestMapping(value = "wxapi/shopAddressAction")
public class WxReceiveAddressAction {
    @Autowired
    private ShopReceiveAddressDao shopReceiveAddressDao;
 
    @Autowired
    private RedisUserLoginUtils redisUserLoginUtils;
    /**
     * 接收保存收货地址
     */
    @PostMapping(value = "/saveReceiveAddress")
    public @ResponseBody
    AjaxResult saveReceiveAddress(@RequestBody ShopReceiveAddress receiveAddress) {
        BizUser loginUser = redisUserLoginUtils.getLoginUser(BizUser.class);
        receiveAddress.setCreateBy(loginUser.getOpenId());
        receiveAddress.setUpdateBy(loginUser.getOpenId());
        receiveAddress.setAddrUserid(loginUser.getOpenId());
 
        //通过经纬度查询省市区
        String addrLongitude = receiveAddress.getAddrLongitude();
        String addrLatitude = receiveAddress.getAddrLatitude();
        if(null != addrLatitude && !"".equals(addrLatitude) && null != addrLongitude && !"".equals(addrLongitude)){
            try{
                AddressUntilsPOJO address = AddressUntils.getAdd(addrLongitude, addrLatitude);
                receiveAddress.setAddrProvince(address.getProvince());
                receiveAddress.setAddrCity(address.getCity());
                receiveAddress.setAddrArea(address.getDistrict());
            }catch (Exception e){
                throw new GlobleException("通过经纬度查询省市区错误", e);
            }
 
        }
 
        //查看该用户下是否有地址
        ShopReceiveAddress param = new ShopReceiveAddress();
        param.setAddrUserid(loginUser.getOpenId());
        List<ShopReceiveAddress> receiveList = shopReceiveAddressDao.selectByModel(param);
        if(null == receiveList || receiveList.size() ==0){
            receiveAddress.setAddrDefault(1);
        }
 
        //如果默认地址传来1就将前面地址的默认状态改为2
        if(receiveAddress.getAddrDefault()==1){
            ShopReceiveAddress params = new ShopReceiveAddress();
            params.setAddrDefault(receiveAddress.getAddrDefault());
            params.setAddrUserid(loginUser.getOpenId());
            List<ShopReceiveAddress> shopReceiveList = shopReceiveAddressDao.selectByModel(params);
            if(null != shopReceiveList && shopReceiveList.size()>0){
                for (ShopReceiveAddress receiveAddre :shopReceiveList){
                    receiveAddre.setAddrDefault(2);
                    shopReceiveAddressDao.updateByModel(receiveAddre);
                }
            }
        }
        int i = shopReceiveAddressDao.insert(receiveAddress);
 
        if (i == 0) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "更新失败");
        }
        return new AjaxResult(AjaxResult.STATUS_SUCCESS, "更新成功");
    }
 
    /**
     * 根据用户ID查询收货地址
     * @param
     * @return
     */
    @PostMapping("/findReceiveAddress")
    @ResponseBody
    public AjaxResult getByAddrUserId(@RequestBody ShopReceiveAddress receiveAddress) {
        BizUser loginUser = redisUserLoginUtils.getLoginUser(BizUser.class);
        String userId = loginUser.getOpenId();
        receiveAddress.setAddrUserid(userId);
        List<ShopReceiveAddress> list = shopReceiveAddressDao.selectByModel(receiveAddress);
        AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, list, list.size());
        return result;
    }
 
    /**
     * 根据用户ID和地址ID查询收货地址
     * @param
     * @return
     */
    @PostMapping("/findAddrByAddrId/{addrId}")
    @ResponseBody
    public AjaxResult getByAddrId(@PathVariable("addrId") Integer addrId) {
        BizUser loginUser = redisUserLoginUtils.getLoginUser(BizUser.class);
        String userId = loginUser.getOpenId();
        ShopReceiveAddress params = new ShopReceiveAddress();
        params.setAddrUserid(userId);
        params.setAddrId(addrId);
        List<ShopReceiveAddress> list = shopReceiveAddressDao.selectByModel(params);
        AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, list, list.size());
        return result;
    }
 
    /**
     * 根据ID删除收货地址
     * @param
     * @return
     */
    @PostMapping("/deleteByAddrId/{addrId}")
    @ResponseBody
    public AjaxResult deleteByAddrId(@PathVariable("addrId") Integer addrId){
        ShopReceiveAddress shopReceiveAddress = shopReceiveAddressDao.selectById(addrId);
        if(null != shopReceiveAddress && AppConstance.ADDR_DEFAULT .equals(shopReceiveAddress.getAddrDefault())){
            return new AjaxResult(AjaxResult.STATUS_FAIL, "默认地址不能删除");
        }
        int i = shopReceiveAddressDao.deleteById(addrId);
        if (i == 0) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "删除失败");
        }
        return new AjaxResult(AjaxResult.STATUS_SUCCESS, "删除成功");
    }
 
    /**
     * 接收修改收货地址
     */
    @PostMapping(value = "/updateReceiveAddress")
    public @ResponseBody
    AjaxResult updateReceiveAddress(@RequestBody ShopReceiveAddress receiveAddress) {
        BizUser loginUser = redisUserLoginUtils.getLoginUser(BizUser.class);
        receiveAddress.setCreateBy(loginUser.getOpenId());
        receiveAddress.setUpdateBy(loginUser.getOpenId());
        receiveAddress.setAddrUserid(loginUser.getOpenId());
 
        //通过经纬度查询省市区
        String addrLongitude = receiveAddress.getAddrLongitude();
        String addrLatitude = receiveAddress.getAddrLatitude();
        if(null != addrLatitude && !"".equals(addrLatitude) && null != addrLongitude && !"".equals(addrLongitude)){
            try{
                AddressUntilsPOJO address = AddressUntils.getAdd(addrLongitude, addrLatitude);
                receiveAddress.setAddrProvince(address.getProvince());
                receiveAddress.setAddrCity(address.getCity());
                receiveAddress.setAddrArea(address.getDistrict());
            }catch (Exception e){
                throw new GlobleException("通过经纬度查询省市区错误", e);
            }
 
        }
 
        if(receiveAddress.getAddrDefault()==1){
            ShopReceiveAddress params = new ShopReceiveAddress();
            params.setAddrDefault(receiveAddress.getAddrDefault());
            params.setAddrUserid(loginUser.getOpenId());
            List<ShopReceiveAddress> shopReceiveList = shopReceiveAddressDao.selectByModel(params);
            if(null != shopReceiveList && shopReceiveList.size()>0){
                for (ShopReceiveAddress receiveAddre :shopReceiveList){
                    receiveAddre.setAddrDefault(2);
                    shopReceiveAddressDao.updateByModel(receiveAddre);
                }
            }
        }
 
        int i = shopReceiveAddressDao.updateByModel(receiveAddress);
 
        if (i == 0) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "修改失败");
        }
        return new AjaxResult(AjaxResult.STATUS_SUCCESS, "修改成功");
    }
 
    /**
     * 根据经纬度和详细地址查询出省市区并返回给前端
     * @param
     * @return
     */
    @PostMapping("/findAddrByLogAndLat")
    @ResponseBody
    public AjaxResult getAddrByLogAndLat(@RequestBody ShopReceiveAddress receiveAddress) {
        String addrLatitude = receiveAddress.getAddrLatitude();
        String addrLongitude = receiveAddress.getAddrLongitude();
        String addrRegion = receiveAddress.getAddrRegion();
        ShopReceiveAddress receAddress = new ShopReceiveAddress();
        if(null != addrLatitude && !"".equals(addrLatitude) && null != addrLongitude && !"".equals(addrLongitude)){
            try{
                AddressUntilsPOJO address = AddressUntils.getAdd(addrLongitude, addrLatitude);
                //截取出详细地址
                String replaceProvince = addrRegion.replace(address.getProvince(), "");
                String replaceCity = replaceProvince.replace(address.getCity(), "");
                String addrDetailaddr = replaceCity.replace(address.getDistrict(), "");
                String addrReplace = addrDetailaddr.replace("()", "");
                //详细地址
                receAddress.setAddrDetailaddr(addrReplace);
                //所在地区
                receAddress.setAddrRegion(address.getProvince()+address.getCity()+address.getDistrict());
            }catch (Exception e){
                throw new GlobleException("通过经纬度查询省市区错误", e);
            }
 
        }
 
        AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, Collections.singletonList(receAddress));
        return result;
    }
 
}