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

import com.gic.api.base.commons.Page;
import com.gic.haoban.base.api.common.BasePageInfo;
import com.gic.haoban.common.utils.EntityUtil;
import com.gic.haoban.common.utils.PageUtil;
import com.gic.haoban.manage.api.dto.PreDealLogInfoDTO;
import com.gic.haoban.manage.api.enums.PreDealStatusEnum;
import com.gic.haoban.manage.api.enums.PreDealTypeEnum;
import com.gic.haoban.manage.service.dao.mapper.PreDealLogMapper;
import com.gic.haoban.manage.service.entity.TabHaobanPreDealLog;
import com.gic.haoban.manage.service.service.PreDealService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created 2020/4/9.
 *
 * @author hua
 */
@Service
public class PreDealServiceImpl implements PreDealService {

    @Autowired
    private PreDealLogMapper preDealLogMapper;
    private static final Logger logger = LoggerFactory.getLogger(PreDealServiceImpl.class);
    private static final int pageSize = 1000;

    @Override
    public boolean insert(List<TabHaobanPreDealLog> list) {
        if (CollectionUtils.isEmpty(list)) {
            return true;
        }
        //插入不存在的我们pre表的企业微信数据
        int count = list.size();
        int pre = count / pageSize + 1;
        int yushu = count % pageSize;
        if (yushu == 0) {
            pre = pre - 1;
        }
        int i = 0;
        while (i < pre) {
            logger.info("这是第{}次，进入", i);
            i++;
            int fromIndex = (i - 1) * pageSize;
            int toIndex = (count - fromIndex) > pageSize ? (fromIndex + pageSize) : count;
            List<TabHaobanPreDealLog> subList = list.subList(fromIndex, toIndex);
            preDealLogMapper.insertSelectiveBatch(subList);
        }
        return true;
    }

    @Override
    public TabHaobanPreDealLog getByDataId(String taskId, String dataId, int dataType, int status) {
        List<TabHaobanPreDealLog> list = preDealLogMapper.getByDataId(taskId, dataId, dataType, status);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        } else {
            return list.get(0);
        }
    }

    @Override
    public List<TabHaobanPreDealLog> listByPDataId(String taskId, String pDataId, int status) {
        return preDealLogMapper.listByPDataId(taskId, pDataId, status, PreDealTypeEnum.dept.getVal());
    }

    @Override
    public List<TabHaobanPreDealLog> listReByPDataId(String taskId, String pDataId) {
        return preDealLogMapper.listReByPDataId(taskId, pDataId, PreDealTypeEnum.dept.getVal());
    }

    @Override
    public boolean updateStatusByDataId(String taskId, String dataId, int status, String reason) {
        int i = preDealLogMapper.updateStatusByDataId(taskId, dataId, -1, status, reason);
        return i >= 0;
    }

    @Override
    public boolean updateStatusByDataId(String taskId, String dataId, int dataType, int status, String reason) {
        int i = preDealLogMapper.updateStatusByDataId(taskId, dataId, dataType, status, reason);
        return i >= 0;
    }

    @Override
    public boolean checkTask(String taskId, int dataType) {
        int i = preDealLogMapper.checkTask(taskId, dataType, PreDealStatusEnum.pre.getVal());
        return i == 0;
    }

    @Override
    public List<TabHaobanPreDealLog> listByTaskId(String taskId, int dataType, int status) {
        return preDealLogMapper.listByTaskId(taskId, dataType, status);
    }

    @Override
    public int countByTaskId(String taskId, int dataType, int status) {
        return preDealLogMapper.countByTaskId(taskId, dataType, status);
    }

    @Override
    public int countExcepAndPreByTaskId(String taskId, int dataType) {
        return preDealLogMapper.countExcepAndPreByTaskId(taskId, dataType);
    }

    @Override
    public List<TabHaobanPreDealLog> queryDepartRebuildDealLog(String taskId) {
        return preDealLogMapper.listRebuildDepartByTaskId(taskId);
    }

    @Override
    public List<TabHaobanPreDealLog> listRebuildByTaskId(String taskId, int dataType) {
        return preDealLogMapper.listRebuildByTaskId(taskId, dataType);
    }

    @Override
    public boolean updateExceptionToPre(String taskId) {
        int i = preDealLogMapper.updateExceptionToPre(taskId);
        return i >= 0;
    }

    @Override
    public Page<PreDealLogInfoDTO> listExceptionByTaskId(String taskId, BasePageInfo pageInfo) {
        PageHelper.startPage(pageInfo);
        List<TabHaobanPreDealLog> page = preDealLogMapper.listExceptionByTask(taskId);
        Page<PreDealLogInfoDTO> retPage = PageUtil.changePageHelperToCurrentPage(new PageInfo<>(page), PreDealLogInfoDTO.class);
        return retPage;
    }

    @Override
    public List<String> listDataIdByTaskIdAndDataType(String taskId, Integer dateType) {
        List<String> list = preDealLogMapper.listDataIdByTaskIdAndDataType(taskId, dateType);
        if (list == null) {
            list = new ArrayList<String>();
        }
        return list;
    }

    @Override
    public PreDealLogInfoDTO getLastPreDataByTaskId(String taskId) {
        TabHaobanPreDealLog preDealLog = preDealLogMapper.getLastPreDataByTaskId(taskId);
        return EntityUtil.changeEntityByOrika(PreDealLogInfoDTO.class, preDealLog);
    }

    @Override
    public boolean updateFriendStatusByDataId(String taskId, String dataId, int status, String reason, String dataContent) {
        int i = preDealLogMapper.updateFriendStatusByDataId(taskId, dataId, status, reason, dataContent);
        return i >= 0;

    }

    @Override
    public boolean updateFriendStatusByDataId(String taskId, String dataId, int status, String reason, String dataContent, String relationKey , int type) {
        int i = preDealLogMapper.updateFriendStatusAndRelationKeyByDataId(taskId, dataId, status, reason, dataContent, relationKey , type);
        return i >= 0;
    }

    @Override
    public TabHaobanPreDealLog getByRelationKey(String taskId, Integer dataType, String relationKey) {
        return preDealLogMapper.getByRelationKey(taskId, dataType, relationKey);
    }

    @Override
    public boolean checkFriendTask(String taskId, int dataType) {
        int i = preDealLogMapper.checkTaskFriend(taskId, null, dataType);
        return i == 0;
    }

    @Override
    public boolean checkFriendTaskByPDataId(String taskId, String pDataId, int dataType) {
        int i = preDealLogMapper.checkTaskFriend(taskId, pDataId, dataType);
        return i == 0;
    }

    @Override
    public List<String> listReDataIdByPDataId(String taskId, String pDataId, Integer dataType) {
        return preDealLogMapper.listReDataIdByPDataId(taskId, pDataId, dataType);
    }
}
