package com.hfkj.openapi.service;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.pay.util.SignatureUtil;
import com.hfkj.config.CommonSysConst;
import com.hfkj.entity.*;
import com.hfkj.model.GasListModel;
import com.hfkj.model.GasModel;
import com.hfkj.model.GasOilPriceModel;
import com.hfkj.openapi.model.GasOilPriceDetail;
import com.hfkj.openapi.model.request.RequestOrderPushModel;
import com.hfkj.openapi.model.response.ResponseOrderPushModel;
import com.hfkj.openapi.model.response.ResponseQueryGasInfoModel;
import com.hfkj.service.agent.BsAgentMerService;
import com.hfkj.service.agent.BsAgentPriceService;
import com.hfkj.service.agent.BsAgentService;
import com.hfkj.service.gas.BsGasClassGroupTaskService;
import com.hfkj.service.gas.BsGasOilGunNoService;
import com.hfkj.service.gas.BsGasOrderService;
import com.hfkj.service.gas.BsGasService;
import com.hfkj.service.merchant.BsMerchantService;
import com.hfkj.service.order.BsOrderService;
import com.hfkj.service.order.OrderPaySuccessService;
import com.hfkj.sysenum.gas.GasOilNoEnum;
import com.hfkj.sysenum.gas.GasOrderReceiptStatusEnum;
import com.hfkj.sysenum.gas.OrderOilStatus;
import com.hfkj.sysenum.merchant.MerchantSourceTypeEnum;
import com.hfkj.sysenum.merchant.MerchantStatusEnum;
import com.hfkj.sysenum.order.OrderStatusEnum;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @className: GasService
 * @author: HuRui
 * @date: 2024/11/12
 **/
@Component("apiGasService")
public class ApiGasService {
    @Resource
    private BsGasService gasService;
    @Resource
    private BsAgentPriceService agentPriceService;
    /**
     * 查询油站详情
     * @param gasNo
     */
    public ResponseQueryGasInfoModel queryGasInfo(Long agentId,String gasNo) {
        // 油站详情
        GasModel gasDetail = gasService.getGasDetail(gasNo);
        if (gasDetail == null) {
            throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.COMMON_ERROR, "未查到加油站信息");
        }
        ResponseQueryGasInfoModel response = new ResponseQueryGasInfoModel();
        response.setGasNo(gasDetail.getMerNo());
        response.setGasName(gasDetail.getMerName());
        response.setProvinceCode(gasDetail.getProvinceCode());
        response.setProvinceName(gasDetail.getProvinceName());
        response.setCityCode(gasDetail.getCityCode());
        response.setCityName(gasDetail.getCityName());
        response.setCountyCode(gasDetail.getAreaCode());
        response.setCountyName(gasDetail.getAreaName());
        response.setGasAddress(gasDetail.getAddress());
        response.setGasAddressLatitude(gasDetail.getLatitude());
        response.setGasAddressLongitude(gasDetail.getLongitude());
        response.setGasLogo(gasDetail.getSourceType().equals(MerchantSourceTypeEnum.type1.getNumber()) ? CommonSysConst.getSysConfig().getDomainName()+"/"+gasDetail.getMerLogo() : gasDetail.getMerLogo());
        response.setGasStatus(gasDetail.getStatus());
        response.setOilPriceList(new ArrayList<>());

        List<GasOilPriceModel> gasOilPrice = gasDetail.getGasOilPrice();
        for (GasOilPriceModel oilPrice : gasOilPrice) {
            // 油品
            GasOilPriceDetail gasOilPriceDetail = new GasOilPriceDetail();
            gasOilPriceDetail.setOilNo(oilPrice.getOilNo());
            gasOilPriceDetail.setOilNoName(oilPrice.getOilNoName());
            gasOilPriceDetail.setOilType(oilPrice.getOilType());
            gasOilPriceDetail.setOilTypeName(oilPrice.getOilTypeName());
            gasOilPriceDetail.setPriceOfficial(oilPrice.getPriceOfficial());
            gasOilPriceDetail.setPriceGun(oilPrice.getPriceGun());
            // 代理商价格
            BigDecimal priceRate = new BigDecimal("100");
            BsAgentPrice agentPrice = agentPriceService.getPrice(agentId, gasDetail.getId(), oilPrice.getOilNo());
            if (agentPrice != null) {
                priceRate = agentPrice.getPriceRate();
            }
            gasOilPriceDetail.setPriceVip(oilPrice.getPriceGun().multiply(priceRate.divide(new BigDecimal("100"))).setScale(2, BigDecimal.ROUND_HALF_UP));
            gasOilPriceDetail.setGunNoList(oilPrice.getGasOilGunNo().stream().map(BsGasOilGunNo::getGunNo).collect(Collectors.toList()));
            response.getOilPriceList().add(gasOilPriceDetail);
        }
        return response;
    }

}
