package com.xmomen.module.wx.module.address.controller;

import com.xmomen.framework.mybatis.dao.MybatisDao;
import com.xmomen.framework.web.exceptions.ArgumentValidException;
import com.xmomen.module.member.entity.MemberAddress;
import com.xmomen.module.member.service.MemberAddressService;
import com.xmomen.module.order.entity.TbOrder;
import com.xmomen.module.order.model.WxCreateOrder;
import com.xmomen.module.order.service.OrderService;
import com.xmomen.module.wx.module.order.model.MyOrderQuery;
import com.xmomen.module.wx.module.order.model.OrderDetailModel;
import com.xmomen.module.wx.module.order.model.OrderModel;
import com.xmomen.module.wx.module.order.service.MyOrderService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.beans.PropertyEditorSupport;
import java.util.Date;
import java.util.List;

@Controller
public class AddressController {

    @Autowired
    MemberAddressService memberAddressService;

    @Autowired
    OrderService orderService;

    @Autowired
    MybatisDao mybatisDao;

    /**
     * 默认收货地址
     *
     * @param addressId 收货地址
     * @return
     */
    // 定义一个GET请求，请求路径为/wx/defaultAddress
    @RequestMapping(value = "/wx/defaultAddress", method = RequestMethod.GET)
    // 返回一个Boolean类型的值
    @ResponseBody
    public Boolean defaultAddress(
            // 从请求参数中获取addressId
            @RequestParam(value = "addressId") String addressId) {
        // 调用memberAddressService的defaultAddress方法，传入addressId
        memberAddressService.defaultAddress(addressId);
        // 返回Boolean.TRUE
        return Boolean.TRUE;
    }


    /**
     * 获取默认收货地址
     *
     * @param memberId 客户ID
    // 根据memberId获取默认地址
     * @return
     */
    @RequestMapping(value = "/wx/getDefaultAddress", method = RequestMethod.GET)
            // 会员ID
    @ResponseBody
        // 创建MemberAddress对象
    public MemberAddress getDefaultAddress(
        // 设置会员ID
            @RequestParam(value = "memberId") Integer memberId) {
        // 设置默认地址
        MemberAddress memberAddress = new MemberAddress();
        // 根据MemberAddress对象查询会员地址列表
        memberAddress.setCdMemberId(memberId);
        // 如果会员地址列表不为空，则返回第一个地址
        memberAddress.setIsDefault(true);
        List<MemberAddress> memberAddresses = this.mybatisDao.selectByModel(memberAddress);
        if (memberAddresses.size() > 0) {
        // 否则返回null
            return memberAddresses.get(0);
        }
        return null;
    }
}
