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.query.RechargeCardTransitionRequest;
import com.ssm.manage.response.RechargeCardTransitionResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.service.SsmRechargeCardTransitionService;
import com.ssm.util.RedisClient;
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/rechargeCardTransition")
@Slf4j
public class ssmRechargeCardTransitionController {


    @Autowired
    private SsmRechargeCardTransitionService ssmRechargeCardTransitionService;
    @Autowired
    private RedisClient<StaffResponse> redisClient;

    @ApiOperation(value = "查询会员转卡申请单",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody RechargeCardTransitionRequest turnMembershipCardRequest, HttpSession session){
        PageResponse<RechargeCardTransitionResponse> pageResponse = new PageResponse<>();
        PageInfo<RechargeCardTransitionResponse> pageInfo = new PageInfo<>();
        Long storeId = SessionHelp.getStoreId(session);
        turnMembershipCardRequest.setStoreId(storeId);
        try {
            pageInfo = ssmRechargeCardTransitionService.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("/pageNew")
    public PageResponse selectPageNew(@RequestBody RechargeCardTransitionRequest turnMembershipCardRequest, HttpSession session){
        PageResponse<RechargeCardTransitionResponse> pageResponse = new PageResponse<>();
        PageInfo<RechargeCardTransitionResponse> pageInfo = new PageInfo<>();
//        Long storeId = SessionHelp.getStoreId(session);
//        List<Long> listStoreId=new ArrayList<>();
//        if(null == turnMembershipCardRequest.getStoreList() || turnMembershipCardRequest.getStoreList().size() == 0){
//            //自动查询
//            turnMembershipCardRequest.setSelectType(1);
//            turnMembershipCardRequest.setStoreId(storeId);
//            listStoreId.add(storeId);
//            turnMembershipCardRequest.setStoreList(listStoreId);
//        }else{
//            //指定查询
//            turnMembershipCardRequest.setSelectType(0);
//        }
        try {
            pageInfo = ssmRechargeCardTransitionService.selectPageNew(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, notes = "")
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody RechargeCardTransitionRequest rechargeTurnCourseRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        String staffPhone = SessionHelp.getLoginAccount(session);
        rechargeTurnCourseRequest.setStoreName(storeName);
        rechargeTurnCourseRequest.setStoreId(storeId);
        if(null == rechargeTurnCourseRequest.getInRechargeTypeId()){
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"未选择转入的充值卡");
            return genericResponse;
        }
        if(null == rechargeTurnCourseRequest.getOutRechargeTypeId()){
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"未选择转出的充值卡");
            return genericResponse;
        }
        int i = 0;
        try {
            if(rechargeTurnCourseRequest.getInMoney().longValue()<0||rechargeTurnCourseRequest.getInGiftMoney().longValue()<0){
                throw new SmsException("010000","操作失败，不能输入负数");
            }
            i = ssmRechargeCardTransitionService.add(rechargeTurnCourseRequest,staffPhone);
        } catch (SmsException e){
            log.error("SsmStaffController.add:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStaffController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if(i>0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }else{
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "充值账户内调单修改", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody RechargeCardTransitionRequest rechargeTurnCourseRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        String staffPhone = SessionHelp.getLoginAccount(session);
        rechargeTurnCourseRequest.setStoreName(storeName);
        rechargeTurnCourseRequest.setStoreId(storeId);
        if(null == rechargeTurnCourseRequest.getInRechargeTypeId()){
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"未选择转入的充值卡");
            return genericResponse;
        }
        if(null == rechargeTurnCourseRequest.getOutRechargeTypeId()){
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"未选择转出的充值卡");
            return genericResponse;
        }
        int i = 0;
        try {
            if(rechargeTurnCourseRequest.getInMoney().longValue()<0||rechargeTurnCourseRequest.getInGiftMoney().longValue()<0){
                throw new SmsException("010000","操作失败，不能输入负数");
            }
            i = ssmRechargeCardTransitionService.update(rechargeTurnCourseRequest,staffPhone);
        } catch (SmsException e){
            log.error("SsmStaffController.add:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStaffController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if(i>0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }else{
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "充值账户内调单状态修改", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/updateState")
    public GenericResponse updateState(@RequestBody RechargeCardTransitionRequest rechargeTurnCourseRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        String staffPhone = SessionHelp.getLoginAccount(session);
        if (redisClient.hasKey("staffKey"+staffPhone)) {
            StaffResponse staffResponse = redisClient.get("staffKey"+staffPhone);
            rechargeTurnCourseRequest.setUpdateId(staffResponse.getId());
            rechargeTurnCourseRequest.setUpdateName(staffResponse.getStaffName());
        }
//        if(null == rechargeTurnCourseRequest.getInRechargeTypeId()){
//            genericResponse.setCodeAndMsg(ResponseCode.MSG,"未选择转入的充值卡");
//            return genericResponse;
//        }
//        if(null == rechargeTurnCourseRequest.getOutRechargeTypeId()){
//            genericResponse.setCodeAndMsg(ResponseCode.MSG,"未选择转出的充值卡");
//            return genericResponse;
//        }
        int i = 0;
        try {
            i = ssmRechargeCardTransitionService.updateState(rechargeTurnCourseRequest,staffPhone);
        } catch (SmsException e){
            log.error("SsmStaffController.add:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStaffController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if(i>0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }else{
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "充值账户内调单删除", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/delete")
    public GenericResponse delete(@RequestBody RechargeCardTransitionRequest rechargeTurnCourseRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        String staffPhone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmRechargeCardTransitionService.delete(rechargeTurnCourseRequest);
        } catch (SmsException e){
            log.error("SsmStaffController.add:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStaffController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if(i>0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }else{
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

}
