package com.cweb.controller.hlt;

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.OrderUtil;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.model.ResponseData;
import com.hfkj.service.cms.CmsContentService;
import com.hfkj.service.hlt.HltCanteenService;
import com.hfkj.service.hlt.HuiLianTongUnionCardService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
@RequestMapping(value="/hltCanteen")
@Api(value="汇联通食堂接口")
public class HltCanteenController {

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


    @RequestMapping(value="/canteenConsume",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "食堂消费")
    public ResponseData canteenConsume(@RequestParam(value = "cardNo" , required = false) String cardNo,
                                       @RequestParam(value = "instCode" , required = false) String instCode,
                                       @RequestParam(value = "canteenId" , required = false) String canteenId) {
        try {

            String orderNo = OrderUtil.generateOrderNo();

            JSONObject info = HltCanteenService.canteenConsume(orderNo , cardNo , instCode , canteenId);
            if (StringUtils.isBlank(info.getString("data"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请求失败");
            }
            return ResponseMsgUtil.success(HuiLianTongUnionCardService.resolveResponse(info.getString("data")));

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

    @RequestMapping(value="/queryCanteenOrder",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "食堂消费记录")
    public ResponseData queryCanteenOrder(@RequestParam(value = "cardNo" , required = false) String cardNo) {
        try {

            JSONObject info = HltCanteenService.queryCanteenOrder(cardNo);
            if (StringUtils.isBlank(info.getString("data"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请求失败");
            }
            return ResponseMsgUtil.success(HuiLianTongUnionCardService.resolveResponse(info.getString("data")));

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

}
