<?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.XcglAssoGztztMapper">

    <resultMap id="BaseResultMap" type="cn.timer.api.bean.xcgl.XcglAssoGztzt" >
                <id column="id" property="id" />
                <result column="userid" property="userid" />
                <result column="xzyf" property="xzyf" />
                <result column="fs_type" property="fsType" />
                <result column="ck_type" property="ckType" />
                <result column="qr_type" property="qrType" />
                <result column="fkyj" property="fkyj" />
                <result column="yhqm" property="yhqm" />
                <result column="qyid" property="qyid" />
                <result column="gztszid" property="gztszid" />
				<result column="sfgz" property="sfgz" />
				<result column="xzzid" property="xzzid" />
    </resultMap>
    
    <resultMap id="PayslipDetailsMap" type="cn.timer.api.dto.xcgl.PayslipDetailsDto" >
                <result column="id" property="id" />
                <result column="empname" property="empname" />
                <result column="empjobstatus" property="empjobstatus" />
                <result column="fstype" property="fstype" />
                <result column="cktype" property="cktype" />
                <result column="qrtype" property="qrtype" />
				<result column="sfgz" property="sfgz" />
                <result column="dept" property="dept" />
                <result column="post" property="post" />
    </resultMap>

    <sql id="Base_Column_List">
                id,
                userid,
                xzyf,
                fs_type,
                ck_type,
                qr_type,
                fkyj,
                yhqm,
                qyid,
                gztszid,
                sfgz
    </sql>
    
    <sql id="Base_Column_List_Alias">
                id XcglAssoGztzt_id,
                userid XcglAssoGztzt_userid,
                xzyf XcglAssoGztzt_xzyf,
                fs_type XcglAssoGztzt_fs_type,
                ck_type XcglAssoGztzt_ck_type,
                qr_type XcglAssoGztzt_qr_type,
                sfgz XcglAssoGztzt_sfgz,
                fkyj XcglAssoGztzt_fkyj,
                yhqm XcglAssoGztzt_yhqm,
                qyid XcglAssoGztzt_qyid,
                gztszid XcglAssoGztzt_gztszid
    </sql>
    
    <select id="payslipSearch" resultMap="PayslipDetailsMap">
	    	select emp.`name` as empname,
				case emp.job_status 
					when 0 then '试用'
					when 1 then '正式'
					when 2 then '离职中'
					else '已离职' end as empjobstatus,
			      gztzt.fs_type as fstype,
						gztzt.ck_type as cktype,
						gztzt.qr_type as qrtype,
						gztzt.id,
						gztzt.sfgz,
						bmgw.dept,
			 			bmgw.post
					from xcgl_asso_gztzt gztzt
		LEFT JOIN yggl_main_emp as emp on emp.emp_num = gztzt.userid and emp.org_code = #{orgcode}
		LEFT JOIN (select m.name as post,um.name as dept,m.id as mid
						 from zzgl_bmgw_m m 
						 left join zzgl_bmgw_m um on um.id = m.up_id  
					) as bmgw on bmgw.mid = emp.bmgw_id
		<if test="xzzid != null">
			LEFT JOIN xcgl_asso_xzury xzury ON xzury.userid = emp.emp_num and xzury.xzzid = #{xzzid}
		</if>
		where gztzt.qyid = #{orgcode}
		<if test="gztszid != null"> and gztzt.gztszid = #{gztszid} </if>
		<if test="fsType != null"> and gztzt.fs_type = #{fsType} </if>
		<if test="qrType != null"> and gztzt.qr_type = #{qrType} </if>
		<if test="xzzid != null"> and gztzt.xzzid = #{xzzid} </if>
		<if test="text != null and text != ''" >
			and (emp.`name` like CONCAT('%',#{text},'%') or emp.emp_num = #{text})
		</if>
    </select>
    
    <delete id="deletexcglassogztzt" >
        DELETE FROM xcgl_asso_gztzt
        WHERE gztszid = #{gztszid}
    </delete>
    
    <delete id="deletegztztByxzzid" >
		    delete from xcgl_asso_gztzt
		where gztszid in (
		select gztsz.id from xcgl_asso_gztsz gztsz
		where gztsz.xzzid = #{xzzid})
	</delete>

	<insert id="insertxcglassogztztList" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id" >
	    insert into xcgl_asso_gztzt (userid,xzyf,fs_type,ck_type,qr_type,fkyj,yhqm,qyid,gztszid,sfgz)
	    <foreach collection="list" item="item" index="index" open="values " close="" separator=",">
	   	(
	      <if test="item.userid != null" >
	        #{item.userid,jdbcType=INTEGER},
	      </if>
	      <if test="item.xzyf != null" >
	        #{item.xzyf,jdbcType=VARCHAR},
	      </if>
	      <if test="item.fsType != null" >
	        #{item.fsType,jdbcType=INTEGER},
	      </if>
	      <if test="item.ckType != null" >
	        #{item.ckType,jdbcType=INTEGER},
	      </if>
	      <if test="item.qrType != null" >
	        #{item.qrType,jdbcType=INTEGER},
	      </if>
	      <if test="item.fkyj != null" >
	        #{item.fkyj,jdbcType=VARCHAR},
	      </if>
	      <if test="item.yhqm != null" >
	        #{item.yhqm,jdbcType=VARCHAR},
	      </if>
			<if test="item.sfgz != null" >
				#{item.sfgz,jdbcType=DOUBLE},
			</if>
	      <if test="item.qyid != null" >
	        #{item.qyid,jdbcType=INTEGER},
	      </if>
	      <if test="item.gztszid != null" >
	        #{item.gztszid,jdbcType=INTEGER}
	      </if>
	      )
	    </foreach>
  	 </insert>

	<!-- 发送工资条 操作 -->
	<update id="payslipSend" parameterType="java.lang.Integer">
		UPDATE xcgl_asso_gztzt SET fs_type = #{fsType}
		WHERE id = #{id}
	</update>

	<!-- 发送工资条 批量发送 -->
	<update id="payslipSendBatch">
		UPDATE xcgl_asso_gztzt SET fs_type = 1
		WHERE fs_type in (0, 2)
			AND id in
				<foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
					#{item}
				</foreach>
	</update>
</mapper>