package com.xhpc.tradebill.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelWriter;
import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.utils.poi.ExcelUtil;
import com.xhpc.tradebill.domain.XhpcHistoryOrderDomain;
import com.xhpc.tradebill.domain.XhpcTradebillInternetCheckRecordDomain;
import com.xhpc.tradebill.mapper.EtOrderMappingMapper;
import com.xhpc.tradebill.mapper.XhpcHistoryOrderMapper;
import com.xhpc.tradebill.mapper.XhpcTradebillInternetCheckRecordMapper;
import com.xhpc.tradebill.service.InternetBillService;
import com.xhpc.tradebill.utils.DownloadUtil;
import com.xhpc.tradebill.vo.HDTradebillVo;
import com.xhpc.tradebill.vo.KDTradebillVo;
import com.xhpc.tradebill.vo.XDTTradebillVo;
import com.xhpc.tradebill.vo.XJTradeBillVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.*;


@Slf4j
@Service
public class InternetBillServiceImpl implements InternetBillService {

    @Resource
    XhpcTradebillInternetCheckRecordMapper internetCheckRecordMapper;

    @Resource
    XhpcHistoryOrderMapper historyOrderMapper;

    @Resource
    EtOrderMappingMapper orderMappingMapper;



    @Override
    public XhpcTradebillInternetCheckRecordDomain getInfoByPk(int pk){
        return internetCheckRecordMapper.selectByPrimaryKey(pk);
    }

    @Override
    public boolean updateInfoByDomain(XhpcTradebillInternetCheckRecordDomain domain){
        domain.setUpdateBy(SecurityUtils.getUserId());
        return internetCheckRecordMapper.updateStatusByPk(domain);
    }


    @Override
    public Integer insertInternetCheckList(int uploadId, List<XhpcTradebillInternetCheckRecordDomain> internetCheckRecordDomainList){
        Long userId = SecurityUtils.getUserId();
        for(XhpcTradebillInternetCheckRecordDomain domain: internetCheckRecordDomainList){
            domain.setUploadId(uploadId);
            domain.setCreateBy(userId);
            domain.setUpdateBy(userId);
        }
        return internetCheckRecordMapper.insertCheckList(internetCheckRecordDomainList);
    }


    @Override
    public List<XhpcTradebillInternetCheckRecordDomain> getInternetCheckList(Map<String, Object> params){
        return internetCheckRecordMapper.selectListByParams(params);
    }

    @Override
    public  Map<String, Object> getStatusTotal(Map<String, Object> params){
        List<Map<String, Object>> resultList = internetCheckRecordMapper.selectTotalGroupbyStatus(params);
        int successCount = 0;
        int failCount = 0;
        int uncheckCount = 0;
        int checkCount = 0;
        for(Map<String, Object> res : resultList){
            switch (res.get("status").toString()){
                case "0":
                    uncheckCount = Integer.parseInt(res.get("total").toString()); break;
                case "1":
                    failCount = Integer.parseInt(res.get("total").toString()); break;
                case "2":
                    successCount = Integer.parseInt(res.get("total").toString()); break;
                default: break;
            }
        }
        checkCount = successCount + failCount;
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("successCount", successCount);
        resultMap.put("failCount", failCount);
        resultMap.put("uncheckCount", uncheckCount);
        resultMap.put("checkCount", checkCount);
        return resultMap;
    }


    /**
     * 获取恒大对账数据
     * @param fileUrl 文件访问地址
     * @return  对账数据
     * @throws Exception 异常
     */
    @Override
    public List<XhpcTradebillInternetCheckRecordDomain> getHdInternetDataList(String fileUrl) throws Exception {
        File dataFile = DownloadUtil.downloadFile(fileUrl);
        return getHdInternetDataList(dataFile);
    }


    /**
     * 获取小桔对账数据
     * @param fileUrl 文件访问地址
     * @return  对账数据
     * @throws Exception 异常
     */
    @Override
    public List<XhpcTradebillInternetCheckRecordDomain> getXjInternetDataList(String fileUrl) throws Exception {
        File dataFile = DownloadUtil.downloadFile(fileUrl);
        return getXjInternetDataList(dataFile);
    }


