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.utils.SessionHelp;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.entity.TurnMembershipCard;
import com.ssm.manage.query.TurnMembershipCardMxRequest;
import com.ssm.manage.query.TurnMembershipCardRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.TurnMembershipCardResponse;
import com.ssm.manage.service.SsmTurnMembershipCardService;
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/turnMembershipCard")
@Slf4j
public class SsmTurnMembershipCardController {
    @Autowired
    public SsmTurnMembershipCardService ssmTurnMembershipCardService;

    @ApiOperation(value = "查询会员转卡申请单",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody TurnMembershipCardRequest turnMembershipCardRequest, HttpSession session){
        PageResponse<TurnMembershipCardResponse> pageResponse = new PageResponse<>();
        PageInfo<TurnMembershipCardResponse> pageInfo = new PageInfo<>();
        Long storeId = SessionHelp.getStoreId(session);
        turnMembershipCardRequest.setStoreId(storeId);
        try {
            pageInfo = ssmTurnMembershipCardService.selectPage(turnMembershipCardRequest);
        }catch (Exception e){
            log.error("SsmCashController.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("/add")
    public GenericResponse add(@RequestBody TurnMembershipCardRequest turnMembershipCardRequest, HttpSession session){
        GenericResponse<TurnMembershipCard> genericResponse = new GenericResponse<>();
        if (null ==turnMembershipCardRequest.getRollOutMemberId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"卡包转出会员");
            return genericResponse;
        }
        if (null ==turnMembershipCardRequest.getRollInMemberId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"卡包转入会员");
            return genericResponse;
        }
        if (null ==turnMembershipCardRequest.getAccountId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"转出会员卡包");
            return genericResponse;
        }
        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        String key = SessionHelp.getLoginAccount(session);
        turnMembershipCardRequest.setStoreId(storeId);
        turnMembershipCardRequest.setStoreName(storeName);
        int i = 0;
        try {
            i = ssmTurnMembershipCardService.add(turnMembershipCardRequest,key);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmProjectTypeController.add",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "会员卡包转送申请单(多卡包转)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/addNew")
    public GenericResponse addNew(@RequestBody TurnMembershipCardRequest turnMembershipCardRequest, HttpSession session){
        GenericResponse<TurnMembershipCard> genericResponse = new GenericResponse<>();
        if (null ==turnMembershipCardRequest.getRollOutMemberId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"卡包转出会员");
            return genericResponse;
        }
        if (null ==turnMembershipCardRequest.getRollInMemberId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"卡包转入会员");
            return genericResponse;
        }
        if (null ==turnMembershipCardRequest.getTurnMembershipCardMxRequestList()||turnMembershipCardRequest.getTurnMembershipCardMxRequestList().size()==0){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"转出会员卡包");
            return genericResponse;
        }
        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        String key = SessionHelp.getLoginAccount(session);
        turnMembershipCardRequest.setStoreId(storeId);
        turnMembershipCardRequest.setStoreName(storeName);
        int i = 0;
        try {
            for(TurnMembershipCardMxRequest turnMembershipCardMxRequest:turnMembershipCardRequest.getTurnMembershipCardMxRequestList()){
                if(turnMembershipCardMxRequest.getOutCourseRestCount()!=null){
                    if(turnMembershipCardMxRequest.getOutCardRestMoney().doubleValue()<0||turnMembershipCardMxRequest.getOutCourseRestCount()<0){
                        throw new SmsException("010000","操作失败，不能输入负数");
                    }
                }else{
                    if(turnMembershipCardMxRequest.getOutCardRestMoney().doubleValue()<0||turnMembershipCardMxRequest.getOutGiftConsumeMoney().doubleValue()<0){
                        throw new SmsException("010000","操作失败，不能输入负数");
                    }
                }

            }

            i = ssmTurnMembershipCardService.addNew(turnMembershipCardRequest,key);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmProjectTypeController.add",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }


    @ApiOperation(value = "修改会员卡包转送申请单",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody TurnMembershipCardRequest turnMembershipCardRequest, HttpSession session){
        GenericResponse<TurnMembershipCard> genericResponse = new GenericResponse<>();
        if (null ==turnMembershipCardRequest.getRollOutMemberId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"卡包转出会员");
            return genericResponse;
        }
        if (null ==turnMembershipCardRequest.getRollInMemberId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"卡包转入会员");
            return genericResponse;
        }
        if (null ==turnMembershipCardRequest.getAccountId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"转出会员卡包");
            return genericResponse;
        }
        String key = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmTurnMembershipCardService.update(turnMembershipCardRequest,key);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmProjectTypeController.add",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "修改会员卡包转送申请单",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/updateNew")
    public GenericResponse updateNew(@RequestBody TurnMembershipCardRequest turnMembershipCardRequest, HttpSession session){
        GenericResponse<TurnMembershipCard> genericResponse = new GenericResponse<>();
        if (null ==turnMembershipCardRequest.getRollOutMemberId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"卡包转出会员");
            return genericResponse;
        }
        if (null ==turnMembershipCardRequest.getRollInMemberId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"卡包转入会员");
            return genericResponse;
        }
        if (null ==turnMembershipCardRequest.getTurnMembershipCardMxRequestList()||turnMembershipCardRequest.getTurnMembershipCardMxRequestList().size()==0){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"转出会员卡包");
            return genericResponse;
        }
        String key = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            for(TurnMembershipCardMxRequest turnMembershipCardMxRequest:turnMembershipCardRequest.getTurnMembershipCardMxRequestList()){
                if(turnMembershipCardMxRequest.getOutCardRestMoney().longValue()<0||turnMembershipCardMxRequest.getOutCourseRestCount()<0){
                    throw new SmsException("010000","操作失败，不能输入负数");
                }
            }
            i = ssmTurnMembershipCardService.updateNew(turnMembershipCardRequest,key);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmProjectTypeController.add",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "会员卡包转送申请单状态修改",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/updateState")
    public GenericResponse updateState(@RequestBody TurnMembershipCardRequest turnMembershipCardRequest, HttpSession session){
        GenericResponse<TurnMembershipCard> genericResponse = new GenericResponse<>();
        String key = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmTurnMembershipCardService.updateState(turnMembershipCardRequest,key);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmProjectTypeController.add",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "会员卡包转送申请单状态修改",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/updateStateNew")
    public GenericResponse updateStateNew(@RequestBody TurnMembershipCardRequest turnMembershipCardRequest, HttpSession session){
        GenericResponse<TurnMembershipCard> genericResponse = new GenericResponse<>();
        String key = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmTurnMembershipCardService.updateStateNew(turnMembershipCardRequest,key);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmProjectTypeController.add",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

}
