package com.ssm.controller;

import com.ssm.core.bean.GenericResponse;
import com.ssm.core.util.SessionHelp;
import com.ssm.core.utils.ChuangLanBeanSmsUtil;
import com.ssm.manage.entity.BaseStore;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.RechargeListRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmBaseStoreService;
import com.ssm.manage.service.SsmRechargeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
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;
import java.io.IOException;

@Api(value="app充值controller",tags={"app充值相关接口"})
@RestController
@RequestMapping("/demay/ssm/rec")
@Slf4j
public class SsmRechargeController {

    @Autowired
    private SsmRechargeService ssmRechargeService;

    @Autowired
    private ChuangLanBeanSmsUtil chuangLanBeanSmsUtil;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;




    @ApiOperation(value = "充值单添加  (门店端-充值单-充值单添加)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody RechargeListRequest rechargeListRequest,HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        String staffPhone = SessionHelp.getLoginAccount(session);
//        rechargeListRequest.setStoreId(storeId);
//        rechargeListRequest.setStoreName(storeName);
//        rechargeListRequest.setStaffPhone(staffPhone);

        int i = 0;
        try {
            i = ssmRechargeService.addRecharge(rechargeListRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmRechargeController.add:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
            BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(rechargeListRequest.getStoreId());
            //设置短信账户 发送 否则不发送
            if (StringUtils.isNotBlank(baseStore.getSmsAccount())) {
                try {
                    chuangLanBeanSmsUtil.sendRechargeSms(baseStore, rechargeListRequest.getMobilePhone(), rechargeListRequest.getCollectionAmount());
                } catch (IOException e) {
                    log.error("SsmRechargeController.add:", e);
                } catch (SmsException e) {
                    genericResponse.setCodeAndMsg(ResponseCode.SMS_SEND_OK_EXCEPTION);
                    log.error("SsmRechargeController.add:", e);
                }
            }
        }
        return genericResponse;
    }

    @ApiOperation(value = "充值单作废  (门店端-充值单-单据记录-充值单作废)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/cancel")
    public GenericResponse cancel(@RequestBody RechargeListRequest rechargeListRequest) {
        GenericResponse genericResponse = new GenericResponse();
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        String staffPhone = SessionHelp.getLoginAccount(session);
//        rechargeListRequest.setStoreId(storeId);
//        rechargeListRequest.setStoreName(storeName);
//        rechargeListRequest.setStaffPhone(staffPhone);
        int i = 0;
        try {
            i = ssmRechargeService.cancel(rechargeListRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmRechargeController.cancel:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "充值单业务员修改  (门店端-充值单-充值单业务员修改)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/updateStaff")
    public GenericResponse updateStaff(@RequestBody RechargeListRequest rechargeListRequest) {
        GenericResponse genericResponse = new GenericResponse();
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        String staffPhone = SessionHelp.getLoginAccount(session);
//        rechargeListRequest.setStoreId(storeId);
//        rechargeListRequest.setStoreName(storeName);
//        rechargeListRequest.setStaffPhone(staffPhone);
        int i = 0;
        try {
            i = ssmRechargeService.updateStaff(rechargeListRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmRechargeController.updateStaff:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "充值单添加  升级",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/addUpgrade")
    public GenericResponse
    addUpgrade(@RequestBody RechargeListRequest rechargeListRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        String staffPhone = SessionHelp.getLoginAccount(session);
        rechargeListRequest.setStoreId(storeId);
        rechargeListRequest.setStoreName(storeName);
        rechargeListRequest.setStaffPhone(staffPhone);
        int i = 0;
        try {
            i = ssmRechargeService.addRecharges(rechargeListRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmRechargeController.add:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
            BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(rechargeListRequest.getStoreId());
            //设置短信账户 发送 否则不发送
            if (StringUtils.isNotBlank(baseStore.getSmsAccount())) {
                try {
                    chuangLanBeanSmsUtil.sendRechargeSms(baseStore, rechargeListRequest.getMobilePhone(), rechargeListRequest.getCollectionAmount());
                } catch (IOException e) {
                    log.error("SsmRechargeController.add:", e);
                } catch (SmsException e) {
                    log.error("SsmRechargeController.add:", e);
                }
            }
        }
        return genericResponse;
    }
}
