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.utils.SessionHelp;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.entity.StoreMemberClass;
import com.ssm.manage.entity.StoreRoom;
import com.ssm.manage.query.StoreMemberClassRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmStoreMemberClassService;
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/StoreMemberClass")
@Slf4j
public class ssmStoreMemberClassController {

    @Autowired
    private SsmStoreMemberClassService ssmStoreMemberClassService;

    @ApiOperation(value = "查询房间",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody StoreMemberClassRequest storeMemberClassRequest, HttpSession session){
        PageResponse<StoreMemberClass> pageResponse = new PageResponse<>();
        PageInfo<StoreMemberClass> pageInfo = new PageInfo<>();
        Long storeId = SessionHelp.getStoreId(session);
        storeMemberClassRequest.setStoreId(storeId);
        try {
            pageInfo = ssmStoreMemberClassService.selectPage(storeMemberClassRequest);
        }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 StoreMemberClassRequest storeMemberClassRequest, HttpSession session) {
        GenericResponse<StoreRoom> genericResponse = new GenericResponse();

        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        storeMemberClassRequest.setStoreName(storeName);
        storeMemberClassRequest.setStoreId(storeId);
        String phone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmStoreMemberClassService.add(storeMemberClassRequest,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 StoreMemberClassRequest storeMemberClassRequest, HttpSession session) {
        GenericResponse<StoreRoom> genericResponse = new GenericResponse();

        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        storeMemberClassRequest.setStoreName(storeName);
        storeMemberClassRequest.setStoreId(storeId);
        String phone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmStoreMemberClassService.update(storeMemberClassRequest,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("/delete")
    public GenericResponse delete(@RequestBody StoreMemberClassRequest storeMemberClassRequest, HttpSession session) {
        GenericResponse<StoreRoom> genericResponse = new GenericResponse();
        int i = 0;
        try {
            i = ssmStoreMemberClassService.delete(storeMemberClassRequest);
        } 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;
    }

}
