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

<!--
  ~
  ~      Copyright (c) 2018-2025, lengleng All rights reserved.
  ~
  ~  Redistribution and use in source and binary forms, with or without
  ~  modification, are permitted provided that the following conditions are met:
  ~
  ~ Redistributions of source code must retain the above copyright notice,
  ~  this list of conditions and the following disclaimer.
  ~  Redistributions in binary form must reproduce the above copyright
  ~  notice, this list of conditions and the following disclaimer in the
  ~  documentation and/or other materials provided with the distribution.
  ~  Neither the name of the yifu4cloud.com developer nor the names of its
  ~  contributors may be used to endorse or promote products derived from
  ~  this software without specific prior written permission.
  ~  Author: lengleng (wangiegie@gmail.com)
  ~
  -->

<!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.social.mapper.TSocialSoldierMapper">

	<!-- 社保士兵基本查询-->
	<sql id="getSocialSoldierBase" >
		SELECT
			s.id as socialId,
			if(a.TYPE='0','派增','派减') type,
			case a.SOCIAL_HANDLE_STATUS when '0' then '未办理' when '1' then '全部办理成功' when '2' then '全部办理失败' when '3' then '部分办理成功' when '4' then '办理中' else '-' end socialHandleStatus,
			case if(ifnull(h.id,0) != '0',h.PENSION_HANDLE,s.PENSION_HANDLE) when '0' then '派增待办理' when '1' then '派增办理成功' when '2' then '派增办理失败' when '3' then '派减-已派减' when '4' then '派减失败' when '5' then '派减待办理' else '——' end pensionHandle,
			case if(ifnull(h.id,0) != '0',h.MEDICAL_HANDLE,s.MEDICAL_HANDLE) when '0' then '派增待办理' when '1' then '派增办理成功' when '2' then '派增办理失败' when '3' then '派减-已派减' when '4' then '派减失败' when '5' then '派减待办理' else '——' end medicalHandle,
			case if(ifnull(h.id,0) != '0',h.UNEMPLOY_HANDLE,s.UNEMPLOY_HANDLE) when '0' then '派增待办理' when '1' then '派增办理成功' when '2' then '派增办理失败' when '3' then '派减-已派减' when '4' then '派减失败' when '5' then '派减待办理' else '——' end unemployHandle,
			case if(ifnull(h.id,0) != '0',h.WORK_INJURY_HANDLE,s.WORK_INJURY_HANDLE) when '0' then '派增待办理' when '1' then '派增办理成功' when '2' then '派增办理失败' when '3' then '派减-已派减' when '4' then '派减失败' when '5' then '派减待办理' else '——' end workInjuryHandle,
			case if(ifnull(h.id,0) != '0',h.BIRTH_HANDLE,s.BIRTH_HANDLE) when '0' then '派增待办理' when '1' then '派增办理成功' when '2' then '派增办理失败' when '3' then '派减-已派减' when '4' then '派减失败' when '5' then '派减待办理' else '——' end birthHandle,
			case if(ifnull(h.id,0) != '0',h.BIGAILMENT_HANDLE,s.BIGAILMENT_HANDLE) when '0' then '派增待办理' when '1' then '派增办理成功' when '2' then '派增办理失败' when '3' then '派减-已派减' when '4' then '派减失败' when '5' then '派减待办理' else '——' end bigailmentHandle,
			a.EMP_NAME empName,
			a.EMP_IDCARD empIdcard,
			a.EDUCATION_NAME educationName,
			a.BELONG_UNIT_NAME belongUnit,
			a.SETTLE_DOMAIN_NAME settleDomain,
			emp_natrue.label empType,
			a.POST post,
			a.EMP_MOBILE empMobile,
			a.ID_CARD_ADDRESS idCardAddress,
			a.CONTACT_ADDRESS contactAddress,
			registype.label regisType,
			nation.label empNational,
			workingHours.label workingHours,
			a.CONTRACT_TERM contractTerm,
			s.SOCIAL_START_DATE contractStart,
			a.CONTRACT_END contractEnd,
			a.CREATE_NAME createUserName,
			s.RECORD_BASE recordBase,
			case s.PAYMENT_TYPE when '0' then '最低' when '1' then '自定义' when '2' then '最高' else '-' end paymentType,
			a.SOCIAL_HOUSEHOLD_NAME socialHouseholdName,
			if(ifnull(h.id,0) != '0',sp1.AREA_NAME,sp2.AREA_NAME) socialProvince,
			if(ifnull(h.id,0) != '0',sc1.AREA_NAME,sc2.AREA_NAME) socialCity,
			if(ifnull(h.id,0) != '0',st1.AREA_NAME,st2.AREA_NAME) socialTown,
			if(ifnull(h.id,0) != '0',h.PENSION_START,s.PENSION_START) pensionStart,
			if(ifnull(h.id,0) != '0',h.UNIT_PENSION_CARDINAL,s.UNIT_PENSION_CARDINAL) unitPensionCardinal,
			if(ifnull(h.id,0) != '0',h.MEDICAL_START,s.MEDICAL_START) medicalStart,
			if(ifnull(h.id,0) != '0',h.UNIT_MEDICAL_CARDINAL,s.UNIT_MEDICAL_CARDINAL) unitMedicalCardinal,
			if(ifnull(h.id,0) != '0',h.UNEMPLOY_START,s.UNEMPLOY_START) unemployStart,
			if(ifnull(h.id,0) != '0',h.UNIT_UNEMPLOYMENT_CARDINAL,s.UNIT_UNEMPLOYMENT_CARDINAL) unitUnemploymentCardinal,
			if(ifnull(h.id,0) != '0',h.WORK_INJURY_START,s.WORK_INJURY_START) workInjuryStart,
			if(ifnull(h.id,0) != '0',h.UNIT_WORK_INJURY_CARDINAL,s.UNIT_WORK_INJURY_CARDINAL) unitWorkInjuryCardinal,
			if(ifnull(h.id,0) != '0',h.BIRTH_START,s.BIRTH_START) birthStart,
			if(ifnull(h.id,0) != '0',h.UNIT_BIRTH_CARDINAL,s.UNIT_BIRTH_CARDINAL) unitBirthCardinal,
			if(ifnull(h.id,0) != '0',h.BIGAILMENT_START,s.BIGAILMENT_START) bigailmentStart,
			if(ifnull(h.id,0) != '0',h.UNIT_BIGAILMENT_CARDINAL,s.UNIT_BIGAILMENT_CARDINAL) unitBigailmentCardinal,
			if(ifnull(h.id,0) != '0',h.UNIT_BIGAILMENT_MONEY,s.UNIT_BIGAILMENT_MONEY) unitBigailmentMoney,
			if(ifnull(h.id,0) != '0',h.PERSONAL_BIGAILMENT_MONEY,s.PERSONAL_BIGAILMENT_MONEY) personalBigailmentMoney,
			a.TRUST_REMARK socailTrustRemark,
			reduceReason.label reduceReason,
			'1'  as exportSocialFlag,
			a.SCHOOL_NAME schoolName,
			a.MAJOR major,
			a.FIRST_PAY_TIME graduationTime,
			a.GRADUATION_TIME firstPayTime
		FROM t_dispatch_info a
		left join t_social_info s on a.SOCIAL_ID = s.id
		left join t_dispatch_social_fund_info h on a.id = h.REDUCE_SOCIAL_FUND_ID
		left join view_sys_dict_item emp_natrue on emp_natrue.value=a.EMP_TYPE and emp_natrue.type='emp_natrue'
		left join view_sys_dict_item registype on registype.value=a.EMP_REGIS_TYPE and registype.type='emp_registype'
		left join view_sys_dict_item nation on nation.value=a.EMP_NATIONAL and nation.type='emp_national'
		left join view_sys_dict_item workingHours on workingHours.value=a.EMP_NATIONAL and workingHours.type='working_hours'
		left join view_sys_dict_item reduceReason on reduceReason.value=a.EMP_NATIONAL and reduceReason.type='social_reduce_reason'
		left join sys_area sp1 on sp1.id = h.SOCIAL_PROVINCE
		left join sys_area sp2 on sp2.id = s.SOCIAL_PROVINCE
		left join sys_area sc1 on sc1.id = h.SOCIAL_CITY
		left join sys_area sc2 on sc2.id = s.SOCIAL_CITY
		left join sys_area st1 on st1.id = h.SOCIAL_TOWN
		left join sys_area st2 on st2.id = s.SOCIAL_TOWN
		left join t_social_deadline_info sd on sd.MONTH = DATE_FORMAT(a.AUDIT_TIME,'%Y%m') and sd.STATUS = '0'
		and sd.PROVINCE=s.SOCIAL_PROVINCE and sd.CITY=a.SOCIAL_CITY and ( (s.SOCIAL_TOWN is null and sd.TOWN is null) or (sd.TOWN=s.SOCIAL_TOWN))
		<include refid="where_getSocialSoldier_base_handler"/>
		and DATE_FORMAT(a.AUDIT_TIME,'%Y%m') = DATE_FORMAT(now(),'%Y%m')
	</sql>

	<!-- 社保士兵养工失增加模板-->
	<select id="getSocialSoldierYgsAddVoList" resultType="com.yifu.cloud.plus.v1.yifu.social.vo.SocialSoldierYgsAddVo">
		<include refid="getSocialSoldierBase"/>
		and a.type = '0' and s.YGS_HANDLE_STATUS in ('0','1','2','3')
		and (
			if(ifnull(h.id,0) != '0',h.PENSION_HANDLE,s.PENSION_HANDLE) = '0' or
			if(ifnull(h.id,0) != '0',h.WORK_INJURY_HANDLE,s.WORK_INJURY_HANDLE) = '0' or
			if(ifnull(h.id,0) != '0',h.UNEMPLOY_HANDLE,s.UNEMPLOY_HANDLE) = '0'
		)
		and (sd.id is null or NOW() <![CDATA[ <= ]]> sd.SOCIAL_END_DATE)
		group by s.id
	</select>

	<sql id="where_getSocialSoldier_base_handler">
		<where>
			a.DELETE_FLAG = '0'
			<if test="idsStr != null and idsStr.size > 0">
				AND a.ID in
				<foreach item="items" index="index" collection="idsStr" open="(" separator="," close=")">
					#{items}
				</foreach>
			</if>
			/** 社保办理状态:0待办理/1已办理2办理失败3已派减4 办理中 5 部分办理失败 派单社保办理状态 0 未办理 1 全部办理成功(原-已办理) 2 全部办理失败(原-办理失败)  3 部分办理成功 4 办理中  **/
			/*社保代办理 1.派单审核通过 2.派增(type=0):待办理(social_handle_status='0')  or 派减(type=1):已办理完成或部分办理失败 且已派减*/
			AND a.social_id is not null
			AND a.STATUS = "2"
			and (( a.type = '0' AND (s.HANDLE_STATUS = "0" or s.HANDLE_STATUS = "4") ) or ( a.type ='1'  AND (if(ifnull(h.id,0) != '0',h.HANDLE_STATUS,s.HANDLE_STATUS) in ('1','5','6','3','7') or (s.HANDLE_STATUS in ('1','5','6','3','7') and s.REDUCE_CAN = '1'))  ))
			and a.SOCIAL_HANDLE_STATUS in ('0','4')
			and a.AUTO_FLAG = '0'
		</where>
	</sql>

	<!-- 社保士兵养工失减少模板-->
	<select id="getSocialSoldierYgsReduceVoList" resultType="com.yifu.cloud.plus.v1.yifu.social.vo.SocialSoldierYgsAddVo">
		<include refid="getSocialSoldierBase"/>
		and a.type = '1' and s.YGS_HANDLE_STATUS in ('0','1','2','3')
		and (
			if(ifnull(h.id,0) != '0',h.PENSION_HANDLE,s.PENSION_HANDLE) = '5' or
			if(ifnull(h.id,0) != '0',h.WORK_INJURY_HANDLE,s.WORK_INJURY_HANDLE) = '5' or
			if(ifnull(h.id,0) != '0',h.UNEMPLOY_HANDLE,s.UNEMPLOY_HANDLE) = '5'
		)
		and (sd.id is null or NOW() <![CDATA[ <= ]]> sd.SOCIAL_END_DATE)
		group by a.id
	</select>

	<!-- 社保士兵医生大增加模板-->
	<select id="getSocialSoldierYsdAddVoList" resultType="com.yifu.cloud.plus.v1.yifu.social.vo.SocialSoldierYgsAddVo">
		<include refid="getSocialSoldierBase"/>
		and a.type = '0' and s.YSD_HANDLE_STATUS in ('0','1','2','3')
		and (
			if(ifnull(h.id,0) != '0',h.MEDICAL_HANDLE,s.MEDICAL_HANDLE) = '0' or
			if(ifnull(h.id,0) != '0',h.BIRTH_HANDLE,s.BIRTH_HANDLE) = '0' or
			if(ifnull(h.id,0) != '0',h.BIGAILMENT_HANDLE,s.BIGAILMENT_HANDLE) = '0'
		)
		and (sd.id is null or NOW() <![CDATA[ <= ]]> sd.MEDICAL_END_DATE)
		group by s.id
	</select>

	<!-- 社保士兵医生大减少模板-->
	<select id="getSocialSoldierYsdReduceVoList" resultType="com.yifu.cloud.plus.v1.yifu.social.vo.SocialSoldierYgsAddVo">
		<include refid="getSocialSoldierBase"/>
		and a.type = '1' and s.YSD_HANDLE_STATUS in ('0','1','2','3')
		and (
			if(ifnull(h.id,0) != '0',h.MEDICAL_HANDLE,s.MEDICAL_HANDLE) = '5' or
			if(ifnull(h.id,0) != '0',h.BIRTH_HANDLE,s.BIRTH_HANDLE) = '5' or
			if(ifnull(h.id,0) != '0',h.BIGAILMENT_HANDLE,s.BIGAILMENT_HANDLE) = '5'
		)
		and (sd.id is null or NOW() <![CDATA[ <= ]]> sd.MEDICAL_END_DATE)
		group by s.id
	</select>

</mapper>