<?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="com.yanzhu.jh.project.mapper.SurProjectAttendanceDataMapper">
    
    <resultMap type="SurProjectAttendanceData" id="SurProjectAttendanceDataResult">
        <result property="id"    column="id"    />
        <result property="cfgid"    column="cfgid"    />
        <result property="appId"    column="app_id"    />
        <result property="projectId"    column="projectId"    />
        <result property="projectName"    column="projectName"    />
        <result property="deptId"    column="deptId"    />
        <result property="deptName"    column="deptName"    />
        <result property="vendorsCode"    column="vendors_code"    />
        <result property="serverid"    column="serverid"    />
        <result property="workerId"    column="workerId"    />
        <result property="workerName"    column="workerName"    />
        <result property="identification"    column="identification"    />
        <result property="workerPhoto"    column="workerPhoto"    />
        <result property="workerGender"    column="workerGender"    />
        <result property="birthDate"    column="birthDate"    />
        <result property="ethnic"    column="ethnic"    />
        <result property="nativePlace"    column="nativePlace"    />
        <result property="phone"    column="phone"    />
        <result property="workTypeName"    column="workTypeName"    />
        <result property="specWorkType"    column="specWorkType"    />
        <result property="groupName"    column="groupName"    />
        <result property="companyTypeId"    column="companyTypeId"    />
        <result property="companyName"    column="companyName"    />
        <result property="attendanceTime"    column="attendance_time"    />
        <result property="attendanceOutTime"    column="attendance_out_time"    />
        <result property="scanPhoto"    column="scanPhoto"    />
        <result property="teamId"    column="teamId"    />
        <result property="workTypeCode"    column="workTypeCode"    />
        <result property="companyId"    column="companyId"    />
        <result property="vendorId"    column="vendorId"    />
        <result property="deviceCode"    column="device_code"    />
        <result property="isDel"    column="is_del"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectSurProjectAttendanceDataVo">
        select * from sur_project_attendance_data_${year}
    </sql>

    <select id="findCurrentAttendanceData" parameterType="SurProjectAttendanceData" resultMap="SurProjectAttendanceDataResult">
        select * from sur_project_attendance_data_${year}
        <where>
            <if test="cfgid != null "> and cfgid = #{cfgid}</if>
            <if test="appId != null "> and app_id = #{appId}</if>
            <if test="workerId != null "> and workerId = #{workerId}</if>
            <if test="attendanceTime != null  and attendanceTime != ''"> and date(attendance_time) = date(#{attendanceTime})</if>
        </where>
        order by attendance_time desc
    </select>

    <select id="selectSurProjectAttendanceDataListEx" parameterType="SurProjectAttendanceData" resultMap="SurProjectAttendanceDataResult">
        select * from sur_project_attendance_data_${year}
        <where>  
            <if test="cfgid != null "> and cfgid = #{cfgid}</if>
            <if test="appId != null "> and app_id = #{appId}</if>
            <if test="projectId != null "> and projectId = #{projectId}</if>
            <if test="deptId != null "> and deptId = #{deptId}</if>
            <if test="vendorsCode != null  and vendorsCode != ''"> and vendors_code = #{vendorsCode}</if>
            <if test="serverid != null "> and serverid = #{serverid}</if>
            <if test="workerId != null "> and workerId = #{workerId}</if>
            <if test="attendanceTime != null  and attendanceTime != ''"> and date(attendance_time) = date(#{attendanceTime})</if>
            <if test="identification != null  and identification != ''"> and identification = #{identification}</if>
            <if test="teamId != null "> and teamId = #{teamId}</if>
            <if test="workTypeCode != null  and workTypeCode != ''"> and workTypeCode = #{workTypeCode}</if>
            <if test="companyId != null "> and companyId = #{companyId}</if>
            <if test="vendorId != null "> and vendorId = #{vendorId}</if>            
            <if test="deviceCode != null  and deviceCode != ''"> and device_code = #{deviceCode}</if>
            <if test="companyTypeId!=null">
                <if test="companyTypeId>100">
                    <if test="companyTypeId==101">
                        and companyTypeId in (1,6)
                    </if>
                    <if test="companyTypeId==102">
                        and companyTypeId =8
                    </if>
                    <if test="companyTypeId==103">
                        and companyTypeId in (0,2,3,4,5)
                    </if>
                </if>
                <if test="companyTypeId &lt;100">
                    and companyTypeId=#{companyTypeId}
                </if>
            </if>
            <if test="scanPhoto != null  and scanPhoto != ''"> and scanPhoto = #{scanPhoto}</if>
            <if test="isDel != null "> and is_del = #{isDel}</if>
        </where>
        order by attendance_time desc
    </select>

    <select id="selectSurProjectAttendanceDataList" parameterType="SurProjectAttendanceData" resultMap="SurProjectAttendanceDataResult">
        <include refid="selectSurProjectAttendanceDataVo"/>
        <where>
            <if test="cfgid != null "> and cfgid = #{cfgid}</if>
            <if test="appId != null "> and app_id = #{appId}</if>
            <if test="projectId != null "> and projectId = #{projectId}</if>
            <if test="deptId != null "> and deptId = #{deptId}</if>
            <if test="vendorsCode != null  and vendorsCode != ''"> and vendors_code = #{vendorsCode}</if>
            <if test="serverid != null "> and serverid = #{serverid}</if>
            <if test="workerId != null "> and workerId = #{workerId}</if>
            <if test="attendanceTime != null  and attendanceTime != ''"> and date(attendance_time) =date(#{attendanceTime})</if>
            <if test="identification != null  and identification != ''"> and identification = #{identification}</if>
            <if test="teamId != null "> and teamId = #{teamId}</if>
            <if test="workTypeCode != null  and workTypeCode != ''"> and workTypeCode = #{workTypeCode}</if>
            <if test="companyId != null "> and companyId = #{companyId}</if>
            <if test="companyTypeId!=null">
                <if test="companyTypeId>100">
                    <if test="companyTypeId==101">
                        and companyTypeId in (1,6)
                    </if>
                    <if test="companyTypeId==102">
                        and companyTypeId =8
                    </if>
                    <if test="companyTypeId==103">
                        and companyTypeId in (0,2,3,4,5)
                    </if>
                </if>
                <if test="companyTypeId &lt;100">
                    and companyTypeId=#{companyTypeId}
                </if>
            </if>
            <if test="vendorId != null "> and vendorId = #{vendorId}</if>            
            <if test="deviceCode != null  and deviceCode != ''"> and device_code = #{deviceCode}</if>            
            <if test="scanPhoto != null  and scanPhoto != ''"> and scanPhoto = #{scanPhoto}</if>            
            <if test="isDel != null "> and is_del = #{isDel}</if>
        </where>
        order by attendance_time desc
    </select>
    
    <select id="selectSurProjectAttendanceDataById" parameterType="Long" resultMap="SurProjectAttendanceDataResult">
        <include refid="selectSurProjectAttendanceDataVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertSurProjectAttendanceData" parameterType="SurProjectAttendanceData" useGeneratedKeys="true" keyProperty="id">
        insert into sur_project_attendance_data_${year}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="cfgid != null">cfgid,</if>
            <if test="appId != null">app_id,</if>
            <if test="projectId != null">projectId,</if>
            <if test="projectName != null">projectName,</if>
            <if test="deptId != null">deptId,</if>
            <if test="deptName != null">deptName,</if>
            <if test="vendorsCode != null">vendors_code,</if>
            <if test="serverid != null">serverid,</if>
            <if test="workerId != null">workerId,</if>
            <if test="workerName != null">workerName,</if>
            <if test="identification != null">identification,</if>
            <if test="workerPhoto != null">workerPhoto,</if>
            <if test="workerGender != null">workerGender,</if>
            <if test="birthDate != null">birthDate,</if>
            <if test="ethnic != null">ethnic,</if>
            <if test="nativePlace != null">nativePlace,</if>
            <if test="phone != null">phone,</if>
            <if test="workTypeName != null">workTypeName,</if>
            <if test="specWorkType != null">specWorkType,</if>
            <if test="groupName != null">groupName,</if>
            <if test="companyTypeId != null">companyTypeId,</if>
            <if test="companyName != null">companyName,</if>
            <if test="attendanceTime != null">attendance_time,</if>
            <if test="attendanceOutTime != null">attendance_out_time,</if>
            <if test="scanPhoto != null">scanPhoto,</if>
            <if test="teamId != null">teamId,</if>
            <if test="workTypeCode != null">workTypeCode,</if>
            <if test="companyId != null">companyId,</if>
            <if test="vendorId != null">vendorId,</if>
            <if test="deviceCode != null">device_code,</if>
            <if test="isDel != null">is_del,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="cfgid != null">#{cfgid},</if>
            <if test="appId != null">#{appId},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="projectName != null">#{projectName},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="deptName != null">#{deptName},</if>
            <if test="vendorsCode != null">#{vendorsCode},</if>
            <if test="serverid != null">#{serverid},</if>
            <if test="workerId != null">#{workerId},</if>
            <if test="workerName != null">#{workerName},</if>
            <if test="identification != null">#{identification},</if>
            <if test="workerPhoto != null">#{workerPhoto},</if>
            <if test="workerGender != null">#{workerGender},</if>
            <if test="birthDate != null">#{birthDate},</if>
            <if test="ethnic != null">#{ethnic},</if>
            <if test="nativePlace != null">#{nativePlace},</if>
            <if test="phone != null">#{phone},</if>
            <if test="workTypeName != null">#{workTypeName},</if>
            <if test="specWorkType != null">#{specWorkType},</if>
            <if test="groupName != null">#{groupName},</if>
            <if test="companyTypeId != null">#{companyTypeId},</if>
            <if test="companyName != null">#{companyName},</if>
            <if test="attendanceTime != null">#{attendanceTime},</if>
            <if test="attendanceOutTime != null">#{attendanceOutTime},</if>
            <if test="scanPhoto != null">#{scanPhoto},</if>
            <if test="teamId != null">#{teamId},</if>
            <if test="workTypeCode != null">#{workTypeCode},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="vendorId != null">#{vendorId},</if>
            <if test="deviceCode != null">#{deviceCode},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateSurProjectAttendanceData" parameterType="SurProjectAttendanceData">
        update sur_project_attendance_data_${year}
        <trim prefix="SET" suffixOverrides=",">
            <if test="cfgid != null">cfgid = #{cfgid},</if>
            <if test="appId != null">app_id = #{appId},</if>
            <if test="projectId != null">projectId = #{projectId},</if>
            <if test="projectName != null">projectName = #{projectName},</if>
            <if test="deptId != null">deptId = #{deptId},</if>
            <if test="deptName != null">deptName = #{deptName},</if>
            <if test="vendorsCode != null">vendors_code = #{vendorsCode},</if>
            <if test="serverid != null">serverid = #{serverid},</if>
            <if test="workerId != null">workerId = #{workerId},</if>
            <if test="workerName != null">workerName = #{workerName},</if>
            <if test="identification != null">identification = #{identification},</if>
            <if test="workerPhoto != null">workerPhoto = #{workerPhoto},</if>
            <if test="workerGender != null">workerGender = #{workerGender},</if>
            <if test="birthDate != null">birthDate = #{birthDate},</if>
            <if test="ethnic != null">ethnic = #{ethnic},</if>
            <if test="nativePlace != null">nativePlace = #{nativePlace},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="workTypeName != null">workTypeName = #{workTypeName},</if>
            <if test="specWorkType != null">specWorkType = #{specWorkType},</if>
            <if test="groupName != null">groupName = #{groupName},</if>
            <if test="companyTypeId != null">companyTypeId = #{companyTypeId},</if>
            <if test="companyName != null">companyName = #{companyName},</if>
            <if test="attendanceTime != null">attendance_time = #{attendanceTime},</if>
            <if test="attendanceOutTime != null">attendance_out_time = #{attendanceOutTime},</if>
            <if test="scanPhoto != null">scanPhoto = #{scanPhoto},</if>
            <if test="teamId != null">teamId = #{teamId},</if>
            <if test="workTypeCode != null">workTypeCode = #{workTypeCode},</if>
            <if test="companyId != null">companyId = #{companyId},</if>
            <if test="vendorId != null">vendorId = #{vendorId},</if>
            <if test="deviceCode != null">device_code = #{deviceCode},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSurProjectAttendanceDataById" parameterType="Long">
        delete from sur_project_attendance_data_${year} where id = #{id}
    </delete>

    <delete id="deleteSurProjectAttendanceDataByIds" parameterType="String">
        delete from sur_project_attendance_data_${year} where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteSurProjectAttendanceDataByParams" parameterType="String">
        delete from sur_project_attendance_data_${year} where CONCAT(app_id,'-',serverid,'-',workerId) in
        <foreach collection="list" item="item" separator=","  open="(" close=")">
            #{item}
        </foreach>
    </delete>

    <insert id="batchSurProjectAttendanceData">
        insert into sur_project_attendance_data_${year}( id, cfgid, app_id, projectId, projectName, deptId, deptName, vendors_code, serverid, workerId, workerName, identification, workerPhoto, workerGender, birthDate, ethnic, nativePlace, phone, workTypeName, specWorkType, groupName, companyTypeId,
        companyName, attendance_time, attendance_out_time, scanPhoto, teamId,workTypeCode, companyId, vendorId, device_code, is_del, create_by, create_time, update_by, update_time) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.id}, #{item.cfgid},#{item.appId}, #{item.projectId}, #{item.projectName}, #{item.deptId}, #{item.deptName}, #{item.vendorsCode}, #{item.serverid}, #{item.workerId}, #{item.workerName}, #{item.identification},#{item.workerPhoto}, #{item.workerGender}, #{item.birthDate},#{item.ethnic}, #{item.nativePlace}, #{item.phone}, #{item.workTypeName}, #{item.specWorkType}, #{item.groupName}, #{item.companyTypeId},
            #{item.companyName}, #{item.attendanceTime}, #{item.attendanceOutTime}, #{item.scanPhoto},#{item.teamId}, #{item.workTypeCode}, #{item.companyId}, #{item.vendorId}, #{item.deviceCode},#{item.isDel}, #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime})
        </foreach>
    </insert>

    <select id="getLastServerId" parameterType="SurProjectAttendanceData" resultType="Long">
        SELECT IF(MAX(serverid+0),MAX(serverid+0),0) serverid FROM sur_project_attendance_data_${year} WHERE cfgid=#{cfgid}
    </select>
    <select id="groupByComanyOld"  parameterType="SurProjectAttendanceData" resultMap="SurProjectAttendanceDataResult">
        select companyTypeId,count(1) id from ( <include refid="selectSurProjectAttendanceDataVo"/> ) x2 where id in (
        select min(id) from ( <include refid="selectSurProjectAttendanceDataVo"/> ) x1
                       where  date(attendance_time)=#{attendanceTime}
                        <if test="subDeptId!=null and subDeptId>0"> and project_id in (
                            SELECT id FROM sur_project WHERE isdel=0 AND deptid = #{subDeptId}
                            )
                        </if>
        <if test="prjIds !=null and prjIds.size()>0">
            and project_id in
            <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
                        <if test="projectId!=null and projectId>0"> and project_id=#{projectId}</if>
                       group by workerId )
        and  companyTypeId in (1,2,3,4,5,6,8) group by companyTypeId
    </select>
    <select id="groupByComany"  parameterType="SurProjectAttendanceData" resultMap="SurProjectAttendanceDataResult">
        select g.companyTypeId,count(1) id from sur_project_attendance_user u, sur_project_attendance_cfg c,view_sur_project_attendance_group g
        where u.cfgid=c.id and u.state=0 and u.companyId=g.companyId
        and u.workerid in(
        SELECT workerid FROM sur_project_attendance_data WHERE DATE(attendance_time)=CURDATE()
        and cfgid in (select id from sur_project_attendance_cfg
            <where>
                <if test="projectId!=null and projectId>0">
                    and project_id=#{projectId}
                </if>
                <if test="subDeptId!=null and subDeptId>0">
                    and  project_id in (SELECT id FROM sur_project WHERE deptid=#{subDeptId})
                </if>
                <if test="prjIds !=null and prjIds.size()>0">
                    and project_id in
                    <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                </if>
            </where>
        )
        )
        and g.companyTypeId in (1,2,3,4,5,6,8)

        <if test="projectId!=null and projectId>0">
            and c.project_id=#{projectId}
        </if>
        <if test="subDeptId!=null and subDeptId>0">
            and  c.project_id in (SELECT id FROM sur_project WHERE deptid=#{subDeptId})
        </if>
        <if test="prjIds !=null and prjIds.size()>0">
            and c.project_id in
            <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        group by g.companyTypeId
    </select>
    <select id="groupAllByComany"  parameterType="SurProjectAttendanceData" resultMap="SurProjectAttendanceDataResult">
        select g.companyTypeId,count(1) id from sur_project_attendance_user u, sur_project_attendance_cfg c,view_sur_project_attendance_group g
        where u.cfgid=c.id and u.state=0 and u.companyId=g.companyId and u.state=0
        and g.companyTypeId in (1,2,3,4,5,6,8)
        <if test="projectId!=null and projectId>0">
            and c.project_id=#{projectId}
        </if>
        <if test="subDeptId!=null and subDeptId>0">
            and  c.project_id in (SELECT id FROM sur_project WHERE deptid=#{subDeptId})
        </if>
        <if test="prjIds !=null and prjIds.size()>0">
            and c.project_id in
            <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        group by g.companyTypeId
    </select>

    <select id="getHuazhuPage" parameterType="long" resultType="Long">
        select max(vendorId) vendorId from sur_project_attendance_data_${year} WHERE cfgid=#{id}
    </select>

    <select id="initOtherData" parameterType="map" resultType="map">
        select d.workerId,d.app_id,d.serverid,u.companyId,u.companyName,u.`name`,d.identification,u.recentPhoto,u.gender,u.birthDate,u.ethnic,u.nativePlace,u.phone,
        u.workTypeName,u.specWorkType,u.groupName,g.companyTypeId,d.teamId,u.workTypeCode,d.vendorId,d.device_code,d.scanPhoto,d.is_del
        from sur_project_attendance_data d
        left JOIN sur_project_attendance_user u on d.workerId = u.workerId and d.cfgid = u.cfgid
        left join sur_project_attendance_group g on u.companyId = g.companyId and g.cfgid = u.cfgid
        where d.cfgid=#{cfgid} and date(d.attendance_time) = #{date}
        GROUP BY d.workerId
    </select>

    <select id="initHuaZhuData" parameterType="map" resultType="map">
        select d.workerId,d.app_id,d.serverid,u.companyId,u.companyName,u.`name`,d.identification,u.recentPhoto,u.gender,u.birthDate,u.ethnic,u.nativePlace,u.phone,
        u.workTypeName,u.specWorkType,u.groupName,d.teamId,u.workTypeCode,d.vendorId,d.device_code,d.scanPhoto,d.is_del,
        CASE WHEN sd.type_flag = 2 THEN '1' WHEN sd.type_flag = 3 THEN '2' WHEN sd.type_flag = 4 THEN '8' else '0' end as companyTypeId
        from sur_project_attendance_data d
        left JOIN sur_project_attendance_user u on d.workerId = u.workerId and d.cfgid = u.cfgid
        left join sys_dept sd on u.companyName = sd.dept_name
        where d.cfgid=#{cfgid} and date(d.attendance_time) = #{date}
        GROUP BY d.workerId
    </select>

    <select id="findHuaZhuCompanyType" parameterType="string" resultType="string">
        select CASE WHEN sd.type_flag = 2 THEN '1' WHEN sd.type_flag = 3 THEN '2' WHEN sd.type_flag = 4 THEN '8' else '0' end as companyTypeId
        from sys_dept sd
        where sd.dept_name = #{deptName}
    </select>

    <select id="todayAttendance" parameterType="SurProjectAttendanceData" resultMap="SurProjectAttendanceDataResult">
        select * from sur_project_attendance_data_${year}
        <where>
            <if test="id==1">
                and companyTypeId in (1,6)
            </if>
            <if test="id==2">
                and companyTypeId in (0,2,3,4,5)
            </if>
            <if test="id==8">
                and companyTypeId =8
            </if>
            <if test="projectId!=null and projectId>0">
                and projectId=#{projectId}
            </if>
            <if test="deptId!=null and deptId>0">
                and  projectId in (SELECT id FROM sur_project WHERE deptid=#{deptId})
            </if>
            <if test="prjIds !=null and prjIds.size()>0">
                and projectId in
                <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="attendanceTime != null  and attendanceTime != ''"> and date(attendance_time) =date(#{attendanceTime})</if>
        </where>
    </select>

    <select id="groupTodayCompanyTypeId" parameterType="SurProjectAttendanceData" resultMap="SurProjectAttendanceDataResult">
        select companyTypeId,count(1) id from sur_project_attendance_data_${year}
        <where>
            <if test="projectId!=null and projectId>0">
                and projectId=#{projectId}
            </if>
            <if test="deptId!=null and deptId>0">
                and  projectId in (SELECT id FROM sur_project WHERE deptid=#{deptId})
            </if>
            <if test="prjIds !=null and prjIds.size()>0">
                and projectId in
                <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="attendanceTime != null  and attendanceTime != ''"> and date(attendance_time) =date(#{attendanceTime})</if>
        </where>
        group by companyTypeId
    </select>

</mapper>