<?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.ruoyi.project.business.mapper.TjCivilEngineeringMapper">

    <resultMap type="TjCivilEngineering" id="TjCivilEngineeringResult">
        <result property="id" column="id"/>
        <result property="civilEngineeringNo" column="civil_engineering_no"/>
        <result property="projectId" column="project_id"/>
        <result property="templateId" column="template_id"/>
        <result property="commonShaftMaterial" column="common_shaft_material"/>
        <result property="status" column="status"/>
        <result property="permissionChar" column="permission_char"/>
        <result property="language" column="language"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="remark" column="remark"/>
        <result property="projectNo" column="project_no"/>
        <result property="projectName" column="project_name"/>
        <result property="quotationId" column="quotation_id"/>
        <result property="quotationNo" column="quotation_no"/>
        <result property="version" column="version"/>
        <result property="elevatorModel" column="elevator_model"/>
        <result property="elevatorNumber" column="elevator_number"/>
        <result property="specification" column="specification"/>
        <result property="submitter" column="submitter"/>
        <result property="countryName" column="country_name"/>
        <result property="countryNameUS" column="country_nameUS"/>
        <result property="countryNameRU" column="country_nameRU"/>
        <result property="countryNameES" column="country_nameES"/>
        <result property="city" column="city"/>
        <result property="developer" column="developer"/>
        <result property="elevatorPosition" column="elevator_position"/>
    </resultMap>

    <resultMap type="com.ruoyi.project.business.domain.vo.Civil.EngineeringInfoVO" id="EngineeringInfoVOResult"
        extends="TjCivilEngineeringResult">
        <result property="areaId" column="area_id"/>
        <result property="countryId" column="country_id"/>
    </resultMap>

    <resultMap type="com.ruoyi.project.business.domain.vo.Civil.TjCivilEngineeringVo" id="collectionResult">
        <result property="id" column="id"/>
        <result property="civilEngineeringNo" column="civil_engineering_no"/>
        <result property="projectId" column="project_id"/>
        <result property="templateId" column="template_id"/>
        <result property="commonShaftMaterial" column="common_shaft_material"/>
        <result property="status" column="status"/>
        <result property="language" column="language"/>
        <result property="permissionChar" column="permission_char"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="remark" column="remark"/>
        <result property="projectNo" column="project_no"/>
        <result property="projectName" column="project_name"/>
        <result property="quotationId" column="quotation_id"/>
        <result property="quotationNo" column="quotation_no"/>
        <result property="version" column="version"/>
        <result property="elevatorModel" column="elevator_model"/>
        <result property="elevatorNumber" column="elevator_number"/>
        <result property="specification" column="specification"/>
        <result property="submitter" column="submitter"/>
        <result property="countryName" column="country_name"/>
        <result property="countryNameUS" column="country_nameUS"/>
        <result property="countryNameRU" column="country_nameRU"/>
        <result property="countryNameES" column="country_nameES"/>
        <result property="city" column="city"/>
        <result property="developer" column="developer"/>
        <result property="elevatorPosition" column="elevator_position"/>
        <result property="templateName" column="template_name"/>
        <result property="elevatorQuantity" column="elevator_quantity"/>
        <collection property="elevators" column="id" javaType="ArrayList"
            ofType="com.ruoyi.project.business.domain.vo.Civil.TjElevatorVo" select="findElevatorSelect"/>
    </resultMap>

    <resultMap id="tjElevatorResultMap" type="com.ruoyi.project.business.domain.vo.Civil.TjElevatorVo">
        <!--分机列表-->
        <result property="id" column="id"/>
        <result property="civilEngineeringId" column="civil_engineering_id"/>
        <result property="elevatorModelId" column="elevator_model_id"/>
        <result property="quantity" column="quantity"/>
        <result property="elevatorCode" column="elevator_code"/>
        <result property="addType" column="add_type"/>
        <result property="quotationId" column="quotation_id"/>
        <result property="specification" column="specification"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="remark" column="remark"/>
        <result property="sortNumber" column="sort_number"/>
        <result property="offsetX" column="offset_x"/>
        <result property="offsetY" column="offset_y"/>
        <result property="elevatorModel" column="elevator_model"/>
        <result property="elevatorType" column="elevator_type"/>
        <!--子分机列表-->
        <collection property="paramList" ofType="com.ruoyi.project.business.domain.TjElevatorParaDetail">
            <result property="elevatorId" column="elevator_id"/>
            <result property="paraCode" column="para_code"/>
            <result property="paraValue" column="para_value"/>
            <result property="paraCategory" column="para_category"/>
            <result property="delFlag" column="del_flag"/>
            <result property="createTime" column="create_time"/>
            <result property="createBy" column="create_by"/>
            <result property="updateTime" column="update_time"/>
            <result property="updateBy" column="update_by"/>
            <result property="remark" column="remark"/>
        </collection>
        <collection property="floorParamList" column="id" javaType="ArrayList"
            ofType="com.ruoyi.project.business.domain.TjElevatorFloorPara" select="findFloorParamList"/>
    </resultMap>


    <select id="findFloorParamList" resultType="com.ruoyi.project.business.domain.TjElevatorFloorPara">
        SELECT tefp.floor_display as floorDisplay,
               tefp.floor_height  as floorHeight
        FROM tj_elevator_floor_para tefp
        WHERE tefp.del_flag = 0
          AND tefp.elevator_id = #{id}
    </select>

    <select id="getOutList" resultMap="collectionResult">
        select distinct ce.id,
                        ce.civil_engineering_no,
                        ce.project_id,
                        ce.template_id,
                        ce.common_shaft_material,
                        ce.status,
                        ce.language,
                        p.project_no,
                        p.project_name,
                        t.elevator_position,
                        t.template_name,
                        t.elevator_quantity
        from tj_civil_engineering ce
                 left join tc_project p on ce.project_id = p.project_id
                 left join tj_elevator e on ce.id = e.civil_engineering_id
                 left join ti_templates t on ce.template_id = t.id
        where ce.status = '1'
          and ce.del_flag = '0'
    </select>

    <select id="findElevatorSelect" resultMap="tjElevatorResultMap">
        select e.id,
               e.civil_engineering_id,
               e.elevator_model_id,
               e.quantity,
               e.elevator_code,
               e.add_type,
               e.sort_number,
               e.offset_x,
               e.offset_y,
               e.quotation_id,
               e.specification,
               m.elevator_model,
               m.elevator_type,
               para.para_code,
               para.para_value,
               para.para_category,
               para.elevator_id
        from tj_elevator e
                 left join tj_elevator_para_detail para on e.id = para.elevator_id
                 left join tp_elevator_model m on e.elevator_model_id = m.id
        where e.civil_engineering_id = #{id}
          and e.del_flag = '0'
        order by sort_number asc
    </select>

    <sql id="selectTjCivilEngineeringVo">
        select id,
               civil_engineering_no,
               project_id,
               template_id,
               common_shaft_material,
               status,
               permission_char,
               del_flag,
               create_time,
               create_by,
               update_time,
               update_by,
               remark
        from tj_civil_engineering
    </sql>

    <select id="selectTjCivilEngineeringList"
        parameterType="com.ruoyi.project.business.domain.vo.selectParam.CivilEngineeringParam"
        resultMap="TjCivilEngineeringResult">
        select ce.*, p.project_no, p.project_name, e.quotation_id,
        group_concat(q.quotation_no separator '\n') as quotation_no,
        group_concat(q.version separator '\n') as version,
        group_concat(m.elevator_model separator '\n') as elevator_model,
        sum(e.quantity) as elevator_number,
        group_concat(e.specification separator '\n') as specification,
        u.nick_name as submitter
        from tj_civil_engineering ce
        left join tc_project p on ce.project_id = p.project_id
        inner join tj_elevator e on ce.id = e.civil_engineering_id
        left join tq_quotation q on q.id = e.quotation_id and q.project_id = ce.project_id
        left join tp_elevator_model m on e.elevator_model_id = m.id
        left join sys_user u on ce.create_by = u.user_id
        inner join (SELECT p.premession_char FROM (SELECT @premessionStr:= #{permissionChar} p) param ,
        getpremessionview_chars p
        ) permission on permission.premession_char = ce.permission_char
        <where>
            <if test="id != null">and ce.id = #{id}</if>
            <if test="projectId != null">and ce.project_id = #{projectId}</if>
            <if test="projectNo != null and projectNo != ''">and p.project_no like concat('%',#{projectNo},'%')</if>
            <if test="projectName != null and projectName != ''">and p.project_name like
                concat('%',#{projectName},'%')
            </if>
            <if test="civilEngineeringNo != null and civilEngineeringNo != '' ">and ce.civil_engineering_no like
                concat('%', #{civilEngineeringNo}, '%')
            </if>
            <if test="quotationNo != null and quotationNo != ''">and q.quotation_no like
                concat('%',#{quotationNo},'%')
            </if>
            <if test="elevatorModel != null and elevatorModel != ''">and m.elevator_model like
                concat('%',#{elevatorModel},'%')
            </if>
            <if test="status != null and status.size() > 0">
                and ce.status in
                <foreach collection="status" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            and ce.del_flag = '0'
        </where>
        group by ce.id
        order by ce.id desc
    </select>

    <select id="selectTjCivilEngineeringById" parameterType="Long" resultMap="EngineeringInfoVOResult">
        select ce.*,
               p.project_no,
               p.project_name,
               p.developer,
               p.city,
               c.country_name,
               c.country_nameUS,
               c.country_nameRU,
               c.country_nameES,
               e.quotation_id,
               q.quotation_no,
               q.version,
               t.elevator_position,
               p.area_id,
               p.country_id
        from tj_civil_engineering ce
                 left join tc_project p on ce.project_id = p.project_id
                 left join tb_country c on p.country_id = c.country_id
                 left join tj_elevator e on ce.id = e.civil_engineering_id
                 left join tq_quotation q on q.id = e.quotation_id and q.project_id = ce.project_id
                 left join ti_templates t on ce.template_id = t.id
        where ce.id = #{id}
        order by q.quotation_no desc
        limit 1
    </select>

    <insert id="insertTjCivilEngineering" parameterType="TjCivilEngineering" useGeneratedKeys="true" keyProperty="id">
        insert into tj_civil_engineering
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="civilEngineeringNo != null">civil_engineering_no,</if>
            <if test="projectId != null">project_id,</if>
            <if test="templateId != null">template_id,</if>
            <if test="commonShaftMaterial != null">common_shaft_material,</if>
            <if test="status != null">status,</if>
            <if test="permissionChar != null">permission_char,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="remark != null">remark,</if>
            <if test="language != null">language,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="civilEngineeringNo != null">#{civilEngineeringNo},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="templateId != null">#{templateId},</if>
            <if test="commonShaftMaterial != null">#{commonShaftMaterial},</if>
            <if test="status != null">#{status},</if>
            <if test="permissionChar != null">#{permissionChar},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="remark != null">#{remark},</if>
            <if test="language != null">#{language},</if>
        </trim>
    </insert>

    <update id="updateTjCivilEngineering" parameterType="TjCivilEngineering">
        update tj_civil_engineering
        <trim prefix="SET" suffixOverrides=",">
            <if test="civilEngineeringNo != null">civil_engineering_no = #{civilEngineeringNo},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="templateId != null">template_id = #{templateId},</if>
            <if test="commonShaftMaterial != null">common_shaft_material = #{commonShaftMaterial},</if>
            <if test="status != null">status = #{status},</if>
            <if test="permissionChar != null">permission_char = #{permissionChar},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTjCivilEngineeringById" parameterType="Long">
        delete
        from tj_civil_engineering
        where id = #{id}
    </delete>

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

    <update id="changeStatus" parameterType="com.ruoyi.project.business.domain.vo.Civil.CivilParam">
        update tj_civil_engineering
        set status = #{status}
        where id = #{id}
    </update>
</mapper>