    /**
     * 获取新电途对账数据
     * @param fileUrl 文件访问地址
     * @return  对账数据
     * @throws Exception 异常
     */
    @Override
    public List<XhpcTradebillInternetCheckRecordDomain> getXDTInternetDataList(String fileUrl) throws Exception {
        File dataFile = DownloadUtil.downloadFile(fileUrl);
        return getXDTInternetDataList(dataFile);
    }


    /**
     * 获取快电对账数据
     * @param fileUrl 文件访问地址
     * @return  对账数据
     * @throws Exception 异常
     */
    @Override
    public List<XhpcTradebillInternetCheckRecordDomain> getKDInternetDataList(String fileUrl) throws Exception {
        File dataFile = DownloadUtil.downloadFile(fileUrl);
        return getKDInternetDataList(dataFile);
    }

    /**
     * 获取恒大对账数据
     * @param dataFile 对账单文件,格式为xlsx
     * @return 对账数据
     */
    @Override
    public List<XhpcTradebillInternetCheckRecordDomain> getHdInternetDataList(File dataFile) throws Exception {
        List<XhpcTradebillInternetCheckRecordDomain> checkRecordDomainList = new ArrayList<>();
        String suffix = dataFile.getName().substring(dataFile.getName().lastIndexOf(".") + 1);
        List<HDTradebillVo> hdTradebillVoList = new ArrayList<>();

        List<String> serialNumberList = new ArrayList<>();
        switch (suffix.toUpperCase()){
            case "XLS":
            case "XLSX":
                ExcelUtil<HDTradebillVo> util = new ExcelUtil<HDTradebillVo>(HDTradebillVo.class);
                hdTradebillVoList = util.importExcel(new FileInputStream(dataFile));
                break;
            case "CSV":
                CsvReader reader = CsvUtil.getReader();
                FileReader fileReader = new FileReader(dataFile.getAbsolutePath(), Charset.forName("GBK"));
                hdTradebillVoList = reader.read(fileReader.readString(), HDTradebillVo.class);
                break;
            default:break;
        }

        for(HDTradebillVo vo: hdTradebillVoList){
            XhpcTradebillInternetCheckRecordDomain domain = new XhpcTradebillInternetCheckRecordDomain();
            domain.setPlatformInternetSerialNumber(vo.getOrderNo().trim());
            domain.setInternetSerialNumber(vo.getOrderNo().trim());
            domain.setSource("恒大");
            domain.setInternetName("恒大");
            domain.setInternetChargeStation(vo.getStationName());
            BigDecimal chargingDegree = vo.getChargeDegree()!=null ? BigDecimal.valueOf(vo.getChargeDegree()) : BigDecimal.ZERO;
            domain.setInternetChargingDegree(chargingDegree);

            BigDecimal orderAmount = vo.getOrderTotalAmount() !=null? BigDecimal.valueOf(vo.getOrderTotalAmount()): BigDecimal.ZERO;
            domain.setInternetOrderAmount(orderAmount);

            BigDecimal powerAmount = vo.getPowerAmount()!=null? BigDecimal.valueOf(vo.getPowerAmount()): BigDecimal.ZERO;
            domain.setInternetPowerAmount(powerAmount);

            BigDecimal serverAmount = vo.getServerAmount()!= null? BigDecimal.valueOf(vo.getServerAmount()): BigDecimal.ZERO;
            domain.setInternetServerAmount(serverAmount);
            domain.setInternetDiscountAmount(BigDecimal.ZERO);

            BigDecimal payAmount = vo.getOrderTotalAmount()!=null ? BigDecimal.valueOf(vo.getOrderTotalAmount()): BigDecimal.ZERO;
            domain.setInternetPayAmount(payAmount);

            serialNumberList.add(domain.getInternetSerialNumber());

//            getHistoryOrderInfo(domain);
            checkRecordDomainList.add(domain);
        }
        getHistoryOrderList(checkRecordDomainList, serialNumberList);

        return checkRecordDomainList;
    }


