<?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="cn.timer.api.dao.xcgl.XcglAssoXzdazdyMapper">

    <resultMap id="BaseResultMap" type="cn.timer.api.bean.xcgl.XcglAssoXzdazdy" >
                <id column="id" property="id" />
                <result column="zdmc" property="zdmc" />
                <result column="zdsrklx" property="zdsrklx" />
                <result column="zdlx" property="zdlx" />
                <result column="qyid" property="qyid" />
                <result column="xhpx" property="xhpx" />
    </resultMap>
    
    <resultMap id="FixedSalaryStaffMap" type="cn.timer.api.dto.xcgl.FixedSalaryStaffDto" >
            <result column="id" property="id" />
            <result column="empnum" property="empnum" />
            <result column="empname" property="empname" />
            <result column="empjobtype" property="empjobtype" />
            <result column="empjobstatus" property="empjobstatus" />
            <result column="emprztime" property="emprztime" />
            <result column="empzztime" property="empzztime" />
            <result column="sfdx" property="sfdx" />
            <result column="tratime" property="tratime" />
			<result column="txzt" property="txzt" />
			<result column="remarks" property="remarks" />
			
			<result column="phone" property="phone" />
			<result column="sxrq" property="sxrq" />

            
            <collection property="defdto" ofType="cn.timer.api.dto.xcgl.DefinedItemsDto">
		   		<result column="dazid" property="dazid"/>
		   		<result column="xzdazdyid" property="xzdazdyid"/>
		   		<result column="dazrsz" property="dazrsz"/>
		   		<result column="xzdadxid" property="xzdadxid"/>
		    </collection>
            
    </resultMap>
    
    <resultMap id="ChangeSalaryDtoMap" type="cn.timer.api.dto.xcgl.ChangeSalaryDto" >
                <result column="empnum" property="empnum" />
                <result column="empname" property="empname" />
                <result column="tratime" property="tratime" />
                <result column="xzdazdyid" property="xzdazdyid" />
                <result column="dazrsz" property="dazrsz" />
                <result column="xzdadxid" property="xzdadxid" />
                <result column="zdyzdmc" property="zdyzdmc" />
    </resultMap>

    <sql id="Base_Column_List">
                id,
                zdmc,
                zdsrklx,
                zdlx,
                qyid,
                xhpx
    </sql>
    
    <sql id="Base_Column_List_Alias">
                id XcglAssoXzdazdy_id,
                zdmc XcglAssoXzdazdy_zdmc,
                zdsrklx XcglAssoXzdazdy_zdsrklx,
                zdlx XcglAssoXzdazdy_zdlx,
                qyid XcglAssoXzdazdy_qyid,
                xhpx XcglAssoXzdazdy_xhpx
    </sql>


	  <select id="selectxcglassoxzdazdy" resultMap="BaseResultMap">
			select * from xcgl_asso_xzdazdy xzzdy
			where xzzdy.qyid = #{qyid}
			and xzzdy.zdmc in ('底薪','岗位津贴')
	  </select>

	<select id="SalaryFixedWages" resultMap="BaseResultMap">
		select * from xcgl_asso_xzdazdy xzdazdy
			where xzdazdy.zdmc = #{zdmc} LIMIT 1
	</select>

	<insert id="insertxcglassoxzdazdyList" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
	    insert into xcgl_asso_xzdazdy (zdmc, zdsrklx, zdlx,qyid,xhpx)
	    <foreach collection="list" item="item" index="index" open="values " close="" separator=",">
	   	(
	      <if test="item.zdmc != null" >
	        #{item.zdmc,jdbcType=VARCHAR},
	      </if>
	      <if test="item.zdsrklx != null" >
	        #{item.zdsrklx,jdbcType=INTEGER},
	      </if>
	      <if test="item.zdlx != null" >
	        #{item.zdlx,jdbcType=INTEGER},
	      </if>
	      <if test="item.qyid != null" >
	        #{item.qyid,jdbcType=INTEGER},
	      </if>
	      <if test="item.xhpx != null" >
	        #{item.xhpx,jdbcType=INTEGER}
	      </if>
	      )
	    </foreach>
  	 </insert>


    <insert id="insert" useGeneratedKeys="true" keyColumn="id" parameterType="cn.timer.api.bean.xcgl.XcglAssoXzdazdy">
        INSERT INTO xcgl_asso_xzdazdy
        <trim prefix="(" suffix=")" suffixOverrides=",">
                        <if test ='null != zdmc'>
                        zdmc,
                        </if>
                        <if test ='null != zdsrklx'>
                        zdsrklx,
                        </if>
                        <if test ='null != zdlx'>
                        zdlx,
                        </if>
                        <if test ='null != qyid'>
                        qyid,
                        </if>
                        <if test ='null != xhpx'>
                        xhpx
                        </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                        <if test ='null != zdmc'>
                        #{zdmc},
                        </if>
                        <if test ='null != zdsrklx'>
                        #{zdsrklx},
                        </if>
                        <if test ='null != zdlx'>
                        #{zdlx},
                        </if>
                        <if test ='null != qyid'>
                        #{qyid},
                        </if>
                        <if test ='null != xhpx'>
                        #{xhpx}
                        </if>
        </trim>
    </insert>

