package org.xmx0632.deliciousfruit.api.v1; import java.util.List; import javax.servlet.ServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.xmx0632.deliciousfruit.api.v1.bo.AllReceiversResponse; import org.xmx0632.deliciousfruit.api.v1.bo.AllReceiversResponse.ReceiverBo; import org.xmx0632.deliciousfruit.api.v1.bo.Result; import org.xmx0632.deliciousfruit.api.v1.bo.UpdateReceiverRequest; import org.xmx0632.deliciousfruit.api.v1.bo.UpdateReceiverResponse; import org.xmx0632.deliciousfruit.api.v1.helper.WebHelper; import org.xmx0632.deliciousfruit.entity.UserAccount; import org.xmx0632.deliciousfruit.service.ConfigService; import org.xmx0632.deliciousfruit.service.ReceiverInfoService; /** * 用户收货地址管理 * * @author Jefferson * */ @Controller @RequestMapping(value = "/api/v1/address") public class AddressApiController { private static Logger log = LoggerFactory .getLogger(AddressApiController.class); @Autowired private ConfigService configService; @Autowired private ReceiverInfoService receiverInfoService; @RequestMapping(value = "/queryaddress", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity<AllReceiversResponse> queryaddress( ServletRequest request) { UserAccount userAccount = WebHelper.getCurrentUser(request); log.debug("UserAccount:{}", userAccount); AllReceiversResponse response = new AllReceiversResponse(); List<org.xmx0632.deliciousfruit.entity.ReceiverInfo> receivers = receiverInfoService .getAllByUserAccount(userAccount); if (receivers != null) { for (org.xmx0632.deliciousfruit.entity.ReceiverInfo address : receivers) { ReceiverBo receiverInfo = new ReceiverBo(); receiverInfo.setId(address.getId()); receiverInfo.setAddress(address.getAddress()); receiverInfo.setCity(address.getCity()); receiverInfo.setDistrict(address.getDistrict()); receiverInfo.setName(address.getReceiver()); receiverInfo.setIsDefault(address.getIsDefault()); receiverInfo .setPhoneNumber(address.getMobile() != null ? address .getMobile() : (address.getTelephone() != null ? address .getTelephone() : "")); receiverInfo.setProvince(address.getProvince()); response.getReceivers().add(receiverInfo); } } response.setResult(Result.SUCCESS_RESULT); log.debug("response:{}", response); return new ResponseEntity<AllReceiversResponse>(response, HttpStatus.OK); } @RequestMapping(value = "/addaddress", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity<UpdateReceiverResponse> addaddress( @RequestBody UpdateReceiverRequest updateReceiverRequest, ServletRequest request) { UserAccount userAccount = WebHelper.getCurrentUser(request); log.debug("UserAccount:{}", userAccount); log.debug("Request:{}", updateReceiverRequest); Result result = Result.SUCCESS_RESULT; if (updateReceiverRequest.getReceivers() != null) { for (ReceiverBo receiverBo : updateReceiverRequest.getReceivers()) { if (receiverBo.getId() != -1) { result = updateReceiver(receiverBo, userAccount); } else { result = addReceiver(receiverBo, userAccount); } if (!result.getValue().equals(Result.SUCCESS_RESULT.getValue())) { break; } } } UpdateReceiverResponse response = new UpdateReceiverResponse(); response.setResult(result); log.debug("response:{}", response); return new ResponseEntity<UpdateReceiverResponse>(response, HttpStatus.OK); } private Result updateReceiver(ReceiverBo receiverBo, UserAccount userAccount) { Long receriverId = receiverBo.getId(); org.xmx0632.deliciousfruit.entity.ReceiverInfo receiverInfo = receiverInfoService .getReceiverInfo(receriverId); if (receiverInfo == null) { return new Result(Result.FAIL, Result.MSG_ERR_NOT_EXIST + ":" + receriverId); } if (receiverInfo.getUserAccount().getId() != userAccount.getId()) { return new Result(Result.FAIL, Result.MSG_ERR_NO_AUTH + ":" + receriverId); } if (receiverBo.getIsDefault() == org.xmx0632.deliciousfruit.entity.ReceiverInfo.DEFAULT_TRUE) { resetAllToNotDefault(userAccount); } receiverInfo.setAddress(receiverBo.getAddress()); receiverInfo.setCity(receiverBo.getCity()); receiverInfo.setDistrict(receiverBo.getDistrict()); receiverInfo.setProvince(receiverBo.getProvince()); receiverInfo.setReceiver(receiverBo.getName()); receiverInfo.setTelephone(receiverBo.getPhoneNumber()); receiverInfo.setIsDefault(receiverBo.getIsDefault()); receiverInfoService.saveReceiverInfo(receiverInfo); return Result.SUCCESS_RESULT; } private Result addReceiver(ReceiverBo receiverBo, UserAccount userAccount) { if (receiverBo.getIsDefault() == org.xmx0632.deliciousfruit.entity.ReceiverInfo.DEFAULT_TRUE) { resetAllToNotDefault(userAccount); } org.xmx0632.deliciousfruit.entity.ReceiverInfo receiverInfo = new org.xmx0632.deliciousfruit.entity.ReceiverInfo(); receiverInfo.setAddress(receiverBo.getAddress()); receiverInfo.setCity(receiverBo.getCity()); receiverInfo.setDistrict(receiverBo.getDistrict()); receiverInfo.setProvince(receiverBo.getProvince()); receiverInfo.setReceiver(receiverBo.getName()); receiverInfo.setTelephone(receiverBo.getPhoneNumber()); receiverInfo.setUserAccount(userAccount); receiverInfo.setIsDefault(receiverBo.getIsDefault()); receiverInfoService.saveReceiverInfo(receiverInfo); return Result.SUCCESS_RESULT; } private void resetAllToNotDefault(UserAccount userAccount) { List<org.xmx0632.deliciousfruit.entity.ReceiverInfo> receivers = receiverInfoService .getAllByUserAccount(userAccount); if (receivers != null) { for (org.xmx0632.deliciousfruit.entity.ReceiverInfo address : receivers) { address.setIsDefault(org.xmx0632.deliciousfruit.entity.ReceiverInfo.DEFAULT_FALSE); receiverInfoService.saveReceiverInfo(address); } } } }