    /**
     * 获取小桔对账数据
     * @param dataFile 对账单文件,格式为xlsx
     * @return 对账数据
     */
    @Override
    public List<XhpcTradebillInternetCheckRecordDomain> getXjInternetDataList(File dataFile) throws Exception {

        String suffix = dataFile.getName().substring(dataFile.getName().lastIndexOf(".") + 1);
        List<XhpcTradebillInternetCheckRecordDomain> checkRecordDomainList = new ArrayList<>();
        List<XJTradeBillVo> xjTradeBillVoList = new ArrayList<>();

        List<String> serialNumberList = new ArrayList<>();
        switch (suffix.toUpperCase()){
            case "XLS":
            case "XLSX":
                ExcelUtil<XJTradeBillVo> util = new ExcelUtil<XJTradeBillVo>(XJTradeBillVo.class);
                xjTradeBillVoList = util.importExcel(new FileInputStream(dataFile));
                break;
            case "CSV":
                CsvReader reader = CsvUtil.getReader();
                FileReader fileReader = new FileReader(dataFile.getAbsolutePath(), Charset.forName("GBK"));
                xjTradeBillVoList = reader.read(fileReader.readString(), XJTradeBillVo.class);
                break;
            default: break;
        }

        for(XJTradeBillVo vo: xjTradeBillVoList){
            XhpcTradebillInternetCheckRecordDomain domain = new XhpcTradebillInternetCheckRecordDomain();
            domain.setPlatformInternetSerialNumber(vo.getOrderNo().trim());
            domain.setInternetSerialNumber(vo.getOrderNo().trim());
            domain.setSource("小桔");
            domain.setInternetName("小桔");
            domain.setInternetChargeStation(vo.getStationName());
            domain.setInternetStartSoc(vo.getStartSoc());
            domain.setInternetEndSoc(vo.getEndSoc());
            BigDecimal chargingDegree = vo.getChargingDegree()!=null ? BigDecimal.valueOf(vo.getChargingDegree()): BigDecimal.ZERO;
            domain.setInternetChargingDegree(chargingDegree);
            domain.setInternetChargingTime(vo.getChargingTime().toString());
            domain.setInternetStartTime(vo.getChargingStartTime());
            domain.setInternetEndTime(vo.getChargingEndTime());

            BigDecimal orderAmount = vo.getBillAmount() !=null? BigDecimal.valueOf(vo.getBillAmount()): BigDecimal.ZERO;
            domain.setInternetOrderAmount(orderAmount);

            BigDecimal powerAmount = vo.getOrderPowerAmount()!=null? BigDecimal.valueOf(vo.getOrderPowerAmount()): BigDecimal.ZERO;
            domain.setInternetPowerAmount(powerAmount);

            BigDecimal serverAmount = vo.getOrderServerAmount()!= null? BigDecimal.valueOf(vo.getOrderServerAmount()): BigDecimal.ZERO;
            domain.setInternetServerAmount(serverAmount);
            domain.setInternetDiscountAmount(BigDecimal.ZERO);

            BigDecimal payAmount = vo.getOrderTotalAmount()!=null ? BigDecimal.valueOf(vo.getOrderTotalAmount()): BigDecimal.ZERO;
            domain.setInternetPayAmount(payAmount);

            serialNumberList.add(domain.getInternetSerialNumber());
//            getHistoryOrderInfo(domain);
//
            checkRecordDomainList.add(domain);
        }
        getHistoryOrderList(checkRecordDomainList, serialNumberList);
        return checkRecordDomainList;
    }


