package com.bluefire.top.service.impl;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.metadata.Sheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bluefire.top.entity.ExcelMode;
import com.bluefire.top.entity.FlowOrderInfo;
import com.bluefire.top.mapper.FlowOrderInfoMapper;
import com.bluefire.top.service.FlowOrderService;
import com.bluefire.top.util.ExcelModelListener;
import com.bluefire.top.util.ListUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 * @classname: FlowOrderServiceImpl
 * @description TODO
 * @author: liu liu
 * @create: 2020-07-15 13:48
 */

@Service
@Slf4j
public class FlowOrderServiceImpl extends ServiceImpl<FlowOrderInfoMapper, FlowOrderInfo> implements FlowOrderService {


    @Override
    public List<String> getFlowOrderInfo(String startDate, String endDate) {

        LambdaQueryWrapper<FlowOrderInfo> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<FlowOrderInfo> query = queryWrapper.select(FlowOrderInfo::getExtorderId)
                .ge(FlowOrderInfo::getCheckTime, startDate)
                .le(FlowOrderInfo::getCheckTime, endDate)
                .eq(FlowOrderInfo::getEnterpriseId, 54)
                //.notIn(FlowOrderInfo::getChannelId, 101,102,103,110,111,112)
                .eq(FlowOrderInfo::getStatus,"6").orderByAsc(FlowOrderInfo::getApplyDate);

        List<String> list = new ArrayList<>();
        List<FlowOrderInfo> flowOrderInfos = this.baseMapper.selectList(query);
        flowOrderInfos.forEach(f -> {
            list.add(f.getExtorderId().trim());
        });

        return list;
    }

    /**
     * @Description: 读取第三方交给我们的数据校验文件
     * @Param:
     * @return:
     * @Author: liu liu
     * @date: 2020/7/15 20:21
     */
    public List<String> simpleReadImpl(String readPath) {
        List<String> strings = null;
        try {
            Sheet sheet = new Sheet(4, 3, ExcelMode.class);
            ExcelModelListener excelModelListener = new ExcelModelListener();
            EasyExcelFactory.readBySax(new FileInputStream(readPath), sheet, excelModelListener);
            strings = excelModelListener.resultData();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        List<String> strList = new ArrayList<>();
        for (int i = 5; i < strings.size(); i++) {
            strList.add(strings.get(i).trim());
        }
        return strList;
    }

    /**
     * @Description: 通过查询到的数据和数据文件的数据进行读取获取数据
     * @Param:
     * @return:
     * @Author: liu liu
     * @date: 2020/7/15 17:13
     */
    @Override
    public void queryLostOrder(String startDate, String endDate, String redPath) {

        //时间间隔我方的订单列表
        List<String> flowOrderInfo = this.getFlowOrderInfo(startDate, endDate);

        double mePrice = this.baseMapper.getSumFacePrice(flowOrderInfo) * 0.985;

        //读取文件的订单列表
        List<String> strings = this.simpleReadImpl(redPath);
        double herPrice = this.baseMapper.getSumFacePrice(strings) * 0.985;
        log.info("我方中间订单号数量：{}  我方的所有订单金额为：{}", flowOrderInfo.size(), mePrice);
        log.info("读取对方文件的中间订单号数量：{}   对方的实际金额为：{}", strings.size(), herPrice);

        List<String> diffrent1 = ListUtil.getDiffrent1(flowOrderInfo, strings);

        log.info("我方据差异数量：{}", diffrent1.size());
        System.out.println("我方有的数据对方没有的");
        diffrent1.forEach(list1 -> {
            System.out.println(list1);
        });


        List<String> diffrent2 = ListUtil.getDiffrent2(flowOrderInfo, strings);
        log.info("对方有我方没有的订单数据：{}", diffrent2.size());
        System.out.println("对方有我方没有的数据");
        diffrent2.forEach(list2 -> {
            System.out.println(list2);
        });

    }

    /**
     * @Description: 获取 我方有的数据对方没有的的订单单号
     * @Param:
     * @return:
     * @Author: liu liu
     * @date: 2020/7/15 19:19
     */
    @Override
    public List<String> queryMyOrderIds(String startDate, String endDate, String redPath) {

        List<String> flowOrderInfo = this.getFlowOrderInfo(startDate, endDate);
        log.info("选取时间的中间订单号数量：{}", flowOrderInfo.size());
        List<String> strings = this.simpleReadImpl(redPath);
        log.info("读取文件的中间订单号数量：{}", strings.size());
        return ListUtil.getDiffrent2(flowOrderInfo, strings);
    }


    /**
     * @Description: 获取 对方有我方没有的订单单号
     * @Param:
     * @return:
     * @Author: liu liu
     * @date: 2020/7/15 19:19
     */
    @Override
    public List<String> queryHerOrderIds(String startDate, String endDate, String redPath) {
        List<String> flowOrderInfo = this.getFlowOrderInfo(startDate, endDate);
        log.info("选取时间的中间订单号数量：{}", flowOrderInfo.size());
        List<String> strings = this.simpleReadImpl(redPath);
        log.info("读取文件的中间订单号数量：{}", strings.size());
        return ListUtil.getDiffrent1(flowOrderInfo, strings);
    }


    /**
     * @Description: 通过 CP订单号 获取订单信息
     * @Param:
     * @return:
     * @Author: liu liu
     * @date: 2020/7/15 19:17
     */
    @Override
    public List<FlowOrderInfo> queryOrderInfoByList(List<String> extorderIds) {
        return this.baseMapper.queryFlowOrders(extorderIds);
    }


}