<!--     <delete id="delete" >
        DELETE FROM xcgl_asso_xzdazdy
        WHERE id = #{id}
    </delete> -->

    <update id="dazdyupdate" parameterType="cn.timer.api.bean.xcgl.XcglAssoXzdazdy">
        UPDATE xcgl_asso_xzdazdy
        <set>
             <if test ='null != zdmc'>zdmc = #{zdmc},</if>
             <if test ='null != zdsrklx'>zdsrklx = #{zdsrklx},</if>
             <if test ='null != zdlx'>zdlx = #{zdlx},</if>
             <if test ='null != qyid'>qyid = #{qyid},</if>
             <if test ='null != xhpx'>xhpx = #{xhpx}</if>
        </set>
        WHERE id = #{id}
    </update>

<!-- 
    <select id="load" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM xcgl_asso_xzdazdy
        WHERE id = #{id}
    </select>

    <select id="pageList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM xcgl_asso_xzdazdy
        LIMIT #{offset}, #{pageSize}
    </select>

    <select id="pageListCount" resultType="java.lang.Integer">
        SELECT count(1)
        FROM xcgl_asso_xzdazdy
    </select> -->

	<select id="selectFixedSalaryStaffCount" resultType="java.lang.Long">
		select COUNT(*) from yggl_main_emp emp
		where emp.org_code = #{param.orgcode}
		<if test="param.status != null and param.status.size() >0" >
			and emp.job_status in
			<foreach collection="param.status" open="(" close=")" separator="," index="i" item="item">
				#{item, jdbcType=INTEGER}
			</foreach>
		</if>
		<if test="param.text != ''" >
			and (emp.`name` like CONCAT('%',#{param.text},'%') or emp.emp_num = #{param.text})
		</if>
	</select>
	<select id="selectFixedSalaryStaff" resultMap="FixedSalaryStaffMap">
			select emp.emp_num empnum, 
			emp.`name` empname,
			case emp.job_type
				when 0 then '全职'
				when 1 then '实习生'
				when 2 then '兼职'
				when 3 then '劳务派遣'
				when 4 then '劳务'
				when 5 then '派遣'
				when 6 then '外包'
				else '退休返聘' end as empjobtype,
			case emp.job_status 
				when 0 then '试用'
				when 1 then '正式'
				when 2 then '离职中'
				else '已离职' end as empjobstatus,
			emp.phone,
			(select xsz.sxrq from xcgl_asso_xzdadx xsz where xsz.txzt = 0 and xsz.userid = emp.emp_num) as sxrq,
			SUBSTR(emp.rz_time,1,10) emprztime ,
			SUBSTR(emp.zz_time,1,10) empzztime,
			IF(dadx.userid IS NOT NULL,1,0) as sfdx,
			dadx.sxrq as tratime,
			xzdaz.id as dazid,
			xzdaz.xzdazdyid as xzdazdyid,
			xzdaz.rsz as dazrsz,
			xzdaz.xzdadxid as xzdadxid
		  from (select * from yggl_main_emp emp
			where emp.org_code = #{param.orgcode}
		<if test="param.status != null and param.status.size() >0" >
			and emp.job_status in
			<foreach collection="param.status" open="(" close=")" separator="," index="i" item="item">
				#{item, jdbcType=INTEGER}
			</foreach>
		</if>
			<if test="param.text != ''" >
				and (emp.`name` like CONCAT('%',#{param.text},'%') or emp.emp_num = #{param.text})
			</if>
			LIMIT #{param.offset}, #{param.totalPage}
		  	) emp
		LEFT JOIN xcgl_asso_xzdadx as dadx on dadx.userid = emp.emp_num and dadx.txzt = 0
		LEFT JOIN xcgl_asso_xzdaz as xzdaz on xzdaz.xzdadxid = dadx.id
	</select>


	<select id="selectFixedSalaryStaffAll" resultMap="FixedSalaryStaffMap">
		select emp.emp_num empnum,
		emp.`name` empname,
		case emp.job_type
		when 0 then '全职'
		when 1 then '实习生'
		when 2 then '兼职'
		when 3 then '劳务派遣'
		when 4 then '劳务'
		when 5 then '派遣'
		when 6 then '外包'
		else '退休返聘' end as empjobtype,
		case emp.job_status
		when 0 then '试用'
		when 1 then '正式'
		when 2 then '离职中'
		else '已离职' end as empjobstatus,
		SUBSTR(emp.rz_time,1,10) emprztime ,
		SUBSTR(emp.zz_time,1,10) empzztime,
		IF(dadx.userid IS NOT NULL,1,0) as sfdx,
		dadx.sxrq as tratime,
		xzdaz.id as dazid,
		xzdaz.xzdazdyid as xzdazdyid,
		xzdaz.rsz as dazrsz,
		xzdaz.xzdadxid as xzdadxid
		from (select * from yggl_main_emp emp
		where emp.org_code = #{param.orgcode}
		<if test="param.status != null and param.status.size() >0" >
			and emp.job_status in
			<foreach collection="param.status" open="(" close=")" separator="," index="i" item="item">
				#{item, jdbcType=INTEGER}
			</foreach>
		</if>
		<if test="param.text != ''" >
			and (emp.`name` like CONCAT('%',#{param.text},'%') or emp.emp_num = #{param.text})
		</if>
		) emp
		LEFT JOIN xcgl_asso_xzdadx as dadx on dadx.userid = emp.emp_num and dadx.txzt = 0
		LEFT JOIN xcgl_asso_xzdaz as xzdaz on xzdaz.xzdadxid = dadx.id
	</select>
	
	<select id="selectPaygroupStaff" resultMap="FixedSalaryStaffMap">
			select emp.emp_num empnum, 
			emp.`name` empname,
			case emp.job_type
				when 0 then '全职'
				when 1 then '实习生'
				when 2 then '兼职'
				when 3 then '劳务派遣'
				when 4 then '劳务'
				when 5 then '派遣'
				when 6 then '外包'
				else '退休返聘' end as empjobtype,
			case emp.job_status 
				when 0 then '试用'
				when 1 then '正式'
				when 2 then '离职中'
				else '已离职' end as empjobstatus,
			SUBSTR(emp.rz_time,1,10) emprztime ,
			SUBSTR(emp.zz_time,1,10) empzztime,
			IF(dadx.userid IS NOT NULL,1,0) as sfdx,
			dadx.sxrq as tratime,
			xzdaz.id as dazid,
			xzdaz.xzdazdyid as xzdazdyid,
			xzdaz.rsz as dazrsz,
			xzdaz.xzdadxid as xzdadxid
		  from yggl_main_emp emp
		LEFT JOIN xcgl_asso_xzdadx as dadx on dadx.userid = emp.emp_num and dadx.txzt = 0
		LEFT JOIN xcgl_asso_xzdaz as xzdaz on xzdaz.xzdadxid = dadx.id
		LEFT JOIN xcgl_asso_xzury as xz on xz.userid = emp.emp_num and xz.qyid = emp.org_code
		where emp.org_code = #{orgcode}
		and xz.xzzid = #{paygroupid}
		<if test="text != ''" >
			and (emp.`name` like CONCAT('%',#{text},'%') or emp.emp_num = #{text})
		</if>
	</select>



	<select id="AchieveSalaryAdjustment" resultMap="ChangeSalaryDtoMap">
			select emp.emp_num empnum, 
				emp.`name` empname,
				dadx.sxrq as tratime,
				xzdaz.xzdazdyid as xzdazdyid,
				xzdaz.rsz as dazrsz,
				xzdaz.xzdadxid as xzdadxid,
				zdy.zdmc as zdyzdmc,
				dadx.remarks
				from yggl_main_emp emp
			LEFT JOIN xcgl_asso_xzdadx as dadx on dadx.userid = emp.emp_num and dadx.txzt = 0
			LEFT JOIN xcgl_asso_xzdaz as xzdaz on xzdaz.xzdadxid = dadx.id
			LEFT JOIN xcgl_asso_xzdazdy as zdy on zdy.id = xzdaz.xzdazdyid
			where emp.org_code = #{orgcode}
			and emp.emp_num = #{empnum}
	</select>
	
	<select id="SalaryAdjustmentRecord" resultMap="FixedSalaryStaffMap">
		select dadx.id ,emp.emp_num empnum, 
			emp.`name` empname,
			case emp.job_type
				when 0 then '全职'
				when 1 then '实习生'
				when 2 then '兼职'
				when 3 then '劳务派遣'
				when 4 then '劳务'
				when 5 then '派遣'
				when 6 then '外包'
				else '退休返聘' end as empjobtype,
			case emp.job_status 
				when 0 then '试用'
				when 1 then '正式'
				when 2 then '离职中'
				else '已离职' end as empjobstatus,
			SUBSTR(emp.rz_time,1,10) emprztime ,
			SUBSTR(emp.zz_time,1,10) empzztime,
		IF(dadx.userid IS NOT NULL,1,0) AS sfdx,
		dadx.sxrq as tratime,
		dadx.txzt as txzt,
		dadx.remarks as remarks,
		xzdaz.id as dazid,
		xzdaz.xzdazdyid as xzdazdyid,
		xzdaz.rsz as dazrsz,
		xzdaz.xzdadxid as xzdadxid
			from xcgl_asso_xzdadx as dadx
		LEFT JOIN yggl_main_emp emp on dadx.userid = emp.emp_num
		LEFT JOIN xcgl_asso_xzdaz as xzdaz on xzdaz.xzdadxid = dadx.id
		where emp.org_code = #{orgcode}
		and emp.emp_num = #{empnum}
		ORDER BY dadx.txzt, dadx.sxrq DESC
	</select>

</mapper>