package com.qmth.distributed.print.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.distributed.print.business.bean.result.FlowApproveResult;
import com.qmth.distributed.print.business.entity.TFFlow;
import com.qmth.distributed.print.business.mapper.TFFlowMapper;
import com.qmth.distributed.print.business.service.TFFlowService;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.util.ServletUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 流程表 服务实现类
 * </p>
 *
 * @author wangliang
 * @since 2021-08-02
 */
@Service
public class TFFlowServiceImpl extends ServiceImpl<TFFlowMapper, TFFlow> implements TFFlowService {

    @Resource
    TFFlowMapper tfFlowMapper;

    /**
     * 流程列表
     *
     * @param iPage
     * @param name
     * @param schoolId
     * @param orgId
     * @return
     */
    @Override
    public IPage<TFFlow> list(IPage<Map> iPage, String name, Long schoolId, Long orgId) {
        return tfFlowMapper.list(iPage, name, schoolId, orgId);
    }

    /**
     * 流程审批记录列表
     *
     * @param iPage
     * @param startName
     * @param schoolId
     * @param orgId
     * @param flowId
     * @return
     */
    @Override
    public IPage<FlowApproveResult> flowApproveList(IPage<Map> iPage, String startName, Long schoolId, Long orgId, Long flowId) {
        return tfFlowMapper.flowApproveList(iPage, startName, schoolId, orgId, flowId);
    }

    /**
     * 根据key查找流程
     *
     * @param flowKey
     * @return
     */
    @Override
    public Integer findByFlowKey(String flowKey) {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        QueryWrapper<TFFlow> tfFlowQueryWrapper = new QueryWrapper<>();
        tfFlowQueryWrapper.lambda().eq(TFFlow::getSchoolId, sysUser.getSchoolId())
                .eq(TFFlow::getFlowKey, flowKey)
                .eq(TFFlow::getEnable, true)
                .eq(TFFlow::getPublish, true);
        return this.count(tfFlowQueryWrapper);
    }

    /**
     * 根据key查找id
     *
     * @param flowKey
     * @return
     */
    @Override
    public String findActIdByFlowKey(String flowKey) {
        return tfFlowMapper.findActIdByFlowKey(flowKey);
    }
}
