package com.matrix.system.shopXcx.api.action; import com.matrix.core.exception.GlobleException; import com.matrix.core.pojo.AjaxResult; import com.matrix.system.common.constance.AppConstance; import com.matrix.system.common.init.UserCacheManager; import com.matrix.system.hive.bean.SysVipInfo; import com.matrix.system.shopXcx.api.pojo.AddressUntilsPOJO; import com.matrix.system.shopXcx.api.tools.AddressUntils; import com.matrix.system.shopXcx.bean.ShopReceiveAddress; import com.matrix.system.shopXcx.dao.ShopReceiveAddressDao; 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 UserCacheManager userCacheManager; /** * 接收保存收货地址 */ @PostMapping(value = "/saveReceiveAddress") public @ResponseBody AjaxResult saveReceiveAddress(@RequestBody ShopReceiveAddress receiveAddress) { SysVipInfo loginUser = userCacheManager.getLoginUser(); 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 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 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) { SysVipInfo loginUser = userCacheManager.getLoginUser(); String userId = loginUser.getOpenId(); receiveAddress.setAddrUserid(userId); List 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) { SysVipInfo loginUser = userCacheManager.getLoginUser(); String userId = loginUser.getOpenId(); ShopReceiveAddress params = new ShopReceiveAddress(); params.setAddrUserid(userId); params.setAddrId(addrId); List 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) { SysVipInfo loginUser = userCacheManager.getLoginUser(); 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 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; } }