package com.cool.store.dao;

import com.cool.store.entity.PointAuditRecordDO;
import com.cool.store.enums.AuditStatusEnum;
import com.cool.store.enums.NodeNoEnum;
import com.cool.store.mapper.PointAuditRecordMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Repository
public class PointAuditRecordDAO {

    @Resource
    private PointAuditRecordMapper pointAuditRecordMapper;

    /**
     * 新增审批记录
     * @param historyList
     */
    public void addPointAuditRecord(List<PointAuditRecordDO> recordList) {
        pointAuditRecordMapper.batchInsertSelective(recordList);
    }

    public List<PointAuditRecordDO> getPointAuditRecord(Long pointId, Integer cycleCount) {
        if(Objects.isNull(pointId)){
            return Lists.newArrayList();
        }
        return pointAuditRecordMapper.getPointAuditRecord(pointId, cycleCount);
    }

    public Map<Integer, PointAuditRecordDO> getPointAuditRecordMap(Long pointId, Integer cycleCount) {
        if(Objects.isNull(pointId) || Objects.isNull(cycleCount)){
            return Maps.newHashMap();
        }
        List<PointAuditRecordDO> pointAuditRecord = pointAuditRecordMapper.getPointAuditRecord(pointId, cycleCount);
        return pointAuditRecord.stream().collect(Collectors.toMap(k->k.getNodeNo(), v->v));
    }

    public Integer updatePointAuditRecord(PointAuditRecordDO param) {
        if(Objects.isNull(param) || Objects.isNull(param.getId())){
            return null;
        }
        return pointAuditRecordMapper.updateByPrimaryKeySelective(param);
    }

    public Integer deletePointAuditRecord(Long pointId, Integer cycleCount) {
        if(Objects.isNull(pointId) || Objects.isNull(cycleCount)){
            return null;
        }
        return pointAuditRecordMapper.deletePointAuditRecord(pointId, cycleCount);
    }

    public List<PointAuditRecordDO> getPointAllAuditRecord(Long pointId) {
        if(Objects.isNull(pointId)){
            return Lists.newArrayList();
        }
        return pointAuditRecordMapper.getPointAllAuditRecord(pointId);
    }

    /**
     * 获取提交时间
     * @param pointIds
     * @return
     */
    public Map<Long, Date> getPointSubmitTimeMap(List<Long> pointIds){
        if(CollectionUtils.isEmpty(pointIds)){
            return Maps.newHashMap();
        }
        List<PointAuditRecordDO> auditList = pointAuditRecordMapper.getPointSubmitAuditTime(pointIds);
        return auditList.stream().collect(Collectors.toMap(k->k.getPointId(), v->v.getFinishTaskTime(),  (oldDate, newDate) -> oldDate.compareTo(newDate) > 0 ? oldDate : newDate));
    }

}