    /**
     * 获取新电途对账数据
     * @param dataFile 对账单文件,格式为xlsx
     * @return 对账数据
     */
    @Override
    public List<XhpcTradebillInternetCheckRecordDomain> getXDTInternetDataList(File dataFile) throws Exception {
        String suffix = dataFile.getName().substring(dataFile.getName().lastIndexOf(".") + 1);
        List<XhpcTradebillInternetCheckRecordDomain> checkRecordDomainList = new ArrayList<>();
        List<XDTTradebillVo> xdtTradebillVoList = new ArrayList<>();

        List<String> serialNumberList = new ArrayList<>();
        switch (suffix.toUpperCase()){
            case "XLS":
            case "XLSX":
                ExcelUtil<XDTTradebillVo> util = new ExcelUtil<XDTTradebillVo>(XDTTradebillVo.class);
                xdtTradebillVoList = util.importExcel(new FileInputStream(dataFile));
                break;
            case "CSV":
                CsvReader reader = CsvUtil.getReader();
                FileReader fileReader = new FileReader(dataFile.getAbsolutePath(), Charset.forName("GBK"));
                xdtTradebillVoList = reader.read(fileReader.readString(), XDTTradebillVo.class);
                break;
            default: break;
        }

        for(XDTTradebillVo vo: xdtTradebillVoList){
            XhpcTradebillInternetCheckRecordDomain domain = new XhpcTradebillInternetCheckRecordDomain();
            domain.setPlatformInternetSerialNumber(vo.getOrderNo().trim());
            domain.setInternetSerialNumber(vo.getOrderNo().trim());
            domain.setSource("新电途");
            domain.setInternetName("新电途");
            domain.setInternetChargeStation(vo.getStationName());

            BigDecimal chargingDegree = vo.getChargingDegree()!=null? BigDecimal.valueOf(vo.getChargingDegree()): BigDecimal.ZERO;
            domain.setInternetChargingDegree(chargingDegree);

            BigDecimal orderAmount = vo.getOrderAmount() !=null? BigDecimal.valueOf(vo.getOrderAmount()): BigDecimal.ZERO;
            domain.setInternetOrderAmount(orderAmount);

            BigDecimal powerAmount = vo.getPowerAmount()!=null? BigDecimal.valueOf(vo.getPowerAmount()): BigDecimal.ZERO;
            domain.setInternetPowerAmount(powerAmount);

            BigDecimal serverAmount = vo.getServerAmount()!= null? BigDecimal.valueOf(vo.getServerAmount()): BigDecimal.ZERO;
            domain.setInternetServerAmount(serverAmount);
            domain.setInternetDiscountAmount(BigDecimal.ZERO);

            BigDecimal payAmount = vo.getPayAmount()!=null ? BigDecimal.valueOf(vo.getPayAmount()): BigDecimal.ZERO;
            domain.setInternetPayAmount(payAmount);

            serialNumberList.add(domain.getInternetSerialNumber());
//            getHistoryOrderInfo(domain);
            checkRecordDomainList.add(domain);
        }
        getHistoryOrderList(checkRecordDomainList, serialNumberList);
        return checkRecordDomainList;
    }

    /**
     * 获取快电对账数据
     * @param dataFile 对账单文件,格式为xlsx
     * @return 对账数据
     */
    @Override
    public  List<XhpcTradebillInternetCheckRecordDomain> getKDInternetDataList(File dataFile) throws Exception {
        String suffix = dataFile.getName().substring(dataFile.getName().lastIndexOf(".") + 1);
        List<XhpcTradebillInternetCheckRecordDomain> checkRecordDomainList = new ArrayList<>();
        List<KDTradebillVo> kdTradebillVoList = new ArrayList<>();
        List<Map<String, Object>> dataList = new ArrayList<>();

        List<String> serialNumberList = new ArrayList<>();
        switch (suffix.toUpperCase()){
            case "XLS":
            case "XLSX":
                ExcelUtil<KDTradebillVo> util = new ExcelUtil<KDTradebillVo>(KDTradebillVo.class);
                kdTradebillVoList = util.importExcel(new FileInputStream(dataFile), 1);

                break;
            default: break;
        }


        for(KDTradebillVo vo: kdTradebillVoList){
            if (vo.getOrderNo().equals("交易订单号") || StrUtil.hasBlank(vo.getOrderNo())) {
                continue;
            }
            XhpcTradebillInternetCheckRecordDomain domain = new XhpcTradebillInternetCheckRecordDomain();
            domain.setSource("快电");
            domain.setInternetName("快电");
            domain.setInternetSerialNumber(vo.getOrderNo());
            domain.setPlatformInternetSerialNumber(vo.getOrderNo());

            Date startTime = StrUtil.hasBlank(vo.getStartTime()) ? null: DateUtil.parse(vo.getStartTime());
            domain.setInternetStartTime(startTime);

            Date endTime = StrUtil.hasBlank(vo.getEndTime()) ? null: DateUtil.parse(vo.getEndTime());
            domain.setInternetEndTime(endTime);

            domain.setInternetChargeStation(vo.getStationName());

            BigDecimal chargingDegree = vo.getChargingDegree() == null ? BigDecimal.ZERO: BigDecimal.valueOf(vo.getChargingDegree());
            domain.setInternetChargingDegree(chargingDegree);

            BigDecimal powerAmount= vo.getTotalPowerAmount() == null? BigDecimal.ZERO: BigDecimal.valueOf(vo.getTotalPowerAmount());
            domain.setInternetPowerAmount(powerAmount);

            BigDecimal serverAmount =  vo.getServerAmount() == null ? BigDecimal.ZERO : BigDecimal.valueOf(vo.getServerAmount());
            domain.setInternetServerAmount(serverAmount);

            domain.setInternetDiscountAmount(BigDecimal.ZERO);

            String orderAmount = vo.getOrderAmount() == null? "0": vo.getOrderAmount().toString();
            domain.setInternetOrderAmount(new BigDecimal(orderAmount));
            domain.setInternetPayAmount(domain.getInternetOrderAmount());

            serialNumberList.add(domain.getInternetSerialNumber());
            checkRecordDomainList.add(domain);
        }
        getHistoryOrderList(checkRecordDomainList, serialNumberList);
        return checkRecordDomainList;
    }

