package com.sv.api.controller;

import com.sv.api.context.PlatformContext;
import com.sv.dto.api.InformationDTO;
import com.sv.entity.Member;
import com.sv.service.api.MemberCardService;
import com.sv.service.api.MemberService;
import com.ydd.framework.core.annotation.AccessToken;
import com.ydd.framework.core.common.Pagination;
import com.ydd.framework.core.common.dto.ResponseDTO;
import com.ydd.framework.core.controller.BaseApiController;
import com.ydd.framework.core.entity.enums.StatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * Controller - 用户
 *
 * @author zhaoziyu
 * @since 2018-08-01
 */
@RestController
public class MemberController extends BaseApiController {

    private final Logger logger = LoggerFactory.getLogger(MemberController.class);

    @Resource
    private MemberService memberService;
    @Resource
    private MemberCardService memberCardService;

//    /**
//     * 分页查询用户
//     */
//    @RequestMapping(value = "/members", method = RequestMethod.GET)
//    public ResponseDTO findPage(Pagination pagination) {
//        return ResponseDTO.ok()
//                .setPagination(memberService.findPage(pagination));
//    }
//
//    /**
//     * 查询用户信息
//     */
//    @RequestMapping(value = "/member/{id}", method = RequestMethod.GET)
//    public ResponseDTO findById(@PathVariable("id") Integer id) {
//        return ResponseDTO.ok()
//                .addAttribute("member", memberService.findById(id));
//    }
//
//    /**
//     * 保存用户
//     */
//    @RequestMapping(value = "/member", method = RequestMethod.POST)
//    public ResponseDTO save(Member member) {
//        memberService.save(member);
//        return ResponseDTO.ok("保存成功");
//    }
//
//    /**
//     * 删除用户
//     */
//    @RequestMapping(value = "/member/delete", method = RequestMethod.POST)
//    public ResponseDTO delete(@RequestParam("id[]") Integer[] ids) {
//        memberService.deleteByIds(ids);
//        return ResponseDTO.ok("删除成功");
//    }
//
//    /**
//     * 变更用户状态
//     */
//    @RequestMapping(value = "/member/status", method = RequestMethod.POST)
//    public ResponseDTO updateStatus(@RequestParam("id[]") Integer[] ids, Byte status) {
//        memberService.updateStatus(ids, status);
//        String operationName = (StatusEnum.ENABLE.value.equals(status) ? "启用" : "禁用") + "成功";
//        return ResponseDTO.ok(operationName);
//    }

    /**
     * 个人中心
     */
    @RequestMapping(value = "/member/center", method = RequestMethod.POST)
    public ResponseDTO center() {
        return ResponseDTO.ok().
                addAttribute("members", memberService.center(getMemberIdByAccessToken()))
                .addAttribute("memberCard", memberCardService.findMemberCardCount(getMemberIdByAccessToken()));
    }

    /**
     * 个人信息
     */
    @RequestMapping(value = "/member/information", method = RequestMethod.POST)
    public ResponseDTO information() {
        return ResponseDTO.ok().addAttribute("members", memberService.information(getMemberIdByAccessToken()));
    }

    /**
     * 修改个人信息
     */
    @RequestMapping(value = "/member/update/information", method = RequestMethod.POST)
    @AccessToken
    public ResponseDTO updateInformation(InformationDTO informationDTO) {
        memberService.updateInformation(getLoginMemberId(), informationDTO);
        return ResponseDTO.ok();
    }

    /**
     * 上传人脸
     * @param file
     * @return
     */
    @RequestMapping(value = "/member/face/info", method = RequestMethod.POST)
    @AccessToken
    public ResponseDTO setMemberFaceInfo(@RequestParam("image") MultipartFile file) {
        memberService.setMemberFaceInfo(getLoginMemberId(), file, PlatformContext.getValue());
        return ResponseDTO.ok();
    }

    /**
     * 提现申请
     */
    @RequestMapping(value = "/member/withdraw/apply", method = RequestMethod.POST)
    public ResponseDTO withdrawApply(@RequestParam("money")BigDecimal money,@RequestParam("bankId")Integer bankId){
        memberService.withdrawApply(money,getMemberIdByAccessToken(),bankId,PlatformContext.getValue());
        return ResponseDTO.ok();
    }

}
