<?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.changhu.module.management.mapper.ServiceProjectMapper">
    <select id="pager" resultType="com.changhu.module.management.pojo.vo.ServiceProjectPagerVo">
        select
        sp.*,
        JSON_ARRAY(eu.province,eu.city,eu.districts,eu.street) as 'enterprisesUnitAdministrativeDivisionCodes',
        eu.name as 'enterprisesUnitName',
        mpu.name as 'projectManagerMiniProgramUserName',
        msuu.name as 'createUserName'
        from service_project sp
        left join enterprises_unit eu on sp.enterprises_unit_id = eu.snow_flake_id
        left join mini_program_user mpu on sp.project_manager_mini_program_user_id = mpu.snow_flake_id
        left join management_security_unit_user msuu on sp.create_by = msuu.snow_flake_id
        where sp.delete_flag = 0
        and sp.security_unit_id = ${@com.changhu.common.utils.UserUtil@getUnitId()}
        <if test="params.name!=null and params.name!=''">
            and sp.name like concat('%',#{params.name},'%')
        </if>
        <if test="params.type!=null">
            and sp.type = #{params.type.value}
        </if>
        <if test="params.remark!=null and params.remark!=''">
            and sp.remark like concat('%',#{params.remark},'%')
        </if>
        <if test="params.projectManagerMiniProgramUserName!=null and params.projectManagerMiniProgramUserName!=''">
            and mpu.name like concat('%',#{params.projectManagerMiniProgramUserName},'%')
        </if>
        order by sp.create_time desc
    </select>
    <resultMap id="ProjectManageServiceProjectListVoResultMap"
               type="com.changhu.module.miniProgram.pojo.vo.IndexServiceProjectListVo">
        <result
                column="contact_person_info"
                typeHandler="com.baomidou.mybatisplus.extension.handlers.Fastjson2TypeHandler"
                property="contactPersonInfo"/>
        <result
                column="service_project_list"
                typeHandler="com.baomidou.mybatisplus.extension.handlers.Fastjson2TypeHandler"
                property="serviceProjectList"/>
    </resultMap>
    <select id="getServiceProjectList"
            resultMap="ProjectManageServiceProjectListVoResultMap">
        select eu.*,
               ad1.name                      as 'provinceName',
               ad2.name                      as 'cityName',
               ad3.name                      as 'districtsName',
               ad4.name                      as 'streetName',
               JSON_ARRAYAGG(JSON_OBJECT(
                       'snowFlakeId', sp.snow_flake_id,
                       'securityUnitId',sp.security_unit_id,
                       'name', sp.name,
                       'type', sp.type,
                       'isRecruitSecurity', sp.is_recruit_security,
                       'idNumber', sp.id_number,
                       'serviceArea', sp.service_area,
                       'buildingTotal', sp.building_total,
                       'houseTotal', sp.house_total,
                       'staffTotal', sp.staff_total,
                       'securityUserTotal', sp.security_user_total,
                       'projectManagerMiniProgramUserInfo',JSON_OBJECT(
                                                        'name',mpu.name,
                                                        'telephone',mpu.telephone,
                                                        'idCard',mpu.id_card),
                       'remark', sp.remark)) as 'service_project_list'
        from enterprises_unit eu
                 join service_project sp on eu.snow_flake_id = sp.enterprises_unit_id and sp.delete_flag = 0
                 left join mini_program_user mpu on sp.project_manager_mini_program_user_id = mpu.snow_flake_id and mpu.delete_flag = 0
                 left join administrative_division ad1 on eu.province = ad1.code and ad1.delete_flag = 0
                 left join administrative_division ad2 on eu.city = ad2.code and ad2.delete_flag = 0
                 left join administrative_division ad3 on eu.districts = ad3.code and ad3.delete_flag = 0
                 left join administrative_division ad4 on eu.street = ad4.code and ad4.delete_flag = 0
        where eu.delete_flag = 0
          <choose>
              <when test="policeUnitId!=null">
                  and eu.police_unit_id = #{policeUnitId}
              </when>
              <otherwise>
                  and sp.project_manager_mini_program_user_id = #{projectManagerMiniProgramUserId}
              </otherwise>
          </choose>
        group by eu.snow_flake_id
        order by any_value(sp.create_time) desc;
    </select>
    <resultMap id="ServiceProjectDTOResultMap" type="com.changhu.pojo.dto.ServiceProjectDTO">
        <result
                column="projectManagerInfo"
                typeHandler="com.baomidou.mybatisplus.extension.handlers.Fastjson2TypeHandler"
                property="projectManagerInfo"/>
        <result
            column="securityUnitInfo"
            typeHandler="com.baomidou.mybatisplus.extension.handlers.Fastjson2TypeHandler"
            property="securityUnitInfo" />
        <result
                column="securityUserList"
                typeHandler="com.baomidou.mybatisplus.extension.handlers.Fastjson2TypeHandler"
                property="securityUserList" />
    </resultMap>
    <select id="getServiceProjectByEnterprisesUnitId" resultMap="ServiceProjectDTOResultMap">
        SELECT
            sp.*,
            IF(mpu.snow_flake_id IS NOT NULL,json_object( 'name', mpu.NAME, 'sex', mpu.sex, 'telephone', mpu.telephone, 'idCard', mpu.id_card ),NULL) AS 'projectManagerInfo',
            json_object(
            'name',su1.name,
            'socialCode',su1.social_code,
            'businessLicense',su1.business_license,
            'legalPersonInfo',su1.legal_person_info,
            'address',su1.address,
            'nature',su1.nature
            ) as 'securityUnitInfo',
            if(COUNT(su.snow_flake_id)>0,JSON_ARRAYAGG(json_object(
                                                       'snowFlakeId',su.snow_flake_id,
                                                       'name',su.name,
                                                       'telephone',su.telephone,
                                                       'workPost',su.work_post,
                                                       'sex',su.sex,
                                                       'nativePlace',su.native_place,
                                                       'idCard',su.id_card,
                                                       'dateOfBirth',su.date_of_birth,
                                                       'securityNumber',su.security_number,
                                                       'homeAddress',su.home_address,
                                                       'remark',su.remark,
                                                       'createTime',su.create_time
                                                       )),JSON_ARRAY()) as 'securityUserList'
        FROM
            service_project sp
                left join security_unit su1 on sp.security_unit_id = su1.snow_flake_id
                LEFT JOIN mini_program_user mpu ON sp.project_manager_mini_program_user_id = mpu.snow_flake_id AND mpu.identity = 'project_manager'
                left join security_user su on su.service_project_id = sp.snow_flake_id and su.delete_flag = 0
        WHERE
            sp.delete_flag = 0
          AND sp.enterprises_unit_id = #{enterprisesUnitId}
        group by sp.snow_flake_id
        ORDER BY
            sp.create_time DESC
    </select>
</mapper>
