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.manage.entity.BasePost;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.BasePostRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmBasePostService;
import com.ssm.util.RedisClient;
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/BasePost")
@Slf4j
public class SsmBasePostController {

    @Autowired
    private SsmBasePostService ssmBasePostService;
    @Autowired
    public RedisClient redisClient;

    @ApiOperation(value = "查询职务",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody BasePostRequest basePostRequest, HttpSession session){
        PageResponse<BasePost> pageResponse = new PageResponse<>();
        PageInfo<BasePost> pageInfo = new PageInfo<>();
        Long storeId = (long)redisClient.get("StoreId");
        //Long storeId = SessionHelp.getStoreId(session);
        basePostRequest.setStoreId(storeId);
        try {
            pageInfo = ssmBasePostService.selectPage(basePostRequest);
        }catch (Exception e){
            log.error("SsmCashController.selectPage:",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "添加职务", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody BasePostRequest basePostRequest, HttpSession session) {
        GenericResponse<BasePost> genericResponse = new GenericResponse();

        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        basePostRequest.setStoreName(storeName);
        basePostRequest.setStoreId(storeId);
        String phone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmBasePostService.add(basePostRequest,phone);
        } catch (SmsException e){
            log.error("SsmStaffController.add:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStaffController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "添加职务", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody BasePostRequest basePostRequest, HttpSession session) {
        GenericResponse<BasePost> genericResponse = new GenericResponse();

        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        basePostRequest.setStoreName(storeName);
        basePostRequest.setStoreId(storeId);
        String phone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmBasePostService.update(basePostRequest,phone);
        } catch (SmsException e){
            log.error("SsmStaffController.add:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStaffController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "删除职务", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/detele")
    public GenericResponse detele(@RequestBody BasePostRequest basePostRequest, HttpSession session) {
        GenericResponse<BasePost> genericResponse = new GenericResponse();
        int i = 0;
        try {
            i = ssmBasePostService.detele(basePostRequest);
        } catch (SmsException e){
            log.error("SsmStaffController.add:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStaffController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

}
