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.SmsBalance;
import com.ssm.manage.query.SmsBalanceRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.service.SmsBalanceService;
import com.ssm.util.RedisClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
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/smsBalance")
@Slf4j
public class SmsBalanceController {
    @Autowired
    private SmsBalanceService smsBalanceService;
    @Autowired
    private RedisClient<StaffResponse> redisClient;

    private static String staffKey = "staffKey";



    @ApiOperation(value = "查询当前门店短信余额", httpMethod = "POST", response = GenericResponse.class)
    @ApiImplicitParams(@ApiImplicitParam(name = "storeId",value = "门店id",required = true))
    @RequestMapping("/selectBystoreId")
    public GenericResponse selectBystoreId(@RequestBody SmsBalance smsBalance, HttpSession session) {
        GenericResponse<SmsBalance> genericResponse = new GenericResponse<>();
        try {
            SmsBalance select = smsBalanceService.selectBystoreId(smsBalance);
            genericResponse.setData(select);
        } catch (Exception e) {
            log.error("SsmProjectController.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        return genericResponse;
    }

    @ApiOperation(value = "平台端查询所有的短信账号余额", httpMethod = "POST", response = GenericResponse.class)
    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页多少行",required = true)
    })
    public PageResponse page(@RequestBody SmsBalanceRequest smsBalance, HttpSession session) {
        PageResponse<SmsBalance> pageResponse = new PageResponse<>();
        try {
            PageInfo<SmsBalance> select = smsBalanceService.page(smsBalance);
            pageResponse.setPageInfo(select);
        } catch (Exception e) {
            log.error("SsmProjectController.add", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        return pageResponse;
    }

    public String getStaffKey(String phone){
        return staffKey + phone;
    }
}
