package com.crm.ib.task.service.impl;

import com.crm.ib.task.dao.mapper.DailyTradeDao;
import com.crm.ib.task.dao.mapper.TradeHistoryDao;
import com.crm.ib.task.service.CommissionCalcService;
import com.crm.ib.task.service.DailyConfigService;
import com.crm.ib.task.service.DailyTradeService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.entity.daily.DailyCommonParamEntity;
import com.crm.rely.backend.core.pojo.table.TradeHistoryTable;
import com.crm.rely.backend.core.pojo.view.DailyHistoryTradeView;
import com.crm.rely.backend.util.ListSplitUtil;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by max on 2020/12/1.
 */
@Service
public class DailyTradeServiceImpl implements DailyTradeService {

    private final Logger logger = LoggerFactory.getLogger(DailyTradeServiceImpl.class);

    @Autowired
    private DailyTradeDao dailyTradeDao;
    @Autowired
    private TradeHistoryDao tradeHistoryDao;
    @Autowired
    private DailyConfigService dailyConfigService;
    @Autowired
    private CommissionCalcService commissionCalcService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer calc(Map<String, Date> configs) throws Exception {
        try {
            dealData(configs, Constants.MT4_TRADE_STAMP, Constants.MT4);
            dealData(configs, Constants.MT5_TRADE_STAMP, Constants.MT5);
            return 200;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return 400;
        }
    }

    /**
     * 切割并保存信息
     *
     * @param tables
     * @param platform
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void splitAndBatchSave(List<TradeHistoryTable> tables, String platform) throws Exception {
        Date now = new Date();
        for (TradeHistoryTable table : tables) {
            table.setAddTime(now);
        }
        List<List<TradeHistoryTable>> tradesList = ListSplitUtil.split(tables, 200);
        for (List<TradeHistoryTable> trades : tradesList) {
            tradeHistoryDao.saveInBatch(trades, platform, null);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void supply(Date startDate, Date endDate) throws Exception {
        supplyByPlatform(startDate, endDate, Constants.MT4);
        supplyByPlatform(startDate, endDate, Constants.MT5);
    }

    @Transactional(rollbackFor = Exception.class)
    void supplyByPlatform(Date startDate, Date endDate, String platform) throws Exception {
        List<TradeHistoryTable> tables = dailyTradeDao.findAllMissTradeBetweenDate(startDate, endDate, platform);
        if (!CollectionUtils.isEmpty(tables)) {
            //切割并保存信息
            splitAndBatchSave(tables, platform);
            //计算佣金
            commissionCalcService.calc(tables, 1);
        }
    }

    /**
     * 处理数据
     *
     * @param configs
     * @param stampKey
     * @param platform
     * @throws Exception
     */
    private void dealData(Map<String, Date> configs, String stampKey, String platform) throws Exception {
        //获取上次交易结算截止时间
        Date stamp = configs.get(stampKey);
        if (stamp != null) {
            DailyCommonParamEntity entity = new DailyCommonParamEntity(stamp, platform);
            List<TradeHistoryTable> tables = dailyTradeDao.getData(entity);
            if (!CollectionUtils.isEmpty(tables)) {
                //更新配置信息
                dailyConfigService.updateValByKey(stampKey, tables.get(tables.size() - 1).getCloseTime());
                //切割并保存信息
                splitAndBatchSave(tables, platform);
                //计算佣金
                commissionCalcService.calc(tables, 1);
            }

        }
    }
}
