package com.xmomen.module.wb.controller;
	
import java.util.List;

import javax.validation.Valid;

import com.xmomen.framework.mybatis.dao.MybatisDao;
import com.xmomen.module.member.entity.MemberAddress;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xmomen.module.base.constant.AppConstants;
import com.xmomen.module.member.model.MemberAddressModel;
import com.xmomen.module.member.model.MemberAddressQuery;
import com.xmomen.module.member.service.MemberAddressService;
import com.xmomen.module.wb.model.PcMemberAddressModel;

@RestController
@RequestMapping(value = "/wb/memberAddress")
public class PcMemberAddressController extends PcBaseController {

	@Autowired
    MemberAddressService memberAddressService;

    @Autowired
    MybatisDao mybatisDao;

    /**
     * 客户地址列表
     *
     * @param id         主键
     * @param ids        主键数组
     * @param excludeIds 不包含主键数组
     * @return Page<MemberAddressModel> 客户地址领域分页对象
     */
    // 使用@RequestMapping注解，指定请求方法为GET
    @RequestMapping(method = RequestMethod.GET)
    // 定义一个方法，返回一个List类型的MemberAddressModel对象
    public List<MemberAddressModel> getMemberAddressList(@RequestParam(value = "id", required = false) String id,
                                                         @RequestParam(value = "ids", required = false) String[] ids,
                                                         @RequestParam(value = "excludeIds", required = false) String[] excludeIds) {
        // 创建一个MemberAddressQuery对象
        MemberAddressQuery memberAddressQuery = new MemberAddressQuery();
        // 设置MemberAddressQuery对象的id属性
        memberAddressQuery.setId(id);
        // 设置MemberAddressQuery对象的excludeIds属性
        memberAddressQuery.setExcludeIds(excludeIds);
        // 设置MemberAddressQuery对象的ids属性
        memberAddressQuery.setIds(ids);
        // 设置MemberAddressQuery对象的cdMemberId属性，值为当前用户的id
        memberAddressQuery.setCdMemberId(String.valueOf(getCurrentMemberId()));
        // 调用memberAddressService的getMemberAddressModels方法，传入MemberAddressQuery对象，返回一个List类型的MemberAddressModel对象
        return memberAddressService.getMemberAddressModels(memberAddressQuery);
    }

    /**
     * 查询单个客户地址
     *
     * @param id 主键
     * @return MemberAddressModel   客户地址领域对象
        // 调用memberAddressService的getOneMemberAddressModel方法，根据id获取会员地址信息
    // 根据id获取会员地址信息
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public MemberAddressModel getMemberAddressById(@PathVariable(value = "id") String id) {
        return memberAddressService.getOneMemberAddressModel(id);
    }

    /**
     * 新增客户地址
     *
     * @param memberAddressModel 新增对象参数
    // 使用POST方法创建会员地址
     * @return MemberAddressModel   客户地址领域对象
    	// 创建会员地址模型
     */
        // 将pcMemberAddressModel中的属性复制到memberAddressModel中
    @RequestMapping(method = RequestMethod.POST)
        // 设置会员ID
    public MemberAddressModel createMemberAddress(@RequestBody @Valid PcMemberAddressModel pcMemberAddressModel) {
        // 调用会员地址服务创建会员地址
    	MemberAddressModel memberAddressModel = new MemberAddressModel();
        BeanUtils.copyProperties(pcMemberAddressModel, memberAddressModel);
        memberAddressModel.setCdMemberId(getCurrentMemberId());
        return memberAddressService.createMemberAddress(memberAddressModel);
    }

    /**
     * 更新客户地址
     *
    // 根据id更新会员地址
     * @param id                 主键
     * @param memberAddressModel 更新对象参数
     */
    	// 创建MemberAddressModel对象
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
        // 将pcMemberAddressModel对象的属性值复制到memberAddressModel对象中
    public void updateMemberAddress(@PathVariable(value = "id") String id,
        // 设置memberAddressModel对象的会员id
                                    @RequestBody @Valid PcMemberAddressModel pcMemberAddressModel) {
        // 调用memberAddressService的updateMemberAddress方法更新会员地址
    	MemberAddressModel memberAddressModel = new MemberAddressModel();
        BeanUtils.copyProperties(pcMemberAddressModel, memberAddressModel);
        memberAddressModel.setCdMemberId(getCurrentMemberId());
        memberAddressService.updateMemberAddress(memberAddressModel);
    }

    /**
     * 删除客户地址
    // 使用@RequestMapping注解，指定请求的URL路径为/{id}，请求方法为DELETE
    // 定义一个deleteMemberAddress方法，接收一个String类型的id参数
     *
        // 调用memberAddressService的deleteMemberAddress方法，传入id参数
     * @param id 主键
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public void deleteMemberAddress(@PathVariable(value = "id") String id) {
        memberAddressService.deleteMemberAddress(id);
    }

    /**
     * 删除客户地址
     *
     * @param ids 主键
    // 使用@RequestMapping注解，指定请求方法为DELETE
     */
    // 定义一个方法，用于删除会员地址
    @RequestMapping(method = RequestMethod.DELETE)
        // 调用memberAddressService的deleteMemberAddress方法，传入要删除的会员地址id数组
    public void deleteMemberAddresss(@RequestParam(value = "ids") String[] ids) {
        memberAddressService.deleteMemberAddress(ids);
    }

    /**
     * 默认收货地址
     *
     * @param addressId 收货地址
     * @return
    // 使用@RequestMapping注解指定请求路径和请求方法
     */
    // 使用@ResponseBody注解将返回值直接写入响应体
    @RequestMapping(value = "/default", method = RequestMethod.PUT)
    @ResponseBody
            // 使用@RequestParam注解获取请求参数
    public Boolean defaultAddress(
        // 调用memberAddressService的defaultAddress方法
            @RequestParam(value = "addressId") String addressId) {
        // 返回Boolean.TRUE
        memberAddressService.defaultAddress(addressId);
        return Boolean.TRUE;
    }


    /**
     * 获取默认收货地址
     *
    // 使用@RequestMapping注解来映射请求的URL
     * @return
    // 使用@ResponseBody注解将返回值直接写入HTTP响应正文
     */
    @RequestMapping(value = "/default", method = RequestMethod.GET)
        // 创建MemberAddress对象
    @ResponseBody
        // 设置当前用户的ID
    public MemberAddress getDefaultAddress() {
        // 设置默认地址
        MemberAddress memberAddress = new MemberAddress();
        // 根据MemberAddress对象查询数据库
        memberAddress.setCdMemberId(getCurrentMemberId());
        // 如果查询结果不为空，则返回第一个地址
        memberAddress.setIsDefault(true);
        List<MemberAddress> memberAddresses = this.mybatisDao.selectByModel(memberAddress);
        if (memberAddresses.size() > 0) {
        // 否则返回null
            return memberAddresses.get(0);
        }
        return null;
    }
}
