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

    <resultMap id="BaseResultMap" type="cn.timer.api.bean.xcgl.XcglAssoXzb" >
                <id column="id" property="id" />
                <result column="userid" property="userid" />
                <result column="xzyf" property="xzyf" />
                <result column="xzxid" property="xzxid" />
                <result column="xzxjg" property="xzxjg" />
                <result column="qyid" property="qyid" />
    </resultMap>
    
    <resultMap id="CalculationDtoMap" type="cn.timer.api.dto.xcgl.CalculationDto" >
            <result column="orgcode" property="orgcode" />
            <result column="empid" property="empid" />
            <result column="empnum" property="empnum" />
            <result column="empname" property="empname" />
            <result column="dept" property="dept" />
            <result column="post" property="post" />
            
            <collection property="calcu" ofType="cn.timer.api.dto.xcgl.CalculationValueDto">
		   		<result column="xzbid" property="xzbid"/>
		   		<result column="xzyf" property="xzyf"/>
		   		<result column="xzxid" property="xzxid"/>
		   		<result column="xzxjg" property="xzxjg"/>
		    </collection>
    </resultMap>
    
    <resultMap id="CalculationValueMap" type="cn.timer.api.dto.xcgl.CalculationValueDto" >
    			<result column="xzbid" property="xzbid"/>
                <result column="xzyf" property="xzyf"/>
		   		<result column="xzxid" property="xzxid"/>
		   		<result column="xzxjg" property="xzxjg"/>
    </resultMap>
    
    <resultMap id="UserPersonnelDtoMap" type="cn.timer.api.dto.xcgl.UserPersonnelDto" >
            <result column="orgcode" property="orgcode" />
            <result column="empnum" property="empnum" />
            <result column="empname" property="empname" />
            <result column="dept" property="dept" />
            <result column="post" property="post" />
    </resultMap>
    
    <resultMap id="HolidayInquiryDtoMap" type="cn.timer.api.dto.xcgl.HolidayInquiryDto" >
            <result column="num" property="num" />
            <result column="balance" property="balance" />
    </resultMap>

    <sql id="Base_Column_List">
                id,
                userid,
                xzyf,
                xzxid,
                xzxjg,
                qyid
    </sql>
    
    <sql id="Base_Column_List_Alias">
                id XcglAssoXzb_id,
                userid XcglAssoXzb_userid,
                xzyf XcglAssoXzb_xzyf,
                xzxid XcglAssoXzb_xzxid,
                xzxjg XcglAssoXzb_xzxjg,
                qyid XcglAssoXzb_qyid
    </sql>
    
    <select id="PayInColumn" resultMap="BaseResultMap">
    	select * from xcgl_asso_xzb zx
		where zx.xzxid = (select gg.id from xcgl_asso_jsgzzx gg
		where gg.optionid = 70
		and gg.xzzid = (select jsg.xzzid from xcgl_asso_jsgzzx jsg
		where jsg.id = #{xzxid})
		)
		and zx.userid = #{userid}
		and zx.xzyf = #{xzyf}
    </select>
    
    <select id="UserPersonnel" resultMap="UserPersonnelDtoMap">
    	select aa.orgcode,aa.empnum,aa.empname,aa.dept,aa.post
		from (select emp.org_code as orgcode,
						emp.emp_num as empnum,
						emp.`name` as empname,
						bmgw.dept,
			 			bmgw.post
				from yggl_main_emp emp
			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
			where emp.emp_num in (select xzury.userid from xcgl_asso_xzury xzury where xzury.xzzid = #{xzzid})
			) as aa
			where aa.orgcode = #{orgcode}
    </select>
    
    <select id="HolidayamountNum" resultMap="HolidayInquiryDtoMap">
    	select COUNT(*) as num
	    	from kqgl_asso_relation_summary su 
			where su.approval_type = 2
			and su.org_code = #{orgcode}
			and su.ordernum = 1
			and SUBSTR(su.app_time,1,7) = #{apptime}
			and su.leave_type_id = (select ru.id from kqgl_asso_leave_rules ru where ru.org_code = #{orgcode} and ru.rules_type = #{rules_type})
			and su.user_id = #{userid}
    </select>
    
    <select id="Holidayamount" resultMap="HolidayInquiryDtoMap">
	    select IFNULL(SUM(su.duration),0) as balance
	    	from kqgl_asso_relation_summary su 
			where su.approval_type = 2
			and su.org_code = #{orgcode}
			and su.ordernum = 1
			and SUBSTR(su.app_time,1,7) = #{apptime}
			and su.leave_type_id = (select ru.id from kqgl_asso_leave_rules ru where ru.org_code = #{orgcode} and ru.rules_type = #{rules_type})
			and su.user_id = #{userid}
	</select>
	
	<select id="NumberOvertime" resultMap="HolidayInquiryDtoMap">
	    select COUNT(*) as num
	    	from kqgl_asso_relation_summary su 
			where su.approval_type = 5
			and su.org_code = #{orgcode}
			and SUBSTR(su.app_time,1,7) = #{apptime}
			and su.user_id = #{userid}
	</select>
	
	<select id="PeriodsOvertime" resultMap="HolidayInquiryDtoMap">
		select IFNULL(SUM(su.duration),0) as balance
			from kqgl_asso_relation_summary su 
			where su.approval_type = 1
			and su.org_code = #{orgcode}
			and SUBSTR(su.app_time,1,7) = #{apptime}
			and su.overtime_type_id = #{overtimetypeid}
			and su.user_id = #{userid}
	</select>

	<select id="CalculationResults" resultMap="CalculationDtoMap">
	select aa.orgcode,aa.empid,aa.empnum,aa.empname,aa.xzbid,aa.xzyf,
		aa.xzxid,aa.xzxjg,aa.dept,aa.post
	from (
		 		select emp.org_code as orgcode,
		 			emp.id as empid,
					emp.emp_num as empnum,
					emp.`name` as empname,
					xzbs.id as xzbid,
					xzbs.xzyf as xzyf,
					xzbs.xzxid as xzxid,
					xzbs.xzxjg as xzxjg,
					bmgw.dept,
		 			bmgw.post
			from yggl_main_emp emp
		LEFT JOIN  xcgl_asso_xzb as xzbs on xzbs.userid = emp.emp_num
		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
		where emp.emp_num in (select xzury.userid from xcgl_asso_xzury xzury where xzury.xzzid = #{paygroupid})
		<if test="text != null and text != ''" >
			and (emp.`name` like CONCAT('%',#{text},'%') or emp.emp_num = #{text})
		</if>
		) as aa
		where aa.orgcode = #{orgcode}
	</select>
	
	<select id="lateearlyDeduction" resultMap="CalculationValueMap">
			select xzbs.xzyf as xzyf,
					xzbs.xzxid as xzxid,
					xzbs.xzxjg as xzxjg
			from yggl_main_emp emp
		LEFT JOIN  xcgl_asso_xzb as xzbs on xzbs.userid = emp.emp_num
		where emp.emp_num = #{empnum}
		and xzbs.xzxid = (select jsgz.id from xcgl_asso_jsgzzx jsgz
					where jsgz.xzzid = #{xzzid}
					and jsgz.salaryitem = #{salaryitem})
	</select>
	
	<select id="SimultaneousAttendance" resultMap="CalculationDtoMap">
			select emp.id as empid,
					emp.emp_num as empnum,
					emp.`name` as empname,
					xzbs.id as xzbid,
					xzbs.xzyf as xzyf,
					xzbs.xzxid as xzxid,
					xzbs.xzxjg as xzxjg,
					bmgw.dept,
			 		bmgw.post
			from yggl_main_emp emp
		LEFT JOIN  xcgl_asso_xzb as xzbs on xzbs.userid = emp.emp_num
		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
		where emp.emp_num in (select xzury.userid from xcgl_asso_xzury xzury
		where xzury.xzzid = #{xzzid})
		and xzbs.xzxid in (select jsgzzx.id from xcgl_asso_jsgzzx jsgzzx
			LEFT JOIN xcgl_asso_option as opt on opt.id = jsgzzx.optionid
			where opt.itemid = 2
			and jsgzzx.xzzid = #{xzzid})
	</select>
	

	<delete id="deleteByuserid" >
        <!-- DELETE FROM xcgl_asso_xzb
        WHERE userid = #{userid}
		and xzxid not in(select jsgzzx.id from xcgl_asso_jsgzzx jsgzzx
			LEFT JOIN xcgl_asso_option as opt on opt.id = jsgzzx.optionid
			where opt.itemid = 2
			and jsgzzx.xzzid = #{xzzid})
			and xzyf = #{xzyf} -->
			
			DELETE FROM xcgl_asso_xzb
        WHERE userid = #{userid}
			and xzyf = #{xzyf}
    </delete>
    
    <delete id="deleteXcglAssoXzb">
    	DELETE FROM xcgl_asso_xzb
        WHERE userid = #{userid}
    </delete>
    
    <delete id="deleteByAttendance" >
    	DELETE from xcgl_asso_xzb
		where userid = #{userid}
		and xzxid in (select jsgzzx.id from xcgl_asso_jsgzzx jsgzzx
		LEFT JOIN xcgl_asso_option as opt on opt.id = jsgzzx.optionid
		where opt.itemid = 2
		and jsgzzx.xzzid = #{xzzid})
		and xzyf = #{xzyf}
    </delete>


    <insert id="xzbinsert" useGeneratedKeys="true" keyProperty="id" parameterType="cn.timer.api.bean.xcgl.XcglAssoXzb">
        INSERT INTO xcgl_asso_xzb
        <trim prefix="(" suffix=")" suffixOverrides=",">
                        <if test ='null != userid'>
                        userid,
                        </if>
                        <if test ='null != xzyf'>
                        xzyf,
                        </if>
                        <if test ='null != xzxid'>
                        xzxid,
                        </if>
                        <if test ='null != xzxjg'>
                        xzxjg,
                        </if>
                        <if test ='null != qyid'>
                        qyid
                        </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                        <if test ='null != userid'>
                        #{userid},
                        </if>
                        <if test ='null != xzyf'>
                        #{xzyf},
                        </if>
                        <if test ='null != xzxid'>
                        #{xzxid},
                        </if>
                        <if test ='null != xzxjg'>
                        #{xzxjg},
                        </if>
                        <if test ='null != qyid'>
                        #{qyid}
                        </if>
        </trim>
    </insert>


	<update id="ModifyImportedSalary" parameterType="cn.timer.api.bean.xcgl.XcglAssoXzb">
		UPDATE xcgl_asso_xzb 
			set xzxjg = #{xzxjg}
		where userid = #{userid}
		and xzyf = #{xzyf}
		and xzxid = #{xzxid}
	</update>

	<!-- 批量更新薪资结果 -->
	<update id="updateBatchXzxjg" parameterType="java.util.List">
		<foreach collection="list" item="item" index="index" open="" close="" separator=";">
			UPDATE xcgl_asso_xzb
			<set>
				xzxjg=${item.xzxjg}
			</set>
			where id = ${item.id}
		</foreach>
	</update>
	
    <!-- <delete id="delete" >
        DELETE FROM xcgl_asso_xzb
        WHERE id = #{id}
    </delete>

    <update id="update" parameterType="cn.timer.api.bean.xcgl.XcglAssoXzb">
        UPDATE xcgl_asso_xzb
        <set>
                    <if test ='null != userid'>userid = #{userid},</if>
                    <if test ='null != xzyf'>xzyf = #{xzyf},</if>
                    <if test ='null != xzxid'>xzxid = #{xzxid},</if>
                    <if test ='null != xzxjg'>xzxjg = #{xzxjg},</if>
                    <if test ='null != qyid'>qyid = #{qyid}</if>
        </set>
        WHERE id = #{id}
    </update>


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

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

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


</mapper>