package com.bonus.material.task.service.impl;

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

import com.bonus.common.biz.constant.BmConfigItems;
import com.bonus.common.biz.enums.TmTaskTypeEnum;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.material.basic.service.IBmConfigService;
import com.bonus.material.task.domain.TmTaskAuditResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.material.task.mapper.TmTaskAuditLogMapper;
import com.bonus.material.task.domain.TmTaskAuditLog;
import com.bonus.material.task.service.ITmTaskAuditLogService;
import org.springframework.util.CollectionUtils;

/**
 * 任务Service业务层处理
 * 
 * @author xsheng
 * @date 2024-10-18
 */
@Service
public class TmTaskAuditLogServiceImpl implements ITmTaskAuditLogService {
    @Autowired
    private TmTaskAuditLogMapper tmTaskAuditLogsMapper;

    @Autowired
    private IBmConfigService bmConfigService;

    /**
     * 查询任务
     * 
     * @param id 任务主键
     * @return 任务
     */
    @Override
    public TmTaskAuditLog selectTmTaskAuditLogsById(Long id) {
        return tmTaskAuditLogsMapper.selectTmTaskAuditLogById(id);
    }

    /**
     * 查询任务列表
     * 
     * @param tmTaskAuditLog 任务
     * @return 任务
     */
    @Override
    public List<TmTaskAuditLog> selectTmTaskAuditLogsList(TmTaskAuditLog tmTaskAuditLog) {
        return tmTaskAuditLogsMapper.selectTmTaskAuditLogList(tmTaskAuditLog);
    }

    /**
     * 查询任务列表已审核的RoleKey
     *
     * @param tmTaskAuditLog 任务
     * @return 任务集合
     */
    public List<String> selectTmTaskAuditRoleList(TmTaskAuditLog tmTaskAuditLog) {
        return tmTaskAuditLogsMapper.selectTmTaskAuditRoleList(tmTaskAuditLog);
    }

    /**
     * 新增任务
     * 
     * @param tmTaskAuditLog 任务
     * @return 结果
     */
    @Override
    public int insertTmTaskAuditLogs(TmTaskAuditLog tmTaskAuditLog) {
        tmTaskAuditLog.setCreateTime(DateUtils.getNowDate());
        try {
            return tmTaskAuditLogsMapper.insertTmTaskAuditLog(tmTaskAuditLog);
        } catch (Exception e) {
            throw new ServiceException("错误信息描述");
        }
    }

    /**
     * 修改任务
     * 
     * @param tmTaskAuditLog 任务
     * @return 结果
     */
    @Override
    public int updateTmTaskAuditLogs(TmTaskAuditLog tmTaskAuditLog) {
        tmTaskAuditLog.setUpdateTime(DateUtils.getNowDate());
        try {
            return tmTaskAuditLogsMapper.updateTmTaskAuditLog(tmTaskAuditLog);
        } catch (Exception e) {
            throw new ServiceException("错误信息描述");
        }
    }

    /**
     * 批量删除任务
     * 
     * @param ids 需要删除的任务主键
     * @return 结果
     */
    @Override
    public int deleteTmTaskAuditLogsByIds(Long[] ids) {
        return tmTaskAuditLogsMapper.deleteTmTaskAuditLogsByIds(ids);
    }

    /**
     * 删除任务信息
     * 
     * @param id 任务主键
     * @return 结果
     */
    @Override
    public int deleteTmTaskAuditLogsById(Long id) {
        return tmTaskAuditLogsMapper.deleteTmTaskAuditLogById(id);
    }

    public TmTaskAuditResult getAuditResult(TmTaskAuditLog tmTaskAuditLog) {
        List<String> needRoles = new ArrayList<>();
        if (TmTaskTypeEnum.TM_TASK_LEASE.getTaskTypeId().equals(tmTaskAuditLog.getTaskType())) {
            needRoles = bmConfigService.getBmConfigResultAsList(BmConfigItems.LEASE_TASK_AUDIT_ROLE_IDS);
        } else if (TmTaskTypeEnum.TM_TASK_BACK.getTaskTypeId().equals(tmTaskAuditLog.getTaskType())) {
            needRoles = bmConfigService.getBmConfigResultAsList(BmConfigItems.BACK_TASK_AUDIT_ROLE_IDS);
        } //TODO, add more tasks logic to get needRoles

        if (CollectionUtils.isEmpty(needRoles)) {
            return new TmTaskAuditResult(true);
        } else {
            List<String> auditRoleIds = selectTmTaskAuditRoleList(tmTaskAuditLog);
            String nextRole = needRoles.get(0);
            boolean finished = false;
            for (int i = 0; i < needRoles.size(); i++) {
                if (i < needRoles.size() - 1) {
                    // 遍历，还没到最后一个
                    if (auditRoleIds.contains(needRoles.get(i))) {
                        nextRole = needRoles.get(i + 1);
                    }
                } else {
                    // 遍历，已经到最后一个了
                    if (auditRoleIds.contains(needRoles.get(i))) {
                        nextRole = "";
                        finished = true;
                    }
                }
            }
            return new TmTaskAuditResult(finished, needRoles, nextRole);
        }
    }
}
