<?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.css.simulation.resource.server.infra.db.mysql.mapper.SystemScenePackageMapper">

    <insert id="saveSystemScenePackage" parameterType="api.common.pojo.po.scene.SystemScenePackagePO">
        insert into simulation.system_scene_package
        (id, package_name, scene_describe,
         create_user_id, create_time, modify_user_id, modify_time,
         is_deleted)
        values (#{id,jdbcType=VARCHAR}, #{packageName,jdbcType=VARCHAR}, #{sceneDescribe},
                #{createUserId,jdbcType=VARCHAR}, #{createTime}, #{modifyUserId,jdbcType=VARCHAR}, #{modifyTime},
                #{isDeleted,jdbcType=VARCHAR})
    </insert>


    <select id="querySystemScenePackageList" parameterType="api.common.pojo.param.scene.SystemScenePackageParam"
            resultType="api.common.pojo.vo.scene.SystemScenePackageVO">
        select
        t.id,
        t.package_name,
        t.userCount,t.scene_describe,t.create_time,
        SUM(CASE WHEN s.scene_type = '1' THEN 1 ELSE 0 END) as zrCount,
        SUM(CASE WHEN s.scene_type = '2' THEN 1 ELSE 0 END) as bzCount,
        SUM(CASE WHEN s.scene_type = '3' THEN 1 ELSE 0 END) as jtCount,
        SUM(CASE WHEN s.scene_type = '4' THEN 1 ELSE 0 END) as fhCount,
        SUM(1) as totalCount
        FROM (
        select
        p.*,
        COUNT(u.id) as userCount
        FROM system_scene_package p
        left JOIN system_user_scene u on p.id = u.package_id and u.is_deleted = '0' and p.is_deleted = '0'
        <where>
            <if test="userId != null and userId != ''">
                and u.user_id = #{userId,jdbcType=VARCHAR}
            </if>
        </where>
        GROUP BY p.id
        ) t
       left JOIN system_scene_package_sublist s on t.id = s.scene_and_package and  s.is_deleted='0'
        <where>
           t.is_deleted='0'
            <if test="packageName != null and packageName != ''">
                and t.package_name like CONCAT('%',#{packageName,jdbcType=VARCHAR},'%')
            </if>
            <if test="yearMax != null and yearMax != ''">
                and #{yearMax,jdbcType=VARCHAR} &gt; t.create_time
            </if>
            <if test="yearMin != null and yearMin != ''">
                and #{yearMin,jdbcType=VARCHAR} &lt; t.create_time
            </if>
            <if test="packageNames != null and packageNames.length>0">
                and t.package_name in
                <foreach collection="packageNames" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY t.id order by create_time desc
    </select>

    <select id="queryPackageByUserIdList" parameterType="api.common.pojo.param.scene.SystemScenePackageParam"
            resultType="api.common.pojo.vo.scene.SystemScenePackageVO">
        select a.username, GROUP_CONCAT(DISTINCT (p.package_name)) as package_name, max(a.modify_time) modify_time
        from (select u.id user_id, s.is_deleted, u.username, u.company, s.package_id, s.modify_time
        from system_user u
        left join system_user_scene s on u.id = s.user_id and s.is_deleted = '0'
        where u.is_deleted = '0'
        and u.role_code = '2') a
        left join system_scene_package p on a.package_id = p.id and p.is_deleted = '0'
        where 1=1
        <if test="userName != null and userName != ''">
            and username like CONCAT('%',#{userName,jdbcType=VARCHAR},'%')
        </if>
        <if test="yearMax != null and yearMax != ''">
            and #{yearMax,jdbcType=VARCHAR} &gt; a.modify_time
        </if>
        <if test="yearMin != null and yearMin != ''">
            and #{yearMin,jdbcType=VARCHAR} &lt; a.modify_time
        </if>

        GROUP BY a.username
        ORDER BY a.modify_time DESC
    </select>
    <select id="querySystemScenePackageById" parameterType="api.common.pojo.param.scene.SystemScenePackageParam"
            resultType="api.common.pojo.po.scene.SystemScenePackagePO">
        select id,
               package_name,
               scene_describe,
               create_user_id,
               create_time,
               modify_user_id,
               modify_time
        from system_scene_package
        where id = #{id}

    </select>

    <select id="queryPackageByUserId" parameterType="api.common.pojo.param.scene.SystemScenePackageParam"
            resultType="java.util.Map">
        select a.userName,a.company,a.modify_time modifyTime,a.user_id userId,GROUP_CONCAT(DISTINCT(p.package_name)) as
        packageNames from (select u.id user_id,username userName, company,s.package_id,s.modify_time
        from system_user u left join system_user_scene s
        on u.id=s.user_id and u.is_deleted = '0' and s.is_deleted = '0') a
        left join system_scene_package p on a.package_id = p.id AND p.is_deleted = '0'
        <where>
            1=1
            <if test="userName != null and userName != ''">
                and a.userName=#{userName}
            </if>
        </where>
        GROUP BY a.user_id
        ORDER BY a.modify_time DESC

    </select>

    <update id="deleteSystemScenePackage" parameterType="api.common.pojo.po.scene.SystemScenePackagePO">
        update simulation.system_scene_package
        <set>
            <if test="isDeleted != null and isDeleted!=''">
                is_deleted = #{isDeleted,jdbcType=VARCHAR},
            </if>
            <if test="modifyUserId != null and modifyUserId!=''">
                modify_user_id = #{modifyUserId,jdbcType=VARCHAR},
            </if>
            <if test="modifyTime != null">
                modify_time = #{modifyTime},
            </if>
        </set>
        where id = #{id,jdbcType=VARCHAR}
    </update>


    <update id="updateSystemScenePackage" parameterType="api.common.pojo.po.scene.SystemScenePackagePO">
        update simulation.system_scene_package
        set package_name=#{packageName},
            scene_describe=#{sceneDescribe},
            modify_time=#{modifyTime},
            modify_user_id=#{modifyUserId}
        where id = #{id,jdbcType=VARCHAR}
    </update>

    <select id="querySceneNum" parameterType="java.lang.String" resultType="java.lang.Integer">
        SELECT SUM(num)
        FROM (SELECT COUNT(DISTINCT c.scene_name) AS num
              FROM system_user_scene a
                       JOIN system_scene_package b ON a.package_id = b.id AND b.is_deleted = '0'
                       JOIN system_scene_package_sublist c ON b.id = c.scene_and_package AND c.is_deleted = '0'
                       JOIN scene_natural d ON d.natural_name = c.scene_name AND d.is_deleted = '0'
        <where> d.share='2'
            <if test="userId != null and userId!=''">
                and a.user_id = #{userId}
            </if>
        </where>
        UNION ALL SELECT COUNT(*) AS num from scene_natural where is_deleted = '0'  and share='1'
              UNION ALL
              SELECT COUNT(DISTINCT c.scene_name) AS num
              FROM system_user_scene a
                       JOIN system_scene_package b ON a.package_id = b.id AND b.is_deleted = '0'
                       JOIN system_scene_package_sublist c ON b.id = c.scene_and_package AND c.is_deleted = '0'
                       JOIN scene_accident d ON d.scene_name = c.scene_name AND d.is_deleted = '0'
        <where> d.share='2'
            <if test="userId != null and userId!=''">
                and a.user_id = #{userId}
            </if>
        </where>
        UNION ALL SELECT COUNT(*) AS num from scene_accident where is_deleted = '0'  and share='1'
              UNION ALL
              SELECT COUNT(DISTINCT c.scene_name) AS num
              FROM system_user_scene a
                       JOIN system_scene_package b ON a.package_id = b.id AND b.is_deleted = '0'
                       JOIN system_scene_package_sublist c ON b.id = c.scene_and_package AND c.is_deleted = '0'
                       JOIN scene_standards_regulations d ON d.scene_name = c.scene_name AND d.is_deleted = '0'
        <where> d.share='2'
            <if test="userId != null and userId!=''">
                and a.user_id = #{userId}
            </if>
        </where>
        UNION ALL SELECT COUNT(*) AS num from scene_standards_regulations where is_deleted = '0'  and share='1'
              UNION ALL
              SELECT COUNT(DISTINCT c.scene_name) AS num
              FROM system_user_scene a
                       JOIN system_scene_package b ON a.package_id = b.id AND b.is_deleted = '0'
                       JOIN system_scene_package_sublist c ON b.id = c.scene_and_package AND c.is_deleted = '0'
                       JOIN scene_general_template d ON d.scene_id = c.scene_name AND d.is_deleted = '0'
        <where> d.share='2'
            <if test="userId != null and userId!=''">
                and a.user_id = #{userId}
            </if>
        </where>
        UNION ALL SELECT COUNT(*) AS num from scene_general_template where is_deleted = '0'  and share='1') AS t
    </select>


    <select id="queryScenePackageByPackageName" parameterType="api.common.pojo.po.scene.SystemScenePackageNewPO"
            resultType="java.lang.String">
        select
        id
        from system_scene_package
        <where>
            is_deleted = '0'
            <if test="packageName != null and packageName != ''">
                and package_name=#{packageName}
            </if>
            <if test="id != null and id != ''">
                and id!=#{id}
            </if>
        </where>

    </select>

    <select id="querySystemScenePackageListByTJ" parameterType="api.common.pojo.param.scene.SystemScenePackageParam"
            resultType="java.util.Map">
        SELECT
        c.scene_type, COUNT(DISTINCT c.scene_name ) aaa
        FROM
        system_user_scene a
        LEFT JOIN system_scene_package b ON a.package_id = b.id AND b.is_deleted = '0'
        JOIN system_scene_package_sublist c ON b.id = c.scene_and_package AND c.is_deleted = '0'
        <where>
            a.is_deleted='0'
            <if test="packageNames != null and packageNames.length>0">
                and b.package_name in
                <foreach collection="packageNames" item="item" index="index"
                         separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        group by c.scene_type
    </select>


    <select id="querySceneNaturalListByTJ" parameterType="api.common.pojo.param.scene.SystemScenePackageParam"
            resultType="java.lang.String">
        select DISTINCT(a.scene_name) scene_name
        FROM system_scene_package_sublist a join scene_natural s on a.scene_name=s.natural_name join
        system_scene_package b on a.scene_and_package=b.id
        where
        a.is_deleted='0' and s.is_deleted='0' and b.is_deleted='0' and a.scene_type='1'
        and b.package_name in
        <foreach collection="packageNames" item="item" index="index"
                 separator="," open="(" close=")">
            #{item}
        </foreach>

    </select>
    <select id="querySceneStandardsRegulationsListByTJ"
            parameterType="api.common.pojo.param.scene.SystemScenePackageParam"
            resultType="java.lang.String">
        select DISTINCT(a.scene_name) scene_name
        FROM system_scene_package_sublist a join scene_standards_regulations s on a.scene_name=s.scene_name join
        system_scene_package b on a.scene_and_package=b.id
        where
        a.is_deleted='0' and s.is_deleted='0' and b.is_deleted='0' and a.scene_type='2'
        and b.package_name in
        <foreach collection="packageNames" item="item" index="index"
                 separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="querySceneAccidentListByTJ" parameterType="api.common.pojo.param.scene.SystemScenePackageParam"
            resultType="java.lang.String">
        select distinct(ssps.scene_name)
        from system_scene_package_sublist ssps
        join system_scene_package ssp on ssps.scene_and_package = ssp.id
        join scene_accident sa on ssps.scene_name = sa.scene_name
        where ssps.scene_type = '3'
        and ssps.is_deleted = '0'
        and ssp.is_deleted = '0'
        and sa.is_deleted = '0'
        and ssp.package_name in
        <foreach collection="packageNames" item="item" index="index" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="querySceneGeneralTemplateListByTJ" parameterType="api.common.pojo.param.scene.SystemScenePackageParam"
            resultType="java.lang.String">
        select DISTINCT(a.scene_name) scene_name
        FROM system_scene_package_sublist a join scene_general_template s on a.scene_name=s.scene_id join
        system_scene_package b on a.scene_and_package=b.id
        where
        a.is_deleted='0' and s.is_deleted='0' and b.is_deleted='0' and a.scene_type='4'
        and b.package_name in
        <foreach collection="packageNames" item="item" index="index"
                 separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

</mapper>
