package com.friday.custody.service.basicService.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.friday.custody.dao.InvokeFrequencyDao;
import com.friday.custody.entity.InvokeFrequencyEntity;
import com.friday.custody.req.QueryDataReq;
import com.friday.custody.service.basicService.InvokeFrequencyService;
import com.friday.custody.service.logicService.DataProcessService;
import com.friday.custody.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;


@Service("invokeFrequencyService")
@Slf4j
public class InvokeFrequencyServiceImpl extends ServiceImpl<InvokeFrequencyDao, InvokeFrequencyEntity>
        implements InvokeFrequencyService, DataProcessService<InvokeFrequencyEntity> {

    @Autowired
    InvokeFrequencyDao invokeFrequencyDao;


    @Override
    public InvokeFrequencyEntity queryByUniqueKey(String uniqueKey) {
        InvokeFrequencyEntity invokeFrequencyEntity = invokeFrequencyDao.queryByUniqueKey(uniqueKey);
        return invokeFrequencyEntity;
    }

    @Override
    public void updateByUniqueKey(InvokeFrequencyEntity invokeFrequencyEntity) {
        invokeFrequencyDao.updateByUniqueKey(invokeFrequencyEntity);
    }

    @Override
    public String getRemark(String today, String invokeType) {
        return invokeFrequencyDao.getRemark(today,invokeType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveData(String type, List<InvokeFrequencyEntity> datas) {
        if (CollectionUtils.isEmpty(datas)){
            return R.ok();
        }
        List<InvokeFrequencyEntity> uniqueInvokeFrequencyList = datas.stream().collect(Collectors.collectingAndThen
                (Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing
                        (InvokeFrequencyEntity::getUniqueKey))), ArrayList::new));

        List<String> rawUniqueKeyList = uniqueInvokeFrequencyList
                .stream()
                .map(InvokeFrequencyEntity::getUniqueKey)
                .collect(Collectors.toList());

        String ukList = String.join("--", rawUniqueKeyList);
        if (CollectionUtils.isNotEmpty(rawUniqueKeyList)) {
            Set<String> uniqueKeySet = baseMapper.queryUniqueKeyList(rawUniqueKeyList);
            if (CollectionUtils.isNotEmpty(uniqueKeySet)) {
                baseMapper.deleteByUniqueKeys(uniqueKeySet);
            }

        }

        boolean saveStatus = this.saveBatch(uniqueInvokeFrequencyList);
        if (!saveStatus) {
            log.error("save InvokeFrequencyEntity failed:{}", ukList);
        }
        return R.ok();
    }

    public R saveData(InvokeFrequencyEntity entity) {
        if (ObjectUtils.isEmpty(entity)){
            return R.ok();
        }
        Set<String> uniqueKeySet = baseMapper.queryUniqueKeyList(Arrays.asList(entity.getUniqueKey()));
        if (CollectionUtils.isNotEmpty(uniqueKeySet)) {
            baseMapper.deleteByUniqueKeys(uniqueKeySet);
        }
        int insertCount = baseMapper.insert(entity);
        if (insertCount != 1) {
            log.error("save InvokeFrequencyEntity failed:{}", entity.getUniqueKey());
        }
        return R.ok();
    }
    @Override
    public R queryData(QueryDataReq vo) {
        return null;
    }

    @Override
    public R updateData(String type, List<InvokeFrequencyEntity> datas) {
        return null;
    }
}