package com.rongwei.sfcommon.sys.dao;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rongwe.scentity.domian.HiddenDangerTrackDo;
import com.rongwe.scentity.vo.HiddenDangerTrackVo;
import com.rongwei.rwadmincommon.system.domain.SysRoleDo;
import com.rongwei.rwadmincommon.system.domain.SysUserRoleDo;
import com.rongwei.rwcommon.base.BaseDao;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 隐患跟踪表
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2020-08-04 18:39:10
 */
public interface HiddenDangerTrackDao extends BaseDao<HiddenDangerTrackDo> {

    @Select("<script>select t3.SHIFT, t1.* from asp_hidden_danger_track t1 " +
            "LEFT JOIN asp_point_check_item t2 ON t1.RELATIONID = t2.ID " +
            "LEFT JOIN asp_point_check t3 ON t3.ID = t2.POINTCHECKID " +
            "where t1.TRACKUSERID like '%${userid}%' \n" +
//            "where (t1.TRACKUSERID like '%${userid}%' \n" +
//            "or EXISTS (select a.ID,c.ORGID from sys_user_role a left join sys_role b on a.ROLEID=b.ID \n" +
//            "left join sys_user_org c on c.USERID=a.USERID \n" +
//            "where b.CODE='gdzg' " +
//            "<if test='userid != null'>" +
//            "and a.USERID = #{userid} " +
//            "</if>"+
//            "and a.DELETED='0' and b.DELETED='0' and c.DELETED='0' and c.ORGID = t1.DANGERWORKPARK)) " +
            "and t1.DELETED = '0' and t1.STATUS = #{status} " +
            "order by t1.CODE desc" +
            "</script>")
    IPage<HiddenDangerTrackVo> checkTaskList(Page page, @Param("userid") String userid, @Param("status") String status);

    @Select("select DISTINCT b.ORGID,GROUP_CONCAT(a.USERID) AS USERID,GROUP_CONCAT(c.NAME) AS USERNAME \n" +
            "from sys_user_role a left join sys_user_org b on a.USERID=b.USERID \n" +
            "left join sys_user c on a.USERID=c.ID left join sys_role d on a.ROLEID=d.ID  \n" +
            "where a.DELETED='0' and b.DELETED='0' and c.DELETED='0' and d.DELETED='0' and d.CODE='GDZG' \n" +
            "GROUP BY b.ORGID")
    List<Map<String,Object>> getAllDeptZGSPA();

    @Select("select b.* from sys_user_role a left JOIN sys_role b on a.ROLEID=b.ID where a.DELETED='0' and b.DELETED='0' and a.USERID=#{userid}")
    List<SysRoleDo> getTrackUserRoles(@Param("userid") String userid);

}