<?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.kqgl.AttendanceGroupMapper" >
  <resultMap id="BaseResultMap" type="cn.timer.api.bean.kqgl.AttendanceGroup" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="kqbz" property="kqbz" jdbcType="VARCHAR" />
    <result column="pbfs" property="pbfs" jdbcType="INTEGER" />
    <result column="qyid" property="qyid" jdbcType="INTEGER" />
    <result column="is_fdjjr" property="isFdjjr" jdbcType="INTEGER" />
    <result column="kqkssj_time" property="kqkssjTime" jdbcType="VARCHAR" />
    
    <result column="zsgzsc" property="zsgzsc" jdbcType="VARCHAR" />
    <result column="zcgzsc" property="zcgzsc" jdbcType="VARCHAR" />
    <result column="jbzdsc" property="jbzdsc" jdbcType="VARCHAR" />
    
    <result column="is_dqtx" property="isDqtx" jdbcType="INTEGER" />
    <result column="txry" property="txry" jdbcType="VARCHAR" />
    <result column="txfs" property="txfs" jdbcType="INTEGER" />
    <result column="txxhts" property="txxhts" jdbcType="INTEGER" />
    <result column="txsj_time" property="txsjTime" jdbcType="VARCHAR" />
    <result column="tsfs" property="tsfs" jdbcType="VARCHAR" />
    <result column="is_wpbsdk" property="isWpbsdk" jdbcType="INTEGER" />
    <result column="sybc" property="sybc" jdbcType="VARCHAR" />
    <result column="is_xzbcdk" property="isXzbcdk" jdbcType="INTEGER" />
    
    <result column="kqznum" property="kqznum" jdbcType="INTEGER" />
    <result column="pbfsnm" property="pbfsnm" jdbcType="VARCHAR" />
    <result column="dkfs" property="dkfs" jdbcType="VARCHAR" />
    <result column="kqjid" property="kqjid" jdbcType="INTEGER" />
    <result column="is_wq" property="isWq" jdbcType="INTEGER" />
    <result column="overtime_rules_id" property="overtimeRulesId" />
    
    <result column="overtimeRulesName" property="overtimeRulesName" />
    
    <result column="restdayclock" property="restdayclock" />
    
    <collection property="kqzdkfslist" ofType="cn.timer.api.bean.kqgl.AttGroupBinPunchMode">
   		<result column="kqzId" property="kqzId" jdbcType="INTEGER" />
   		<result column="type" property="type" jdbcType="INTEGER" />
    </collection>
    
  </resultMap>
  
  <resultMap id="YgglEmpInfoMap" type="cn.timer.api.bean.yggl.YgglMainEmp" >
        	<id column="id" property="id" />
                <id column="id" property="id" />
                <result column="emp_num" property="empNum" />
                <result column="phone" property="phone" />
                <result column="password" property="password" />
                <result column="head_url" property="headUrl" />
                <result column="name" property="name" />
                <result column="english_name" property="englishName" />
                <result column="sex" property="sex" />
                <result column="zj_type" property="zjType" />
                <result column="zj_num" property="zjNum" />
                <result column="sfzyx_time" property="sfzyxTime" />
                <result column="birthday" property="birthday" />
                <result column="age" property="age" />
                <result column="is_married" property="isMarried" />
                <result column="is_pregnant" property="isPregnant" />
                <result column="area" property="area" />
                <result column="mz" property="mz" />
                <result column="zzmm" property="zzmm" />
                <result column="jg" property="jg" />
                <result column="city" property="city" />
                <result column="hk_type" property="hkType" />
                <result column="hk_address" property="hkAddress" />
                <result column="qq" property="qq" />
                <result column="wechat" property="wechat" />
                <!-- <result column="email" property="email" /> -->
                <result column="blood_type" property="bloodType" />
                <result column="language" property="language" />
                <result column="edu" property="edu" />
                <result column="zy" property="zy" />
                <result column="zz_remark" property="zzRemark" />
                <result column="job_type" property="jobType" />
                <result column="job_status" property="jobStatus" />
                <result column="rz_time" property="rzTime" />
                <result column="syq" property="syq" />
                <result column="zz_time" property="zzTime" />
                <result column="sjzz_time" property="sjzzTime" />
                <result column="is_jrkq" property="isJrkq" />
                <result column="job_num" property="jobNum" />
                <result column="work_address" property="workAddress" />
                <result column="work_phone" property="workPhone" />
                <result column="work_email" property="workEmail" />
                <result column="zpqd" property="zpqd" />
                <result column="bmgw_id" property="bmgwId" />
                <result column="org_code" property="orgCode" />
                <result column="unionid" property="unionid" />
                <result column="openid" property="openid" />
                <result column="mpopenid" property="mpopenid" />
                <result column="appopenid" property="appopenid" />
    </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>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select  kqz.*,
	(select bc.`name` from kqgl_asso_bcsz bc where bc.id = kqz.overtime_rules_id) as overtimeRulesName
    from kqgl_main_kqz kqz
    where kqz.id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from kqgl_main_kqz
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="cn.timer.api.bean.kqgl.AttendanceGroup" useGeneratedKeys="true" keyProperty="id">
    insert into kqgl_main_kqz (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,restdayclock)
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{kqbz,jdbcType=VARCHAR}, 
      #{pbfs,jdbcType=INTEGER}, #{qyid,jdbcType=INTEGER}, #{isFdjjr,jdbcType=INTEGER}, 
      #{kqkssjTime,jdbcType=VARCHAR}, #{zsgzsc,jdbcType=VARCHAR}, #{zcgzsc,jdbcType=VARCHAR}, 
      #{jbzdsc,jdbcType=VARCHAR}, #{isDqtx,jdbcType=INTEGER}, #{txry,jdbcType=VARCHAR}, 
      #{txfs,jdbcType=INTEGER}, #{txxhts,jdbcType=INTEGER}, #{txsjTime,jdbcType=VARCHAR}, 
      #{tsfs,jdbcType=VARCHAR}, #{isWpbsdk,jdbcType=INTEGER}, #{sybc,jdbcType=VARCHAR},
      #{isXzbcdk,jdbcType=INTEGER},#{isWq,jdbcType=INTEGER},#{restdayclock,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="cn.timer.api.bean.kqgl.AttendanceGroup" useGeneratedKeys="true" keyProperty="id">
    insert into kqgl_main_kqz
    <trim prefix="(" suffix=")" suffixOverrides="," >
      
      <if test="name != null" >
        name,
      </if>
      <if test="kqbz != null" >
        kqbz,
      </if>
      <if test="pbfs != null" >
        pbfs,
      </if>
      <if test="qyid != null" >
        qyid,
      </if>
      <if test="isFdjjr != null" >
        is_fdjjr,
      </if>
      <if test="kqkssjTime != null" >
        kqkssj_time,
      </if>
      <if test="zsgzsc != null" >
        zsgzsc,
      </if>
      <if test="zcgzsc != null" >
        zcgzsc,
      </if>
      <if test="jbzdsc != null" >
        jbzdsc,
      </if>
      <if test="isDqtx != null" >
        is_dqtx,
      </if>
      <if test="txry != null" >
        txry,
      </if>
      <if test="txfs != null" >
        txfs,
      </if>
      <if test="txxhts != null" >
        txxhts,
      </if>
      <if test="txsjTime != null" >
        txsj_time,
      </if>
      <if test="tsfs != null" >
        tsfs,
      </if>
      <if test="isWpbsdk != null" >
        is_wpbsdk,
      </if>
      <if test="sybc != null" >
        sybc,
      </if>
      <if test="isXzbcdk != null" >
        is_xzbcdk,
      </if>
      <if test="isWq != null" >
        is_wq,
      </if>
      <if test="overtimeRulesId != null" >
        overtime_rules_id,
      </if>
      <if test="restdayclock != null" >
        restdayclock,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      
      <if test="name != null" >
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="kqbz != null" >
        #{kqbz,jdbcType=VARCHAR},
      </if>
      <if test="pbfs != null" >
        #{pbfs,jdbcType=INTEGER},
      </if>
      <if test="qyid != null" >
        #{qyid,jdbcType=INTEGER},
      </if>
      <if test="isFdjjr != null" >
        #{isFdjjr,jdbcType=INTEGER},
      </if>
      <if test="kqkssjTime != null" >
        #{kqkssjTime,jdbcType=VARCHAR},
      </if>
      <if test="zsgzsc != null" >
        #{zsgzsc,jdbcType=VARCHAR},
      </if>
      <if test="zcgzsc != null" >
        #{zcgzsc,jdbcType=VARCHAR},
      </if>
      <if test="jbzdsc != null" >
        #{jbzdsc,jdbcType=VARCHAR},
      </if>
      <if test="isDqtx != null" >
        #{isDqtx,jdbcType=INTEGER},
      </if>
      <if test="txry != null" >
        #{txry,jdbcType=VARCHAR},
      </if>
      <if test="txfs != null" >
        #{txfs,jdbcType=INTEGER},
      </if>
      <if test="txxhts != null" >
        #{txxhts,jdbcType=INTEGER},
      </if>
      <if test="txsjTime != null" >
        #{txsjTime,jdbcType=VARCHAR},
      </if>
      <if test="tsfs != null" >
        #{tsfs,jdbcType=VARCHAR},
      </if>
      <if test="isWpbsdk != null" >
        #{isWpbsdk,jdbcType=INTEGER},
      </if>
      <if test="sybc != null" >
        #{sybc,jdbcType=VARCHAR},
      </if>
      <if test="isXzbcdk != null" >
        #{isXzbcdk,jdbcType=INTEGER},
      </if>
      <if test="isWq != null" >
        #{isWq,jdbcType=INTEGER},
      </if>
      <if test="overtimeRulesId != null" >
        #{overtimeRulesId,jdbcType=INTEGER},
      </if>
      <if test="restdayclock != null" >
        #{restdayclock,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="cn.timer.api.bean.kqgl.AttendanceGroup" >
    update kqgl_main_kqz
    <set >
      <if test="name != null" >
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="kqbz != null" >
        kqbz = #{kqbz,jdbcType=VARCHAR},
      </if>
      <if test="pbfs != null" >
        pbfs = #{pbfs,jdbcType=INTEGER},
      </if>
      <if test="qyid != null" >
        qyid = #{qyid,jdbcType=INTEGER},
      </if>
      <if test="isFdjjr != null" >
        is_fdjjr = #{isFdjjr,jdbcType=INTEGER},
      </if>
      <if test="kqkssjTime != null" >
        kqkssj_time = #{kqkssjTime,jdbcType=VARCHAR},
      </if>
      <if test="zsgzsc != null" >
        zsgzsc = #{zsgzsc,jdbcType=VARCHAR},
      </if>
      <if test="zcgzsc != null" >
        zcgzsc = #{zcgzsc,jdbcType=VARCHAR},
      </if>
      <if test="jbzdsc != null" >
        jbzdsc = #{jbzdsc,jdbcType=VARCHAR},
      </if>
      <if test="isDqtx != null" >
        is_dqtx = #{isDqtx,jdbcType=INTEGER},
      </if>
      <if test="txry != null" >
        txry = #{txry,jdbcType=VARCHAR},
      </if>
      <if test="txfs != null" >
        txfs = #{txfs,jdbcType=INTEGER},
      </if>
      <if test="txxhts != null" >
        txxhts = #{txxhts,jdbcType=INTEGER},
      </if>
      <if test="txsjTime != null" >
        txsj_time = #{txsjTime,jdbcType=VARCHAR},
      </if>
      <if test="tsfs != null" >
        tsfs = #{tsfs,jdbcType=VARCHAR},
      </if>
      <if test="isWpbsdk != null" >
        is_wpbsdk = #{isWpbsdk,jdbcType=INTEGER},
      </if>
      <if test="sybc != null" >
        sybc = #{sybc,jdbcType=VARCHAR},
      </if>
      <if test="isXzbcdk != null" >
        is_xzbcdk = #{isXzbcdk,jdbcType=INTEGER},
      </if>
      <if test="isWq != null" >
        is_wq = #{isWq,jdbcType=INTEGER},
      </if>
      <if test="overtimeRulesId != null" >
        overtime_rules_id = #{overtimeRulesId,jdbcType=INTEGER},
      </if>
      <if test="restdayclock != null" >
        restdayclock = #{restdayclock,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="cn.timer.api.bean.kqgl.AttendanceGroup" >
    update kqgl_main_kqz
    set name = #{name,jdbcType=VARCHAR},
      kqbz = #{kqbz,jdbcType=VARCHAR},
      pbfs = #{pbfs,jdbcType=INTEGER},
      qyid = #{qyid,jdbcType=INTEGER},
      is_fdjjr = #{isFdjjr,jdbcType=INTEGER},
      kqkssj_time = #{kqkssjTime,jdbcType=VARCHAR},
      zsgzsc = #{zsgzsc,jdbcType=VARCHAR},
      zcgzsc = #{zcgzsc,jdbcType=VARCHAR},
      jbzdsc = #{jbzdsc,jdbcType=VARCHAR},
      is_dqtx = #{isDqtx,jdbcType=INTEGER},
      txry = #{txry,jdbcType=VARCHAR},
      txfs = #{txfs,jdbcType=INTEGER},
      txxhts = #{txxhts,jdbcType=INTEGER},
      txsj_time = #{txsjTime,jdbcType=VARCHAR},
      tsfs = #{tsfs,jdbcType=VARCHAR},
      is_wpbsdk = #{isWpbsdk,jdbcType=INTEGER},
      sybc = #{sybc,jdbcType=VARCHAR},
      is_xzbcdk = #{isXzbcdk,jdbcType=INTEGER},
      is_wq = #{isWq,jdbcType=INTEGER},
      overtime_rules_id = #{overtimeRulesId,jdbcType=INTEGER},
      restdayclock = #{restdayclock,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
  
  <select id="selectByPrimaryAttOwn" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select kqz.*
    from kqgl_main_kqz kqz
    LEFT JOIN kqgl_asso_kqzdkfs as zp on zp.kqz_id = kqz.id
	LEFT JOIN kqgl_asso_kqj as kqj on kqj.id = zp.dkfsid
	where zp.type = 1
	and kqz.qyid = #{qyid}
	and kqj.id = #{id}
  </select>
  
  <select id="selectByPrimaryAttAddressOwn" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select kqz.*
    from kqgl_main_kqz kqz
    LEFT JOIN kqgl_asso_kqzdkfs as zp on zp.kqz_id = kqz.id
	LEFT JOIN kqgl_asso_dkdz as dkdz on dkdz.id = zp.dkfsid
	where zp.type = 2
	and kqz.qyid = #{qyid,jdbcType=INTEGER}
	and dkdz.id = #{id,jdbcType=INTEGER}
  </select>
  
  <select id="selectByPrimaryAttWIFIOwn" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select kqz.*
    from kqgl_main_kqz kqz
    LEFT JOIN kqgl_asso_kqzdkfs as zp on zp.kqz_id = kqz.id
	LEFT JOIN kqgl_asso_dkwx as dkwx on dkwx.id = zp.dkfsid
	where zp.type = 3
	and kqz.qyid = #{qyid}
	and dkwx.id = #{id}
  </select>
  
  <select id="selectAttGroupListByQyid" resultMap="BaseResultMap">
    select kqz.*,
    	case kqz.pbfs 
		WHEN  '1' then '固定排班' 
		WHEN  '2' then '排班制' 
		else '自由工时'  
		end as pbfsnm,
		znm.kqznum,
		dkfs.type
		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 = #{qyid}
				GROUP BY yhkqz.kqzid) as znm on znm.kqzid = kqz.id
	LEFT JOIN (select kqzdkfs.kqz_id as kqzId,kqzdkfs.type as type from kqgl_asso_kqzdkfs kqzdkfs 
			where kqzdkfs.qyid = #{qyid,jdbcType=INTEGER}) as dkfs on dkfs.kqzId = kqz.id
		where 1=1
		and kqz.qyid = #{qyid}
		
  </select>
  
  <select id="selectKqglMainKqzByQuery" resultMap="BaseResultMap" parameterType="cn.timer.api.bean.kqgl.KqzAttendanceGroupSearch">
  select kqz.*,
    	case kqz.pbfs 
		WHEN  '1' then '固定排班' 
		WHEN  '2' then '排班制' 
		else '自由工时'  
		end as pbfsnm,
		znm.kqznum,
		dkfs.type
		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 = #{qyid,jdbcType=INTEGER}
				GROUP BY yhkqz.kqzid) as znm on znm.kqzid = kqz.id
	LEFT JOIN (select kqzdkfs.kqz_id as kqzId,kqzdkfs.type as type from kqgl_asso_kqzdkfs kqzdkfs 
			where kqzdkfs.qyid = #{qyid,jdbcType=INTEGER}) as dkfs on dkfs.kqzId = kqz.id
		where 1=1
		and kqz.qyid = #{qyid}
		<if test="overall != null" >
	    	and kqz.name like CONCAT('%',#{overall,jdbcType=VARCHAR},'%')
	    </if>
  </select>
  
  <select id="selectAttGroupMachineByQyid" resultMap="BaseResultMap">
	select kqz.*,
		 kqzdkfs.dkfsid as kqjid 
	 from kqgl_main_kqz kqz
	 LEFT JOIN kqgl_asso_kqzdkfs as kqzdkfs on kqzdkfs.kqz_id = kqz.id and kqzdkfs.type = 1 and kqzdkfs.state = 1
	 where kqz.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="selectAttendanceOfficer" resultMap="YgglEmpInfoMap">
    select * from yggl_main_emp info
    where org_code = #{orgCode,jdbcType=VARCHAR}
	<if test="text != ''" >
		and (info.`name` like CONCAT('%',#{text,jdbcType=VARCHAR},'%') or info.emp_num = #{text,jdbcType=VARCHAR})
	</if>
  </select>
  
  
  
  <select id="EmployeeListByorgCode" resultMap="YgglEmpInfoMap">
    select * from yggl_main_emp
    where org_code = #{orgCode,jdbcType=VARCHAR}
  </select>
  
  <select id="selectByPrimaryByempNum" resultMap="YgglEmpInfoMap" parameterType="java.lang.Integer" >
    select * from yggl_main_emp emp
    where emp.emp_num = #{empNum,jdbcType=VARCHAR} and emp.org_code = #{orgCode,jdbcType=INTEGER}
  </select>
  
  
  
  
  <delete id="deleteTBlFkcmdTransCmdParamByTransId" parameterType="java.lang.String">
		delete from kqjg_fkcmd_trans_cmd_param where trans_id = #{transId}
	</delete>
	
	<insert id="insertTBlFkcmdTransCmdParam" parameterType="cn.timer.api.bean.kqgl.TBlFkcmdTransCmdParam" >
		insert into kqjg_fkcmd_trans_cmd_param
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="transId != null">
				trans_id,
			</if>
			<if test="deviceId != null">
				device_id,
			</if>
			<if test="cmdParam != null">
				cmd_param,
			</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<if test="transId != null">
				#{transId,jdbcType=VARCHAR},
			</if>
			<if test="deviceId != null">
				#{deviceId,jdbcType=VARCHAR},
			</if>
			<if test="cmdParam != null">
				#{cmdParam,jdbcType=VARBINARY},
			</if>
		</trim>
	</insert>
	
	<delete id="deleteTBlFkcmdTransCmdResultByTransId" parameterType="java.lang.String">
		delete from kqjg_fkcmd_trans_cmd_result where trans_id = #{transId}
	</delete>
	
	
	<insert id="insertTBlFkcmdTrans" parameterType="cn.timer.api.bean.kqgl.TBlFkcmdTrans" >
		insert into kqjg_fkcmd_trans
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="transId != null">
				trans_id,
			</if>
			<if test="deviceId != null">
				device_id,
			</if>
			<if test="cmdCode != null">
				cmd_code,
			</if>
			<if test="returnCode != null">
				return_code,
			</if>
			<if test="status != null">
				status,
			</if>
			<if test="updateTime != null">
				update_time,
			</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<if test="transId != null">
				#{transId,jdbcType=VARCHAR},
			</if>
			<if test="deviceId != null">
				#{deviceId,jdbcType=VARCHAR},
			</if>
			<if test="cmdCode != null">
				#{cmdCode,jdbcType=VARCHAR},
			</if>
			<if test="returnCode != null">
				#{returnCode,jdbcType=VARCHAR},
			</if>
			<if test="status != null">
				#{status,jdbcType=VARCHAR},
			</if>
			<if test="updateTime != null">
				#{updateTime,jdbcType=VARCHAR},
			</if>
		</trim>
	</insert>
	
	<select id="selectTBlFkcmdTransNum" resultType="java.lang.String" >
		select max(trans_id) as trans_id from kqjg_fkcmd_trans
	</select>
  
</mapper>