package com.younike.member;

import com.younike.common.annotation.Log;
import com.younike.common.core.controller.BaseController;
import com.younike.common.core.domain.AjaxResult;
import com.younike.common.enums.BusinessType;
import com.younike.common.utils.StringUtils;
import com.younike.common.utils.poi.ExcelUtil;
import com.younike.framework.config.AnonymousAccess;
import com.younike.system.domain.MemberAddress;
import com.younike.system.param.MemberAddressParam;
import com.younike.system.service.IMemberAddressService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员服务地址Controller
 *
 * @author elev
 * @date 2022-01-04
 */
@RestController
@RequestMapping("/member/address")
public class MemberAddressController extends BaseController {
    @Autowired
    private IMemberAddressService memberAddressService;



    /**
     * 查询会员服务地址列表
     */
//    @PreAuthorize("@ss.hasPermi('system:address:list')")
    @GetMapping("/list")
    @AnonymousAccess
    @ApiOperation("获取会员服务地址列表")
    public AjaxResult list(MemberAddressParam param) {
//        startPage();
        if(ObjectUtils.isEmpty(param)){
            return AjaxResult.error("参数错误");
        }
        //验证会员d是否为空
        if(!StringUtils.isNotNull(param.getMemberId())){
            return AjaxResult.error("参数错误(会员id不能为空)");
        }
       return memberAddressService.getAddressList(param);
    }


    /**
     * 导出会员服务地址列表
     */
    @PreAuthorize("@ss.hasPermi('system:address:export')")
    @Log(title = "会员服务地址", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MemberAddress memberAddress) {
        List<MemberAddress> list = memberAddressService.selectMemberAddressList(memberAddress);
        ExcelUtil<MemberAddress> util = new ExcelUtil<MemberAddress>(MemberAddress.class);
        return util.exportExcel(list, "会员服务地址数据");
    }

    /**
     * 获取会员服务地址详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:address:query')")
    @ApiOperation("获取会员服务地址详细信息")
    @GetMapping(value = "/{addressId}")
    @AnonymousAccess
    public AjaxResult getInfo(@PathVariable("addressId") Long addressId) {
        if(ObjectUtils.isEmpty(addressId)){
            return AjaxResult.error("参数错误");
        }
        return AjaxResult.success(memberAddressService.selectMemberAddressByAddressId(addressId));
    }

    /**
     * 新增会员服务地址
     */
//    @PreAuthorize("@ss.hasPermi('system:address:add')")
//    @Log(title = "会员服务地址", businessType = BusinessType.INSERT)
    @PostMapping
    @AnonymousAccess
    @ApiOperation("新增会员服务地址")
    public AjaxResult add(@RequestBody MemberAddress memberAddress) {
        //参数验证
        if(ObjectUtils.isEmpty(memberAddress)){
            return AjaxResult.error("参数错误");
        }
        //验证会员d是否为空
        if(!StringUtils.isNotNull(memberAddress.getMemberId())){
            return AjaxResult.error("参数错误(会员id不能为空)");
        }
        //手地址详情
        if(!StringUtils.isNotNull(memberAddress.getAddress())){
            return AjaxResult.error("参数错误(地址详情不能为空)");
        }
        //门牌号
        if(!StringUtils.isNotNull(memberAddress.getDetailed())){
            return AjaxResult.error("参数错误(门牌号不能为空)");
        }
        //经纬度
        if(!StringUtils.isNotNull(memberAddress.getLongitude())||!StringUtils.isNotNull(memberAddress.getLatitude())){
            return AjaxResult.error("参数错误(经纬度不能为空)");
        }
        //联系人姓名
        if(!StringUtils.isNotNull(memberAddress.getName())){
            return AjaxResult.error("参数错误(联系人姓名不能为空)");
        }
        //手机号
        if(!StringUtils.isNotNull(memberAddress.getPhone())){
            return AjaxResult.error("参数错误(手机号不能为空)");
        }
        return toAjax(memberAddressService.insertMemberAddress(memberAddress));
    }

    /**
     * 修改会员服务地址
     */
//    @PreAuthorize("@ss.hasPermi('system:address:edit')")
//    @Log(title = "会员服务地址", businessType = BusinessType.UPDATE)
    @PutMapping
    @AnonymousAccess
    @ApiOperation("编辑会员服务地址列表")
    public AjaxResult edit(@RequestBody MemberAddress memberAddress) {
        //参数验证
        if(ObjectUtils.isEmpty(memberAddress)){
            return AjaxResult.error("参数错误");
        }
        //验证会员d是否为空
        if(!StringUtils.isNotNull(memberAddress.getAddressId())){
            return AjaxResult.error("参数错误(地址id不能为空)");
        }
        //验证会员d是否为空
//        if(!StringUtils.isNotNull(memberAddress.getMemberId())){
//            return AjaxResult.error("参数错误(会员id不能为空)");
//        }
        //手地址详情
        if(!StringUtils.isNotNull(memberAddress.getAddress())){
            return AjaxResult.error("参数错误(地址详情不能为空)");
        }
        //门牌号
        if(!StringUtils.isNotNull(memberAddress.getDetailed())){
            return AjaxResult.error("参数错误(门牌号不能为空)");
        }
        //经纬度
        if(!StringUtils.isNotNull(memberAddress.getLongitude())||!StringUtils.isNotNull(memberAddress.getLatitude())){
            return AjaxResult.error("参数错误(经纬度不能为空)");
        }
        //联系人姓名
        if(!StringUtils.isNotNull(memberAddress.getName())){
            return AjaxResult.error("参数错误(联系人姓名不能为空)");
        }
        //手机号
        if(!StringUtils.isNotNull(memberAddress.getPhone())){
            return AjaxResult.error("参数错误(手机号不能为空)");
        }

        return toAjax(memberAddressService.updateMemberAddress(memberAddress));
    }

    /**
     * 删除会员服务地址
     */
    @DeleteMapping("/{addressId}")
    @AnonymousAccess
    @ApiOperation("删除会员服务地址")
    public AjaxResult remove(@PathVariable("addressId") Long addressId) {
        if(!StringUtils.isNotNull(addressId)){
            return AjaxResult.error("参数错误");
        }
        return toAjax(memberAddressService.deleteMemberAddressByAddressId(addressId));
    }
}
