package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.core.bean.GenericResponse;
import com.ssm.core.bean.PageResponse;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.extendEntity.HairCallEntity;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmHairCallNumService;
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 java.util.ArrayList;
import java.util.List;

@Api(value="叫号controller",tags={"叫号相关接口"})
@RestController
@RequestMapping("/demay/ssm/HairCall")
@Slf4j
public class SsmHairCallNumberController {
    @Autowired
    private SsmHairCallNumService ssmHairCallNumService;


    @ApiOperation(value = "查询号码列表  (美发小程序-排队叫号-查询号码列表(包括已叫号 未叫号 过号))",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/selectList")
    public GenericResponse selectList(@RequestBody HairCallEntity callEntity){
        GenericResponse genericResponse = new GenericResponse<>();
        List<HairCallEntity> list = new ArrayList<>();
        try {
            list = ssmHairCallNumService.selectList(callEntity);
        }catch (Exception e){
            log.error("SsmCallNumberController.selectList:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "查询当前叫号的号码  (美发小程序-排队叫号-查询当前叫号的号码)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/selectNum")
    public GenericResponse selectNum(@RequestBody HairCallEntity callEntity){
        GenericResponse genericResponse = new GenericResponse<>();
        int num = 0;
        try {
            num = ssmHairCallNumService.selectNum(callEntity);
        }catch (Exception e){
            log.error("SsmCallNumberController.selectNum:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setData(num);
        return genericResponse;
    }

    @ApiOperation(value = "取号 (美发小程序-排队叫号-取号)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/addNumber")
    public GenericResponse addNumber(@RequestBody HairCallEntity callEntity){
        GenericResponse<HairCallEntity> genericResponse = new GenericResponse<>();
        HairCallEntity hairCallEntity = new HairCallEntity();
        try {
            hairCallEntity = ssmHairCallNumService.addNum(callEntity);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmCallNumberController.addNumber:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setData(hairCallEntity);
        return genericResponse;
    }

    @ApiOperation(value = "叫号 (美发小程序-排队叫号-叫号)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/callNumber")
    public GenericResponse callNumber(@RequestBody HairCallEntity callEntity){
        GenericResponse<HairCallEntity> genericResponse = new GenericResponse<>();
        HairCallEntity hairCallEntity = new HairCallEntity();
        try {
             hairCallEntity = ssmHairCallNumService.callNum(callEntity);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmCallNumberController.callNumber:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if(hairCallEntity.getState() == 1) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
            genericResponse.setData(hairCallEntity);
        }else {
            genericResponse.setCodeAndMsg(ResponseCode.DATA_DOES_NOT_EXISTS_S, "有效排队号码");
        }
        return genericResponse;
    }

    @ApiOperation(value = "过号 (美发小程序-排队叫号-过号)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/passNumber")
    public GenericResponse passNumber(@RequestBody HairCallEntity callEntity){
        GenericResponse<HairCallEntity> genericResponse = new GenericResponse<>();

        try {
            ssmHairCallNumService.passNum(callEntity);
        }catch (Exception e){
            log.error("SsmCallNumberController.passNumber:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "服务完成后删号 (美发小程序-排队叫号-服务完成后删号)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/deleteNumber")
    public GenericResponse deleteNumber(@RequestBody HairCallEntity callEntity){
        GenericResponse<HairCallEntity> genericResponse = new GenericResponse<>();

        try {
            ssmHairCallNumService.deleteCallNum(callEntity);
        }catch (Exception e){
            log.error("SsmCallNumberController.deleteNumber:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "服务单据号关联叫号号码 (美发小程序-排队叫号-服务单据号关联叫号号码)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/connectNum")
    public GenericResponse connectNum(@RequestBody HairCallEntity callEntity){
        GenericResponse<HairCallEntity> genericResponse = new GenericResponse<>();

        try {
            ssmHairCallNumService.connectNum(callEntity);
        }catch (Exception e){
            log.error("SsmCallNumberController.connectNum:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "过号号码重新叫号 (美发小程序-排队叫号-过号号码重新叫号)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/passCallNum")
    public GenericResponse passCallNum(@RequestBody HairCallEntity callEntity){
        GenericResponse<HairCallEntity> genericResponse = new GenericResponse<>();

        try {
            ssmHairCallNumService.passCallNum(callEntity);
        }catch (Exception e){
            log.error("SsmCallNumberController.passCallNum:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "等待服务号 (美发小程序-排队叫号-等待服务号)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/nextWaitNum")
    public GenericResponse nextWaitNum(@RequestBody HairCallEntity callEntity){
        GenericResponse<HairCallEntity> genericResponse = new GenericResponse<>();
        HairCallEntity hairCallEntity = new HairCallEntity();
        try {
            hairCallEntity = ssmHairCallNumService.nextWaitNum(callEntity);
        }catch (Exception e){
            log.error("SsmCallNumberController.nextWaitNum:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setData(hairCallEntity);
        return genericResponse;
    }

}
