<?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.com.taiji.beidou.ship.warning.model.mapper.ShipWarningModelMapper">

    <!--预警规则列表-->
    <resultMap id="ruleResult" type="warningRuleResult">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="enabled" column="enabled"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <collection property="modelList" javaType="java.util.List" ofType="warningModelResult"
                    select="modelList" column="id">
        </collection>
    </resultMap>

<!--    <resultMap id="ruleModelResult" type="warningModelResult">-->
<!--        <id property="id" column="id"/>-->
<!--        <result property="modelName" column="model_name"/>-->
<!--        <result property="code" column="code"/>-->
<!--        <result property="description" column="description"/>-->
<!--        <collection property="paramList" ofType="warningParamVo"-->
<!--                    column="{modelId=id,ruleId=rule_id}" select="ruleParamList"/>-->

<!--        <collection property="areaList" ofType="warningAreaVo"-->
<!--                    column="{modelId=id,ruleId=rule_id}" select="ruleAreaList"/>-->
<!--    </resultMap>-->

    <select id="selectRuleList" parameterType="string" resultMap="ruleResult">
        select rule.id,rule.`name`,rule.enabled,rule.del_flag,rule.start_time,rule.end_time
         from ax_beidou_warning_rule rule
        where  rule.del_flag='0'
        <if test="enabled != null">and rule.enabled = #{enabled}</if>
        <if test="id != null and id != ''">and rule.id = #{id}</if>
        <if test="ruleName != null and ruleName != ''">and rule.name like concat('%',#{ruleName},'%')</if>
        ORDER BY rule.create_time DESC
    </select>

    <resultMap id="ruleModelResult" type="warningModelResult">
        <id property="id" column="id"/>
        <result property="modelName" column="model_name"/>
        <result property="code" column="code"/>
        <result property="description" column="description"/>
        <collection property="paramList" ofType="warningParamVo"
                    column="{modelId=id,ruleId=rule_id}">
            <id property="id" column="param_id"/>
            <result property="paramProperty" column="param_property"/>
            <result property="paramName" column="param_name"/>
            <result property="paramValue" column="param_value"/>
            <result property="isMust" column="is_must"/>
            <result property="dictId" column="dict_id"/>
            <result property="recommend" column="recommend"/>
            <result property="paramUnit" column="paramUnit"/>
        </collection>

        <collection property="areaList" ofType="warningAreaVo"
                    column="{modelId=id,ruleId=rule_id}">
            <id property="id" column="area_id"/>
            <result property="name" column="area_name"/>
            <result property="location" column="location"/>
        </collection>
    </resultMap>

    <select id="modelList" resultMap="ruleModelResult">
        select model.id as id,rm.rule_id as rule_id,model.`name` as model_name,model.code,model.description,model.del_flag,
                param.id as param_id,rp.param_value,param.recommend,param.is_must,param.del_flag as param_del_flag,
                dict.param_code as param_property,dict.param_name,dict.id as dict_id,dict.unit as paramUnit,
                area.id as area_id,area.`name` as area_name,area.location
        from ax_beidou_warning_rule_model rm
        inner join ax_beidou_warning_model model on model.id = rm.model_id and model.del_flag = '0'
        left JOIN ax_beidou_warning_model_param mp on mp.model_id = model.id
        left join ax_beidou_warning_rule_model_param rp on mp.param_id = rp.param_id and rp.model_id = rm.model_id and rp.rule_id = rm.rule_id
        left join ax_beidou_warning_param param on param.id = mp.param_id and param.del_flag = '0'
        left join ax_beidou_warning_param_dict dict on param.dict_id = dict.id
        left JOIN ax_beidou_warning_rule_model_area ma on ma.model_id = model.id and ma.rule_id = rm.rule_id
        left join ax_beidou_warning_area area on area.id = ma.area_id and area.del_flag = '0'
        where model.del_flag='0' and rm.rule_id = #{id}
    </select>

    <!--预警规则列表 end-->


    <!--预警模型列表-->
    <resultMap id="modelResult" type="warningModelResult">
        <id property="id" column="id"/>
        <result property="modelName" column="model_name"/>
        <result property="code" column="code"/>
        <result property="description" column="description"/>
        <collection property="paramList" ofType="warningParamVo"
                    column="id" select="selectParamList"/>
        <collection property="areaList" ofType="warningAreaVo"
                    column="id" select="areaList"/>
    </resultMap>

    <resultMap id="paramVo" type="warningParamVo">
        <id property="id" column="param_id"/>
        <result property="paramProperty" column="param_property"/>
        <result property="paramName" column="param_name"/>
        <result property="paramValue" column="param_value"/>
        <result property="isMust" column="is_must"/>
        <result property="dictId" column="dict_id"/>
        <result property="recommend" column="recommend"/>
        <result property="paramUnit" column="paramUnit"/>
    </resultMap>

    <resultMap id="areaVo" type="warningAreaVo">
        <id property="id" column="area_id"/>
        <result property="name" column="area_name"/>
        <result property="location" column="location"/>
    </resultMap>

    <select id="selectModelList" parameterType="string" resultMap="modelResult">
        select model.id,model.`name` as model_name, model.code,model.description,model.del_flag
		from ax_beidou_warning_model model
        where model.del_flag='0'
        <if test="id != null and id != ''">and model.id = #{id}</if>
        <if test="modelName != null and modelName != ''">and model.name like concat('%',#{modelName},'%')</if>
        ORDER BY model.create_time DESC
    </select>

    <select id="selectParamList" parameterType="string" resultMap="paramVo">
        select param.id as param_id,param.param_value,param.recommend,
                param.is_must,param.del_flag as param_del_flag,
                dict.param_code as param_property,dict.param_name,dict.unit as paramUnit,dict.id as dict_id
        from ax_beidou_warning_model_param mp
        left join ax_beidou_warning_param param on param.id = mp.param_id
        left join ax_beidou_warning_param_dict dict on param.dict_id = dict.id
        where param.del_flag = '0' and mp.model_id = #{id}
    </select>

    <select id="areaList" parameterType="string" resultMap="areaVo">
        select area.id as area_id,area.`name` as area_name,area.location,
        area.del_flag as area_del_flag
        from ax_beidou_warning_model_area ma
        left join ax_beidou_warning_area area on area.id = ma.area_id
        where  area.del_flag = '0' and ma.model_id = #{id}
    </select>

    <!--预警模型列表 end-->


    <update id="updateEnabledById" parameterType="updateEnabledReq">
        update ax_beidou_warning_rule set enabled = #{enabled} where id=#{id}
    </update>

    <insert id="insertParam" parameterType="warningParamData">
        insert into ax_beidou_warning_param(
            id,
            dict_id,
            <if test="paramProperty != null and paramProperty != ''">param_property,</if>
            <if test="paramValue != null and paramValue != ''">param_value,</if>
            recommend,
            is_must,
            del_flag,
            create_by,
            create_time
        ) values (
            #{id},
            #{dictId},
            <if test="paramProperty != null and paramProperty != ''">#{paramProperty},</if>
            <if test="paramValue != null and paramValue != ''">#{paramValue},</if>
            #{recommend},
            #{isMust},
            '0',
            #{createBy},
            NOW()
        )
    </insert>

    <insert id="insertArea" parameterType="warningAreaData">
        insert into ax_beidou_warning_area(
            id,
            `name`,
            location,
            del_flag,
            create_by,
            create_time
        ) values (
            #{id},
            #{name},
            #{location},
           '0',
            #{createBy},
            NOW()
        )
    </insert>

    <insert id="insertModel" parameterType="warningModelData">
        insert into ax_beidou_warning_model(
            id,
            <if test="code != null and code != ''">code,</if>
            `name`,
            del_flag,
            create_by,
            create_time
        ) values (
            #{id},
            <if test="code != null and code != ''">#{code},</if>
            #{name},
            '0',
            #{createBy},
            NOW()
        )
    </insert>

    <insert id="insertRule" parameterType="warningRuleData">
        insert into ax_beidou_warning_rule(
            id,
            enabled,
            `name`,
            start_time,
            end_time,
            del_flag,
            create_by,
            create_time
        ) values (
            #{id},
            #{enabled},
            #{name},
            #{startTime},
            #{endTime},
            '0',
            #{createBy},
            NOW()
        )
    </insert>

    <insert id="insertModelAndArea" parameterType="string">
        insert into ax_beidou_warning_model_area(
            id,
            model_id,
            area_id,
            create_by,
            create_time
        ) values (
            #{id},
            #{modelId},
            #{areaId},
            #{createBy},
            NOW()
        )
    </insert>

    <insert id="insertModelAndParam" parameterType="string">
        insert into ax_beidou_warning_model_param(
            id,
            model_id,
            param_id,
            create_by,
            create_time
        ) values (
            #{id},
            #{modelId},
            #{paramId},
            #{createBy},
            NOW()
        )
    </insert>

    <insert id="insertRuleAndModel" parameterType="string">
        insert into ax_beidou_warning_rule_model(
            id,
            rule_id,
            model_id,
            create_by,
            create_time
        ) values (
            #{id},
            #{ruleId},
            #{modelId},
            #{createBy},
            NOW()
        )
    </insert>

    <insert id="insertParamDict" parameterType="warningParamDictData">
        insert into ax_beidou_warning_param_dict(
            id,
            param_code,
            param_name,
            unit,
            del_flag
        ) values (
            #{id},
            #{paramCode},
            #{paramName},
            #{unit},
            #{delFlag}
        )
    </insert>

    <insert id="insertRuleAndParam" parameterType="string">
        insert into ax_beidou_warning_rule_param(
            id,
            rule_id,
            param_id,
            param_value,
            create_by,
            create_time
        ) values (
            #{id},
            #{ruleId},
            #{paramId},
            #{paramValue},
            #{createBy},
            NOW()
        )
    </insert>

    <select id="selectParamDict" resultType="warningParamDictData">
        select id,param_code as paramCode,param_name as paramName,unit as paramUnit from ax_beidou_warning_param_dict where del_flag = '0'
    </select>

    <select id="selectAreaList" resultType="warningAreaVo">
        select id,`name`,location,dept_name as deptName from ax_beidou_warning_area area
        LEFT JOIN sys_dept dept ON area.dept_id = dept.dept_id
        where area.del_flag = '0'
        <if test="areaName != null and areaName != ''">and `name` like concat('%',#{areaName},'%')</if>
    </select>

    <update id="updateParamById" parameterType="string">
        update ax_beidou_warning_param set param_value = #{paramValue},
        update_by = #{userId},
        update_time = NOW()
        where id = #{paramId}
    </update>

    <select id="selectRuleByIdCount" parameterType="string" resultType="integer">
        select count(*) from ax_beidou_warning_rule where del_flag = '0'
        and enabled = '1'
        and id = #{id}
    </select>

    <delete id="delRuleAndModelByRule" parameterType="string">
        delete from ax_beidou_warning_rule_model where rule_id = #{ruleId}
    </delete>

    <delete id="delRuleAndParamByRule" parameterType="string">
        delete from ax_beidou_warning_rule_param where rule_id = #{ruleId}
    </delete>

    <update id="updateRule" parameterType="warningRuleData">
        update ax_beidou_warning_rule set
        <if test="name != null and name != ''">`name` = #{name},</if>
        <if test="enabled != null ">enabled = #{enabled},</if>
        <if test="startTime != null and startTime != ''">start_time = #{startTime},</if>
        <if test="endTime != null and endTime != ''">end_time = #{endTime},</if>
        update_by = #{updateBy},
        update_time = NOW()
        where id=#{id}
    </update>

    <update id="updateModel" parameterType="warningModelData">
        update ax_beidou_warning_model set
        <if test="code != null and code != ''">code = #{code},</if>
        <if test="name != null and name != ''">`name` = #{name},</if>
        <if test="description != null and description != ''">description = #{description},</if>
        update_by = #{updateBy},
        update_time = NOW()
        where id=#{id}
    </update>

    <delete id="delModelAndParam" parameterType="string">
        delete from ax_beidou_warning_model_param where model_id = #{modelId}
    </delete>

    <delete id="delModelAndArea" parameterType="string">
        delete from ax_beidou_warning_model_area  where model_id = #{modelId}
    </delete>

    <update id="delRule" parameterType="string">
        update ax_beidou_warning_rule set del_flag = '1' where id = #{id}
    </update>

    <update id="delModel" parameterType="string">
        update ax_beidou_warning_model set del_flag = '1' where id = #{id}
    </update>

    <select id="selectRuleModelByModelIdCount" parameterType="string" resultType="integer">
        select count(*) from ax_beidou_warning_rule_model where  model_id = #{modelId}
    </select>

    <select id="isEnableByModel" parameterType="string" resultType="integer">
        select count(*) from ax_beidou_warning_rule_model m,ax_beidou_warning_rule r
        where m.rule_id = r.id and r.enabled = '1' and del_flag='0' and m.model_id = #{modelId}
    </select>
    
    <select id="getParamDataByModelIdAndDictId" parameterType="string" resultType="warningParamData">

    </select>

    <select id="getRuleAndAreaByMId" parameterType="string" resultType="ruleModelAreaData">
        select id,rule_id as ruleId,model_id as modelId,area_id as areaId
        from ax_beidou_warning_rule_model_area where model_id = #{modelId}
    </select>

    <select id="getRuleAndParamByMId" parameterType="string" resultType="ruleModelParamData">
        select rule_id as ruleId,model_id as modelId
        from ax_beidou_warning_rule_model_param where model_id = #{modelId} group by ruleId,modelId
    </select>

    <insert id="insertRuleModelParam" parameterType="ruleModelParamData">
        insert into ax_beidou_warning_rule_model_param(
            id,
            rule_id,
            model_id,
            param_id,
            <if test="paramValue != null and paramValue != ''">param_value,</if>
            create_by,
            create_time
        )values (
            #{id},
            #{ruleId},
            #{modelId},
            #{paramId},
            <if test="paramValue != null and paramValue != ''">#{paramValue},</if>
            #{createBy},
            NOW()
        )
    </insert>

    <insert id="insertRuleModelArea" parameterType="ruleModelAreaData">
        insert into ax_beidou_warning_rule_model_area(
        id,
        rule_id,
        model_id,
        area_id,
        create_by,
        create_time
        )values (
        #{id},
        #{ruleId},
        #{modelId},
        #{areaId},
        #{createBy},
        NOW()
        )
    </insert>

    <delete id="delRuleModelParamByMId" parameterType="string">
        delete from ax_beidou_warning_rule_model_param where model_id = #{modelId}
    </delete>

    <delete id="delRuleModelAreaByRId" parameterType="string">
        delete from ax_beidou_warning_rule_model_area where rule_id = #{ruleId}
    </delete>

    <delete id="delRuleModelParamByRId" parameterType="string">
        delete from ax_beidou_warning_rule_model_param where rule_id = #{ruleId}
    </delete>

    <select id="getRuleModelParamByRMP" parameterType="string" resultType="ruleModelParamData">
        select id,rule_id as ruleId,model_id as modelId,param_id as paramId,create_by as createBy from ax_beidou_warning_rule_model_param
        where rule_id = #{ruleId} and model_id = #{modelId} and param_id = #{paramId}
    </select>


    <select id="selectWarningModelByName" parameterType="string" resultType="integer">
        select count(*) from ax_beidou_warning_model where del_flag = '0' and `name` = #{modelName}
        <if test="id != null and id != ''">
            and id != #{id}
        </if>
    </select>

    <select id="selectWarningRuleByName" parameterType="string" resultType="integer">
        select count(*) from ax_beidou_warning_rule where del_flag = '0' and `name` = #{ruleName}
        <if test="id != null and id != ''">
            and id != #{id}
        </if>
    </select>

    <!--按勾选区域查询预警区域-->
    <select id="findInId" parameterType="string" resultType="warningAreaVo">
        select id,`name`,location from ax_beidou_warning_area
        where del_flag = '0'
        and id in
        <foreach collection="array" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

</mapper>