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.SmsException;
import com.ssm.manage.entity.StoreRoom;
import com.ssm.manage.query.StoreRoomRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmStoreRoomService;
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/storeRoom")
@Slf4j
public class SsmStoreRoomController {

    @Autowired
    private SsmStoreRoomService ssmStoreRoomService;


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

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

    /**
     * 删除，全部弄完再弄
     */

}
