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.ServiceOrder;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.ServiceOrderRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmServiceOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
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.ArrayList;
import java.util.List;

@Api(value="门店技术上钟排序表controller",tags={"门店技术上钟排序表相关接口"})
@RestController
@RequestMapping("/demay/ssm/serviceOrder")
@Slf4j
public class SsmServiceOrderController {

    @Autowired
    private SsmServiceOrderService ssmServiceOrderService;

    @ApiOperation(value = "查询全部员工排班",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody ServiceOrderRequest serviceOrderRequest, HttpSession session){
        PageResponse<ServiceOrder> pageResponse = new PageResponse<>();
        PageInfo<ServiceOrder> pageInfo = new PageInfo<>();
        Long storeId = SessionHelp.getStoreId(session);
        serviceOrderRequest.setStoreId(storeId);
        try {
            pageInfo = ssmServiceOrderService.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)
    @RequestMapping("/list")
    public GenericResponse selectList(HttpSession session){
        ServiceOrderRequest serviceOrderRequest = new ServiceOrderRequest();
        GenericResponse<ServiceOrder> pageResponse = new GenericResponse<>();
        List<ServiceOrder> pageInfo = new ArrayList<>();
        Long storeId = SessionHelp.getStoreId(session);
        serviceOrderRequest.setStoreId(storeId);
        try {
            pageInfo = ssmServiceOrderService.selectList(serviceOrderRequest);
        }catch (Exception e){
            log.error("SsmCashController.selectPage:",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setRows(pageInfo);
        return pageResponse;
    }
    @ApiOperation(value = "查询全部员工排班(非上班状态)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/lists")
    public GenericResponse selectLists(HttpSession session){
        ServiceOrderRequest serviceOrderRequest = new ServiceOrderRequest();
        GenericResponse<ServiceOrder> pageResponse = new GenericResponse<>();
        List<ServiceOrder> pageInfo = new ArrayList<>();
        Long storeId = SessionHelp.getStoreId(session);
        serviceOrderRequest.setStoreId(storeId);
        try {
            pageInfo = ssmServiceOrderService.selectLists(serviceOrderRequest);
        }catch (Exception e){
            log.error("SsmCashController.selectPage:",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setRows(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "添加员工排班上钟", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody List<ServiceOrderRequest> serviceOrderRequest, HttpSession session) {
        GenericResponse<ServiceOrder> genericResponse = new GenericResponse();

        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        for (ServiceOrderRequest orderRequest : serviceOrderRequest) {
            orderRequest.setStoreName(storeName);
            orderRequest.setStoreId(storeId);
        }

        int i = 0;
        try {
            i = ssmServiceOrderService.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;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "修改员工排班上钟顺序", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody ServiceOrderRequest serviceOrderRequest, HttpSession session) {
        GenericResponse<ServiceOrder> genericResponse = new GenericResponse();

        int i = 0;
        try {
            i = ssmServiceOrderService.update(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("/updates")
    public GenericResponse updates(@RequestBody ServiceOrderRequest serviceOrderRequest, HttpSession session) {
        GenericResponse<ServiceOrder> genericResponse = new GenericResponse();
        ServiceOrder serviceOrder = new ServiceOrder();
        BeanUtils.copyProperties(serviceOrderRequest,serviceOrder);

        int i = 0;
        try {
            i = ssmServiceOrderService.updates(serviceOrder);
        }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("/down")
    public GenericResponse down (@RequestBody ServiceOrderRequest serviceOrderRequest, HttpSession session) {
        GenericResponse<ServiceOrder> genericResponse = new GenericResponse();

        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        serviceOrderRequest.setStoreName(storeName);
        serviceOrderRequest.setStoreId(storeId);
        int i = 0;
        try {
            i = ssmServiceOrderService.down(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;
    }

}
