package com.gyee.frame.mapper.ticket;

import com.gyee.frame.model.ticket.Labor;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author chenmh
 * @since 2020-07-26
 */
public interface LaborMapper extends BaseMapper<Labor> {

    /**
     * 根据流程节点角色查询人员
     *
     * WFROLE -- 人员组
     *
     * @param groupName
     * @return
     */
    @Select("SELECT t3.* FROM WFROLE t1 LEFT JOIN LABORGRPS t2 ON t1.VALUE = t2.GROUPNAME LEFT JOIN LABOR t3 ON t2.LABORNUM = t3.LABORNUM WHERE t1.ROLETYPE = '人员组' and t1.WFROLE = #{groupName}")
    List<Labor> getLaborOfPersonGroup(String groupName);

    /**
     * 根据流程节点角色查询人员
     *
     * 过滤部门
     *
     * @param dept
     * @param groupName
     * @return
     */
    @Select("SELECT t3.* FROM  LABORGROUP t1 LEFT JOIN LABORGRPS t2 ON t1.GROUPNAME = t2.GROUPNAME LEFT JOIN LABOR t3 ON t2.LABORNUM = t3.LABORNUM WHERE t3.DEPTNUM = #{dept} AND t1.DESCRIPTION = #{groupName}")
    List<Labor> getLaborOfDept(String dept, String groupName);

    /**
     * 根据流程节点角色查询人员
     *
     * WFROLE -- 人员
     *
     * @param role
     * @return
     */
    @Select("SELECT t1.* FROM LABOR t1 LEFT JOIN WFROLE t2 ON t1.LABORNUM = t2.VALUE WHERE t2.ROLETYPE = '人员' and t2.WFROLE = #{role}")
    List<Labor> getLaborOfPerson(String role);

    /**
     * 查询之前节点出现过的人员
     *
     * @param instId
     * @param role
     * @return
     */
    @Select("select top 1 t1.* from LABOR t1 LEFT JOIN  WFASSIGNMENT t2 ON t1.LABORNUM=t2.ASSIGNCODE WHERE t2.WFINSTID = #{instId} and t2.WFROLE = #{role}")
    List<Labor> getLaborSingle(double instId, String role);

    /**
     * 查询流程启动者
     *
     * @param instId
     * @return
     */
    @Select("select top 1 t1.* from LABOR t1 LEFT JOIN  WFINSTANCE t2 ON t1.LABORNUM=t2.STARTOR WHERE t2.id = #{instId}")
    List<Labor> getFlowStarter(double instId);

    /**
     * 用户登录查询
     *
     * @param username
     * @param password
     * @return
     */
    @Select("select t1.* from LABOR t1 left join IBSUSERS t2 on t1.labornum = t2.labornum where t2.labornum = #{username} and t2.password = #{password}")
    Labor getLaborOfLogin(String username, String password);
}
