package com.ghy.web.controller.customer;

import cn.hutool.core.util.ObjectUtil;
import com.ghy.common.core.controller.BaseController;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.common.utils.ExceptionUtil;
import com.ghy.customer.domain.CustomerAddress;
import com.ghy.customer.service.CustomerAddressService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/customer/address")
public class CustomerAddressController extends BaseController {

    @Resource
    private CustomerAddressService customerAddressService;

    /**
     * @param customerAddress 筛选符合条件的地址list
     * @return 地址list
     */
    @PostMapping("/list")
    @ResponseBody
    AjaxResult getCustomerAddressList(@RequestBody CustomerAddress customerAddress){
        try {
            return AjaxResult.success(customerAddressService.getCustomerAddressList(customerAddress));
        }catch (Exception e){
            return AjaxResult.error(ExceptionUtil.getExceptionMessage(e));
        }
    }

    /**
     * @param ids 地址id
     * @return 删除成功条数
     */
    @PostMapping("/deleteByIds")
    @ResponseBody
    AjaxResult deleteByIds(String [] ids){
        try {
            customerAddressService.deleteByIds(ids);
            return AjaxResult.success("操作成功");
        }catch (Exception e){
            return AjaxResult.error(ExceptionUtil.getExceptionMessage(e));
        }
    }

    /**
     * @param customerAddressId 地址id
     * @return 删除成功条数
     */
    @PostMapping("/delete")
    @ResponseBody
    AjaxResult deleteByCustomerAddressId(Long customerAddressId){
        try {
            customerAddressService.deleteByCustomerAddressId(customerAddressId);
            return AjaxResult.success("操作成功");
        }catch (Exception e){
            return AjaxResult.error(ExceptionUtil.getExceptionMessage(e));
        }
    }

    /**
     * @param customerId 消费者id
     * @return 消费者所有的地址list
     */
    @PostMapping("/getByCustomerId")
    @ResponseBody
    AjaxResult selectByCustomerId(Long customerId){
        try {
            return AjaxResult.success(customerAddressService.selectByCustomerId(customerId));
        }catch (Exception e){
            return AjaxResult.error(ExceptionUtil.getExceptionMessage(e));
        }
    }

    /**
     * @param customerAddressId 地址id
     * @return 地址
     */
    @PostMapping("/getByCustomerAddressId")
    @ResponseBody
    AjaxResult selectByCustomerAddressId(Long customerAddressId){
        try {
            return AjaxResult.success(customerAddressService.selectByCustomerAddressId(customerAddressId));
        }catch (Exception e){
            return AjaxResult.error(ExceptionUtil.getExceptionMessage(e));
        }
    }

    /**
     * @param customerAddress 地址信息
     * @return 新增成功条数
     */
    @PostMapping("/insert")
    @ResponseBody
    AjaxResult insertCustomerAddress(@RequestBody CustomerAddress customerAddress){
        try {
            // 判断下当前是否是默认地址
            if(ObjectUtil.equals(customerAddress.getIsDefault(), 1)){
                CustomerAddress param = new CustomerAddress();
                param.setNeedNameFlag(false);
                param.setCustomerId(customerAddress.getCustomerId());
                param.setIsDefault(1);
                List<CustomerAddress> sourceDefaultAddress = customerAddressService.getCustomerAddressList(param);
                for (CustomerAddress defaultAddress : sourceDefaultAddress) {
                    CustomerAddress defaultParam = new CustomerAddress();
                    defaultParam.setCustomerAddressId(defaultAddress.getCustomerAddressId());
                    defaultParam.setIsDefault(0);
                    customerAddressService.updateCustomerAddress(defaultParam);
                }
            }
            customerAddressService.insertCustomerAddress(customerAddress);
            return AjaxResult.success("操作成功");
        }catch (Exception e){
            return AjaxResult.error(ExceptionUtil.getExceptionMessage(e));
        }
    }

    /**
     * @param customerAddress 修改地址信息
     * @return 修改成功条数
     */
    @PostMapping("/update")
    @ResponseBody
    AjaxResult updateCustomerAddress(@RequestBody CustomerAddress customerAddress){
        logger.info("地址库的订单修改方法{}",customerAddress);
        try {
            // 判断下当前是否是默认地址
            if(ObjectUtil.equals(customerAddress.getIsDefault(), 1)){
                CustomerAddress param = new CustomerAddress();
                param.setNeedNameFlag(false);
                param.setCustomerId(customerAddress.getCustomerId());
                param.setIsDefault(1);
                List<CustomerAddress> sourceDefaultAddress = customerAddressService.getCustomerAddressList(param);
                for (CustomerAddress defaultAddress : sourceDefaultAddress) {
                    if(!Objects.equals(defaultAddress.getCustomerAddressId(), customerAddress.getCustomerAddressId())){
                        CustomerAddress defaultParam = new CustomerAddress();
                        defaultParam.setCustomerAddressId(defaultAddress.getCustomerAddressId());
                        defaultParam.setIsDefault(0);
                        customerAddressService.updateCustomerAddress(defaultParam);
                    }
                }
            }
            customerAddressService.updateCustomerAddress(customerAddress);
            return AjaxResult.success("操作成功");
        }catch (Exception e){
            return AjaxResult.error(ExceptionUtil.getExceptionMessage(e));
        }
    }


}
