package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.core.bean.GenericResponse;
import com.ssm.core.bean.PageResponse;
import com.ssm.core.util.SessionHelp;
import com.ssm.core.utils.BigDecimalUtils;
import com.ssm.manage.entity.ModifyCourseRecord;
import com.ssm.manage.entity.ModifyRechargeRecord;
import com.ssm.manage.entity.ProjectProgress;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.ModifyCourseRecordRequest;
import com.ssm.manage.query.ModifyRechargeRecordRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmModifyCourseRecordService;
import com.ssm.manage.service.SsmModifyRechargeRecordService;
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 = {"门店账户修改记录相关接口"})
@RequestMapping("/demay/ssm/modifyAccount")
@RestController
@Slf4j
public class SsmModifyAccountRecordController {
    @Autowired
    private SsmModifyRechargeRecordService ssmModifyRechargeRecordService;

    @Autowired
    private SsmModifyCourseRecordService ssmModifyCourseRecordService;

    @ApiOperation(value = "充值账户修改记录分页查询  门店端-账户修改记录-充值账户修改记录分页查询",httpMethod = "POST",response = PageResponse.class)
    @RequestMapping("/recPage")
    public PageResponse selectRechargePage(@RequestBody ModifyRechargeRecordRequest modifyRechargeRecordRequest){
        PageResponse pageResponse = new PageResponse();
        PageInfo<ModifyRechargeRecord> pageInfo = new PageInfo<>();
//        modifyRechargeRecordRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            pageInfo = ssmModifyRechargeRecordService.selectPage(modifyRechargeRecordRequest);
        }catch (Exception e){
            log.error("SsmModifyAccountRecordController.selectRechargePage:", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "疗程账户修改记录分页查询  门店端-账户修改记录-疗程账户修改记录分页查询",httpMethod = "POST",response = PageResponse.class)
    @RequestMapping("/coursePage")
    public PageResponse selectCoursePage(@RequestBody ModifyCourseRecordRequest modifyCourseRecordRequest){
        PageResponse pageResponse = new PageResponse();
        PageInfo<ModifyCourseRecord> pageInfo = new PageInfo<>();
//        modifyCourseRecordRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            pageInfo = ssmModifyCourseRecordService.selectPage(modifyCourseRecordRequest);
        }catch (Exception e){
            log.error("SsmModifyAccountRecordController.selectCoursePage:", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "门店充值账户修改记录添加  门店端-账户修改记录-门店充值账户修改记录添加",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/recAdd")
    public GenericResponse add(@RequestBody ModifyRechargeRecordRequest modifyRechargeRecordRequest){
        GenericResponse<ProjectProgress> genericResponse = new GenericResponse<>();
        String phone = modifyRechargeRecordRequest.getStaffPhone();
        if (BigDecimalUtils.compareTo(modifyRechargeRecordRequest.getCardRestMoneyNew(), modifyRechargeRecordRequest.getCardConsumeMoneyNew()) == -1){
            genericResponse.setCodeAndMsg(ResponseCode.PRICE_COMPARE_MORE_THAN,"可用金额不允许比剩余金额");
            return genericResponse;
        }
        if (BigDecimalUtils.compareTo(modifyRechargeRecordRequest.getCardGiftMoneyNew(), modifyRechargeRecordRequest.getGiftConsumeMoneyNew()) == -1){
            genericResponse.setCodeAndMsg(ResponseCode.PRICE_COMPARE_MORE_THAN,"赠送可用金额不允许比赠送剩余金额");
            return genericResponse;
        }
        int i = 0;
        try {
            if(modifyRechargeRecordRequest.getCardConsumeMoneyModify().longValue()<0||modifyRechargeRecordRequest.getCardGiftMoneyModify().longValue()<0||modifyRechargeRecordRequest.getCardRestMoneyModify().longValue()<0||modifyRechargeRecordRequest.getGiftConsumeMoneyModify().longValue()<0){
                throw new SmsException("010000","操作失败，不能输入负数");
            }
            i = ssmModifyRechargeRecordService.add(modifyRechargeRecordRequest, phone);
        }catch (Exception e){
            log.error("SsmModifyAccountRecordController.recAdd:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "门店疗程账户修改记录添加  门店端-账户修改记录-门店疗程账户修改记录添加",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/courseAdd")
    public GenericResponse courseAdd(@RequestBody ModifyCourseRecordRequest modifyCourseRecordRequest){
        GenericResponse<ProjectProgress> genericResponse = new GenericResponse<>();
        String phone = modifyCourseRecordRequest.getStaffPhone();
        if (modifyCourseRecordRequest.getCourseRestCountNew() < modifyCourseRecordRequest.getCourseConsumeCountNew()){
            genericResponse.setCodeAndMsg(ResponseCode.PRICE_COMPARE_MORE_THAN,"可用次数不允许比剩余次数");
            return genericResponse;
        }
        int i = 0;
        try {
            if(modifyCourseRecordRequest.getCourseConsumeCountModify().longValue()<0||modifyCourseRecordRequest.getCourseRestCountModify().longValue()<0){
                throw new SmsException("010000","操作失败，不能输入负数");
            }
            i = ssmModifyCourseRecordService.add(modifyCourseRecordRequest, phone);
        }catch (Exception e){
            log.error("SsmModifyAccountRecordController.add:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

}
