package com.sv.oms.controller;

import com.sv.entity.Member;
import com.sv.service.api.MemberService;
import com.sv.service.common.FaceService;
import com.ydd.framework.core.common.Pagination;
import com.ydd.framework.core.common.dto.ResponseDTO;
import com.ydd.framework.core.entity.enums.StatusEnum;
import com.ydd.framework.core.exception.ServiceException;
import com.ydd.oms.controller.OmsController;
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.text.ParseException;
import java.util.Date;

/**
 * Controller - 用户
 *
 * @author yechanglin
 * @since 2018-08-03
 */
@RestController
public class MemberController extends OmsController {

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

    @Resource
    private MemberService memberService;

    @Resource
    private com.sv.service.oms.MemberCardService memberCardService;
    @Resource
    private com.sv.service.oms.MemberService memberServiceOms;

    /**
     * 分页查询用户
     */
    @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 findByIds(@PathVariable("id") Integer id) {
        return ResponseDTO.ok()
                .addAttribute("member", memberService.findByIds(id));
    }

    /**
     * 保存用户
     */
    @RequestMapping(value = "/member", method = RequestMethod.POST)
    public ResponseDTO save(Member member) throws ParseException {
        memberServiceOms.saveMember(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/giveCard", method = RequestMethod.POST)
    public ResponseDTO createCard(@RequestParam("memberId") Integer[] memberId, @RequestParam("cardType") String cardType,
                                  @RequestParam("venueType") Integer venueType, @RequestParam("platformId") Integer platformId,
                                  @RequestParam("venueId") Integer venueId, @RequestParam("star") Date star,
                                  @RequestParam("end") Date end) {
        for (int i = 0; i < memberId.length; i++) {
            memberCardService.createCard(memberId[i], cardType, venueType, platformId, venueId, star, end);
        }
        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);
    }

    /**
     * 上传认脸头像
     *
     * @param memberId
     * @param file
     * @return
     */
    @RequestMapping(value = "/member/upload/face", method = RequestMethod.POST)
    public ResponseDTO uploadFace(@RequestParam("memberId") Integer memberId, @RequestParam("file") MultipartFile file) {
        try {
            memberService.setMemberFaceInfo(memberId, file, 1);
        } catch (ServiceException e) {
            return new ResponseDTO(-1, e.getMessage());
        }

        return ResponseDTO.ok();
    }

}
