package cn.iocoder.yudao.module.bpm.service.definition;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.copy.TaskCopyUserSaveReqVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.TaskCopyDO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.TaskCopyUserDO;
import cn.iocoder.yudao.module.bpm.dal.mysql.definition.TaskCopyMapper;
import cn.iocoder.yudao.module.bpm.dal.mysql.definition.TaskCopyUserMapper;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: jinyd
 * @CreateTime: 2024/9/25  16:13
 * @Description:
 * @Version: 4.0
 */

@Service
@Validated
public class TaskCopyUserServiceImpl implements TaskCopyUserService{

    @Resource
    private TaskCopyMapper taskCopyMapper;

    @Resource
    private TaskCopyUserMapper taskCopyUserMapper;

    @Override
    public Long createTaskCopyUser(TaskCopyUserSaveReqVO createReqVO) {
        // 插入
        TaskCopyUserDO taskCopyUser = BeanUtils.toBean(createReqVO, TaskCopyUserDO.class);
        taskCopyUserMapper.insert(taskCopyUser);
        // 返回
        return taskCopyUser.getId();
    }

    @Override
    public void updateTaskCopyUser(TaskCopyUserSaveReqVO updateReqVO) {
        // 校验存在
        validateTaskCopyUserExists(updateReqVO.getId());
        // 更新
        TaskCopyUserDO updateObj = BeanUtils.toBean(updateReqVO, TaskCopyUserDO.class);
        taskCopyUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteTaskCopyUser(Long id) {
        // 校验存在
        validateTaskCopyUserExists(id);
        // 删除
        taskCopyUserMapper.deleteById(id);
    }

    private void validateTaskCopyUserExists(Long id) {
        if (taskCopyUserMapper.selectById(id) == null) {
//            throw exception(TASK_COPY_USER_NOT_EXISTS);
        }
    }

    @Override
    public TaskCopyUserDO getTaskCopyUser(Long id) {
        return taskCopyUserMapper.selectById(id);
    }

    @Override
    @TenantIgnore
    public String getByProcessDefinitionId(String processDefinitionId) {
        List<TaskCopyDO> list = taskCopyMapper.selectList(new LambdaQueryWrapperX<TaskCopyDO>()
                .eq(TaskCopyDO::getProcessDefinitionId, processDefinitionId));
        if (null == list) return "";
        Optional<TaskCopyDO> taskOptional = list.stream().max(Comparator.comparing(TaskCopyDO::getSort));
        // 判断是否有值
        if (!taskOptional.isPresent()) return "";
        TaskCopyDO taskCopyDO = list.stream().max(Comparator.comparing(TaskCopyDO::getSort)).get();
        List<AdminUserRespDTO> userList = taskCopyUserMapper.selectUserList(taskCopyDO.getId());
        if (null == userList) return "";
        return userList.stream().map(AdminUserRespDTO::getNickname).collect(Collectors.joining(","));
    }
}
