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.MemberOrder;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.extendEntity.CallEntity;
import com.ssm.manage.query.CashListRequest;
import com.ssm.manage.query.MemberOrderRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmCallNumberService;
import com.ssm.manage.service.SsmMemberOrderService;
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.ArrayList;
import java.util.List;

@Api(value="叫号controller",tags={"叫号相关接口"})
@RestController
@RequestMapping("/demay/ssm/call")
@Slf4j
public class SsmCallNumberController {
    @Autowired
    private SsmCallNumberService ssmCallNumberService;

    @Autowired
    private SsmMemberOrderService ssmMemberOrderService;

    @ApiOperation(value = "查询排队取号列表  (门店端-排队叫号-查询排队取号列表)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/list")
    public GenericResponse selectList(@RequestBody CallEntity callEntity){
        GenericResponse<CallEntity> genericResponse = new GenericResponse<>();
        List<CallEntity> list = new ArrayList<>();
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        callEntity.setStoreId(storeId);
//        callEntity.setStoreName(storeName);
        try {
            list = ssmCallNumberService.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("/addNumber")
    public GenericResponse addNumber(@RequestBody CallEntity callEntity){
        GenericResponse<CallEntity> genericResponse = new GenericResponse<>();
        int i = 0;
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        callEntity.setStoreId(storeId);
//        callEntity.setStoreName(storeName);
        try {
             i = ssmCallNumberService.addNumber(callEntity);
        }catch (Exception e){
            log.error("SsmCallNumberController.addNumber:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "叫号 (门店端-排队叫号-叫号)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/callNumber")
    public GenericResponse callNumber(@RequestBody CallEntity callEntity){
        GenericResponse<CallEntity> genericResponse = new GenericResponse<>();

//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        callEntity.setStoreId(storeId);
//        callEntity.setStoreName(storeName);
        try {
             ssmCallNumberService.callNumber(callEntity);
        }catch (Exception e){
            log.error("SsmCallNumberController.callNumber:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);

        return genericResponse;
    }


    @ApiOperation(value = "开单 (门店端-排队叫号-开单)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/addCashList")
    public GenericResponse addCashList(@RequestBody List<CashListRequest> listRequests){
        GenericResponse<CallEntity> genericResponse = new GenericResponse<>();
        Long storeId = listRequests.get(0).getStoreId();
        String storeName = listRequests.get(0).getStoreName();
        int i = 0;
        try {
            i = ssmCallNumberService.addCashList(listRequests,storeId,storeName);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmCallNumberController.addCashList:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "分页查询会员预约列表  (门店端-预约叫号-分页查询会员预约列表)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody MemberOrderRequest memberOrderRequest){
        PageResponse<MemberOrder> pageResponse = new PageResponse<>();
        PageInfo<MemberOrder> pageInfo = new PageInfo<>();
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        memberOrderRequest.setStoreId(storeId);
//        memberOrderRequest.setStoreName(storeName);
        try {
            pageInfo = ssmMemberOrderService.selectPage(memberOrderRequest);
        }catch (Exception e){
            log.error("SsmCallNumberController.selectPage:",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

}
