<?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="org.springblade.manager.mapper.ContractInfoMapper">


    <!-- 通用查询映射结果 -->
    <resultMap id="contractInfoResultMap" type="org.springblade.manager.vo.ContractInfoVO">
        <result column="id" property="id"/>
        <result column="create_time" property="createTime"/>
        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="contract_name" property="contractName"/>
        <result column="contract_type" property="contractType"/>
        <result column="p_id" property="pId"/>
        <result column="project_name" property="projectName"/>
        <result column="contract_number" property="contractNumber"/>
        <result column="contractor_unit_name" property="contractorUnitName"/>
        <result column="construction_unit_name" property="constructionUnitName"/>
        <result column="supervision_unit_name" property="supervisionUnitName"/>
        <result column="laboratory_name" property="laboratoryName"/>
        <result column="contractor_join_time" property="contractorJoinTime"/>
        <result column="plan_start_time" property="planStartTime"/>
        <result column="plan_end_time" property="planEndTime"/>
        <result column="actual_start_time" property="actualStartTime"/>
        <result column="actual_end_time" property="actualEndTime"/>
        <result column="start_file_url" property="startFileUrl"/>
        <result column="contract_amount" property="contractAmount"/>
        <result column="contract_amount" property="contractAmount"/>
        <result column="project_place" property="projectPlace"/>
        <result column="start_station" property="startStation"/>
        <result column="end_station" property="endStation"/>
        <result column="filer" property="filer"/>
        <result column="reviewer" property="reviewer"/>
        <result column="prefix" property="prefix"/>
        <result column="security_level" property="securityLevel"/>
        <result column="storage_period" property="storagePeriod"/>

    </resultMap>
    <!-- 通用查询映射结果 -->
    <resultMap id="contractInfoResultMap1" type="org.springblade.manager.vo.ContractlnfoCountVO">
        <result column="contract_name" property="contractName"/>
        <result column="contract_counts" property="contractCounts"/>
    </resultMap>

    <resultMap id="treeNodeResultMap" type="org.springblade.manager.vo.TreeNodeVO">
        <id column="id" property="id"/>
        <result column="parent_id" property="parentId"/>
        <result column="title" property="title"/>
        <result column="value" property="value"/>
        <result column="key" property="key"/>
        <result column="has_children" property="hasChildren"/>
        <result column="type" property="type"/>
        <result column="primaryKeyId" property="primaryKeyId"/>
        <result column="wbsType" property="wbsType"/>
        <result column="partition_code" property="partitionCode"/>
    </resultMap>

    <resultMap id="resultMap2" type="org.springblade.manager.vo.ContractInfoVO">
        <id column="id" property="id"/>
        <collection property="idList" ofType="org.springblade.manager.vo.WbsTreeContractVO3">
            <id column="id" property="id"/>
            <result property="pkeyId" column="pkeyId"/>
            <result property="contractId" column="contractId"/>
            <result property="contractIdRelation" column="contractIdRelation"/>
        </collection>
    </resultMap>


    <select id="selectContractByProjectIds" resultMap="contractInfoResultMap">
        select id, p_id, contract_name, contract_number from m_contract_info where is_deleted = 0 and p_id in
        <foreach collection="ids" item="pId" open="(" separator="," close=")">
            #{pId}
        </foreach>
    </select>

    <update id="updatePasswordByUserId">
        update blade_user set password = #{password} where id = #{userId}
    </update>
    <update id="deleteFile">
        update blade_attach set is_deleted = 1 where link = #{url} and `status`= 1
    </update>

    <select id="selectContractInfoPage" resultMap="contractInfoResultMap">
        select c.* ,p.project_name as project_name from m_contract_info c INNER JOIN m_project_info p on c.p_id=p.id where c.is_deleted = 0
    </select>

    <select id="selectContractInfoCount" resultMap="contractInfoResultMap1">
        select c.contract_name,count(*) as contract_counts from m_contract_info c where c.is_deleted = 0  GROUP BY c.p_id
    </select>

    <select id="selectContractInfoPageByPid" resultMap="contractInfoResultMap">
        select c.*,p.project_name as project_name  from m_contract_info c
        INNER JOIN m_project_info p on c.p_id=p.id where p.id = #{pid} and c.is_deleted = 0
    </select>

    <select id="findJobByRoleId" resultType="org.springblade.manager.vo.CRolePostVO">
        select p.id,p.post_name from blade_post p INNER JOIN m_role_post m ON p.id = m.p_id
        INNER JOIN blade_role r ON r.id = m.r_id WHERE p.is_deleted = 0 and p.`status`= 1
        and r.id = #{id}
    </select>

    <select id="findAllUserByCondition" resultType="org.springblade.system.user.vo.UserVO2">
        SELECT
        u.id as uId,mpau.id,pi.project_name,ci.contract_name,u.id as uid,u.`name`,u.account,u.plaintext_password as
        "password",u.`phone`
        FROM
        blade_user u
        INNER JOIN m_project_assignment_user mpau ON u.id = mpau.user_id
        INNER JOIN m_contract_info ci ON ci.id = mpau.contract_id
        INNER JOIN m_project_info pi ON pi.id = mpau.project_id
        INNER JOIN blade_role r ON r.id = mpau.role_id
        WHERE mpau.is_deleted = 0 AND mpau.`status` = 1
        AND pi.id = #{Values.pId}
        <if test="Values.cId != null and Values.cId != '' ">
            AND ci.id = #{Values.cId}
        </if>
        <if test="Values.rId !=null and Values.rId !='' ">
            AND r.id = #{Values.rId}
        </if>
    </select>

    <select id="findUserListByCondition" resultType="org.springblade.system.user.vo.UserVO3">
        SELECT
        mpau.id,
        u.`name`,
        u.account,
        (select dict_value from blade_dict_biz where id = mpau.role_id) as postName,
        (select contract_name from m_contract_info where id = mpau.contract_id) as contractName,
        u.`password`,
        u.`phone`
        FROM
        blade_user u
        INNER JOIN m_project_assignment_user mpau ON u.id = mpau.user_id
        INNER JOIN m_project_info pi ON pi.id = mpau.project_id
        WHERE
        mpau.is_deleted = 0
        AND mpau.`status` = 1
        AND mpau.project_id = #{Values.pId}
        AND mpau.role_id = #{Values.rId}
        <if test="Values.cId != null and Values.cId != '' ">
            AND mpau.contract_id = #{Values.cId}
        </if>
    </select>


    <select id="findUserInfoByCondition" resultType="org.springblade.system.user.entity.User">
        SELECT `name`,`account`,`password` FROM blade_user
        WHERE is_deleted = 0 AND `status` = 1
        AND role_id = #{rId}
        AND post_id = #{pId}
        <if test=" userName != null and userName != '' ">
            AND `name` = #{userName}
        </if>
    </select>

    <select id="findUserList" resultType="org.springblade.system.user.entity.User">
        select `id`,`name`,account,`password` from blade_user WHERE is_deleted = 0 and `status` = 1 order by create_time
    </select>

    <select id="tree" resultMap="treeNodeResultMap">
        SELECT
        p_key_id AS primaryKeyId,
        wbs_type AS wbsType,
        id,
        parent_id,
        IFNULL(if(length(trim(full_name)) > 0, full_name, dept_name),dept_name) AS title,
        type AS "type",
        dept_category AS deptCategory,
        id AS "value",
        id AS "key",
        partition_code
        FROM
        m_wbs_tree_contract
        WHERE
        is_deleted = 0
        AND STATUS = 1
        /* major_data_type = 5是日志类型，日志类型不需要直接在划分树上体现 */
        AND (major_data_type != 5 or major_data_type is null)
        <if test="wbsId!=null and wbsId!=''">
            and wbs_id = #{wbsId}
        </if>
        <if test="projectId !=null and projectId!=''">
            and project_id = #{projectId}
        </if>
        <if test="contractId!=null and contractId!=''">
            and contract_id = #{contractId}
        </if>
        <if test="wbsType != null and wbsType != ''">
            and wbs_type = #{wbsType}
        </if>
        <if test="parentId != null and parentId != ''">
            and parent_id = #{parentId}
        </if>
        ORDER BY sort
    </select>

    <select id="selectByCondition" resultMap="resultMap2">
        SELECT
        t.p_key_id AS "pKeyId",t.contract_id AS "contractId",t.contract_id_relation AS "contractIdRelation",
        c.*
        FROM
        m_contract_info c
        INNER JOIN m_wbs_tree_contract t ON c.id = t.contract_id
        WHERE c.id = #{contractInfo.id}
        AND t.parent_id = 0
    </select>

    <select id="selectLists" resultType="org.springblade.manager.vo.WbsTreeContractVO3">
        SELECT
        full_name AS "fullName",
        id AS "id",
        p_key_id AS "pKeyId",
        contract_id AS "contractId",
        contract_id_relation  AS "contractIdRelation"
        FROM m_wbs_tree_contract
        WHERE
        contract_id = #{contractInfo.id}
        AND contract_type = #{contractType}
        AND parent_id = 0
        AND status = 1
        AND is_deleted = 0
    </select>


</mapper>
