package com.cweb.controller.goods;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.GoodsUserAddress;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserSessionObject;
import com.hfkj.service.goods.GoodsUserAddressService;
import com.hfkj.service.goods.impl.GoodsUserAddressServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@Controller
@RequestMapping(value = "/deliveryAddress")
@Api(value = "收货地址")
public class DeliveryAddressController {
    Logger log = LoggerFactory.getLogger(DeliveryAddressController.class);
    @Resource
    private GoodsUserAddressService deliveryAddressService;

    @Resource
    private UserCenter userCenter;

    @Resource
    private GoodsUserAddressServiceImpl goodsUserAddressService;


    @RequestMapping(value = "/getDeliveryAddressList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询收货地址列表")
    public ResponseData getDeliveryAddressList() {
        try {

            // 用户session
            UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class);

            Map<String, Object> map = new HashMap<>();

            map.put("userId", userSession.getUser().getId());

            return ResponseMsgUtil.success(deliveryAddressService.getList(map));

        } catch (Exception e) {
            log.error("GoodsDetailController --> getListUser() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }


    @RequestMapping(value = "/insertDeliveryAddress", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增收货地址")
    public ResponseData insertDeliveryAddress(@RequestBody GoodsUserAddress deliveryAddress) {
        try {

            // 用户session
            UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class);

            Map<String, Object> map = new HashMap<>();

            map.put("userId", userSession.getUser().getId());
            List<GoodsUserAddress> list = deliveryAddressService.getList(map);

            if (list.size() == 0)  {
                deliveryAddress.setWhetherDefault(true);
            }

            if (deliveryAddress == null ||
                    deliveryAddress.getAddress() == null ||
                    deliveryAddress.getWhetherDefault() == null ||
                    deliveryAddress.getConsignee() == null ||
                    deliveryAddress.getPhone() == null ||
                    deliveryAddress.getRegionName() == null ||
                    deliveryAddress.getRegionId() == null
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            deliveryAddress.setUserId(userSession.getUser().getId());
            deliveryAddress.setCreateTime(new Date());
            deliveryAddress.setUpdateTime(new Date());
            deliveryAddressService.create(deliveryAddress);

            return ResponseMsgUtil.success(deliveryAddress);

        } catch (Exception e) {
            log.error("GoodsDetailController --> insertPrice() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/updateDeliveryAddress", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改收货地址")
    public ResponseData updateDeliveryAddress(@RequestBody GoodsUserAddress deliveryAddress) {
        try {

            // 用户session
            UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class);

            if (deliveryAddress == null ||
                    deliveryAddress.getId() == null ||
                    deliveryAddress.getAddress() == null ||
                    deliveryAddress.getWhetherDefault() == null ||
                    deliveryAddress.getConsignee() == null ||
                    deliveryAddress.getPhone() == null ||
                    deliveryAddress.getRegionName() == null ||
                    deliveryAddress.getRegionId() == null
            ) {
                log.error("GoodsDetailController -> insertProduct() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            if (deliveryAddress.getWhetherDefault()) {
                deliveryAddressService.cleanDeliveryAddressDefault(userSession.getUser().getId());
            }

            GoodsUserAddress goodsDeliveryAddress = deliveryAddressService.queryDetail(deliveryAddress.getId());

            if (goodsDeliveryAddress == null) {
                log.error("GoodsDetailController -> insertProduct() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "当前收货地址异常！");
            }

            deliveryAddress.setUserId(userSession.getUser().getId());
            deliveryAddress.setCreateTime(goodsDeliveryAddress.getCreateTime());
            deliveryAddress.setUpdateTime(new Date());
            deliveryAddressService.update(deliveryAddress);

            return ResponseMsgUtil.success("修改成功");

        } catch (Exception e) {
            log.error("GoodsDetailController --> insertPrice() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/findDeliveryAddressById", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询收货地址详情")
    public ResponseData findDeliveryAddressById(
            @RequestParam(value = "id", required = true) Long id
    ) {
        try {

            GoodsUserAddress deliveryAddress = deliveryAddressService.queryDetail(id);

            if (deliveryAddress == null) {
                log.error("GoodsDetailController -> findGoodsDetailById() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到相信息");
            }

            return ResponseMsgUtil.success(deliveryAddress);

        } catch (Exception e) {
            log.error("GoodsDetailController --> findGoodsDetailById() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/deleteDeliveryAddress", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "删除删除收货地址")
    public ResponseData deleteDeliveryAddress(
            @RequestParam(value = "id", required = true) Long id) {
        try {
            // 用户session
            UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class);

            GoodsUserAddress deliveryAddress = deliveryAddressService.queryDetail(id);

            if (deliveryAddress == null) {
                log.error("GoodsDetailController -> findGoodsDetailById() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到相关信息");
            }

            if (!Objects.equals(userSession.getUser().getId(), deliveryAddress.getUserId())) {
                log.error("GoodsDetailController -> findGoodsDetailById() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "用户信息错误");
            }

            deliveryAddressService.delete(id);

            return ResponseMsgUtil.success("删除成功");

        } catch (Exception e) {
            log.error("GoodsDetailController --> findGoodsDetailById() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getUserAddress", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取默认地址")
    public ResponseData getUserAddress() {
        try {

            // 用户session
            UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class);

            Map<String , Object> map = new HashMap<>();
            map.put("userId" , userSession.getUser().getId());
            map.put("whetherDefault" , true);

            GoodsUserAddress userAddress = goodsUserAddressService.queryDetailByMap(map);

            if (userAddress == null) {
                log.error("GoodsDetailController -> findGoodsDetailById() error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到相信息");
            }

            return ResponseMsgUtil.success(userAddress);

        } catch (Exception e) {
            log.error("GoodsDetailController --> findGoodsDetailById() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
