package com.yunzhupaas.flowable.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yunzhupaas.base.service.SuperServiceImpl;
import com.yunzhupaas.flowable.entity.CirculateEntity;
import com.yunzhupaas.flowable.entity.TaskEntity;
import com.yunzhupaas.flowable.enums.TaskStatusEnum;
import com.yunzhupaas.flowable.mapper.CirculateMapper;
import com.yunzhupaas.flowable.service.CirculateService;
import com.yunzhupaas.flowable.model.operator.OperatorVo;
import com.yunzhupaas.flowable.model.task.TaskPagination;
import com.yunzhupaas.util.StringUtil;
import com.yunzhupaas.util.UserProvider;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author ：云筑产品开发平台组
 * @version: V3.1.0
 * @copyright 深圳市乐程软件有限公司
 * @date ：2024/4/29 下午5:21
 */
@Service
public class CirculateServiceImpl extends SuperServiceImpl<CirculateMapper, CirculateEntity> implements CirculateService {

    @Override
    public List<CirculateEntity> getList(String taskId) {
        QueryWrapper<CirculateEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CirculateEntity::getTaskId, taskId);
        return this.list(queryWrapper);
    }

    @Override
    public List<OperatorVo> getList(TaskPagination pagination) {
        String userId = StringUtil.isNotEmpty(pagination.getUserId()) ? pagination.getUserId() : UserProvider.getLoginUserId();
        MPJLambdaWrapper<CirculateEntity> wrapper = JoinWrappers.lambda(CirculateEntity.class)
                .selectAll(TaskEntity.class)
                .leftJoin(TaskEntity.class, TaskEntity::getId, CirculateEntity::getTaskId)
                .selectAs(CirculateEntity::getNodeName, OperatorVo::getNodeName)
                .selectAs(CirculateEntity::getNodeCode, OperatorVo::getNodeCode)
                .selectAs(CirculateEntity::getId, OperatorVo::getId)
                .selectAs(TaskEntity::getId, OperatorVo::getTaskId)
                .selectAs(TaskEntity::getUrgent, OperatorVo::getFlowUrgent)
                .selectAs(CirculateEntity::getCreatorTime, OperatorVo::getCreatorTime)
                .ne(TaskEntity::getStatus, TaskStatusEnum.CANCEL.getCode())
                .eq(CirculateEntity::getUserId, userId);
        //关键字（流程名称、流程编码）
        String keyWord = pagination.getKeyword();
        if (ObjectUtil.isNotEmpty(keyWord)) {
            wrapper.and(t -> t.like(TaskEntity::getEnCode, keyWord).or().like(TaskEntity::getFullName, keyWord));
        }
        //日期范围（近7天、近1月、近3月、自定义）
        if (ObjectUtil.isNotEmpty(pagination.getStartTime()) && ObjectUtil.isNotEmpty(pagination.getEndTime())) {
            wrapper.between(TaskEntity::getStartTime, new Date(pagination.getStartTime()), new Date(pagination.getEndTime()));
        }
        //所属流程
        String templateId = pagination.getTemplateId();
        if (ObjectUtil.isNotEmpty(templateId)) {
            wrapper.eq(TaskEntity::getTemplateId, templateId);
        }
        //所属分类
        String category = pagination.getFlowCategory();
        if (ObjectUtil.isNotEmpty(category)) {
            wrapper.in(TaskEntity::getFlowCategory, category.split(","));
        }
        //是否已读
        Integer status = pagination.getStatus();
        if (ObjectUtil.isNotEmpty(status)) {
            wrapper.in(CirculateEntity::getCirculateRead, status);
        }
        //发起人员
        String creatorUserId = pagination.getCreatorUserId();
        if (ObjectUtil.isNotEmpty(creatorUserId)) {
            wrapper.in(TaskEntity::getCreatorUserId, creatorUserId);
        }
        //紧急程度
        Integer flowUrgent = pagination.getFlowUrgent();
        if (ObjectUtil.isNotEmpty(flowUrgent)) {
            wrapper.in(TaskEntity::getUrgent, flowUrgent);
        }
        wrapper.orderByDesc(CirculateEntity::getCreatorTime);
        Page<OperatorVo> page = new Page(pagination.getCurrentPage(), pagination.getPageSize());
        Page<OperatorVo> data = this.selectJoinListPage(page, OperatorVo.class, wrapper);
        return pagination.setData(data.getRecords(), page.getTotal());
    }
}

