<?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.kqmk.KqglMainKqzMapper">

    <resultMap id="BaseResultMap" type="cn.timer.api.bean.kqmk.KqglMainKqz" >
                <id column="id" property="id" />
                <result column="name" property="name" />
                <result column="kqbz" property="kqbz" />
                <result column="pbfs" property="pbfs" />
                <result column="qyid" property="qyid" />
                <result column="is_fdjjr" property="isFdjjr" />
                <result column="kqkssj_time" property="kqkssjTime" />
                <result column="zsgzsc" property="zsgzsc" />
                <result column="zcgzsc" property="zcgzsc" />
                <result column="jbzdsc" property="jbzdsc" />
                <result column="is_dqtx" property="isDqtx" />
                <result column="txry" property="txry" />
                <result column="txfs" property="txfs" />
                <result column="txxhts" property="txxhts" />
                <result column="txsj_time" property="txsjTime" />
                <result column="tsfs" property="tsfs" />
                <result column="is_wpbsdk" property="isWpbsdk" />
                <result column="sybc" property="sybc" />
                <result column="is_xzbcdk" property="isXzbcdk" />
                <result column="is_wq" property="isWq" />
                <result column="overtime_rules_id" property="overtimeRulesId" />
                <result column="restdayclock" property="restdayclock" />
    </resultMap>
    
    <resultMap id="AuxiliaryMap" type="cn.timer.api.dto.kqmk.KqglMainKqzDto" >
                <id column="id" property="id" />
                <result column="name" property="name" />
                <result column="kqbz" property="kqbz" />
                <result column="pbfs" property="pbfs" />
                <result column="qyid" property="qyid" />
                <result column="is_fdjjr" property="isFdjjr" />
                <result column="kqkssj_time" property="kqkssjTime" />
                <result column="zsgzsc" property="zsgzsc" />
                <result column="zcgzsc" property="zcgzsc" />
                <result column="jbzdsc" property="jbzdsc" />
                <result column="is_dqtx" property="isDqtx" />
                <result column="txry" property="txry" />
                <result column="txfs" property="txfs" />
                <result column="txxhts" property="txxhts" />
                <result column="txsj_time" property="txsjTime" />
                <result column="tsfs" property="tsfs" />
                <result column="is_wpbsdk" property="isWpbsdk" />
                <result column="sybc" property="sybc" />
                <result column="is_xzbcdk" property="isXzbcdk" />
                <result column="is_wq" property="isWq" />
                <result column="overtime_rules_id" property="overtimeRulesId" />
                
                <result column="kqznum" property="kqznum" />
			    <result column="pbfsnm" property="pbfsnm" />
			    <result column="dkfs" property="dkfs" />
			    <result column="kqjid" property="kqjid" />
			    
			    <collection property="kqzdkfslist" ofType="cn.timer.api.bean.kqmk.KqglAssoKqzdkfs">
			   		<result column="kqzId" property="kqzId" jdbcType="INTEGER" />
			   		<result column="type" property="type" jdbcType="INTEGER" />
			    </collection>
    </resultMap>

    <sql id="Base_Column_List">
                id,
                name,
                kqbz,
                pbfs,
                qyid,
                is_fdjjr,
                kqkssj_time,
                zsgzsc,
                zcgzsc,
                jbzdsc,
                is_dqtx,
                txry,
                txfs,
                txxhts,
                txsj_time,
                tsfs,
                is_wpbsdk,
                sybc,
                is_xzbcdk,
                is_wq,
                overtime_rules_id,
                restdayclock
    </sql>
    
    <sql id="Base_Column_List_Alias">
                id KqglMainKqz_id,
                name KqglMainKqz_name,
                kqbz KqglMainKqz_kqbz,
                pbfs KqglMainKqz_pbfs,
                qyid KqglMainKqz_qyid,
                is_fdjjr KqglMainKqz_is_fdjjr,
                kqkssj_time KqglMainKqz_kqkssj_time,
                zsgzsc KqglMainKqz_zsgzsc,
                zcgzsc KqglMainKqz_zcgzsc,
                jbzdsc KqglMainKqz_jbzdsc,
                is_dqtx KqglMainKqz_is_dqtx,
                txry KqglMainKqz_txry,
                txfs KqglMainKqz_txfs,
                txxhts KqglMainKqz_txxhts,
                txsj_time KqglMainKqz_txsj_time,
                tsfs KqglMainKqz_tsfs,
                is_wpbsdk KqglMainKqz_is_wpbsdk,
                sybc KqglMainKqz_sybc,
                is_xzbcdk KqglMainKqz_is_xzbcdk,
                is_wq KqglMainKqz_is_wq,
                overtime_rules_id KqglMainKqz_overtime_rules_id,
                restdayclock KqglMainKqz_restdayclock
    </sql>
    
    <sql id="Base_Column_List_a">
                a.id,
                a.name,
                a.kqbz,
                a.pbfs,
                a.qyid,
                a.is_fdjjr,
                a.kqkssj_time,
                a.zsgzsc,
                a.zcgzsc,
                a.jbzdsc,
                a.is_dqtx,
                a.txry,
                a.txfs,
                a.txxhts,
                a.txsj_time,
                a.tsfs,
                a.is_wpbsdk,
                a.sybc,
                a.is_xzbcdk,
                a.is_wq,
                a.overtime_rules_id,
                a.restdayclock
    </sql>
    
    <select id="selectAttGroupMachineByQyid" resultMap="AuxiliaryMap">
		select 
			<include refid="Base_Column_List_a" />,
			 kqzdkfs.dkfsid as kqjid 
		 from kqgl_main_kqz a
		 LEFT JOIN kqgl_asso_kqzdkfs as kqzdkfs on kqzdkfs.kqz_id = a.id and kqzdkfs.type = 1 and kqzdkfs.state = 1
		 where a.qyid = #{qyid,jdbcType=INTEGER}
	</select>
	
	<select id="getAttendanceGroupInformationByUserid" resultMap="BaseResultMap">
	  	select * from kqgl_main_kqz kqz
			where kqz.id = (select yhkqz.kqzid from kqgl_asso_yhkqz yhkqz
			where yhkqz.userid = #{userid,jdbcType=INTEGER} and yhkqz.qyid = #{qyid,jdbcType=INTEGER})
	  </select>
	  
	  
	  <select id="selectPageByQueryKqglMain" resultMap="AuxiliaryMap">
	    select kqz.*,
		    	case kqz.pbfs 
				WHEN  '1' then '固定排班' 
				WHEN  '2' then '排班制' 
				else '自由工时'  
				end as pbfsnm,
				znm.kqznum
			from kqgl_main_kqz kqz
		LEFT JOIN (select count(yhkqz.kqzid) as kqznum,
						yhkqz.kqzid as kqzid 
					from kqgl_asso_yhkqz yhkqz 
					where yhkqz.qyid = #{param.orgCode} 
					GROUP BY yhkqz.kqzid) as znm on znm.kqzid = kqz.id
		
			where 1=1
			and kqz.qyid = #{param.orgCode} 
			<if test="param.query != null and param.query != ''">
				and kqz.`name` like CONCAT('%',#{param.query},'%')
			</if>
			ORDER BY kqz.id DESC
			
			<!-- LEFT JOIN (select kqzdkfs.kqz_id as kqzId,kqzdkfs.type as type from kqgl_asso_kqzdkfs kqzdkfs 
				where kqzdkfs.qyid = #{param.orgCode} ) as dkfs on dkfs.kqzId = kqz.id 
				
				dkfs.type-->
	  </select>

	<!-- 

    <insert id="insert" useGeneratedKeys="true" keyColumn="id" parameterType="cn.timer.api.bean.kqmk.KqglMainKqz">
        INSERT INTO kqgl_main_kqz
        <trim prefix="(" suffix=")" suffixOverrides=",">
                        <if test ='null != name'>
                        name,
                        </if>
                        <if test ='null != kqbz'>
                        kqbz,
                        </if>
                        <if test ='null != pbfs'>
                        pbfs,
                        </if>
                        <if test ='null != qyid'>
                        qyid,
                        </if>
                        <if test ='null != isFdjjr'>
                        is_fdjjr,
                        </if>
                        <if test ='null != kqkssjTime'>
                        kqkssj_time,
                        </if>
                        <if test ='null != zsgzsc'>
                        zsgzsc,
                        </if>
                        <if test ='null != zcgzsc'>
                        zcgzsc,
                        </if>
                        <if test ='null != jbzdsc'>
                        jbzdsc,
                        </if>
                        <if test ='null != isDqtx'>
                        is_dqtx,
                        </if>
                        <if test ='null != txry'>
                        txry,
                        </if>
                        <if test ='null != txfs'>
                        txfs,
                        </if>
                        <if test ='null != txxhts'>
                        txxhts,
                        </if>
                        <if test ='null != txsjTime'>
                        txsj_time,
                        </if>
                        <if test ='null != tsfs'>
                        tsfs,
                        </if>
                        <if test ='null != isWpbsdk'>
                        is_wpbsdk,
                        </if>
                        <if test ='null != sybc'>
                        sybc,
                        </if>
                        <if test ='null != isXzbcdk'>
                        is_xzbcdk,
                        </if>
                        <if test ='null != isWq'>
                        is_wq,
                        </if>
                        <if test ='null != overtimeRulesId'>
                        overtime_rules_id
                        </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                        <if test ='null != name'>
                        #{name},
                        </if>
                        <if test ='null != kqbz'>
                        #{kqbz},
                        </if>
                        <if test ='null != pbfs'>
                        #{pbfs},
                        </if>
                        <if test ='null != qyid'>
                        #{qyid},
                        </if>
                        <if test ='null != isFdjjr'>
                        #{isFdjjr},
                        </if>
                        <if test ='null != kqkssjTime'>
                        #{kqkssjTime},
                        </if>
                        <if test ='null != zsgzsc'>
                        #{zsgzsc},
                        </if>
                        <if test ='null != zcgzsc'>
                        #{zcgzsc},
                        </if>
                        <if test ='null != jbzdsc'>
                        #{jbzdsc},
                        </if>
                        <if test ='null != isDqtx'>
                        #{isDqtx},
                        </if>
                        <if test ='null != txry'>
                        #{txry},
                        </if>
                        <if test ='null != txfs'>
                        #{txfs},
                        </if>
                        <if test ='null != txxhts'>
                        #{txxhts},
                        </if>
                        <if test ='null != txsjTime'>
                        #{txsjTime},
                        </if>
                        <if test ='null != tsfs'>
                        #{tsfs},
                        </if>
                        <if test ='null != isWpbsdk'>
                        #{isWpbsdk},
                        </if>
                        <if test ='null != sybc'>
                        #{sybc},
                        </if>
                        <if test ='null != isXzbcdk'>
                        #{isXzbcdk},
                        </if>
                        <if test ='null != isWq'>
                        #{isWq},
                        </if>
                        <if test ='null != overtimeRulesId'>
                        #{overtimeRulesId}
                        </if>
        </trim>
    </insert>

    <delete id="delete" >
        DELETE FROM kqgl_main_kqz
        WHERE id = #{id}
    </delete>

    <update id="update" parameterType="cn.timer.api.bean.kqmk.KqglMainKqz">
        UPDATE kqgl_main_kqz
        <set>
                    <if test ='null != name'>name = #{name},</if>
                    <if test ='null != kqbz'>kqbz = #{kqbz},</if>
                    <if test ='null != pbfs'>pbfs = #{pbfs},</if>
                    <if test ='null != qyid'>qyid = #{qyid},</if>
                    <if test ='null != isFdjjr'>is_fdjjr = #{isFdjjr},</if>
                    <if test ='null != kqkssjTime'>kqkssj_time = #{kqkssjTime},</if>
                    <if test ='null != zsgzsc'>zsgzsc = #{zsgzsc},</if>
                    <if test ='null != zcgzsc'>zcgzsc = #{zcgzsc},</if>
                    <if test ='null != jbzdsc'>jbzdsc = #{jbzdsc},</if>
                    <if test ='null != isDqtx'>is_dqtx = #{isDqtx},</if>
                    <if test ='null != txry'>txry = #{txry},</if>
                    <if test ='null != txfs'>txfs = #{txfs},</if>
                    <if test ='null != txxhts'>txxhts = #{txxhts},</if>
                    <if test ='null != txsjTime'>txsj_time = #{txsjTime},</if>
                    <if test ='null != tsfs'>tsfs = #{tsfs},</if>
                    <if test ='null != isWpbsdk'>is_wpbsdk = #{isWpbsdk},</if>
                    <if test ='null != sybc'>sybc = #{sybc},</if>
                    <if test ='null != isXzbcdk'>is_xzbcdk = #{isXzbcdk},</if>
                    <if test ='null != isWq'>is_wq = #{isWq},</if>
                    <if test ='null != overtimeRulesId'>overtime_rules_id = #{overtimeRulesId}</if>
        </set>
        WHERE id = #{id}
    </update>


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

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

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

	 -->

</mapper>