package com.rongwei.sfcommon.sys.dao;

import com.rongwe.scentity.domian.SafetyObservationDo;
import com.rongwei.rwadmincommon.system.domain.SysUserDo;
import com.rongwei.rwcommon.base.BaseDao;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 安全观察表
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2020-09-11 14:21:07
 */
public interface SafetyObservationDao extends BaseDao<SafetyObservationDo> {
    @Select("SELECT COUNT(*) from asp_safety_observation a WHERE a.DELETED='0'")
    int countSafe();

    @Select("SELECT \n" +
            "        c.SYSUSERORGIDS, \n" +
            "        c.SYSUSERORGNAMES, \n" +
            "        e.SYSUSERROLEIDS, \n" +
            "        e.SYSUSERROLENAMES, \n" +
            "        f.`NAME` 'RCREATEUSERNAME', \n" +
            "        g.`NAME` 'RMODIFYUSERNAME', \n" +
            "        a.* \n" +
            "        FROM \n" +
            "        sys_user a \n" +
            "        LEFT JOIN ( \n" +
            "        SELECT \n" +
            "        GROUP_CONCAT( t3.`ID` ) 'SYSUSERORGIDS', \n" +
            "        GROUP_CONCAT( t3.`SHORTNAME` ) 'SYSUSERORGNAMES', \n" +
            "        t1.ID 'USERID' \n" +
            "        FROM \n" +
            "        sys_user t1 \n" +
            "        LEFT JOIN sys_user_org t2 ON t1.ID = t2.USERID \n" +
            "        AND t2.DELETED = '0' \n" +
            "        LEFT JOIN sys_organization t3 ON t2.ORGID = t3.ID \n" +
            "        AND t3.DELETED = '0' \n" +
            "        WHERE \n" +
            "        t1.DELETED = '0' \n" +
            "        GROUP BY \n" +
            "        t1.ID \n" +
            "        ) c ON c.USERID = a.ID \n" +
            "        LEFT JOIN sys_user f ON a.CREATEUSERID = f.id \n" +
            "        AND f.DELETED = '0' \n" +
            "        LEFT JOIN sys_user g ON a.MODIFYUSERID = g.id \n" +
            "        AND g.DELETED = '0' \n" +
            "        LEFT JOIN ( \n" +
            "        SELECT \n" +
            "        GROUP_CONCAT( e.`id` ) 'SYSUSERROLEIDS', \n" +
            "        GROUP_CONCAT( e.`NAME` ) 'SYSUSERROLENAMES', \n" +
            "        a.ID USERID \n" +
            "        FROM \n" +
            "        sys_user a \n" +
            "        LEFT JOIN sys_user_role d ON a.ID = d.USERID \n" +
            "        AND d.DELETED = '0'  \n" +
            "        LEFT JOIN sys_role e ON d.ROLEID = e.id  \n" +
            "        AND e.DELETED = '0'  \n" +
            "        WHERE  \n" +
            "        a.DELETED = '0'  \n" +
            "        GROUP BY  \n" +
            "        a.ID \n" +
            "        ) e ON e.USERID = a.ID \n" +
            "       where \n" +
            "          a.DELETED = '0' and a.SAFEOBSERVEINDICATOR >0 and a.SAFEOBSERVEINDICATOR <#{count} \n" +
            "        GROUP BY \n" +
            "            a.ID")
    List<SysUserDo> listSafe(int count);
}
