package com.hai.openApi.service.impl;

import com.hai.dao.ApiProductMapper;
import com.hai.entity.*;
import com.hai.openApi.config.PriceComputeConfig;
import com.hai.openApi.model.ApiMobileProductModel;
import com.hai.openApi.service.ApiMchProductService;
import com.hai.openApi.service.ApiMobileRechargeProductService;
import com.hai.order.type.OrderProductType;
import com.hai.service.OutRechargePriceService;
import org.springframework.stereotype.Service;

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

@Service("apiMobileRechargeProductService")
public class ApiMobileRechargeProductServiceImpl implements ApiMobileRechargeProductService {

    @Resource
    private ApiProductMapper apiProductMapper;

    @Resource
    private ApiMchProductService apiMchProductService;

    @Resource
    private PriceComputeConfig priceComputeConfig;

    @Resource
    private OutRechargePriceService rechargePriceService;

    @Override
    public List<ApiProduct> getApiProductByList() {
        ApiProductExample example = new ApiProductExample();
        example.createCriteria().andStatusEqualTo(101);

        return apiProductMapper.selectByExample(example);
    }

    @Override
    public ApiProduct findMobileProduct(Long id) {
        return null;
    }

    @Override
    public List<ApiMobileProductModel> getMobileProductByList(String mchId) {

        Map<String, Object> mapProduct = new HashMap<>();
        mapProduct.put("mchId" , mchId);
        mapProduct.put("productType" , OrderProductType.PRODUCT_TYPE9.getNumber());

        ApiMchProduct apiMchProducts = apiMchProductService.findByMap(mapProduct);

        List<ApiMobileProductModel> list = new ArrayList<>();

        Map<String, Object> map = new HashMap<>();

        map.put("status", 1);
        map.put("companyId", 22L);

        // 获取话费产品
        List<OutRechargePrice> rechargePrices = rechargePriceService.getListRechargePrice(map);

        for (OutRechargePrice product : rechargePrices) {
            ApiMobileProductModel productModel = new ApiMobileProductModel();
            productModel.setDiscount(apiMchProducts.getDiscount());
            productModel.setProductId(product.getId());
            productModel.setRechargePrice(product.getRechargePrice());
            productModel.setOperatorType(product.getOperatorType());
            productModel.setCostPrice(priceComputeConfig.computationPriceCost(apiMchProducts.getDiscount() , product.getRechargePrice()));
            list.add(productModel);
        }


        return list;
    }
}
