package com.ssm.controller;

import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.manage.entity.ClockService;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.ClockServiceRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmClockServiceService;
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/clockService")
@Slf4j
public class SsmClockServiceController {

    @Autowired
    private SsmClockServiceService ssmClockServiceService;

    @ApiOperation(value = "添加正在服务的单据", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody ClockServiceRequest serviceOrderRequest, HttpSession session) {
        GenericResponse<ClockService> genericResponse = new GenericResponse();
        if(SessionHelp.getStoreId(session)==null){
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"登录失效，请退出重新登录！");
            return genericResponse;
        }
        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        serviceOrderRequest.setStoreId(storeId);
        serviceOrderRequest.setStoreName(storeName);

        int i = 0;
        try {
            i = ssmClockServiceService.add(serviceOrderRequest);
        } 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("/upClock")
    public GenericResponse upClock(@RequestBody ClockServiceRequest serviceOrderRequest, HttpSession session) {
        GenericResponse<ClockService> genericResponse = new GenericResponse();

        int i = 0;
        try {
            i = ssmClockServiceService.upClock(serviceOrderRequest);
        } 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("/nextClock")
    public GenericResponse nextClock(@RequestBody ClockServiceRequest serviceOrderRequest, HttpSession session) {
        GenericResponse<ClockService> genericResponse = new GenericResponse();

        int i = 0;
        try {
            i = ssmClockServiceService.nextClock(serviceOrderRequest);
        } 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;
    }


}
