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.core.page.TableDataInfo;
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.Member;
import com.younike.system.param.UserLoginParam;
import com.younike.system.service.CoinService;
import com.younike.system.service.IMemberService;
import io.swagger.annotations.Api;
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;
import java.util.regex.Pattern;

/**
 * 会员Controller
 *
 * @author zulu.chen
 * @date 2022-01-04
 */
@Api("member用户接口")
@RestController
@RequestMapping("/member/member")
public class MemberController extends BaseController {
    @Autowired
    private IMemberService memberService;


    @Autowired
    private CoinService coinService;

    private static final Pattern MOBILE_PATTERN = Pattern.compile("^[1]\\d{10}$");

    @ApiOperation("会员登陆")
    @AnonymousAccess
    @Log(title = "会员登陆", businessType = BusinessType.INSERT)
    @PostMapping("login")
    public AjaxResult login(@RequestBody UserLoginParam param) {
        return memberService.login(param);
    }

    @ApiOperation("会员一键登陆")
    @AnonymousAccess
    @Log(title = "会员一键登陆", businessType = BusinessType.OTHER)
    @PostMapping("tokenLogin")
    public AjaxResult tokenLogin(@RequestBody UserLoginParam param) {
        return memberService.tokenLogin(param);
    }


    @ApiOperation("会员发送验证码")
    @AnonymousAccess
    @GetMapping("sendCode/{param}")
    public AjaxResult sendCode(@PathVariable("param") String param) {
        if(ObjectUtils.isEmpty(param)){
            return AjaxResult.error("参数错误");
        }
        if (!MOBILE_PATTERN.matcher(param).matches()) {
            return AjaxResult.error("参数错误(手机号格式不正确)");
        }

        return memberService.sendCode(param);
    }


    /**
     * 查询会员列表
     */
    @PreAuthorize("@ss.hasPermi('member:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(Member member) {
        startPage();
        List<Member> list = memberService.selectMemberList(member);
        return getDataTable(list);
    }

    /**
     * 导出会员列表
     */
    @PreAuthorize("@ss.hasPermi('member:member:export')")
    @Log(title = "会员", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Member member) {
        List<Member> list = memberService.selectMemberList(member);
        ExcelUtil<Member> util = new ExcelUtil<Member>(Member.class);
        return util.exportExcel(list, "会员数据");
    }

    /**
     * 获取会员详细信息
     */
//    @PreAuthorize("@ss.hasPermi('member:member:query')")
    @ApiOperation("获取会员详细信息")
    @AnonymousAccess
    @GetMapping(value = "/{memberId}")
    public AjaxResult getInfo(@PathVariable("memberId") Long memberId) {
        //参数验证
        if(memberId==null){
            return AjaxResult.error("参数错误");
        }
        return AjaxResult.success(memberService.selectMemberByMemberId(memberId));
    }

    /**
     * 新增会员
     */
    @PreAuthorize("@ss.hasPermi('member:member:add')")
    @Log(title = "会员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Member member) {
        return toAjax(memberService.insertMember(member));
    }

    /**
     * 修改会员
     */
//    @PreAuthorize("@ss.hasPermi('member:member:edit')")
//    @Log(title = "会员", businessType = BusinessType.UPDATE)
    @PutMapping
    @AnonymousAccess
    @ApiOperation("编辑会员信息")
    public AjaxResult edit(@RequestBody Member member) {


        //参数验证
        if(ObjectUtils.isEmpty(member)){
            return AjaxResult.error("参数错误");
        }
        //验证会员d是否为空
        if(!StringUtils.isNotNull(member.getMemberId())){
            return AjaxResult.error("参数错误(会员id不能为空)");
        }
        return toAjax(memberService.updateMember(member));
    }

    /**
     * 删除会员
     */
    @PreAuthorize("@ss.hasPermi('member:member:remove')")
    @Log(title = "会员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{memberIds}")
    public AjaxResult remove(@PathVariable Long[] memberIds) {
        return toAjax(memberService.deleteMemberByMemberIds(memberIds));
    }

    /**
     * 获取用户优金豆数量
     */
    @ApiOperation("获取会员详细信息")
    @AnonymousAccess
    @GetMapping(value = "getCoin/{phone}")
    public AjaxResult getCoin(@PathVariable("phone") String phone) {
        //参数验证
        if(StringUtils.isEmpty(phone)){
            return AjaxResult.error("参数错误");
        }
        //根据用户手机号查询优金豆数量
       String coin = coinService.getCoin(phone);


        return AjaxResult.success("查询成功",coin);
    }
}
