<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.yifu.cloud.plus.v1.yifu.archives.mapper.TRiskMonitorMapper">
	<resultMap id="tRiskMonitorMap" type="com.yifu.cloud.plus.v1.yifu.archives.entity.TRiskMonitor">
		<id property="id" column="ID"/>
		<result property="deptId" column="DEPT_ID"/>
		<result property="departNo" column="DEPART_NO"/>
		<result property="departName" column="DEPART_NAME"/>
		<result property="stopFlag" column="STOP_FLAG"/>
		<result property="businessPrimaryType" column="BUSINESS_PRIMARY_TYPE"/>
		<result property="businessSecondType" column="BUSINESS_SECOND_TYPE"/>
		<result property="businessThirdType" column="BUSINESS_THIRD_TYPE"/>
		<result property="customerName" column="CUSTOMER_NAME"/>
		<result property="customerNo" column="CUSTOMER_NO"/>
		<result property="secondIndicatorBelong" column="SECOND_INDICATOR_BELONG"/>
		<result property="lineType" column="LINE_TYPE"/>
		<result property="buBelong" column="BU_BELONG"/>
		<result property="isRisk" column="IS_RISK"/>
		<result property="riskType" column="RISK_TYPE"/>
		<result property="projectNum" column="PROJECT_NUM"/>
		<result property="unproContractNum" column="UNPRO_CONTRACT_NUM"/>
		<result property="proContractNum" column="PRO_CONTRACT_NUM"/>
		<result property="unproInsuranceNum" column="UNPRO_INSURANCE_NUM"/>
		<result property="proInsuranceNum" column="PRO_INSURANCE_NUM"/>
		<result property="unproSocialNum" column="UNPRO_SOCIAL_NUM"/>
		<result property="proSocialNum" column="PRO_SOCIAL_NUM"/>
		<result property="unproFundNum" column="UNPRO_FUND_NUM"/>
		<result property="proFundNum" column="PRO_FUND_NUM"/>
		<result property="unproSalaryNum" column="UNPRO_SALARY_NUM"/>
		<result property="proSalaryNum" column="PRO_SALARY_NUM"/>
		<result property="lastSalaryTime" column="LAST_SALARY_TIME"/>
		<result property="deleteFlag" column="DELETE_FLAG"/>
		<result property="createBy" column="CREATE_BY"/>
		<result property="createName" column="CREATE_NAME"/>
		<result property="createTime" column="CREATE_TIME"/>
		<result property="updateBy" column="UPDATE_BY"/>
		<result property="updateTime" column="UPDATE_TIME"/>
	</resultMap>

	<resultMap id="tRiskMonitorExportMap" type="com.yifu.cloud.plus.v1.yifu.archives.vo.TRiskMonitorVo">
		<result property="departNo" column="DEPART_NO"/>
		<result property="departName" column="DEPART_NAME"/>
		<result property="stopFlag" column="STOP_FLAG"/>
		<result property="businessPrimaryType" column="BUSINESS_PRIMARY_TYPE"/>
		<result property="businessSecondType" column="BUSINESS_SECOND_TYPE"/>
		<result property="businessThirdType" column="BUSINESS_THIRD_TYPE"/>
		<result property="customerName" column="CUSTOMER_NAME"/>
		<result property="customerNo" column="CUSTOMER_NO"/>
		<result property="secondIndicatorBelong" column="SECOND_INDICATOR_BELONG"/>
		<result property="lineType" column="LINE_TYPE"/>
		<result property="buBelong" column="BU_BELONG"/>
		<result property="isRisk" column="IS_RISK"/>
		<result property="riskType" column="RISK_TYPE"/>
		<result property="projectNum" column="PROJECT_NUM"/>
		<result property="unproContractNum" column="UNPRO_CONTRACT_NUM"/>
		<result property="proContractNum" column="PRO_CONTRACT_NUM"/>
		<result property="unproInsuranceNum" column="UNPRO_INSURANCE_NUM"/>
		<result property="proInsuranceNum" column="PRO_INSURANCE_NUM"/>
		<result property="unproSocialNum" column="UNPRO_SOCIAL_NUM"/>
		<result property="proSocialNum" column="PRO_SOCIAL_NUM"/>
		<result property="unproFundNum" column="UNPRO_FUND_NUM"/>
		<result property="proFundNum" column="PRO_FUND_NUM"/>
		<result property="unproSalaryNum" column="UNPRO_SALARY_NUM"/>
		<result property="proSalaryNum" column="PRO_SALARY_NUM"/>
		<result property="lastSalaryTime" column="LAST_SALARY_TIME"/>
	</resultMap>

	<sql id="Base_Column_List">
		a.ID,
				a.DEPT_ID,
                a.DEPART_NO,
                a.DEPART_NAME,
                a.STOP_FLAG,
                a.BUSINESS_PRIMARY_TYPE,
                a.BUSINESS_SECOND_TYPE,
                a.BUSINESS_THIRD_TYPE,
                a.CUSTOMER_NAME,
                a.CUSTOMER_NO,
                a.SECOND_INDICATOR_BELONG,
                a.LINE_TYPE,
                a.BU_BELONG,
                a.IS_RISK,
                substring(a.risk_type,1,CHAR_LENGTH(a.risk_type) -2) risk_type,
                a.PROJECT_NUM,
                a.UNPRO_CONTRACT_NUM,
                a.PRO_CONTRACT_NUM,
                a.UNPRO_INSURANCE_NUM,
                a.PRO_INSURANCE_NUM,
                a.UNPRO_SOCIAL_NUM,
                a.PRO_SOCIAL_NUM,
                a.UNPRO_FUND_NUM,
                a.PRO_FUND_NUM,
                a.UNPRO_SALARY_NUM,
                a.PRO_SALARY_NUM,
                a.LAST_SALARY_TIME,
                a.DELETE_FLAG,
                a.CREATE_BY,
                a.CREATE_NAME,
                a.CREATE_TIME,
                a.UPDATE_BY,
                a.UPDATE_TIME
	</sql>
	<select id="getTRiskMonitorPage" resultMap="tRiskMonitorMap">
		SELECT
		<include refid="Base_Column_List"/>
		FROM t_risk_monitor a
		<where>
			a.DELETE_FLAG = '0'
			<include refid="tRiskMonitor_where"/>
		</where>
	</select>

	<!--tRiskMonitor简单分页查询-->
	<sql id="tRiskMonitor_where">
		<if test="tRiskMonitor != null">
			<if test="tRiskMonitor.id != null and tRiskMonitor.id.trim() != ''">
				AND a.ID = #{tRiskMonitor.id}
			</if>
			<if test="tRiskMonitor.departNo != null and tRiskMonitor.departNo.trim() != ''">
				AND a.DEPART_NO = #{tRiskMonitor.departNo}
			</if>
			<if test="tRiskMonitor.departName != null and tRiskMonitor.departName.trim() != ''">
				AND a.DEPART_NAME like concat('%',#{tRiskMonitor.departName},'%')
			</if>
			<if test="tRiskMonitor.stopFlag != null and tRiskMonitor.stopFlag.trim() != ''">
				AND a.STOP_FLAG = #{tRiskMonitor.stopFlag}
			</if>
			<if test="tRiskMonitor.businessPrimaryType != null and tRiskMonitor.businessPrimaryType.trim() != ''">
				AND a.BUSINESS_PRIMARY_TYPE = #{tRiskMonitor.businessPrimaryType}
			</if>
			<if test="tRiskMonitor.businessSecondType != null and tRiskMonitor.businessSecondType.trim() != ''">
				AND a.BUSINESS_SECOND_TYPE = #{tRiskMonitor.businessSecondType}
			</if>
			<if test="tRiskMonitor.businessThirdType != null and tRiskMonitor.businessThirdType.trim() != ''">
				AND a.BUSINESS_THIRD_TYPE = #{tRiskMonitor.businessThirdType}
			</if>
			<if test="tRiskMonitor.customerName != null and tRiskMonitor.customerName.trim() != ''">
				AND a.CUSTOMER_NAME = #{tRiskMonitor.customerName}
			</if>
			<if test="tRiskMonitor.customerNo != null and tRiskMonitor.customerNo.trim() != ''">
				AND a.CUSTOMER_NO = #{tRiskMonitor.customerNo}
			</if>
			<if test="tRiskMonitor.secondIndicatorBelong != null and tRiskMonitor.secondIndicatorBelong.trim() != ''">
				AND a.SECOND_INDICATOR_BELONG like concat('%',#{tRiskMonitor.secondIndicatorBelong},'%')
			</if>
			<if test="tRiskMonitor.lineType != null and tRiskMonitor.lineType.trim() != ''">
				AND a.LINE_TYPE = #{tRiskMonitor.lineType}
			</if>
			<if test="tRiskMonitor.buBelong != null and tRiskMonitor.buBelong.trim() != ''">
				AND a.BU_BELONG = #{tRiskMonitor.buBelong}
			</if>
			<if test="tRiskMonitor.isRisk != null and tRiskMonitor.isRisk.trim() != ''">
				AND a.IS_RISK = #{tRiskMonitor.isRisk}
			</if>
			<if test="tRiskMonitor.riskType != null and tRiskMonitor.riskType.trim() != ''">
				AND a.RISK_TYPE regexp #{tRiskMonitor.riskType}
			</if>
			<if test="tRiskMonitor.projectNumStart != null">
				AND a.PROJECT_NUM <![CDATA[ >= ]]> #{tRiskMonitor.projectNumStart}
			</if>
			<if test="tRiskMonitor.projectNumEnd != null">
				AND a.PROJECT_NUM <![CDATA[ <= ]]> #{tRiskMonitor.projectNumEnd}
			</if>
			<if test="tRiskMonitor.authSql != null and tRiskMonitor.authSql.trim() != ''">
				${tRiskMonitor.authSql}
			</if>
		</if>
	</sql>

	<select id="getTRiskMonitorExportCount" resultType="java.lang.Long">
		SELECT
		count(1)
		FROM t_risk_monitor a
		<where>
			a.DELETE_FLAG = '0'
			<include refid="tRiskMonitor_where"/>
		</where>
	</select>

	<!--tRiskMonitor导出查询-->
	<select id="getRiskMonitorExportList" resultMap="tRiskMonitorExportMap">
		SELECT
		a.DEPART_NAME,
		a.DEPART_NO,
		CASE WHEN a.STOP_FLAG=0 THEN "正常"
		WHEN a.STOP_FLAG =1 THEN "停止合作"
		WHEN a.STOP_FLAG =2 THEN "已冻结"
		ELSE a.STOP_FLAG END as STOP_FLAG,
		a.SECOND_INDICATOR_BELONG,
		CASE WHEN a.IS_RISK=0 THEN "是"
		WHEN a.IS_RISK =1 THEN "否"
		ELSE a.IS_RISK END as IS_RISK,
		substring(a.risk_type,1,CHAR_LENGTH(a.risk_type) -2) risk_type,
		a.PROJECT_NUM,
		a.UNPRO_CONTRACT_NUM,
		a.PRO_CONTRACT_NUM,
		a.UNPRO_INSURANCE_NUM,
		a.PRO_INSURANCE_NUM,
		a.UNPRO_SOCIAL_NUM,
		a.PRO_SOCIAL_NUM,
		a.UNPRO_FUND_NUM,
		a.PRO_FUND_NUM,
		a.UNPRO_SALARY_NUM,
		a.PRO_SALARY_NUM,
		a.LAST_SALARY_TIME,
		a.CUSTOMER_NAME,
		a.CUSTOMER_NO,
		a.BUSINESS_PRIMARY_TYPE,
		a.BUSINESS_SECOND_TYPE,
		a.BUSINESS_THIRD_TYPE,
		a.LINE_TYPE,
		a.BU_BELONG
		FROM t_risk_monitor a
		<where>
			a.DELETE_FLAG = '0'
			<include refid="tRiskMonitor_where"/>
		</where>
		<if test="tRiskMonitor.limitStart != null">
			limit #{tRiskMonitor.limitStart},#{tRiskMonitor.limitEnd}
		</if>
	</select>

	<!--tRiskMonitor批量生成-->
	<select id="selectRiskMonitorList" resultMap="tRiskMonitorMap">
		SELECT
			A.id AS DEPT_ID,
			a.STOP_FLAG,
			a.DEPART_NAME,
			a.DEPART_NO,
			ifnull( a.SECOND_INDICATOR_BELONG, '' ) SECOND_INDICATOR_BELONG,
			a.CUSTOMER_NAME,
			a.CUSTOMER_NO,
			ifnull( a.BUSINESS_SECOND_TYPE, '' ) BUSINESS_SECOND_TYPE,
			ifnull( a.BUSINESS_PRIMARY_TYPE, '' ) BUSINESS_PRIMARY_TYPE,
			ifnull( a.BUSINESS_THIRD_TYPE, '' ) BUSINESS_THIRD_TYPE,
			ifnull( a.BU_BELONG, '' ) BU_BELONG,
			ifnull( a.LINE_TYPE, '' ) LINE_TYPE,
			CONCAT(
					IF
						( ifnull( pro.countPer, 0 ) > ifnull( q.onContractNum, 0 ), '合同漏签 / ', '' ),
					IF
						( ifnull( ht.countContract, 0 ) - ifnull( q.onContractNum, 0 ) > 0, '合同漏减 / ', '' ),
					IF
						( ifnull( pro.countPer, 0 ) > ifnull( r.onInsuranceNum, 0 ), '商险漏买 / ', '' ),
					IF
						( ifnull( sx.countInsurance, 0 ) - ifnull( r.onInsuranceNum, 0 ) > 0, '商险多买 / ', '' ),
					IF
						( ifnull( pro.countPer, 0 ) > ifnull( w.onSocialNum, 0 ), '社保漏买 / ', '' ),
					IF
						( ifnull( so.countSocial, 0 ) - ifnull( w.onSocialNum, 0 ) > 0, '社保多买 / ', '' ),
					IF
						( ifnull( pro.countPer, 0 ) > ifnull( e.onFundNum, 0 ), '公积金漏买 / ', '' ),
					IF
						( ifnull( fu.countFund, 0 ) - ifnull( e.onFundNum, 0 ) > 0, '公积金多买 / ', '' ),
					IF
						( ifnull( pro.countPer, 0 ) > ifnull( t.onSalaryNum, 0 ), '工资漏发 / ', '' ),
					IF
						( ifnull( sa.countSalary, 0 ) - ifnull( t.onSalaryNum, 0 ) > 0, '工资多发 / ', '' )
				) RISK_TYPE,
			IF
				(
						length(
								CONCAT(
										IF
											( ifnull( pro.countPer, 0 ) > ifnull( q.onContractNum, 0 ), '合同漏签 / ', '' ),
										IF
											( ifnull( ht.countContract, 0 ) - ifnull( q.onContractNum, 0 ) > 0, '合同漏减 / ', '' ),
										IF
											( ifnull( pro.countPer, 0 ) > ifnull( r.onInsuranceNum, 0 ), '商险漏买 / ', '' ),
										IF
											( ifnull( sx.countInsurance, 0 ) - ifnull( r.onInsuranceNum, 0 ) > 0, '商险多买 / ', '' ),
										IF
											( ifnull( pro.countPer, 0 ) > ifnull( w.onSocialNum, 0 ), '社保漏买 / ', '' ),
										IF
											( ifnull( so.countSocial, 0 ) - ifnull( w.onSocialNum, 0 ) > 0, '社保多买 / ', '' ),
										IF
											( ifnull( pro.countPer, 0 ) > ifnull( e.onFundNum, 0 ), '公积金漏买 / ', '' ),
										IF
											( ifnull( fu.countFund, 0 ) - ifnull( e.onFundNum, 0 ) > 0, '公积金多买 / ', '' ),
										IF
											( ifnull( pro.countPer, 0 ) > ifnull( t.onSalaryNum, 0 ), '工资漏发 / ', '' ),
										IF
											( ifnull( sa.countSalary, 0 ) - ifnull( t.onSalaryNum, 0 ) > 0, '工资多发 / ', '' )
									)) > 0,
						'0',
						'1'
				) IS_RISK,
			ifnull( q.onContractNum, 0 ) PRO_CONTRACT_NUM,
			ifnull( w.onSocialNum, 0 ) PRO_SOCIAL_NUM,
			ifnull( e.onFundNum, 0 ) PRO_FUND_NUM,
			ifnull( r.onInsuranceNum, 0 ) PRO_INSURANCE_NUM,
			ifnull( t.onSalaryNum, 0 ) PRO_SALARY_NUM,
			ifnull( ht.countContract, 0 ) - ifnull( q.onContractNum, 0 ) UNPRO_CONTRACT_NUM,
			ifnull( so.countSocial, 0 ) - ifnull( w.onSocialNum, 0 ) UNPRO_SOCIAL_NUM,
			ifnull( fu.countFund, 0 ) - ifnull( e.onFundNum, 0 ) UNPRO_FUND_NUM,
			ifnull( sx.countInsurance, 0 ) - ifnull( r.onInsuranceNum, 0 ) UNPRO_INSURANCE_NUM,
			ifnull( sa.countSalary, 0 ) - ifnull( t.onSalaryNum, 0 ) UNPRO_SALARY_NUM,
			ifnull( DATE_FORMAT( sa.CREATE_TIME, '%Y-%m-%d %H:%i:%s' ), '' ) LAST_SALARY_TIME,
			ifnull( pro.countPer, 0 ) PROJECT_NUM,
			'1' create_by,
			'超管' create_name,
			'1' UPDATE_BY
		FROM
			t_settle_domain a
				LEFT JOIN (
				SELECT
					a.DEPT_NO,
					count( DISTINCT a.EMP_IDCARD ) AS countContract
				FROM
					t_employee_contract_info a
				WHERE
					a.IN_USE = '0'
				  AND a.DELETE_FLAG = 0
				  AND (CONTRACT_END >= CURDATE() or (ifnull(CONTRACT_END,-1) = -1 and a.CONTRACT_TYPE = '2'))
				  AND length(
							  ifnull(a.DEPT_NO, '')) > 0
				GROUP BY
					a.DEPT_NO
			) ht ON a.DEPART_NO = ht.DEPT_NO
				LEFT JOIN (
				SELECT
					a.SETTLE_DOMAIN_CODE,
					count( DISTINCT a.EMP_IDCARD ) AS countSocial
				FROM
					mvp_social.t_social_fund_info a
				WHERE
					a.SOCIAL_STATUS IN ( '3', '4', '9', '12' )
				  AND length(
							  ifnull( a.SETTLE_DOMAIN_CODE, '')) > 0
				GROUP BY
					a.SETTLE_DOMAIN_CODE
			) so ON so.SETTLE_DOMAIN_CODE = a.DEPART_NO
				LEFT JOIN (
				SELECT
					a.SETTLE_DOMAIN_CODE,
					count( DISTINCT a.EMP_IDCARD ) AS countFund
				FROM
					mvp_social.t_social_fund_info a
				WHERE
					a.fund_STATUS IN ( '3', '8' )
				  AND length(
							  ifnull( a.SETTLE_DOMAIN_CODE, '')) > 0
				GROUP BY
					a.SETTLE_DOMAIN_CODE
			) fu ON fu.SETTLE_DOMAIN_CODE = a.DEPART_NO
				LEFT JOIN (
				SELECT
					a.DEPT_NO,
					count( DISTINCT a.EMP_IDCARD_NO ) AS countInsurance
				FROM
					mvp_insurances.t_insurance_detail a
				WHERE
					a.IS_USE = 0
				  AND IS_EFFECT = 0
				  AND a.IS_OVERDUE = 0
				  AND a.DELETE_FLAG = 0
				  AND length(
							  ifnull( a.DEPT_NO, '')) > 0
				GROUP BY
					a.DEPT_NO
			) sx ON a.DEPART_NO = sx.DEPT_NO
				LEFT JOIN (
				SELECT
					count( DISTINCT acc.EMP_IDCARD ) AS countSalary,
					lll.CREATE_TIME,
					lll.DEPT_NO
				FROM
					mvp_salary.t_salary_standard lll
						LEFT JOIN mvp_salary.t_salary_account acc ON lll.id = acc.SALARY_FORM_ID
						AND acc.DELETE_FLAG = 0
				WHERE
						lll.id IN (
						SELECT
							b.id
						FROM
							(
								SELECT
									a.DEPT_NO,
									a.id,
									max( a.CREATE_TIME ) CREATE_TIME
								FROM
									mvp_salary.t_salary_standard a
								WHERE
									a.DELETE_FLAG = 0
								  AND length(
											  ifnull( a.DEPT_NO, '')) > 0
								GROUP BY
									a.DEPT_NO
							) hh
								INNER JOIN mvp_salary.t_salary_standard b ON hh.DEPT_NO = b.DEPT_NO
								AND hh.CREATE_TIME = b.CREATE_TIME
					)
				GROUP BY
					lll.id
			) sa ON a.DEPART_NO = sa.DEPT_NO
				LEFT JOIN (
				SELECT
					a.DEPT_NO,
					a.EMP_IDCARD,
					count( DISTINCT a.EMP_IDCARD ) countPer
				FROM
					t_employee_project a
				WHERE
					a.PROJECT_STATUS = 0
				  AND a.DELETE_FLAG = '0'
				GROUP BY
					a.DEPT_NO
			) pro ON a.DEPART_NO = pro.DEPT_NO
				LEFT JOIN (
				SELECT
					a.DEPT_NO,
					count( DISTINCT a.EMP_IDCARD ) onContractNum
				FROM
					t_employee_contract_info a
						INNER JOIN t_employee_project jj ON a.EMP_IDCARD = jj.EMP_IDCARD
						AND a.DEPT_NO = jj.DEPT_NO
						AND jj.PROJECT_STATUS = 0
						AND jj.DELETE_FLAG = '0'
				WHERE
					a.IN_USE = '0'
				  AND (CONTRACT_END >= CURDATE() or (ifnull(CONTRACT_END,-1) = -1 and a.CONTRACT_TYPE = '2'))
				  AND a.DELETE_FLAG = 0
				  AND length(
							  ifnull( a.DEPT_NO, '')) > 0
				GROUP BY
					a.DEPT_NO
			) q ON a.DEPART_NO = q.DEPT_NO
				LEFT JOIN (
				SELECT
					a.SETTLE_DOMAIN_CODE,
					count( DISTINCT a.EMP_IDCARD ) onSocialNum
				FROM
					mvp_social.t_social_fund_info a
						INNER JOIN t_employee_project jj ON a.EMP_IDCARD = jj.EMP_IDCARD
						AND a.SETTLE_DOMAIN_CODE = jj.DEPT_NO
						AND jj.PROJECT_STATUS = 0
						AND jj.DELETE_FLAG = '0'
				WHERE
					a.SOCIAL_STATUS IN ( '3', '4', '9', '12' )
				  AND length(
							  ifnull( a.SETTLE_DOMAIN_CODE, '')) > 0
				GROUP BY
					a.SETTLE_DOMAIN_CODE
			) w ON a.DEPART_NO = w.SETTLE_DOMAIN_CODE
				LEFT JOIN (
				SELECT
					a.SETTLE_DOMAIN_CODE,
					count( DISTINCT a.EMP_IDCARD ) onFundNum
				FROM
					mvp_social.t_social_fund_info a
						INNER JOIN t_employee_project jj ON a.EMP_IDCARD = jj.EMP_IDCARD
						AND a.SETTLE_DOMAIN_CODE = jj.DEPT_NO
						AND jj.PROJECT_STATUS = 0
						AND jj.DELETE_FLAG = '0'
				WHERE
					a.fund_STATUS IN ( '3', '8' )
				  AND length(
							  ifnull( a.SETTLE_DOMAIN_CODE, '')) > 0
				GROUP BY
					a.SETTLE_DOMAIN_CODE
			) e ON a.DEPART_NO = e.SETTLE_DOMAIN_CODE
				LEFT JOIN (
				SELECT
					a.DEPT_NO,
					count( DISTINCT a.EMP_IDCARD_NO ) onInsuranceNum
				FROM
					mvp_insurances.t_insurance_detail a
						INNER JOIN t_employee_project jj ON a.EMP_IDCARD_NO = jj.EMP_IDCARD
						AND a.DEPT_NO = jj.DEPT_NO
						AND jj.PROJECT_STATUS = 0
						AND jj.DELETE_FLAG = '0'
				WHERE
					a.IS_USE = 0
				  AND IS_EFFECT = 0
				  AND a.IS_OVERDUE = 0
				  AND a.DELETE_FLAG = 0
				  AND length(
							  ifnull( a.DEPT_NO, '')) > 0
				GROUP BY
					a.DEPT_NO
			) r ON a.DEPART_NO = r.DEPT_NO
				LEFT JOIN (
				SELECT
					count( DISTINCT acc.EMP_IDCARD ) AS onSalaryNum,
					lll.DEPT_NO
				FROM
					mvp_salary.t_salary_standard lll
						LEFT JOIN mvp_salary.t_salary_account acc ON lll.id = acc.SALARY_FORM_ID
						AND acc.DELETE_FLAG = 0
						INNER JOIN t_employee_project jj ON acc.EMP_IDCARD = jj.EMP_IDCARD
						AND lll.DEPT_NO = jj.DEPT_NO
						AND jj.PROJECT_STATUS = 0
						AND jj.DELETE_FLAG = '0'
				WHERE
						lll.id IN (
						SELECT
							b.id
						FROM
							(
								SELECT
									a.DEPT_NO,
									a.id,
									max( a.CREATE_TIME ) CREATE_TIME
								FROM
									mvp_salary.t_salary_standard a
								WHERE
									a.DELETE_FLAG = 0
								  AND length(
											  ifnull( a.DEPT_NO, '')) > 0
								GROUP BY
									a.DEPT_NO
							) hh
								INNER JOIN mvp_salary.t_salary_standard b ON hh.DEPT_NO = b.DEPT_NO
								AND hh.CREATE_TIME = b.CREATE_TIME
					)
				GROUP BY
					lll.DEPT_NO,
					lll.id
			) t ON a.DEPART_NO = t.DEPT_NO
		WHERE
			a.DELETE_FLAG = '0'
		GROUP BY
			a.DEPART_NO
	</select>

	<select id="getTRiskMonitorPer" resultType="com.yifu.cloud.plus.v1.yifu.archives.vo.RiskMonitorPerVo">
		SELECT
		count(1) as deptNum,
		sum(if(IS_RISK = '0',1,0)) as riskDeptNum,
		concat(round(sum(if(IS_RISK = '0',1,0)) *100/count(1),2),'%') riskDeptPer,
		sum(if(PROJECT_NUM > PRO_INSURANCE_NUM,1,0)) as riskInsuraceNum,
		concat(round(sum(if(PROJECT_NUM > PRO_INSURANCE_NUM,1,0))*100/count(1),2),'%') riskInsuracePer
		FROM
		t_risk_monitor a
		where
			a.DELETE_FLAG = '0' and a.STOP_FLAG = '0'
		<if test="tRiskMonitor.authSql != null and tRiskMonitor.authSql.trim() != ''">
			${tRiskMonitor.authSql}
		</if>
	</select>

	<!--删除所有监控数据-->
	<update id="deleteAllRiskMonitorInfo">
		TRUNCATE t_risk_monitor
	</update>
</mapper>
