<?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.insurances.mapper.ekp.EkpSettleMapper">

	<resultMap id="BaseResultMap" type="com.yifu.cloud.plus.v1.yifu.insurances.vo.SettleVo">
		<id property="id" column="fd_id" jdbcType="VARCHAR"/>
		<result property="detailId" column="fd_3b05ba46bb042e" jdbcType="VARCHAR"/>
		<result property="estimateStatus" column="fd_3b05ba4808a3ca" jdbcType="VARCHAR"/>
		<result property="actualStatus" column="fd_3b05bd5ed0b976" jdbcType="VARCHAR"/>
	</resultMap>

	<resultMap id="BaseResultStatusMap" type="com.yifu.cloud.plus.v1.yifu.insurances.vo.EkpInsuranceViewVo">
		<id property="id" column="detailsId" jdbcType="VARCHAR"/>
		<result property="incomeSettleFlag" column="earningStatus" jdbcType="VARCHAR"/>
		<result property="incomeCollectFlag" column="receiptStatus" jdbcType="VARCHAR"/>
		<result property="paySettleFlag" column="zhichuEarningStatus" jdbcType="VARCHAR"/>
		<result property="payCollectFlag" column="payStatus" jdbcType="VARCHAR"/>
		<result property="orderType" column="orderType" jdbcType="VARCHAR"/>
	</resultMap>

	<resultMap id="BaseResultSocialStatusMap" type="com.yifu.cloud.plus.v1.yifu.insurances.vo.EkpSocialViewVo">
		<id property="id" column="detailsId" jdbcType="VARCHAR"/>
		<result property="incomeSettleFlag" column="earningStatus" jdbcType="VARCHAR"/>
		<result property="incomeCollectFlag" column="receiptStatus" jdbcType="VARCHAR"/>
		<result property="paySettleFlag" column="zhichuEarningStatus" jdbcType="VARCHAR"/>
		<result property="payCollectFlag" column="payStatus" jdbcType="VARCHAR"/>
		<result property="settleFlag" column="orderType" jdbcType="VARCHAR"/>
	</resultMap>

	<sql id="Base_Column_List">
		fd_id,fd_3b05ba46bb042e,fd_3b05ba4808a3ca,fd_3b05bd5ed0b976
	</sql>

	<sql id="Base_Column_List_Status">
		detailsId,earningStatus,receiptStatus,zhichuEarningStatus,payStatus,orderType
	</sql>

	<select id="getSettle" resultMap="BaseResultMap">
		select
			<include refid="Base_Column_List"></include>
		from
			ekp_f80727b647a403b975ae
		where
			fd_3b05ba46bb042e = #{id}
	</select>

	<select id="getSettleStatusFromEkpInsuranceDetail" resultType="java.lang.String">
		select
			fd_3adfe6ec6a8cbe
		from
		ekp_e96a0a28442447c81ec0
		where
		fd_3b0a5743acab7e = #{id} limit 1
	</select>

	<!-- 根据结算单编码获取视图信息 -->
	<select id="selectSettleStatusBySettleNo" resultMap="BaseResultStatusMap">
		select
			b.detailsId,
			if(b.earningStatus = '已结算','0',if(b.earningStatus = '结算中','1',if(b.earningStatus = '无需结算','3','2'))) earningStatus,
			if(b.receiptStatus = '已收','0',if(b.receiptStatus = '垫付','2','1')) receiptStatus,
			if(b.zhichuEarningStatus = '已结算','0',if(b.zhichuEarningStatus = '结算中','1',if(b.zhichuEarningStatus = '无需结算','3','2'))) zhichuEarningStatus,
			if(b.payStatus = '已付','0','1') payStatus,
			b.orderType
		from
			view_insurance_info b
		where
			1 = 1
			<if test="payFlag != null  and payFlag != '' and payFlag == 0">
				and b.earningNo = #{settleNo}
			</if>
			<if test="payFlag != null  and payFlag != '' and payFlag == 1">
				and b.zhichuEarningNo = #{settleNo}
			</if>
	</select>

	<!-- 根据结算单编码获取社保视图信息 -->
	<select id="selectSocialViewStatusBySettleNo" resultMap="BaseResultSocialStatusMap">
		select
			a.detailsId,
			if(a.earningStatus = '已结算','0',if(a.earningStatus = '结算中','1',if(a.earningStatus = '已冻结','3','2'))) earningStatus,
			if(a.receiptStatus = '已收','0',if(a.receiptStatus = '垫付','2','1')) receiptStatus,
			if(a.zhichuEarningStatus = '已结算','0',if(a.zhichuEarningStatus = '结算中','1','2')) zhichuEarningStatus,
			if(a.payStatus = '已付','0','1') payStatus,
			a.orderType
		from
			view_social_info a
		where
			1 = 1
			<if test="payFlag != null  and payFlag != '' and payFlag == 0">
				and a.earningNo = #{settleNo}
			</if>
			<if test="payFlag != null  and payFlag != '' and payFlag == 1">
				and a.zhichuEarningNo = #{settleNo}
			</if>
	</select>

	<!-- 根据结算单编码获取公积金视图信息 -->
	<select id="selectFundViewBySettleNo" resultMap="BaseResultSocialStatusMap">
		select
			a.detailsId,
			if(a.earningStatus = '已结算','0',if(a.earningStatus = '结算中','1',if(a.earningStatus = '已冻结','3','2'))) earningStatus,
			if(a.receiptStatus = '已收','0',if(a.receiptStatus = '垫付','2','1')) receiptStatus,
			if(a.zhichuEarningStatus = '已结算','0',if(a.zhichuEarningStatus = '结算中','1','2')) zhichuEarningStatus,
			if(a.payStatus = '已付','0','1') payStatus,
			a.orderType
		from
			view_funds_info a
		where
			1 = 1
			<if test="payFlag != null  and payFlag != '' and payFlag == 0">
				and a.earningNo = #{settleNo}
			</if>
			<if test="payFlag != null  and payFlag != '' and payFlag == 1">
				and a.zhichuEarningNo = #{settleNo}
			</if>
	</select>

	<!-- 根据结算单编码获取视图信息 -->
	<select id="selectFIncomelViewBySettleNo" resultMap="BaseResultStatusMap">
		select
			a.detailsId,
			if(a.earningStatus = '已结算','0',if(a.earningStatus = '结算中','1',if(a.earningStatus = '已冻结','3','2'))) earningStatus,
			if(a.receiptStatus = '已收','0',if(a.receiptStatus = '垫付','2','1')) receiptStatus
		from
			view_riskfund_info a
		where
			a.earningNo = #{settleNo}
	</select>

	<!-- 根据结算单编码获取视图信息 -->
	<select id="selectIncomeViewBySettleNo" resultMap="BaseResultStatusMap">
		select
			a.detailsId,
			if(a.earningStatus = '已结算','0',if(a.earningStatus = '结算中','1',if(a.earningStatus = '已冻结','3','2'))) earningStatus,
			if(a.receiptStatus = '已收','0',if(a.receiptStatus = '垫付','2','1')) receiptStatus
		from
			view_management_info a
		where
			a.earningNo = #{settleNo}
	</select>

	<!-- 根据结算单编码获取视图信息 -->
	<select id="selectSalarylViewBySettleNo" resultMap="BaseResultSocialStatusMap">
		SELECT
		gongzi.fd_3b10afe8c70742 AS detailsId,
		if(gongzi.fd_3adfee4ba5ad36 = '已结算','0',if(gongzi.fd_3adfee4ba5ad36 = '结算中','1',if(gongzi.fd_3adfee4ba5ad36 = '已冻结','3','2'))) earningStatus,
		if(gongzi.fd_3adfee4c0c59ee = '已收','0',if(gongzi.fd_3adfee4c0c59ee = '垫付','2','1')) receiptStatus,
		if(gongzi.fd_3adfeef9440ab2 = '已结算','0',if(gongzi.fd_3adfeef9440ab2 = '结算中','1','2')) zhichuEarningStatus,
		if(fukuan.fd_3b148bc0c28e48 = '已付','0','1') payStatus
		FROM
		ekp_salary_info gongzi
		LEFT JOIN ekp_db4ed54e25543f4b7a9a fukuan ON gongzi.fd_id = fukuan.fd_3b1fe00b0f5dde
		where
		1 = 1
		<if test="payFlag != null  and payFlag != '' and payFlag == 0">
			and gongzi.fd_3adfee5dd14866 = #{settleNo}
		</if>
		<if test="payFlag != null  and payFlag != '' and payFlag == 1">
			and gongzi.fd_3aeadf17522642 = #{settleNo}
		</if>
	</select>
</mapper>