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.BaseStore;
import com.ssm.manage.service.SsmBaseStoreService;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.entity.SmsFix;
import com.ssm.manage.entity.SmsType;
import com.ssm.manage.query.SmsTypeRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SmsTypeService;
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/smsType")
@Slf4j
public class SmsTypeController {
    @Autowired
    private SmsTypeService smsTypeService;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;


    @ApiOperation(value = "新增", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody SmsType smsType, HttpSession session) {
        GenericResponse<SmsFix> genericResponse = new GenericResponse<>();
        String loginAccount = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = smsTypeService.saveSmsType(smsType,loginAccount);
        } 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("/pageByState")
    public PageResponse pageByState(@RequestBody SmsTypeRequest smsType, HttpSession session) {
        PageResponse<SmsType> pageResponse = new PageResponse<>();
        PageInfo<SmsType> pageInfo = new PageInfo<>();
        BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(SessionHelp.getStoreId(session));
        smsType.setSmsType(baseStore.getMessagingPlatform());
        smsType.setState(1);
        try {
            pageInfo = smsTypeService.selectBystoreId(smsType);
            pageResponse.setPageInfo(pageInfo);
            pageResponse.setCodeAndMsg(ResponseCode.OK);
        } catch (Exception e) {
            log.error("SsmProjectController.add", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        return pageResponse;
    }


    @ApiOperation(value = "查询", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/page")
    public PageResponse page(@RequestBody SmsTypeRequest smsType, HttpSession session) {
        PageResponse<SmsType> pageResponse = new PageResponse<>();
        PageInfo<SmsType> pageInfo = new PageInfo<>();
        try {
            pageInfo = smsTypeService.selectBystoreId(smsType);
            pageResponse.setPageInfo(pageInfo);
            pageResponse.setCodeAndMsg(ResponseCode.OK);
        } catch (Exception e) {
            log.error("SsmProjectController.add", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        return pageResponse;
    }

    @ApiOperation(value = "修改", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody SmsType smsType, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse<>();
        int i = 0;
        String loginAccount = SessionHelp.getLoginAccount(session);
        try {
             smsTypeService.updateBySmsType(smsType,loginAccount);

        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }
        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("/updateState")
    public GenericResponse updateState(@RequestBody SmsType smsType, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse<>();
        int i = 0;
        String loginAccount = SessionHelp.getLoginAccount(session);
        try {
            smsTypeService.updateState(smsType,loginAccount);

        }
        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 SmsType smsType, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse<>();
        //获取添加人的名称
        int i = 0;
        try {
            i = smsTypeService.deleteById(smsType.getId());

        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } 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;
    }

}
