package com.cweb.controller.order;

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.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.model.ResponseData;
import com.hfkj.service.gas.BsGasOrderReceiptService;
import com.hfkj.service.gas.BsGasOrderService;
import com.hfkj.service.user.BsUserReceiptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "/gasOrderReceipt")
@Api(value = "加油订单开票业务")
public class GasOrderReceiptController {
    private static Logger log = LoggerFactory.getLogger(GasOrderReceiptController.class);
    @Resource
    private BsGasOrderReceiptService gasOrderReceiptService;

    @RequestMapping(value="/applyReceipt",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "申请收据")
    public ResponseData applyReceipt(@RequestBody JSONObject body) {
        try {
            if (body == null
                || body.getLong("userReceiptId") == null
                || body.getJSONArray("orderNo") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            List<String> orderNoList = body.getJSONArray("orderNo")
                    .stream().map(Object::toString)
                    .collect(Collectors.toList());

            gasOrderReceiptService.applyReceipt(body.getLong("userReceiptId"), orderNoList);

            return ResponseMsgUtil.success("申请成功");

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

}
