<?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.yanzhu.manage.mapper.ProProjectInfoMapper">
    
    <resultMap type="ProProjectInfo" id="ProProjectInfoResult">
        <result property="id"    column="id"    />
        <result property="comId"    column="com_id"    />
        <result property="comName"    column="com_name"    />
        <result property="disDeptId"    column="dis_dept_id"    />
        <result property="projectName"    column="project_name"    />
        <result property="projectCode"    column="project_code"    />
        <result property="simpleName"    column="simple_name"    />
        <result property="projectType"    column="project_type"    />
        <result property="projectLevel"    column="project_level"    />
        <result property="projectRegional"    column="project_regional"    />
        <result property="projectPackage"    column="project_package"    />
        <result property="projectAddress"    column="project_address"    />
        <result property="projectNature"    column="project_nature"    />
        <result property="licenceNumber"    column="licence_number"    />
        <result property="projectPerson"    column="project_person"    />
        <result property="projectPersonPhone"    column="project_person_phone"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />

        <result property="techLeader"    column="tech_leader"    />
        <result property="techLeaderPhone"    column="tech_leader_phone"    />
        <result property="province"    column="province"    />
        <result property="city"    column="city"    />
        <result property="district"    column="district"    />

        <result property="projectTimeLimit"    column="project_time_limit"    />
        <result property="totalInvestment"    column="total_investment"    />
        <result property="floorArea"    column="floor_area"    />
        <result property="totalOutputValue"    column="total_output_value"    />
        <result property="plannedCompletionTime"    column="planned_completion_time"    />
        <result property="scheduledStartTime"    column="scheduled_start_time"    />
        <result property="actualOperatingTime"    column="actual_operating_time"    />
        <result property="actualCompletionTime"    column="actual_completion_time"    />
        <result property="contractAmount"    column="contract_amount"    />
        <result property="paidAmount"    column="paid_amount"    />
        <result property="onAccountAmount"    column="on_account_amount"    />
        <result property="projectSchedule"    column="project_schedule"    />
        <result property="projectSummarize"    column="project_summarize"    />
        <result property="projectQrCode"    column="project_qr_code"    />
        <result property="projectStatus"    column="project_status"    />
        <result property="projectSort"    column="project_sort"    />
        <result property="isDel"    column="is_del"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <association property="setting"    column="ps_id" javaType="ProProjectInfoSetting" resultMap="ProProjectInfoSettingResult" />
        <collection  property="projectDeptsList"   javaType="java.util.List"           resultMap="ProProjectInfoDeptsResult" />
    </resultMap>

    <resultMap id="ProProjectInfoDeptsResult" type="ProProjectInfoDepts">
        <result property="id"    column="pd_id"    />
        <result property="projectId"    column="id"    />
        <result property="projectName"    column="project_name"    />
        <result property="deptType"    column="pd_type"    />
        <result property="deptName"    column="pd_name"    />
        <result property="deptCode"    column="pd_code"    />
        <result property="leader"    column="pd_leader"    />
        <result property="phone"    column="pd_phone"    />
    </resultMap>

    <resultMap id="ProProjectInfoSettingResult" type="ProProjectInfoSetting">
        <result property="id"    column="ps_id"    />
        <result property="projectId"    column="id"    />
        <result property="projectName"    column="project_name"    />
        <result property="orgName"    column="ps_org_name"    />
        <result property="orgLogo"    column="ps_org_logo"    />
        <result property="orgImage"    column="ps_org_image"    />
        <result property="orgVideo"    column="ps_org_video"    />
        <result property="orgPlane"    column="ps_org_plane"    />
    </resultMap>

    <sql id="selectProProjectInfoVo">
        select pi.id, pi.com_id, sd.dept_name as com_name, pi.dis_dept_id, pi.project_name, pi.project_code,
               pi.tech_leader,pi.tech_leader_phone,pi.province,pi.city,pi.district,
               pi.simple_name, pi.project_type, pi.project_level, pi.project_regional, pi.project_package, pi.project_address, pi.project_nature, pi.licence_number, pi.project_person, pi.project_person_phone, pi.longitude, pi.latitude, pi.project_time_limit, pi.total_investment, pi.floor_area, pi.total_output_value, pi.planned_completion_time, pi.scheduled_start_time, pi.actual_operating_time, pi.actual_completion_time, pi.contract_amount, pi.paid_amount, pi.on_account_amount, pi.project_schedule, pi.project_summarize, pi.project_qr_code, pi.project_status, pi.project_sort, pi.is_del, pi.create_by, pi.create_time, pi.update_by, pi.update_time, pi.remark
        from pro_project_info pi
        left join sys_dept sd on sd.dept_id = pi.com_id
    </sql>

    <select id="selectProProjectInfoList" parameterType="ProProjectInfo" resultMap="ProProjectInfoResult">
        <include refid="selectProProjectInfoVo"/>
        <if test="currentUserId != null "> left join pro_project_info_users pu on pu.project_id = pi.id</if>
        <where>
            <if test="comId != null "> and pi.com_id = #{comId}</if>
            <if test="disDeptId != null "> and pi.dis_dept_id = #{disDeptId}</if>
            <if test="activeComId != null "> and pi.com_id = #{activeComId}</if>
            <if test="currentUserId != null"> and pu.user_id = #{currentUserId} and pu.use_status='1' and pu.is_del=0</if>
            <if test="projectName != null and projectName != ''"> and pi.project_name like concat('%', #{projectName}, '%')</if>
            <if test="projectCode != null and projectCode != ''"> and pi.project_code = #{projectCode}</if>
            <if test="simpleName != null and simpleName != ''"> and pi.simple_name like concat('%', #{simpleName}, '%')</if>

            <if test="techLeader != null and techLeader != ''"> and pi.tech_leader like concat('%', #{techLeader}, '%')</if>
            <if test="techLeaderPhone != null and techLeaderPhone != ''"> and pi.tech_leader_phone like concat('%', #{techLeaderPhone}, '%')</if>
            <if test="province != null and province != ''"> and pi.province like concat('%', #{province}, '%')</if>
            <if test="city != null and city != ''"> and pi.city like concat('%', #{city}, '%')</if>
            <if test="district != null and district != ''"> and pi.district like concat('%', #{district}, '%')</if>

            <if test="projectType != null and projectType != ''"> and pi.project_type = #{projectType}</if>
            <if test="projectLevel != null and projectLevel != ''"> and pi.project_level = #{projectLevel}</if>
            <if test="projectPerson != null and projectPerson != ''"> and pi.project_person = #{projectPerson}</if>
            <if test="projectPersonPhone != null and projectPersonPhone != ''"> and pi.project_person_phone = #{projectPersonPhone}</if>
            <if test="projectStatus != null and projectStatus != ''"> and pi.project_status = #{projectStatus}</if>
            <if test="isDel != null "> and pi.is_del = #{isDel}</if>
        </where>
        <if test="currentUserId == null "> order by pi.project_sort, pi.id desc</if>
        <if test="currentUserId != null "> order by pu.sort_by, pi.id desc</if>
    </select>

    <select id="selectProProjectInfoById" parameterType="Long" resultMap="ProProjectInfoResult">
        select pi.id, pi.com_id, sd.dept_name as com_name, pi.dis_dept_id, pi.project_name,
               pi.project_code, pi.simple_name, pi.project_type, pi.project_level, pi.project_regional, pi.project_package, pi.project_address, pi.project_nature,
               pi.tech_leader,pi.tech_leader_phone,pi.province,pi.city,pi.district,
               pi.licence_number, pi.project_person, pi.project_person_phone, pi.longitude, pi.latitude, pi.project_time_limit, pi.total_investment,
               pi.floor_area, pi.total_output_value, pi.planned_completion_time, pi.scheduled_start_time, pi.actual_operating_time, pi.actual_completion_time,
               pi.contract_amount, pi.paid_amount, pi.on_account_amount, pi.project_schedule, pi.project_summarize, pi.project_qr_code, pi.project_status,
               pi.project_sort, pi.is_del, pi.create_by, pi.create_time, pi.update_by, pi.update_time, pi.remark,
               pd.id pd_id,pd.dept_type pd_type,pd.dept_name pd_name,pd.dept_code pd_code,pd.leader pd_leader,pd.phone pd_phone,
               ps.id ps_id,ps.org_name ps_org_name,ps.org_logo ps_org_logo,ps.org_image ps_org_image,ps.org_video ps_org_video,ps.org_plane ps_org_plane
        from pro_project_info pi
                 left join sys_dept sd on sd.dept_id = pi.com_id
            left join pro_project_info_depts pd on pi.id=pd.project_id
            left join pro_project_info_setting ps on pi.id=ps.project_id
        where pi.id = #{id}
    </select>
        
    <insert id="insertProProjectInfo" parameterType="ProProjectInfo">
        insert into pro_project_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="comId != null">com_id,</if>
            <if test="disDeptId != null">dis_dept_id,</if>
            <if test="projectName != null">project_name,</if>
            <if test="projectCode != null">project_code,</if>
            <if test="simpleName != null">simple_name,</if>
            <if test="projectType != null">project_type,</if>
            <if test="projectLevel != null">project_level,</if>
            <if test="projectRegional != null">project_regional,</if>
            <if test="projectPackage != null">project_package,</if>
            <if test="projectAddress != null">project_address,</if>
            <if test="projectNature != null">project_nature,</if>
            <if test="licenceNumber != null">licence_number,</if>
            <if test="projectPerson != null">project_person,</if>
            <if test="projectPersonPhone != null">project_person_phone,</if>
            <if test="longitude != null">longitude,</if>
            <if test="latitude != null">latitude,</if>

            <if test="techLeader != null">tech_leader,</if>
            <if test="techLeaderPhone != null">tech_leader_phone,</if>
            <if test="province != null">province,</if>
            <if test="city != null">city,</if>
            <if test="district != null">district,</if>

            <if test="projectTimeLimit != null">project_time_limit,</if>
            <if test="totalInvestment != null">total_investment,</if>
            <if test="floorArea != null">floor_area,</if>
            <if test="totalOutputValue != null">total_output_value,</if>
            <if test="plannedCompletionTime != null">planned_completion_time,</if>
            <if test="scheduledStartTime != null">scheduled_start_time,</if>
            <if test="actualOperatingTime != null">actual_operating_time,</if>
            <if test="actualCompletionTime != null">actual_completion_time,</if>
            <if test="contractAmount != null">contract_amount,</if>
            <if test="paidAmount != null">paid_amount,</if>
            <if test="onAccountAmount != null">on_account_amount,</if>
            <if test="projectSchedule != null">project_schedule,</if>
            <if test="projectSummarize != null">project_summarize,</if>
            <if test="projectQrCode != null">project_qr_code,</if>
            <if test="projectStatus != null">project_status,</if>
            <if test="projectSort != null">project_sort,</if>
            <if test="isDel != null">is_del,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="comId != null">#{comId},</if>
            <if test="disDeptId != null">#{disDeptId},</if>
            <if test="projectName != null">#{projectName},</if>
            <if test="projectCode != null">#{projectCode},</if>
            <if test="simpleName != null">#{simpleName},</if>
            <if test="projectType != null">#{projectType},</if>
            <if test="projectLevel != null">#{projectLevel},</if>
            <if test="projectRegional != null">#{projectRegional},</if>
            <if test="projectPackage != null">#{projectPackage},</if>
            <if test="projectAddress != null">#{projectAddress},</if>
            <if test="projectNature != null">#{projectNature},</if>
            <if test="licenceNumber != null">#{licenceNumber},</if>
            <if test="projectPerson != null">#{projectPerson},</if>
            <if test="projectPersonPhone != null">#{projectPersonPhone},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>

            <if test="techLeader != null">#{techLeader},</if>
            <if test="techLeaderPhone != null">#{techLeaderPhone},</if>
            <if test="province != null">#{province},</if>
            <if test="city != null">#{city},</if>
            <if test="district != null">#{district},</if>

            <if test="projectTimeLimit != null">#{projectTimeLimit},</if>
            <if test="totalInvestment != null">#{totalInvestment},</if>
            <if test="floorArea != null">#{floorArea},</if>
            <if test="totalOutputValue != null">#{totalOutputValue},</if>
            <if test="plannedCompletionTime != null">#{plannedCompletionTime},</if>
            <if test="scheduledStartTime != null">#{scheduledStartTime},</if>
            <if test="actualOperatingTime != null">#{actualOperatingTime},</if>
            <if test="actualCompletionTime != null">#{actualCompletionTime},</if>
            <if test="contractAmount != null">#{contractAmount},</if>
            <if test="paidAmount != null">#{paidAmount},</if>
            <if test="onAccountAmount != null">#{onAccountAmount},</if>
            <if test="projectSchedule != null">#{projectSchedule},</if>
            <if test="projectSummarize != null">#{projectSummarize},</if>
            <if test="projectQrCode != null">#{projectQrCode},</if>
            <if test="projectStatus != null">#{projectStatus},</if>
            <if test="projectSort != null">#{projectSort},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateProProjectInfo" parameterType="ProProjectInfo">
        update pro_project_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="comId != null">com_id = #{comId},</if>
            <if test="disDeptId != null">dis_dept_id = #{disDeptId},</if>
            <if test="projectName != null">project_name = #{projectName},</if>
            <if test="projectCode != null">project_code = #{projectCode},</if>
            <if test="simpleName != null">simple_name = #{simpleName},</if>
            <if test="projectType != null">project_type = #{projectType},</if>
            <if test="projectLevel != null">project_level = #{projectLevel},</if>
            <if test="projectRegional != null">project_regional = #{projectRegional},</if>
            <if test="projectPackage != null">project_package = #{projectPackage},</if>
            <if test="projectAddress != null">project_address = #{projectAddress},</if>
            <if test="projectNature != null">project_nature = #{projectNature},</if>
            <if test="licenceNumber != null">licence_number = #{licenceNumber},</if>
            <if test="projectPerson != null">project_person = #{projectPerson},</if>
            <if test="projectPersonPhone != null">project_person_phone = #{projectPersonPhone},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="latitude != null">latitude = #{latitude},</if>

            <if test="techLeader != null">tech_leader = #{techLeader},</if>
            <if test="techLeaderPhone != null">tech_leader_phone = #{techLeaderPhone},</if>
            <if test="province != null">province = #{province},</if>
            <if test="city != null">city = #{city},</if>
            <if test="district != null">district = #{district},</if>


            <if test="projectTimeLimit != null">project_time_limit = #{projectTimeLimit},</if>
            <if test="totalInvestment != null">total_investment = #{totalInvestment},</if>
            <if test="floorArea != null">floor_area = #{floorArea},</if>
            <if test="totalOutputValue != null">total_output_value = #{totalOutputValue},</if>
            <if test="plannedCompletionTime != null">planned_completion_time = #{plannedCompletionTime},</if>
            <if test="scheduledStartTime != null">scheduled_start_time = #{scheduledStartTime},</if>
            <if test="actualOperatingTime != null">actual_operating_time = #{actualOperatingTime},</if>
            <if test="actualCompletionTime != null">actual_completion_time = #{actualCompletionTime},</if>
            <if test="contractAmount != null">contract_amount = #{contractAmount},</if>
            <if test="paidAmount != null">paid_amount = #{paidAmount},</if>
            <if test="onAccountAmount != null">on_account_amount = #{onAccountAmount},</if>
            <if test="projectSchedule != null">project_schedule = #{projectSchedule},</if>
            <if test="projectSummarize != null">project_summarize = #{projectSummarize},</if>
            <if test="projectQrCode != null">project_qr_code = #{projectQrCode},</if>
            <if test="projectStatus != null">project_status = #{projectStatus},</if>
            <if test="projectSort != null">project_sort = #{projectSort},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteProProjectInfoById" parameterType="Long">
        update pro_project_info set is_del=1 where id = #{id}
    </update>

    <update id="deleteProProjectInfoByIds" parameterType="String">
        update pro_project_info set is_del=1 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <insert id="insertDept" parameterType="ProDept" useGeneratedKeys="true" keyProperty="deptId">
        insert into sys_dept(
        <if test="parentId != null and parentId != 0">parent_id,</if>
        <if test="ancestors != null and ancestors != ''">ancestors,</if>
        <if test="deptName != null and deptName != ''">dept_name,</if>
        <if test="deptShortName != null and deptShortName != ''">dept_short_name,</if>
        <if test="deptCode != null and deptName != ''">dept_code,</if>
        <if test="deptType != null and deptName != ''">dept_type,</if>
        <if test="deptInfos != null and deptName != ''">dept_infos,</if>
        <if test="orderNum != null">order_num,</if>
        <if test="leader != null and leader != ''">leader,</if>
        <if test="phone != null and phone != ''">phone,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        create_time
        )values(
        <if test="parentId != null and parentId != 0">#{parentId},</if>
        <if test="ancestors != null and ancestors != ''">#{ancestors},</if>
        <if test="deptName != null and deptName != ''">#{deptName},</if>
        <if test="deptShortName != null and deptShortName != ''">#{deptShortName},</if>
        <if test="deptCode != null and deptCode != ''">#{deptCode},</if>
        <if test="deptType != null and deptType != ''">#{deptType},</if>
        <if test="deptInfos != null and deptInfos != ''">#{deptInfos},</if>
        <if test="orderNum != null">#{orderNum},</if>
        <if test="leader != null and leader != ''">#{leader},</if>
        <if test="phone != null and phone != ''">#{phone},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        sysdate()
        )
    </insert>

    <update id="updateDept" parameterType="ProDept">
        update sys_dept
        <set>
            <if test="parentId != null and parentId != 0">parent_id = #{parentId},</if>
            <if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},</if>
            <if test="deptName != null and deptName != ''">dept_name = #{deptName},</if>
            <if test="deptShortName != null and deptShortName != ''">dept_short_name = #{deptShortName},</if>
            <if test="deptCode != null and deptCode != ''">dept_code = #{deptCode},</if>
            <if test="deptType != null and deptType != ''">dept_type = #{deptType},</if>
            <if test="deptInfos != null and deptInfos != ''">dept_infos = #{deptInfos},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="leader != null">leader = #{leader},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate()
        </set>
        where dept_id = #{deptId}
    </update>

</mapper>