package com.cweb.controller.meal;

import com.alibaba.fastjson.JSONObject;
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.model.ResponseData;
import com.hfkj.service.meal.BsMealShopCartService;
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: BsMealShopCartController
 * @author: HuRui
 * @date: 2023/11/30
 **/
@Controller
@Api(value = "桌台管理")
@RequestMapping(value = "/mealShopCart")
public class BsMealShopCartController {

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

    @Resource
    private BsMealShopCartService mealShopCartService;

    @RequestMapping(value="/addProduct",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "增加产品")
    public ResponseData addProduct(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || StringUtils.isBlank(body.getString("serialNumber"))
                    || body.getLong("productId") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            mealShopCartService.addProduct(body.getString("serialNumber"), body.getLong("productId"), body.getString("userIdentity"));

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

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

    @RequestMapping(value="/reduceProduct",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "减少产品")
    public ResponseData reduceProduct(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || StringUtils.isBlank(body.getString("serialNumber"))
                    || body.getLong("productId") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            mealShopCartService.reduceProduct(body.getString("serialNumber"),body.getLong("productId"),body.getString("userIdentity"));

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

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

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

            mealShopCartService.clear(body.getString("serialNumber"),body.getString("userIdentity"));

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

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

    @RequestMapping(value="/getShopCartProductList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询购物车产品")
    public ResponseData getShopCartProductList(@RequestParam(value = "serialNumber" , required = true) String serialNumber) {
        try {

            Map<String,Object> param = new HashMap<>();
            param.put("serialNumber", serialNumber);
            return ResponseMsgUtil.success(mealShopCartService.getShopCartProductList(param));

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

}