    /**
     * 导出对账单
     * @param response 返回体
     * @param params 查询参数
     * @throws Exception 异常
     */
    @Override
    public void export(HttpServletResponse response, Map<String, Object> params) throws Exception{
        List<Map<String, Object>> list = internetCheckRecordMapper.selectByParams(params);

// 通过工具类创建writer，默认创建xls格式
        ExcelWriter writer = cn.hutool.poi.excel.ExcelUtil.getWriter(true);
        writer.addHeaderAlias("internet_name", "流量方名称");
        writer.addHeaderAlias("user_id", "用户账号");

        writer.addHeaderAlias("platform_order_number", "平台-订单编号");
        writer.addHeaderAlias("platform_charge_station", "平台-充电场站名称");
        writer.addHeaderAlias("platform_start_soc", "平台-开始SOC");
        writer.addHeaderAlias("platform_end_soc", "平台-结束SOC");
        writer.addHeaderAlias("platform_charging_degree", "平台-充电电量");
        writer.addHeaderAlias("platform_charging_time", "平台-充电时长");
        writer.addHeaderAlias("platform_start_time", "平台-开始充电时间");
        writer.addHeaderAlias("platform_end_time", "平台-结束充电时间");
        writer.addHeaderAlias("platform_order_amount", "平台-订单总金额");
        writer.addHeaderAlias("platform_power_amount", "平台-电费金额");
        writer.addHeaderAlias("platform_server_amount", "平台-服务费金额");
        writer.addHeaderAlias("platform_discount_amount", "平台-折扣费用");
        writer.addHeaderAlias("platform_pay_amount", "平台-实际支付金额");

        writer.addHeaderAlias("internet_order_number", "流量方-订单编号");
        writer.addHeaderAlias("internet_charge_station", "流量方-充电场站名称");
        writer.addHeaderAlias("internet_start_soc", "流量方-开始SOC");
        writer.addHeaderAlias("internet_end_soc", "流量方-结束SOC");
        writer.addHeaderAlias("internet_charging_degree", "流量方-充电电量");
        writer.addHeaderAlias("internet_charging_time", "流量方-充电时长");
        writer.addHeaderAlias("internet_start_time", "流量方-开始充电时间");
        writer.addHeaderAlias("internet_end_time", "流量方-结束充电时间");
        writer.addHeaderAlias("internet_order_amount", "流量方-订单总金额");
        writer.addHeaderAlias("internet_power_amount", "流量方-电费金额");
        writer.addHeaderAlias("internet_server_amount", "流量方-服务费金额");
        writer.addHeaderAlias("internet_discount_amount", "流量方-折扣费用");
        writer.addHeaderAlias("internet_pay_amount", "流量方-实际支付金额");

        writer.addHeaderAlias("status", "对账状态（0-待对账，1-对账错误，2-对账正确）");
        writer.addHeaderAlias("remark", "备注");

        writer.setOnlyAlias(true);
        writer.write(list, true);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        ServletOutputStream out = response.getOutputStream();

        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
    }



