package com.ssm.controller;

import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.UserResponse;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.entity.SmsFix;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.service.SmsFixService;
import com.ssm.util.RedisClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
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;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Api(value="短信模板controller",tags={"短信配置"})
@RestController
@RequestMapping("/demay/ssm/smsFix")
@Slf4j
public class SmsFixController {
    @Autowired
    private SmsFixService smsFixService;
    @Autowired
    private RedisClient<StaffResponse> redisClient;

    @Autowired
    private RedisClient<UserResponse> userRedisClient;

    private static String staffKey = "staffKey";

    @ApiOperation(value = "新增", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody SmsFix smsFix, HttpSession session) {
        GenericResponse<SmsFix> genericResponse = new GenericResponse<>();
        //获取添加人的名称
        String staffPhone = SessionHelp.getLoginAccount(session);
        if (redisClient.hasKey(getStaffKey(staffPhone))){
            StaffResponse staffResponse = redisClient.get(getStaffKey(staffPhone));
            smsFix.setInputId(staffResponse.getId());
            smsFix.setInputName(staffResponse.getStaffName());
            smsFix.setInputDate(new Date());
        }
        if(smsFix.getStoreId()==null){
            smsFix.setStoreId(SessionHelp.getStoreId(session));
        }
        //0是收银，1是充值，2是疗程，3是收银现金，4是收银充值卡付，5是收银疗程卡付
        smsFix.setState(0);
        String trim = smsFix.getSmsBody().trim();
        smsFix.setSmsBody(trim);
        long id = new SnowflakeUtil(new Random().nextInt(1023)).nextId();
        smsFix.setId(id);
        int i = 0;
        try {
            i = smsFixService.saveSmsFix(smsFix);

        } catch (Exception e) {
            log.error("SsmProjectController.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if(i ==999){
            genericResponse.setCodeAndMsg(ResponseCode.FAILEDS);
            return genericResponse;
        }else if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }


    @ApiOperation(value = "查询", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/page")
    @ApiImplicitParam(name = "storeId",value = "门店端必传，门店编码",required = true)
    public GenericResponse page(@RequestBody SmsFix smsFix, HttpSession session) {
        GenericResponse<List<SmsFix>> genericResponse = new GenericResponse<>();
        //获取添加人的名称
        if(smsFix.getStoreId()==null){
            smsFix.setStoreId(SessionHelp.getStoreId(session));
        }
        try {
            List<SmsFix> select = smsFixService.selectBystoreId(smsFix);
            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)
    @RequestMapping("/pageByPingTai")
    @ApiImplicitParam(name = "storeId",value = "门店端必传，门店编码",required = true)
    public GenericResponse pageByPingTai(@RequestBody SmsFix smsFix, HttpSession session) {
        GenericResponse<List<SmsFix>> genericResponse = new GenericResponse<>();
        try {
            List<SmsFix> select = smsFixService.pageByPingTai(smsFix);
            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)
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody SmsFix smsFix, HttpSession session) {
        GenericResponse<SmsFix> genericResponse = new GenericResponse<>();
        //获取添加人的名称
        String staffPhone = SessionHelp.getLoginAccount(session);
        if (redisClient.hasKey(getStaffKey(staffPhone))){
            StaffResponse staffResponse = redisClient.get(getStaffKey(staffPhone));
            smsFix.setUpdateId(staffResponse.getId());
            smsFix.setUpdateName(staffResponse.getStaffName());
            smsFix.setUpdateDate(new Date());
        }
        if(smsFix.getStoreId()==null){
            smsFix.setStoreId(SessionHelp.getStoreId(session));
        }
        String trim = smsFix.getSmsBody().trim();
        smsFix.setSmsBody(trim);
        int i = 0;
        try {
             smsFixService.updateBySmsFix(smsFix);

        } catch (Exception e) {
            log.error("SsmProjectController.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);

        return genericResponse;
    }

    @ApiOperation(value = "删除", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/delete")
    public GenericResponse delete(@RequestBody SmsFix smsFix, HttpSession session) {
        GenericResponse<SmsFix> genericResponse = new GenericResponse<>();
        //获取添加人的名称
        int i = 0;
        try {
            i = smsFixService.deleteById(smsFix.getId());

        } catch (Exception e) {
            log.error("SsmProjectController.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if(i ==999){
            genericResponse.setCodeAndMsg(ResponseCode.FAILEDS);
            return genericResponse;
        }else if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }
    @ApiOperation(value = "送审", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/sendAudit")
    public GenericResponse sendAudit(@RequestBody SmsFix smsFix, HttpSession session) {
        GenericResponse<SmsFix> genericResponse = new GenericResponse<>();
        //获取添加人的名称
        smsFix.setState(1);
        try {
              smsFixService.updateBySmsFix(smsFix);

        } catch (Exception e) {
            log.error("SsmProjectController.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
            genericResponse.setCodeAndMsg(ResponseCode.OK);

        return genericResponse;
    }

    @ApiOperation(value = "审核", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/audit")
    public GenericResponse audit(@RequestBody SmsFix smsFix, HttpSession session) {
        GenericResponse<SmsFix> genericResponse = new GenericResponse<>();
        //获取添加人的名称
        String staffPhone = SessionHelp.getLoginAccount(session);
        if (userRedisClient.hasKey(getUserKey(staffPhone))){
            UserResponse staffResponse = userRedisClient.get(getUserKey(staffPhone));
            smsFix.setAuitId(staffResponse.getId());
            smsFix.setAuitName(staffResponse.getUserName());
            smsFix.setAuitDate(new Date());
        }
        if(smsFix.getStoreId()==null){
            smsFix.setStoreId(SessionHelp.getStoreId(session));
        }
        smsFix.setState(2);
        int i = 0;
        try {
            //先将已审核的相同storeId和type的模板进行反审核，
            smsFixService.unAudit(smsFix);
            smsFixService.updateBySmsFix(smsFix);

        } catch (Exception e) {
            log.error("SsmProjectController.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "反审核", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/unAudit")
    public GenericResponse unAudit(@RequestBody SmsFix smsFix, HttpSession session) {
        GenericResponse<SmsFix> genericResponse = new GenericResponse<>();
        //获取添加人的名称

        int i = 0;
        try {
            i = smsFixService.updateUnAudit(smsFix);

        } catch (Exception e) {
            log.error("SsmProjectController.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if(i ==999){
            genericResponse.setCodeAndMsg(ResponseCode.FAILEDS);
            return genericResponse;
        }else if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    public String getStaffKey(String phone){
        return staffKey + phone;
    }
    private static String userKey = "userKey";
    public String getUserKey(String phone){
        return userKey + phone;
    }
}
