package com.pangu.member.controller;

import com.pangu.common.core.controller.BaseController;
import com.pangu.common.core.domain.AjaxResult;
import com.pangu.common.core.page.TableDataInfo;
import com.pangu.member.domain.dto.MemberDTO;
import com.pangu.member.domain.vo.MemberVO;
import com.pangu.member.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 会员管理Controller
 * @author pangu
 */
@RestController
@RequestMapping("/api/member")
public class MemberController extends BaseController {

    @Autowired
    private IMemberService memberService;

    /**
     * 查询会员列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MemberDTO memberDTO) {
        return memberService.selectMemberList(memberDTO);
    }

    /**
     * 获取会员详情
     */
    @GetMapping("/{memberId}")
    public AjaxResult getInfo(@PathVariable Long memberId) {
        return success(memberService.getMemberById(memberId));
    }

    /**
     * 新增会员
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody MemberDTO memberDTO) {
        return toAjax(memberService.insertMember(memberDTO));
    }

    /**
     * 修改会员
     */
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody MemberDTO memberDTO) {
        return toAjax(memberService.updateMember(memberDTO));
    }

    /**
     * 删除会员
     */
    @DeleteMapping("/{memberId}")
    public AjaxResult remove(@PathVariable Long memberId) {
        return toAjax(memberService.deleteMember(memberId));
    }

    /**
     * 重置密码
     */
    @PutMapping("/resetPwd/{memberId}")
    public AjaxResult resetPwd(@PathVariable Long memberId) {
        String newPassword = memberService.resetPassword(memberId);
        return AjaxResult.success("密码重置成功").put("password", newPassword);
    }

    /**
     * 修改状态
     */
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody MemberDTO memberDTO) {
        return toAjax(memberService.changeStatus(memberDTO.getMemberId(), memberDTO.getStatus()));
    }

    /**
     * 绑定学生
     */
    @PostMapping("/bindStudent")
    public AjaxResult bindStudent(@RequestBody MemberDTO memberDTO) {
        return toAjax(memberService.bindStudent(memberDTO.getMemberId(), memberDTO.getStudentId()));
    }

    /**
     * 解绑学生
     */
    @DeleteMapping("/unbindStudent/{memberId}/{studentId}")
    public AjaxResult unbindStudent(@PathVariable Long memberId, @PathVariable Long studentId) {
        return toAjax(memberService.unbindStudent(memberId, studentId));
    }

    /**
     * 检查手机号是否唯一
     */
    @GetMapping("/checkPhone")
    public AjaxResult checkPhoneUnique(@RequestParam String phone, 
                                       @RequestParam(required = false) Long memberId) {
        boolean unique = memberService.checkPhoneUnique(phone, memberId);
        return AjaxResult.success().put("unique", unique);
    }
}
