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.config.SpPrinterTemplate;
import com.hfkj.entity.BsMealTableOrder;
import com.hfkj.entity.BsMealTableOrderChildProduct;
import com.hfkj.entity.BsMealTableOrderProduct;
import com.hfkj.model.ResponseData;
import com.hfkj.service.meal.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: BsMealShopCartController
 * @author: HuRui
 * @date: 2023/11/30
 **/
@Controller
@Api(value = "桌台订单管理")
@RequestMapping(value = "/mealTableOrder")
public class BsMealTableOrderController {

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

    @Resource
    private BsMealTableOrderChildService mealTableOrderChildService;
    @Resource
    private BsMealTableOrderService mealTableOrderService;
    @Resource
    private BsMealTableOrderProductService mealTableOrderProductService;
    @Resource
    private BsMealTableOrderChildProductService mealTableOrderChildProductService;

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

            mealTableOrderChildService.createChildOrder(body.getString("serialNumber"), body.getString("remark"), body.getString("userIdentity"));

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

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


    @RequestMapping(value="/operateOrderProduct",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "操作订单产品")
    public ResponseData operateOrderProduct(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || StringUtils.isBlank(body.getString("orderNo"))
                    || body.getJSONArray("productArray") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            for (Object obj : body.getJSONArray("productArray")) {
                JSONObject product = (JSONObject) obj;
                if (product == null || product.getLong("productId") == null || product.getInteger("number") == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
            }

            mealTableOrderService.operateOrderProduct(body.getString("orderNo"), body.getJSONArray("productArray"));

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

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

    @RequestMapping(value="/queryOrderDetail",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询订单详情")
    public ResponseData queryOrderDetail(@RequestParam(value = "orderNo" , required = true) String orderNo) {
        try {
            // 查询桌台当前订单
            BsMealTableOrder tableOrder = mealTableOrderService.getTableOrderByOrderNo(orderNo);
            if (tableOrder == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的订单号");
            }
            Map<String, Object> map = new HashMap<>();
            map.put("order", tableOrder);

            Map<String,Object> param = new HashMap<>();
            param.put("orderNo", tableOrder.getOrderNo());
            map.put("product", mealTableOrderProductService.getOrderProductList(param));
            return ResponseMsgUtil.success(map);

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

    @RequestMapping(value="/queryCurrentOrder",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询桌台当前订单")
    public ResponseData queryCurrentOrder(@RequestParam(value = "serialNumber" , required = true) String serialNumber) {
        try {
            // 查询桌台当前订单
            BsMealTableOrder tableOrder = mealTableOrderService.getCurrentTableOrder(serialNumber);
            if (tableOrder == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "当前桌台未开台");
            }
            Map<String, Object> map = new HashMap<>();
            map.put("order", tableOrder);

            Map<String,Object> param = new HashMap<>();
            param.put("orderNo", tableOrder.getOrderNo());
            map.put("product", mealTableOrderProductService.getOrderProductList(param));
            return ResponseMsgUtil.success(map);

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


    @RequestMapping(value="/queryOrderList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询订单列表")
    public ResponseData queryOrderList(@RequestParam(value = "storeId" , required = true) String storeId,
                                       @RequestParam(value = "serialNumber" , required = false) String serialNumber,
                                       @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("serialNumber", serialNumber);

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

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


}


