<?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.sooka.applyData.mapper.DataRecordMapper">

    <resultMap type="DataRecord" id="DataRecordResult">
        <result property="id"    column="id"    />
        <result property="applyDeptId"    column="apply_dept_id"    />
        <result property="applyDeptName"    column="apply_dept_name"    />
        <result property="applyUserId"    column="apply_user_id"    />
        <result property="applyUserName"    column="apply_user_name"    />
        <result property="tel"    column="tel"    />
        <result property="ip"    column="ip"    />
        <result property="provideDeptId"    column="provide_dept_id"    />
        <result property="provideDeptName"    column="provide_dept_name"    />
        <result property="applyTime"    column="apply_time"    />
        <result property="shareType"    column="share_type"    />
        <result property="frequency"    column="frequency"    />
        <result property="duration"    column="duration"    />
        <result property="startTime"    column="start_time"    />
        <result property="shareType"    column="share_type"    />
        <result property="endTime"    column="end_time"    />
        <result property="status"    column="status"    />
        <result property="approvalUserId"    column="approval_user_id"    />
        <result property="approvalUserName"    column="approval_user_name"    />
        <result property="approvalTime"    column="approval_time"    />
        <result property="approvalFile"    column="approval_file"    />
        <result property="approvalContent"    column="approval_content"    />
        <result property="examineUserId"    column="examine_user_id"    />
        <result property="examineUserName"    column="examine_user_name"    />
        <result property="examineTime"    column="examine_time"    />
        <result property="examineFile"    column="examine_file"    />
        <result property="examineContent"    column="examine_content"    />
    </resultMap>

    <resultMap id="DataRecordDataDetailedResult" type="DataRecord" extends="DataRecordResult">
        <collection property="DataDetailedList" notNullColumn="id" javaType="java.util.List" resultMap="DataDetailedResult" />
    </resultMap>

    <resultMap type="DataDetailed" id="DataDetailedResult">
        <result property="subId"    column="sub_id"    />
        <result property="recordId"    column="record_id"    />
        <result property="intId"    column="int_id"    />
        <result property="secretKey"    column="secret_key"    />
        <result property="purpose"    column="purpose"    />
        <result property="file"    column="file"    />
        <result property="interfaceName"    column="interface_name"    />
        <result property="interfaceAddress"    column="interface_address"    />
        <result property="code"    column="code"    />
    </resultMap>

    <sql id="selectDataRecordVo">
        SELECT
            id,
            apply_dept_id,
            apply_dept_name,
            apply_user_id,
            apply_user_name,
            tel,
            ip,
            provide_dept_id,
            provide_dept_name,
            apply_time,
            share_type,
            frequency,
            CONCAT(start_time,' - ',end_time) duration,
            start_time,
            end_time,
            status
        FROM
            data_record
    </sql>

    <select id="selectDataRecordList" parameterType="DataRecord" resultMap="DataRecordResult">
        <include refid="selectDataRecordVo"/>
        <where>
            <if test="provideDeptId != null and provideDeptId != ''"> and provide_dept_id = #{provideDeptId}</if>
            <if test="provideDeptName != null and provideDeptName != ''"> and provide_dept_name like concat('%', #{provideDeptName}, '%')</if>
            <if test="applyUserId != null and applyUserId != ''"> and apply_user_id = #{applyUserId}</if>
            <if test="status != null and status != ''"> and status = #{status}</if>
        </where>
        ORDER BY
        status ASC,
        apply_time DESC,
        end_time ASC
    </select>

    <select id="selectDataRecordById" parameterType="String" resultMap="DataRecordDataDetailedResult">
        SELECT
            a.id,
            a.apply_dept_id,
            a.apply_dept_name,
            a.apply_user_id,
            a.apply_user_name,
            a.tel,
            a.ip,
            a.provide_dept_id,
            a.provide_dept_name,
            a.apply_time,
            a.share_type,
            a.frequency,
            CONCAT(
                a.start_time,
                ' - ',
                a.end_time
            ) duration,
            a.start_time,
            a.end_time,
            a.status,
            a.approval_user_id,
            a.approval_user_name,
            a.approval_time,
            a.approval_file,
            a.approval_content,
            a.examine_user_id,
            a.examine_user_name,
            a.examine_time,
            a.examine_file,
            a.examine_content,
            b.id sub_id,
            b.record_id,
            b.int_id,
            b.secret_key,
            b.purpose,
            b.file,
            c.menu_name interface_name
        FROM
            data_record a
        LEFT JOIN data_detailed b ON b.record_id = a.id
        LEFT JOIN sys_menu c on b.int_id = c.menu_id
        WHERE a.id = #{id}
    </select>

    <!--新增主表，返回主键-->
    <insert id="insertDataRecord" parameterType="DataRecord" useGeneratedKeys="true" keyProperty="id">
        insert into data_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="applyDeptId != null">apply_dept_id,</if>
            <if test="applyDeptName != null">apply_dept_name,</if>
            <if test="applyUserId != null">apply_user_id,</if>
            <if test="applyUserName != null">apply_user_name,</if>
            <if test="tel != null">tel,</if>
            <if test="ip != null">ip,</if>
            <if test="provideDeptId != null">provide_dept_id,</if>
            <if test="provideDeptName != null">provide_dept_name,</if>
            <if test="applyTime != null">apply_time,</if>
            <if test="shareType != null">share_type,</if>
            <if test="frequency != null">frequency,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="status != null">status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="applyDeptId != null">#{applyDeptId},</if>
            <if test="applyDeptName != null">#{applyDeptName},</if>
            <if test="applyUserId != null">#{applyUserId},</if>
            <if test="applyUserName != null">#{applyUserName},</if>
            <if test="tel != null">#{tel},</if>
            <if test="ip != null">#{ip},</if>
            <if test="provideDeptId != null">#{provideDeptId},</if>
            <if test="provideDeptName != null">#{provideDeptName},</if>
            <if test="applyTime != null">#{applyTime},</if>
            <if test="shareType != null">#{shareType},</if>
            <if test="frequency != null">#{frequency},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="status != null">#{status},</if>
         </trim>
    </insert>

    <!--新增申请明细-->
    <insert id="insertDataDetailed" parameterType="DataDetailed" useGeneratedKeys="true" keyProperty="id">
        insert into data_detailed(int_id, secret_key, purpose, int_code) values (#{intId} ,#{secretKey}, #{purpose}, #{code})
    </insert>

    <select id="selectDataRecordDetailedBySubId" parameterType="String" resultMap="DataDetailedResult">
        SELECT
            id,
            record_id,
            int_id,
            secret_key,
            purpose,
            file
        FROM
            data_detailed
        WHERE
            id = #{subId}
    </select>

    <select id="selectDataDetailedByIntId" parameterType="String" resultMap="DataDetailedResult">
        SELECT
            id,
            record_id,
            int_id,
            secret_key,
            purpose,
            file
        FROM
            data_detailed
        WHERE
            int_id = #{intId}
    </select>

    <!--批量新增子表-->
    <insert id="batchDataDetailed">
        insert into data_detailed(record_id, int_id, secret_key, purpose, file) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.recordId}, #{item.intId} ,#{item.secretKey}, #{item.purpose}, #{item.file})
        </foreach>
    </insert>

    <update id="editDataRecord" parameterType="DataRecord">
        UPDATE data_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="applyDeptId != null">apply_dept_id = #{applyDeptId},</if>
            <if test="applyDeptName != null">apply_dept_name = #{applyDeptName},</if>
            <if test="applyUserId != null">apply_user_id = #{applyUserId},</if>
            <if test="applyUserName != null">apply_user_name = #{applyUserName},</if>
            <if test="tel != null">tel = #{tel},</if>
            <if test="ip != null">ip = #{ip},</if>
            <if test="provideDeptId != null">provide_dept_id = #{provideDeptId},</if>
            <if test="provideDeptName != null">provide_dept_name = #{provideDeptName},</if>
            <if test="applyTime != null">apply_time = #{applyTime},</if>
            <if test="shareType != null">share_type = #{shareType},</if>
            <if test="frequency != null">frequency = #{frequency},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="approvalUserId != null">approval_user_id = #{approvalUserId},</if>
            <if test="approvalUserName != null">approval_user_name = #{approvalUserName},</if>
            <if test="approvalTime != null">approval_time = #{approvalTime},</if>
            <if test="approvalFile != null">approval_file = #{approvalFile},</if>
            <if test="approvalContent != null">approval_content = #{approvalContent},</if>
            <if test="examineUserId != null">examine_user_id = #{examineUserId},</if>
            <if test="examineUserName != null">examine_user_name = #{examineUserName},</if>
            <if test="examineTime != null">examine_time = #{examineTime},</if>
            <if test="examineFile != null">examine_file = #{examineFile},</if>
            <if test="examineContent != null">examine_content = #{examineContent},</if>
        </trim>
        WHERE id = #{id}
    </update>

    <update id="updateStatus" parameterType="String">
        update data_record
        <set>
            <if test="status != null and status != ''">status = #{status},</if>
        </set>
        where id = #{id}
    </update>







    <update id="changeStatus" parameterType="DataDetailed">
        update data_detailed
        <set>
            <if test="status != null and status != ''">status = #{status},</if>
        </set>
        where id = #{id}
    </update>

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

    <delete id="deleteDataDetailedByRecordIds" parameterType="String">
        delete from data_detailed where record_id in
        <foreach item="recordId" collection="array" open="(" separator="," close=")">
            #{recordId}
        </foreach>
    </delete>

    <select id="applyFrequency" parameterType="String" resultType="java.util.LinkedHashMap">
        SELECT
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 1 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS jan,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 2 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS feb,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 3 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS mar,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 4 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS apr,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 5 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS may,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 6 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS jun,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 7 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS jul,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 8 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS aug,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 9 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS sep,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 10 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS oct,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 11 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS nov,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 12 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS decb
        FROM
            (
                SELECT
                    r.apply_time,
                    i.share_type
                FROM
                    data_record r
                LEFT JOIN data_detailed d ON r.id = d.record_id
                LEFT JOIN t_u_interfaceinfo i ON d.int_id = i.id
                WHERE
                    share_type in
                    <foreach item="share_type" collection="array" open="(" separator="," close=")">
                        #{share_type}
                    </foreach>
            ) tb
    </select>

    <select id="applyCount" parameterType="String" resultType="java.util.LinkedHashMap">
        SELECT
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 1 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS jan,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 2 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS feb,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 3 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS mar,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 4 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS apr,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 5 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS may,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 6 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS jun,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 7 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS jul,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 8 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS aug,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 9 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS sep,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 10 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS oct,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 11 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS nov,
            IFNULL(
                SUM(
                    CASE MONTH (apply_time)
                    WHEN 12 THEN
                        1
                    ELSE
                        0
                    END
                ),
                0
            ) AS decb
        FROM
            (
                SELECT
                    r.apply_time,
                    i.share_type
                FROM
                    data_record r
                LEFT JOIN data_detailed d ON r.id = d.record_id
                LEFT JOIN t_u_interfaceinfo i ON d.int_id = i.id
                WHERE
                    r.provide_dept_id = #{provideDeptId}
                <if test="year != null">
                    and DATE_FORMAT(apply_time,'%Y') = #{year}
                </if>
            ) tb
    </select>

</mapper>
