package com.cweb.controller.meal;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsMealTable;
import com.hfkj.model.ResponseData;
import com.hfkj.service.meal.BsMealTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 扫码点餐 桌台管理
 * @className: BsMealTableController
 * @author: HuRui
 * @date: 2023/11/29
 **/
@Controller
@Api(value = "桌台管理")
@RequestMapping(value = "/mealTable")
public class BsMealTableController {

    Logger log = LoggerFactory.getLogger(BsMealTableController.class);

    @Resource
    private BsMealTableService mealTableService;

    @RequestMapping(value="/generateTable",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "生成桌台")
    public ResponseData generateTable(@RequestBody JSONObject body) {
        try {
            if (body == null || body.getLong("storeId") == null || body.getInteger("generateNum") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            mealTableService.generateTable(body.getLong("storeId"),body.getInteger("generateNum"));

            return ResponseMsgUtil.success("生成成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/editTable",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑桌台")
    public ResponseData editTable(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || StringUtils.isBlank(body.getString("serialNumber"))
                    || body.getInteger("tableNumber") == null
                    || StringUtils.isBlank(body.getString("tableName"))
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 查询桌台
            BsMealTable mealTable = mealTableService.getTableBySerialNumber(body.getString("serialNumber"));
            if (mealTable == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的桌台");
            }
            mealTable.setTableNumber(body.getInteger("tableNumber"));
            mealTable.setTableName(body.getString("tableName"));
            mealTableService.editData(mealTable);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/delTable",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除桌台")
    public ResponseData delTable(@RequestBody JSONObject body) {
        try {
            if (body == null || StringUtils.isBlank(body.getString("serialNumber"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            mealTableService.delTable(body.getString("serialNumber"));

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getTable",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取桌台详情")
    public ResponseData getTable(@RequestParam(value = "serialNumber" , required = true) String serialNumber) {
        try {

            return ResponseMsgUtil.success(mealTableService.getTableBySerialNumber(serialNumber));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getTableList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "商户查询门店列表")
    public ResponseData getTableList(@RequestParam(value = "storeId" , required = true) Long storeId,
                                     @RequestParam(value = "tableName" , required = false) String tableName,
                                     @RequestParam(value = "pageNum" , required = true) Integer pageNum,
                                     @RequestParam(value = "pageSize" , required = true) Integer pageSize) {
        try {

            Map<String,Object> param = new HashMap<>();
            param.put("storeId", storeId);
            param.put("tableName", tableName);

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(mealTableService.getTableList(param)));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }



    @RequestMapping(value="/startTable",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "开台")
    public ResponseData startTable(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || StringUtils.isBlank(body.getString("serialNumber"))
                    || body.getInteger("peopleNum") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            mealTableService.startTable(body.getString("serialNumber"),body.getInteger("peopleNum"));

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }


    @RequestMapping(value="/endTable",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "关台")
    public ResponseData endTable(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || StringUtils.isBlank(body.getString("serialNumber"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            mealTableService.endTable(body.getString("serialNumber"));

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }
}