    private void getHistoryOrderInfo(XhpcTradebillInternetCheckRecordDomain domain){
        XhpcHistoryOrderDomain orderDomain = historyOrderMapper.findOneByInternetSerialNumber(domain.getInternetSerialNumber());
        if(orderDomain != null){
            domain.setHistoryOrderNo(orderDomain.getHistoryOrderId());
            domain.setPlatformInternetSerialNumber(orderDomain.getInternetSerialNumber());
            domain.setPlatformSerialNumber(orderDomain.getSerialNumber());
            domain.setPlatformChargeStation(orderDomain.getChargingStationName());
            domain.setPlatformStartSoc(orderDomain.getStartSoc());
            domain.setPlatformEndSoc(orderDomain.getEndSoc());
            BigDecimal chargingDegree = orderDomain.getChargingDegree() != null?
                    BigDecimal.valueOf(orderDomain.getChargingDegree()): BigDecimal.ZERO;
            domain.setPlatformChargingDegree(chargingDegree);
            domain.setPlatformChargingTime(orderDomain.getChargingTime());
            domain.setPlatformStartTime(orderDomain.getStartTime());
            domain.setPlatformEndTime(orderDomain.getEndTime());
            domain.setPlatformOrderAmount(orderDomain.getTotalPrice());
            domain.setPlatformPowerAmount(orderDomain.getPowerPriceTotal());
            domain.setPlatformServerAmount(orderDomain.getServicePriceTotal());
            domain.setPlatformDiscountAmount(orderDomain.getPromotionDiscount());
            domain.setPlatformPayAmount(orderDomain.getActPrice());
        }
        checkoutOrderResult(domain);
    }


    private List<XhpcTradebillInternetCheckRecordDomain> getHistoryOrderList(List<XhpcTradebillInternetCheckRecordDomain> domainList, List<String> serialNumberList){
        List<XhpcHistoryOrderDomain> orderDomainList = historyOrderMapper.findListByInternetSerialNumbers(serialNumberList);
        Map<String, XhpcHistoryOrderDomain> orderDomainMap = new HashMap<>();
        for(XhpcHistoryOrderDomain orderDomain: orderDomainList){
            orderDomainMap.put(orderDomain.getInternetSerialNumber(), orderDomain);
        }

        for(XhpcTradebillInternetCheckRecordDomain domain: domainList){
            XhpcHistoryOrderDomain orderDomain = orderDomainMap.get(domain.getInternetSerialNumber());
            if(orderDomain != null){
                domain.setHistoryOrderNo(orderDomain.getHistoryOrderId());
                domain.setPlatformInternetSerialNumber(orderDomain.getInternetSerialNumber());
                domain.setPlatformSerialNumber(orderDomain.getSerialNumber());
                domain.setPlatformChargeStation(orderDomain.getChargingStationName());
                domain.setPlatformStartSoc(orderDomain.getStartSoc());
                domain.setPlatformEndSoc(orderDomain.getEndSoc());
                BigDecimal chargingDegree = orderDomain.getChargingDegree() != null?
                        BigDecimal.valueOf(orderDomain.getChargingDegree()): BigDecimal.ZERO;
                domain.setPlatformChargingDegree(chargingDegree);
                domain.setPlatformChargingTime(orderDomain.getChargingTime());
                domain.setPlatformStartTime(orderDomain.getStartTime());
                domain.setPlatformEndTime(orderDomain.getEndTime());
                domain.setPlatformOrderAmount(orderDomain.getTotalPrice());
                domain.setPlatformPowerAmount(orderDomain.getPowerPriceTotal());
                domain.setPlatformServerAmount(orderDomain.getServicePriceTotal());
                domain.setPlatformDiscountAmount(orderDomain.getPromotionDiscount());
                domain.setPlatformPayAmount(orderDomain.getActPrice());
            }
            checkoutOrderResult(domain);
        }

        domainList.sort(Comparator.comparing(XhpcTradebillInternetCheckRecordDomain::getStatus));
       return domainList;
    }

    private void checkoutOrderResult(XhpcTradebillInternetCheckRecordDomain domain){
        int checkoutStatus = 0;
        // 流量方的订单金额大于本平台的历史订单的订单金额，就可以判断成功
        if(domain.getPlatformOrderAmount() != null && domain.getPlatformOrderAmount().compareTo(domain.getInternetOrderAmount()) == 0
            && domain.getPlatformPowerAmount() != null && domain.getPlatformPowerAmount().compareTo(domain.getInternetPowerAmount()) == 0
            && domain.getPlatformServerAmount() != null && domain.getPlatformServerAmount().compareTo(domain.getInternetServerAmount()) == 0
            && domain.getPlatformChargingDegree() != null && domain.getPlatformChargingDegree().compareTo(domain.getInternetChargingDegree()) == 0){
            checkoutStatus = 2;
        }
        domain.setStatus(checkoutStatus);
    }



}
