<?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.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.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(a.EMP_IDCARD) as countContract
			from t_employee_contract_info a
			where a.IN_USE = '0'
			  and CONTRACT_END >= CURDATE()
			  and length(ifnull(a.DEPT_NO, 0)) > 0
			group by a.DEPT_NO
		) ht on a.DEPART_NO = ht.DEPT_NO
				 left join (
			select a.SETTLE_DOMAIN_CODE,
				   count(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)) > 0
			group by a.SETTLE_DOMAIN_CODE
		) so on so.SETTLE_DOMAIN_CODE = a.DEPART_NO
				 left join (
			select a.SETTLE_DOMAIN_CODE,
				   count(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)) > 0
			group by a.SETTLE_DOMAIN_CODE
		) fu on fu.SETTLE_DOMAIN_CODE = a.DEPART_NO
				 left join (
			select a.DEPT_NO,
				   count(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 length(ifnull(a.DEPT_NO, 0)) > 0
			group by a.DEPT_NO
		) sx on a.DEPART_NO = sx.DEPT_NO

				 left join (
			select count(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
			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)) > 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(a.EMP_IDCARD) countPer
				 from t_employee_project a
				 where a.PROJECT_STATUS = 0
				 group by a.DEPT_NO) pro on a.DEPART_NO = pro.DEPT_NO

				 left join (select a.DEPT_NO,
								   count(a.EMP_IDCARD) onContractNum
							from t_employee_contract_info a
							where a.IN_USE = '0'
							  and CONTRACT_END >= CURDATE()
							  and length(ifnull(a.DEPT_NO, 0)) > 0
							  and EXISTS(
									select 1
									from t_employee_project a1
									where a1.PROJECT_STATUS = 0
									  and a1.DELETE_FLAG = '0'
									  and a.EMP_IDCARD = a1.EMP_IDCARD
									  and a.DEPT_NO = a1.DEPT_NO)
							group by a.DEPT_NO) q on a.DEPART_NO = q.DEPT_NO
				 left join
			 (
				 select a.SETTLE_DOMAIN_CODE,
						count(a.EMP_IDCARD) onSocialNum
				 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)) > 0
				   and EXISTS(
						 select 1
						 from t_employee_project a1
						 where a1.PROJECT_STATUS = 0
						   and a1.DELETE_FLAG = '0'
						   and a.EMP_IDCARD = a1.EMP_IDCARD
						   and a.SETTLE_DOMAIN_CODE = a1.DEPT_NO)
				 group by a.SETTLE_DOMAIN_CODE
			 ) w on a.DEPART_NO = w.SETTLE_DOMAIN_CODE
				 left join
			 (
				 select a.SETTLE_DOMAIN_CODE,
						count(a.EMP_IDCARD) onFundNum
				 from mvp_social.t_social_fund_info a
				 where a.fund_STATUS in ('3', '8')
				   and length(ifnull(a.SETTLE_DOMAIN_CODE, 0)) > 0
				   and EXISTS(
						 select 1
						 from t_employee_project a1
						 where a1.PROJECT_STATUS = 0
						   and a1.DELETE_FLAG = '0'
						   and a.EMP_IDCARD = a1.EMP_IDCARD
						   and a.SETTLE_DOMAIN_CODE = a1.DEPT_NO)
				 group by a.SETTLE_DOMAIN_CODE
			 ) e on a.DEPART_NO = e.SETTLE_DOMAIN_CODE
				 left join
			 (
				 select a.DEPT_NO,
						count(a.EMP_IDCARD_NO) onInsuranceNum
				 from mvp_insurances.t_insurance_detail a
				 where a.IS_USE = 0
				   and IS_EFFECT = 0
				   and a.IS_OVERDUE = 0
				   and length(ifnull(a.DEPT_NO, 0)) > 0
				   and EXISTS(
						 select 1
						 from t_employee_project a1
						 where a1.PROJECT_STATUS = 0
						   and a1.DELETE_FLAG = '0'
						   and a.EMP_IDCARD_NO = a1.EMP_IDCARD
						   and a.DEPT_NO = a1.DEPT_NO)
				 group by a.DEPT_NO
			 ) r on a.DEPART_NO = r.DEPT_NO
				 left join
			 (
				 select count(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
				 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)) > 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)
				   and EXISTS(
						 select 1
						 from t_employee_project a1
						 where a1.PROJECT_STATUS = 0
						   and a1.DELETE_FLAG = '0'
						   and acc.EMP_IDCARD = a1.EMP_IDCARD
						   and lll.DEPT_NO = a1.DEPT_NO)
				 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'
		<if test="tRiskMonitor.authSql != null and tRiskMonitor.authSql.trim() != ''">
			${tRiskMonitor.authSql}
		</if>
	</select>
</mapper>
