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.ServiceOrders;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.entity.StoreSettlement;
import com.ssm.manage.query.StoreSettlementRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmStoreSettlementService;
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;
import java.util.List;

@Api(value="门店房间服务上钟结算controller",tags={"门店房间服务上钟结算相关接口"})
@RestController
@RequestMapping("/demay/ssm/storeSettlement")
@Slf4j
public class SsmServiceSettlementController {

    @Autowired
    private SsmStoreSettlementService ssmStoreSettlementService;

    @ApiOperation(value = "查询员工未结算单据",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody StoreSettlementRequest serviceOrderRequest, HttpSession session){
        PageResponse<StoreSettlement> pageResponse = new PageResponse<>();
        PageInfo<StoreSettlement> pageInfo = new PageInfo<>();
        Long storeId = SessionHelp.getStoreId(session);
        serviceOrderRequest.setStoreId(storeId);
        try {
            pageInfo = ssmStoreSettlementService.selectPage(serviceOrderRequest);
        }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 List<StoreSettlementRequest> serviceOrderRequest, HttpSession session) {
        GenericResponse<ServiceOrders> genericResponse = new GenericResponse();

        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        for (StoreSettlementRequest serviceOrdersRequest : serviceOrderRequest) {
            if(null == serviceOrdersRequest.getId()){
                serviceOrdersRequest.setStoreName(storeName);
                serviceOrdersRequest.setStoreId(storeId);
            }
        }

        String phone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmStoreSettlementService.add(serviceOrderRequest,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 StoreSettlementRequest serviceOrderRequest, HttpSession session) {
        GenericResponse<ServiceOrders> genericResponse = new GenericResponse();

        String phone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmStoreSettlementService.update(serviceOrderRequest,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 StoreSettlementRequest serviceOrderRequest, HttpSession session) {
        GenericResponse<ServiceOrders> genericResponse = new GenericResponse();

        String phone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmStoreSettlementService.detele(serviceOrderRequest,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("/updateTime")
    public GenericResponse updateTime(@RequestBody StoreSettlementRequest serviceOrderRequest, HttpSession session) {
        GenericResponse<ServiceOrders> genericResponse = new GenericResponse();

        String phone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmStoreSettlementService.updateTime(serviceOrderRequest,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("/updateTimeX")
    public GenericResponse updateTimeX(@RequestBody StoreSettlementRequest serviceOrderRequest, HttpSession session) {
        GenericResponse<ServiceOrders> genericResponse = new GenericResponse();

        String phone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmStoreSettlementService.updateTimeX(serviceOrderRequest,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("/checkOut")
    public GenericResponse tuifang(@RequestBody StoreSettlementRequest serviceOrderRequest, HttpSession session) {
        GenericResponse<ServiceOrders> genericResponse = new GenericResponse();

        String phone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmStoreSettlementService.tuifang(serviceOrderRequest,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("/roomChange")
    public GenericResponse roomChange(@RequestBody List<StoreSettlementRequest> serviceOrderRequest, HttpSession session) {
        GenericResponse<ServiceOrders> genericResponse = new GenericResponse();

        String phone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmStoreSettlementService.roomChange(serviceOrderRequest,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;
    }

}
