package com.xmomen.module.member.controller;

import com.xmomen.framework.exception.BusinessException;
import com.xmomen.framework.mybatis.page.Page;
import com.xmomen.module.base.constant.AppConstants;
import com.xmomen.module.logger.Log;
import com.xmomen.module.member.model.MemberAddressQuery;
import com.xmomen.module.member.model.MemberAddressModel;
import com.xmomen.module.member.service.MemberAddressService;

import org.apache.commons.io.IOUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.result.ExcelImportResult;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.jeecgframework.poi.exception.excel.ExcelImportException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.io.Serializable;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;


@RestController
@RequestMapping(value = "/memberAddress")
public class MemberAddressController {

    // 自动注入MemberAddressService
    @Autowired
    MemberAddressService memberAddressService;

    // 查询客户地址列表
    @RequestMapping(method = RequestMethod.GET)
    @Log(actionName = "查询客户地址列表")
    public Page<MemberAddressModel> getMemberAddressList(@RequestParam(value = "limit") Integer limit,
                                                         @RequestParam(value = "offset") Integer offset,
                                                         @RequestParam(value = "id", required = false) String id,
                                                         @RequestParam(value = "ids", required = false) String[] ids,
                                                         @RequestParam(value = "cdMemberId", required = false) String cdMemberId,
                                                         @RequestParam(value = "excludeIds", required = false) String[] excludeIds) {
        // 创建MemberAddressQuery对象
        MemberAddressQuery memberAddressQuery = new MemberAddressQuery();
        // 设置查询条件
        memberAddressQuery.setId(id);
        memberAddressQuery.setExcludeIds(excludeIds);
        memberAddressQuery.setIds(ids);
        memberAddressQuery.setCdMemberId(cdMemberId);
        // 调用MemberAddressService的getMemberAddressModelPage方法查询客户地址列表
        return memberAddressService.getMemberAddressModelPage(limit, offset, memberAddressQuery);
    }

    // 查询客户地址
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @Log(actionName = "查询客户地址")
    public MemberAddressModel getMemberAddressById(@PathVariable(value = "id") String id) {
        // 调用MemberAddressService的getOneMemberAddressModel方法查询客户地址
        return memberAddressService.getOneMemberAddressModel(id);
    }

    // 新增客户地址
    @RequestMapping(method = RequestMethod.POST)
    @Log(actionName = "新增客户地址")
    public MemberAddressModel createMemberAddress(@RequestBody @Valid MemberAddressModel memberAddressModel) {
        // 获取当前用户ID
        Integer userId = (Integer) SecurityUtils.getSubject().getSession().getAttribute(AppConstants.SESSION_USER_ID_KEY);
        // 设置客户ID
        memberAddressModel.setCdMemberId(userId);
        // 调用MemberAddressService的createMemberAddress方法新增客户地址
        return memberAddressService.createMemberAddress(memberAddressModel);
    }

    // 更新客户地址
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @Log(actionName = "更新客户地址")
    public void updateMemberAddress(@PathVariable(value = "id") String id,
                                    @RequestBody @Valid MemberAddressModel memberAddressModel) {
        // 调用MemberAddressService的updateMemberAddress方法更新客户地址
        memberAddressService.updateMemberAddress(memberAddressModel);
    }

    // 删除单个客户地址
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @Log(actionName = "删除单个客户地址")
    public void deleteMemberAddress(@PathVariable(value = "id") String id) {
        // 调用MemberAddressService的deleteMemberAddress方法删除客户地址
        memberAddressService.deleteMemberAddress(id);
    }

    // 批量删除客户地址
    @RequestMapping(method = RequestMethod.DELETE)
    @Log(actionName = "批量删除客户地址")
    public void deleteMemberAddresss(@RequestParam(value = "ids") String[] ids) {
        // 调用MemberAddressService的deleteMemberAddress方法批量删除客户地址
        memberAddressService.deleteMemberAddress(ids);
    }


}
