package com.xinelu.manage.service.taskpartitiondict.impl;

import com.xinelu.common.constant.Constants;
import com.xinelu.common.core.domain.entity.SysDictData;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.common.utils.codes.GenerateSystemCodeUtil;
import com.xinelu.manage.domain.taskpartitiondict.TaskPartitionDict;
import com.xinelu.manage.mapper.taskpartitiondict.TaskPartitionDictMapper;
import com.xinelu.manage.service.taskpartitiondict.ITaskPartitionDictService;
import com.xinelu.system.mapper.SysDictDataMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;


/**
 * 任务细分字典Service业务层处理
 *
 * @author xinelu
 * @date 2024-03-11
 */
@Service
public class TaskPartitionDictServiceImpl implements ITaskPartitionDictService {
    @Resource
    private TaskPartitionDictMapper taskPartitionDictMapper;
    @Resource
    private GenerateSystemCodeUtil generateSystemCodeUtil;
    @Resource
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询任务细分字典
     *
     * @param id 任务细分字典主键
     * @return 任务细分字典
     */
    @Override
    public TaskPartitionDict selectTaskPartitionDictById(Long id) {
        return taskPartitionDictMapper.selectTaskPartitionDictById(id);
    }

    /**
     * 查询任务细分字典列表
     *
     * @param taskPartitionDict 任务细分字典
     * @return 任务细分字典
     */
    @Override
    public List<TaskPartitionDict> selectTaskPartitionDictList(TaskPartitionDict taskPartitionDict) {
        List<TaskPartitionDict> taskPartitionDicts = taskPartitionDictMapper.selectTaskPartitionDictList(taskPartitionDict);
        List<SysDictData> sysDictDataList = sysDictDataMapper.selectDictDataByType("task_node_type");
        for (TaskPartitionDict partitionDict : taskPartitionDicts) {
            SysDictData sysDictData = sysDictDataList.stream().filter(Objects::nonNull).filter(item -> item.getDictValue().equals(partitionDict.getTaskNodeType())).findFirst().orElse(new SysDictData());
            if (StringUtils.isNotBlank(sysDictData.getDictLabel())){
                partitionDict.setTaskNodeType(sysDictData.getDictLabel());
            }
        }
        return taskPartitionDicts;
    }

    /**
     * 新增任务细分字典
     *
     * @param taskPartitionDict 任务细分字典
     * @return 结果
     */
    @Override
    public int insertTaskPartitionDict(TaskPartitionDict taskPartitionDict) {
        taskPartitionDict.setTaskPartitionCode(Constants.TASK_PARTITION_CODE + generateSystemCodeUtil.generateSystemCode(Constants.TASK_PARTITION_CODE));
        taskPartitionDict.setCreateTime(LocalDateTime.now());
        taskPartitionDict.setCreateBy(SecurityUtils.getUsername());
        return taskPartitionDictMapper.insertTaskPartitionDict(taskPartitionDict);
    }

    /**
     * 修改任务细分字典
     *
     * @param taskPartitionDict 任务细分字典
     * @return 结果
     */
    @Override
    public int updateTaskPartitionDict(TaskPartitionDict taskPartitionDict) {
        taskPartitionDict.setUpdateTime(LocalDateTime.now());
        taskPartitionDict.setUpdateBy(SecurityUtils.getUsername());
        return taskPartitionDictMapper.updateTaskPartitionDict(taskPartitionDict);
    }

    /**
     * 批量删除任务细分字典
     *
     * @param ids 需要删除的任务细分字典主键
     * @return 结果
     */
    @Override
    public int deleteTaskPartitionDictByIds(Long[] ids) {
        return taskPartitionDictMapper.deleteTaskPartitionDictByIds(ids);
    }

    /**
     * 删除任务细分字典信息
     *
     * @param id 任务细分字典主键
     * @return 结果
     */
    @Override
    public int deleteTaskPartitionDictById(Long id) {
        return taskPartitionDictMapper.deleteTaskPartitionDictById(id);
    }
}
