<?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.modules.baseinfo.project.mapper.BaseProjectInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="projectInfoResultMap" type="org.springblade.modules.baseinfo.project.entity.ProjectInfoEntity">
        <result column="tenant_id" property="tenantId"/>
        <result column="project_name" property="projectName"/>
        <result column="org_id" property="orgId"/>
        <result column="adcd" property="adcd"/>
        <result column="project_name" property="projectManager"/>
        <result column="contact_phone" property="contactPhone"/>
        <result column="agreement_id" property="agreementId"/>
        <result column="power_type_id" property="powerTypeId"/>
        <result column="sim_payer" property="simPayer"/>
        <result column="sim_pay_end_date" property="simPayEndDate"/>
        <result column="remark" property="remark"/>
    </resultMap>

    <!-- 通用查询映射结果 -->
    <resultMap id="projectInfoVOResultMap" type="org.springblade.modules.baseinfo.project.vo.ProjectInfoVO">
        <result column="tenant_id" property="tenantId"/>
        <result column="project_name" property="projectName"/>
        <result column="org_id" property="orgId"/>
        <result column="adcd" property="adcd"/>
        <result column="project_name" property="projectManager"/>
        <result column="contact_phone" property="contactPhone"/>
        <result column="agreement_id" property="agreementId"/>
        <result column="power_type_id" property="powerTypeId"/>
        <result column="sim_payer" property="simPayer"/>
        <result column="sim_pay_end_date" property="simPayEndDate"/>
        <result column="remark" property="remark"/>

        <result column="dept_name" property="orgName"/>
        <result column="agreementName" property="agreementName"/>
        <result column="powerTypeName" property="powerTypeName"/>
    </resultMap>

    <select id="selectList" resultMap="projectInfoVOResultMap">
        SELECT
        n.*,d.dept_name,ab.dict_value agreementName,pb.dict_value powerTypeName
        FROM
        base_project_info n
        LEFT JOIN blade_dept d ON d.id = n.org_id
        LEFT JOIN blade_dict_biz ab ON ab.id = n.agreement_id
        LEFT JOIN blade_dict_biz pb ON pb.id = n.power_type_id
        WHERE
        n.is_deleted = 0 and n.tenant_id = #{dto.tenantId}
        <if test="dto.projectName!=null">
            and n.project_name like concat(concat('%', #{dto.projectName}), '%')
        </if>
        <if test="dto.orgId!=null">
            and (d.id = #{dto.orgId} or FIND_IN_SET(#{dto.orgId},d.ancestors) > 0)
        </if>
        <if test="dto.adcd!=null">
            and FIND_IN_SET(#{dto.adcd},adcd) > 0
        </if>
        order by n.project_name
    </select>

    <select id="selectPage" resultMap="projectInfoVOResultMap">
        SELECT
        n.*,d.dept_name,ab.dict_value agreementName,pb.dict_value powerTypeName
        FROM
        base_project_info n
        LEFT JOIN blade_dept d ON d.id = n.org_id and d.is_deleted = 0
        LEFT JOIN blade_dict_biz ab ON ab.id = n.agreement_id and ab.is_deleted = 0
        LEFT JOIN blade_dict_biz pb ON pb.id = n.power_type_id and pb.is_deleted = 0
        WHERE
        n.is_deleted = 0 and n.tenant_id = #{dto.tenantId}
        <if test="dto.projectName!=null">
            and n.project_name like concat(concat('%', #{dto.projectName}), '%')
        </if>
        <if test="dto.orgId!=null">
            and (d.id = #{dto.orgId} or FIND_IN_SET(#{dto.orgId},d.ancestors) > 0)
        </if>
        <if test="dto.adcd!=null">
            and FIND_IN_SET(#{dto.adcd},adcd) > 0
        </if>
        order by n.project_name
    </select>

    <select id="getByOrgId" resultMap="projectInfoResultMap">
        SELECT
        n.*
        FROM
        base_project_info n
        WHERE
        n.is_deleted = 0
        <if test="orgId!=null">
            and n.org_id = #{orgId}
        </if>
        limit 1
    </select>

    <select id="getProjectInfoById" resultMap="projectInfoResultMap">
        SELECT
        n.*
        FROM
        base_project_info n
        WHERE
        n.is_deleted = 0
        <if test="projectId!=null">
            and n.id = #{projectId}
        </if>
        limit 1
    </select>
</mapper>
