package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.PageResponse;
import com.ssm.manage.entity.MemberList;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.MemberRequest;
import com.ssm.manage.response.MemberResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Api(value="平台会员controller",tags={"平台会员相关接口"})
@RestController
@RequestMapping("/demay/ssm/member")
@Slf4j
public class SsmMemberController {
    @Autowired
    private SsmMemberService ssmMemberService;

    @ApiOperation(value = "分页查询平台会员信息 平台端-平台会员-分页查询平台会员信息",httpMethod = "POST",response = PageResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody MemberRequest memberRequest){
        PageResponse<MemberList> pageResponse = new PageResponse<>();
        PageInfo<MemberList> pageInfo = new PageInfo<>();
        try {
            pageInfo = ssmMemberService.selectPage(memberRequest);
        }catch (Exception e){
            log.error("SsmMemberController.selectPage:",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "查询单个平台会员信息 门店端-开单页面-查询单个平台会员信息",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/one")
    public GenericResponse selectOne(@RequestBody MemberRequest memberRequest){
        GenericResponse<MemberList> genericResponse = new GenericResponse<>();
//        if (StringUtils.isBlank(memberRequest.getMobilePhone())){
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"手机");
//            return genericResponse;
//        }
//        if (StringUtils.isBlank(memberRequest.getCard())){
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"会员卡");
//        }
        MemberList memberList = new MemberList();
        try {
            memberList = ssmMemberService.selectOne(memberRequest);
        }catch (Exception e){
            log.error("SsmMemberController.selectOne:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setData(memberList);
        return genericResponse;
    }

    @ApiOperation(value = "平台会员添加 门店端-开单页面-平台会员添加",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody MemberList memberList, HttpSession session){
        GenericResponse<MemberResponse> genericResponse = new GenericResponse<>();
        MemberResponse memberResponse = new MemberResponse();
        try {
            memberResponse = ssmMemberService.addMember(memberList);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmMemberController.add:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setData(memberResponse);
        return genericResponse;
    }


    @ApiOperation(value = "平台会员修改  手机端-平台会员修改",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody MemberRequest memberRequest){
        GenericResponse<MemberList> genericResponse = new GenericResponse<>();
        int i = 0;
        try {
            i = ssmMemberService.updateMember(memberRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmMemberController.update:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

}
