<?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="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>

    <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,
               count(e.id) 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 p
        ) permission on permission.premession_char = ce.permission_char
        <where>
            <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="TjCivilEngineeringResult">
        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
        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 e.quotation_id 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>
         </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>
         </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>
</mapper>