package com.gic.haoban.manage.service.service.notify.impl;

import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.commons.util.PageHelperUtils;
import com.gic.commons.util.StringUtil;
import com.gic.haoban.common.utils.EntityUtil;
import com.gic.haoban.manage.service.dao.mapper.PendingTaskMapper;
import com.gic.haoban.manage.service.entity.TabPendingTask;
import com.gic.haoban.manage.service.pojo.bo.PendingTaskBO;
import com.gic.haoban.manage.service.service.notify.PendingTaskService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created 2021/12/15.
 *
 * @author hua
 */
@Service
public class PendingTaskServiceImpl implements PendingTaskService {

    @Autowired
    private PendingTaskMapper pendingTaskMapper;

    @Override
    public boolean addOrUpdatePendingTask(PendingTaskBO task) {
        TabPendingTask tabPendingTask = EntityUtil.changeEntityByOrika(TabPendingTask.class, task);
        TabPendingTask pendingTask = pendingTaskMapper.getByRelationId(tabPendingTask.getRelationId());
        tabPendingTask.setUpdateTime(new Date());
        if (pendingTask != null) {
            tabPendingTask.setId(pendingTask.getId());
            tabPendingTask.setDeleteFlag(0);
            pendingTaskMapper.updateByPrimaryKeySelective(tabPendingTask);
        } else {
            tabPendingTask.setCreateTime(new Date());
            pendingTaskMapper.insertSelective(tabPendingTask);
        }
        return true;
    }

    @Override
    public boolean addOrUpdateBatchPendingTask(List<PendingTaskBO> tasks) {
        if (CollectionUtils.isEmpty(tasks)) {
            return true;
        }
        List<String> relationIds = tasks.stream().map(mid -> mid.getRelationId()).collect(Collectors.toList());
        List<TabPendingTask> tabPendingTasks = pendingTaskMapper.listByRelationId(relationIds);
        if (CollectionUtils.isNotEmpty(tabPendingTasks)) {
            Map<String, TabPendingTask> pendingTaskMap = tabPendingTasks.stream().collect(Collectors.toMap(tab -> tab.getRelationId(), tab -> tab));
            Set<String> hasRelationIds = pendingTaskMap.keySet();
            pendingTaskMapper.updateDelToNormalByRelationIds(hasRelationIds,tasks.get(0).getInvalidTime());
            tasks = tasks.stream().filter(mid->!pendingTaskMap.containsKey(mid.getRelationId())).collect(Collectors.toList());
        }
        if (CollectionUtils.isEmpty(tasks)) {
            return true;
        }
        List<TabPendingTask> pendingTasks = EntityUtil.changeEntityListByJSON(TabPendingTask.class, tasks);

        int size=1000;
        int len = pendingTasks.size();
        int yu = len % size;
        int nu = len / size+(yu>0?1:0);
        for (int i=0;i<nu;i++) {
            int start=i*size;
            int end=(start+size)>len?(start+yu):(start+size);
            List<TabPendingTask> midTask = pendingTasks.subList(start, end);
            pendingTaskMapper.insertBatch(midTask);
        }
        return true;
    }

    @Override
    public Page<PendingTaskBO> pagePendingTask(String enterpriseId, String storeId, String clerkId, int taskType, int finishFlag, int overdueFlag, BasePageInfo pageInfo) {
        PageHelper.startPage(pageInfo);
        List<TabPendingTask> tabPendingTasks = pendingTaskMapper.listPendingTask(enterpriseId, storeId, clerkId, taskType, finishFlag, overdueFlag);
        Page<PendingTaskBO> retPage = PageHelperUtils.changePageHelperToCurrentPage(tabPendingTasks, PendingTaskBO.class);
        return retPage;
    }

    @Override
    public List<PendingTaskBO> listPendingTaskByCreateTime(String enterpriseId, String storeId, String clerkId, String startDate, String endDate) {
        List<TabPendingTask> tabPendingTasks = pendingTaskMapper.listPendingTaskByCreateTime(enterpriseId, storeId, clerkId, startDate, endDate);
        return EntityUtil.changeEntityListByJSON(PendingTaskBO.class, tabPendingTasks);
    }

    @Override
    public boolean deletePendingTask(List<String> relationIds) {
        if (CollectionUtils.isEmpty(relationIds)) {
            return true;
        }
        pendingTaskMapper.deleteByRelationIds(null, relationIds);
        return true;
    }

    @Override
    public boolean deletePendingTaskByBusinessId(String businessId) {
        pendingTaskMapper.deleteByBusinessId(businessId);
        return true;
    }

    @Override
    public boolean updateInvalidTimeByBusinessId(String businessId, Date time) {
        pendingTaskMapper.updateInvalidTimeByBusinessId(businessId, time);
        return true;
    }

    @Override
    public boolean updateFinish(String relationId) {
        TabPendingTask pendingTask = pendingTaskMapper.getByRelationId(relationId);
        if (null == pendingTask) {
            return true;
        }
        pendingTask.setFinishFlag(1);
        pendingTask.setFinishTime(new Date());
        pendingTask.setUpdateTime(new Date());
        pendingTaskMapper.updateByPrimaryKeySelective(pendingTask);
        return true;
    }

    @Override
    public boolean changeByRelationId(PendingTaskBO task) {
        if (StringUtils.isBlank(task.getRelationId())) {
            return false;
        }
        TabPendingTask pendingTask = pendingTaskMapper.getByRelationId(task.getRelationId());
        if (null == pendingTask) {
            return true;
        }
        TabPendingTask tabPendingTask = EntityUtil.changeEntityByJSON(TabPendingTask.class, task);
        tabPendingTask.setId(pendingTask.getId());
        tabPendingTask.setUpdateTime(new Date());
        pendingTaskMapper.updateByPrimaryKeySelective(tabPendingTask);
        return true;
    }
    
    @Override
    public void insertPendingTaskBatch(List<PendingTaskBO> tasks) {
        if (CollectionUtils.isEmpty(tasks)) {
            return ;
        }
        List<TabPendingTask> pendingTasks = EntityUtil.changeEntityListByJSON(TabPendingTask.class, tasks);
        if(pendingTasks.size()>1000) {
            int size=1000;
            int len = pendingTasks.size();
            int yu = len % size;
            int nu = len / size+(yu>0?1:0);
            for (int i=0;i<nu;i++) {
                int start=i*size;
                int end=(start+size)>len?(start+yu):(start+size);
                List<TabPendingTask> midTask = pendingTasks.subList(start, end);
                pendingTaskMapper.insertBatch(midTask);
            }
        }else {
            pendingTaskMapper.insertBatch(pendingTasks);
        }
    }
}
