package com.jkcredit.invoice.service.selfcar.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jkcredit.invoice.mapper.waybill.SellCarTradeMapper;
import com.jkcredit.invoice.model.entity.waybill.SelfCarTrade;
import com.jkcredit.invoice.service.lowerservice.SelfCarCustService;
import com.jkcredit.invoice.service.lowerservice.vo.TradeRequestVo;
import com.jkcredit.invoice.service.selfcar.SelfCarTradeService;
import com.jkcredit.invoice.util.DateUtil;
import com.jkcredit.invoice.util.RespR;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自有车交易接口实现类
 * @author mumuxigua
 */
@Service
@Slf4j
public class SelfCarTradeServiceImpl extends ServiceImpl<SellCarTradeMapper, SelfCarTrade> implements SelfCarTradeService {
    @Autowired
    SellCarTradeMapper sellCarTradeMapper;
    @Autowired
    SelfCarCustService selfCarService;

    @Override
    public IPage<List<SelfCarTrade>> findByPageAndTrade(Page page, SelfCarTrade selfCarTrade) {
        return sellCarTradeMapper.selectAllByPage(page, selfCarTrade);
    }


    @Override
    public RespR batchImportSelfcarTrades(List<SelfCarTrade> selfCarTrades) {
        if (selfCarTrades == null || 0 == selfCarTrades.size()) {
            return new RespR<>("失败", "0");
        }

        List<SelfCarTrade> selfCarTradeTmp = new ArrayList<SelfCarTrade>();
        for (SelfCarTrade selfCarTrade : selfCarTrades) {
            if (StringUtils.isNotEmpty(selfCarTrade.getCardId()) && StringUtils.isEmpty(selfCarTrade.getTradeId())) {
                List<SelfCarTrade> cars = sellCarTradeMapper.selectByCardId(selfCarTrade.getCardId());
                selfCarTradeTmp.addAll(cars);
            } else if (StringUtils.isEmpty(selfCarTrade.getCardId()) && StringUtils.isNotEmpty(selfCarTrade.getTradeId())) {
                SelfCarTrade car = sellCarTradeMapper.selectByTradeId(selfCarTrade.getTradeId()).get(0);
                selfCarTradeTmp.add(car);
            } else {
                return new RespR<>("失败", "0");
            }

        }
        return new RespR<>(selfCarTradeTmp, "1");
    }

    @Override
    public boolean updateTrades(List<SelfCarTrade> selfCarTrades) {
        Map<String, TradeRequestVo> map = new HashMap<>(10);
        selfCarTrades.forEach(selfCarTrade -> {
            TradeRequestVo tradeRequestVo = map.get(selfCarTrade.getCardId());
            if (tradeRequestVo == null) {
                tradeRequestVo = new TradeRequestVo();
                tradeRequestVo.setEtcId(selfCarTrade.getCardId());
                tradeRequestVo.setTradeStatus(3);
                tradeRequestVo.setStartTime(selfCarTrade.getExTime());
                tradeRequestVo.setCustomerName(selfCarTrade.getCustomId());
                tradeRequestVo.setCompanyName(selfCarTrade.getCompanyName());
                tradeRequestVo.setEndTime(selfCarTrade.getExTime());
                map.put(selfCarTrade.getCardId(), tradeRequestVo);
            } else {
                if (selfCarTrade.getExTime().compareTo(tradeRequestVo.getStartTime()) < 0) {
                    tradeRequestVo.setStartTime(selfCarTrade.getExTime());
                }

                if (selfCarTrade.getExTime().compareTo(tradeRequestVo.getEndTime()) > 0) {
                    tradeRequestVo.setEndTime(selfCarTrade.getExTime());
                }
            }
        });
        for (Map.Entry<String, TradeRequestVo> elm : map.entrySet()) {
            TradeRequestVo tradeRequestVo = elm.getValue();
            //开始时间和结束时间前后加一天
            String statOrg = tradeRequestVo.getStartTime().split(" ")[0];
            String endOrg = tradeRequestVo.getEndTime().split(" ")[0];
            tradeRequestVo.setStartTime(DateUtil.getDateAfterDays(statOrg, -1) + " 00:00:00");
            tradeRequestVo.setEndTime(DateUtil.getDateAfterDays(endOrg, 1) + " 00:00:00");
            selfCarService.getTradeList(tradeRequestVo);
        }
        return true;
    }

    @Override
    public boolean updateTradesDb(List<SelfCarTrade> selfCarTrades) {
        selfCarTrades.forEach(selfCarTrade -> {
            try{
                if(selfCarTrade.getStatus() ==2){
                    TradeRequestVo tradeRequestVo;
                    tradeRequestVo = new TradeRequestVo();
                    tradeRequestVo.setEtcId(selfCarTrade.getCardId());
                    tradeRequestVo.setTradeStatus(3);
                    tradeRequestVo.setStartTime(DateUtil.getDateAfterDays(selfCarTrade.getExTime().split(" ")[0], -1) + " 00:00:00");
                    tradeRequestVo.setCustomerName(selfCarTrade.getCustomId());
                    tradeRequestVo.setCompanyName(selfCarTrade.getCompanyName());
                    tradeRequestVo.setEndTime(DateUtil.getDateAfterDays(selfCarTrade.getExTime().split(" ")[0], 1) + " 00:00:00");
                    selfCarService.getTradeList(tradeRequestVo);
                }
            }catch (Exception e){
                e.printStackTrace();
                log.error("{}更新失败{}",selfCarTrade.getCardId(),e.getMessage());
            }


        });
        return true;
    }

    @Override
    public void deleteRepeatInvoice(List<SelfCarTrade> selfCarTrades) {
        selfCarTrades.forEach(selfCarTrade -> {
            try{

            }catch (Exception e){
                e.printStackTrace();
                log.error("{}更新失败{}",selfCarTrade.getCardId(),e.getMessage());
            }


        });
    }
}
