<?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.mmc.iuav.user.dao.CooperationDao">


    <resultMap id="UserApplyTagResultMap" type="com.mmc.iuav.user.entity.UserApplyTagDO">
        <result property="id" column="id"/>
        <result property="cooperationTagId" column="cooperation_tag_id"/>
        <result property="userAccountId" column="user_account_id"/>
        <result property="applyName" column="apply_name"/>
        <result property="applyPhone" column="apply_phone"/>
        <result property="applyStatus" column="apply_status"/>
        <result property="remark" column="remark"/>
        <result property="createTime" column="create_time"/>
        <result property="tagName" column="tag_name"/>

        <association property="companyInfoDO" javaType="com.mmc.iuav.user.entity.CompanyInfoDO">
            <id property="id" column="company_info_id"/>
            <result property="companyName" column="company_name"/>
            <result property="brandName" column="brand_name"/>
            <result property="brandLogo" column="brand_logo"/>
            <result property="licenseImg" column="license_img"/>
            <result property="address" column="address"/>
            <result property="lat" column="lat"/>
            <result property="lon" column="lon"/>
            <result property="content" column="content"/>
            <result property="score" column="score"/>
        </association>
        <collection property="attachmentList" ofType="com.mmc.iuav.user.entity.TagApplyResourceDO"
                    select="getUserApplyResource"
                    column="{id=id}">
        </collection>

    </resultMap>


    <insert id="addApply" parameterType="com.mmc.iuav.user.entity.UserApplyTagDO" useGeneratedKeys="true"
            keyProperty="id">
        insert into user_apply_tag(cooperation_tag_id, user_account_id, apply_name, apply_phone, remark, create_time)
        values (#{cooperationTagId}, #{userAccountId}, #{applyName}, #{applyPhone}, #{remark}, NOW())
    </insert>

    <insert id="insertUserTag" useGeneratedKeys="true" parameterType="com.mmc.iuav.user.entity.UserTagDO"
            keyProperty="id">
        insert into user_tag(user_account_id, cooperation_tag_id, create_time)
        values (#{userAccountId}, #{cooperationTagId}, NOW());
    </insert>
    <insert id="addApplyResource">
        insert into tag_apply_resource (user_apply_tag_id,
                                        url,
                                        `type`)
        values (#{userApplyTagId}, #{url}, #{type})
    </insert>

    <insert id="batchInsertUserTag">
        insert into user_tag(user_account_id, cooperation_tag_id, create_time) values
        <foreach item="d" index="index" collection="cooperationTagIds"
                 separator=",">
            (#{userAccountId}, #{d}, NOW())
        </foreach>
    </insert>
    <insert id="addWebsiteInfo">
        insert into website_info (`name`, address, lat, lon, `type`, user_apply_tag)
        values (#{name}, #{address}, #{lat}, #{lon}, #{cooperationTagId}, #{id})
    </insert>

    <update id="updateWebsiteInfo">
        update website_info
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="address != null and address != ''">
                address = #{address},
            </if>
            <if test="lat != null">
                lat = #{lat},
            </if>
            <if test="lon != null">
                lon = #{lon},
            </if>
            <if test="cooperationTagId != null">
                `type` = #{cooperationTagId}
            </if>
        </set>
        where id = #{websiteId}
    </update>
    <update id="updateUserApplyTag" parameterType="com.mmc.iuav.user.entity.UserApplyTagDO">
        UPDATE user_apply_tag
        <set>
            <if test="cooperationTagId != null">
                cooperation_tag_id = #{cooperationTagId},
            </if>
            <if test="userAccountId != null">
                user_account_id = #{userAccountId},
            </if>
            <if test="applyName != null">
                apply_name = #{applyName},
            </if>
            <if test="applyPhone != null">
                apply_phone = #{applyPhone},
            </if>
            <if test="applyStatus != null">
                apply_status = #{applyStatus},
            </if>
            remark = #{remark},
            update_time=NOW()
        </set>
        WHERE id = #{id}
    </update>

    <update id="updateUserTag">
        update user_tag
        set cooperation_tag_id = #{cooperationTagId}
        where id = #{id}
    </update>

    <update id="removeUserTag">
        update user_tag
        set is_deleted = 1
        where user_account_id = #{userAccountId}
          and is_deleted = 0
    </update>

    <update id="updateApplyTagStatus">
        update user_apply_tag
        set apply_status = #{i}
        where id = #{id}
    </update>

    <delete id="deleteApplyTag">
        delete
        from user_apply_tag
        where id = #{id}
    </delete>

    <select id="listTags" resultType="com.mmc.iuav.user.entity.CooperationTagDO">
        select id, tag_name, tag_img, tag_require, tag_description, create_time
        from cooperation_tag
        where is_deleted = 0
    </select>

    <select id="getUserApplyTag" resultMap="UserApplyTagResultMap">
        SELECT ua.id,
               ua.cooperation_tag_id,
               ua.user_account_id,
               ua.apply_name,
               ua.apply_phone,
               ua.apply_status,
               ua.remark,
               ua.create_time,
               ct.tag_name,
               ci.id AS company_info_id,
               ci.company_name,
               ci.brand_name,
               ci.brand_logo,
               ci.credit_code,
               ci.license_img,
               ci.address,
               ci.lat,
               ci.lon,
               ci.content,
               ci.score
        FROM user_apply_tag ua
                 INNER JOIN cooperation_tag ct ON ua.cooperation_tag_id = ct.id
                 INNER JOIN company_member cm ON cm.user_account_id = ua.user_account_id
                 INNER JOIN company_info ci ON cm.company_info_id = ci.id
        WHERE ua.id = #{id}
          AND ua.is_deleted = 0
    </select>

    <select id="listUserCooperationTag" resultType="com.mmc.iuav.user.entity.CooperationTagDO">
        select ct.id, ct.tag_name
        from cooperation_tag ct
                 LEFT JOIN user_tag ut ON ct.id = ut.cooperation_tag_id
        where ct.is_deleted = 0
          and ut.is_deleted = 0
          and ut.user_account_id = #{userAccountId}
    </select>

    <select id="getTagById" resultType="com.mmc.iuav.user.entity.CooperationTagDO">
        select id, tag_name, tag_img, tag_require, tag_description, create_time
        from cooperation_tag
        where id = #{id}
          and is_deleted = 0
    </select>
    <select id="countListUserApplyTag" resultType="java.lang.Integer">
        select count(*)
        from
        user_apply_tag ua
        INNER JOIN cooperation_tag ct ON ua.cooperation_tag_id = ct.id
        INNER JOIN company_member cm ON cm.user_account_id = ua.user_account_id
        INNER JOIN company_info ci ON cm.company_info_id = ci.id
        <where>
            ua.is_deleted = 0
            <if test="cooperationTagId != null">
                and ua.cooperation_tag_id = #{cooperationTagId}
            </if>
            <if test="applyStatus != null">
                and ua.apply_status = #{applyStatus}
            </if>
            <if test="startTime != null and startTime != '' ">
                and ua.create_time &gt;= STR_TO_DATE(#{startTime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="endTime != null and endTime != '' ">
                and ua.create_time &lt;= STR_TO_DATE(#{endTime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="companyName != null and companyName != ''">
                and ci.company_name like CONCAT("%",#{companyName},"%")
            </if>
        </where>
    </select>


    <select id="listUserApplyTag" resultMap="UserApplyTagResultMap">
        SELECT
        ua.id,
        ua.cooperation_tag_id,
        ua.user_account_id,
        ua.apply_name,
        ua.apply_phone,
        ua.apply_status,
        ua.remark,
        ua.create_time,
        ct.tag_name,
        ci.id as company_info_id,
        ci.company_name,
        ci.brand_name,
        ci.brand_logo,
        ci.credit_code,
        ci.license_img,
        ci.address,
        ci.lat,
        ci.lon,
        ci.content,
        ci.score
        FROM
        user_apply_tag ua
        INNER JOIN cooperation_tag ct ON ua.cooperation_tag_id = ct.id
        INNER JOIN company_member cm ON cm.user_account_id = ua.user_account_id
        INNER JOIN company_info ci ON cm.company_info_id = ci.id
        <where>
            ua.is_deleted = 0
            <if test="cooperationTagId != null">
                and ua.cooperation_tag_id = #{cooperationTagId}
            </if>
            <if test="applyStatus != null">
                and ua.apply_status = #{applyStatus}
            </if>
            <if test="startTime != null">
                and ua.create_time &gt;= #{startTime}
            </if>
            <if test="endTime != null">
                and ua.create_time &lt;= #{endTime}
            </if>
            <if test="companyName != null and companyName != ''">
                and ci.company_name like CONCAT("%",#{companyName},"%")
            </if>
        </where>
        order by ua.create_time desc
        limit #{pageNo},#{pageSize}
    </select>
    <select id="getUserApplyResource" resultType="com.mmc.iuav.user.entity.TagApplyResourceDO">
        SELECT user_apply_tag_id,
               url,
               `type`
        FROM tag_apply_resource
        where user_apply_tag_id = #{id}
    </select>
    <select id="userTagInfo" resultType="java.lang.Integer">
        select count(*)
        from user_tag
        where is_deleted = 0
          and cooperation_tag_id = #{cooperationTagId}
          and user_account_id = #{userAccountId}
    </select>

    <select id="listUserCooperationTagByUIds" resultType="com.mmc.iuav.user.entity.CooperationTagDO">
        select ct.id,ct.tag_name,ut.user_account_id from cooperation_tag ct LEFT JOIN user_tag ut ON ct.id =
        ut.cooperation_tag_id
        where ct.is_deleted = 0 and ut.is_deleted = 0
        <if test="uIds != null">
            <foreach collection="uIds" item="userAccountId" separator="," open=" and ut.user_account_id in (" close=")">
                #{userAccountId}
            </foreach>
        </if>
    </select>
    <select id="countServiceBitmapData" resultType="java.lang.Integer">
        SELECT count(*)
        FROM user_apply_tag uat
                 INNER JOIN cooperation_tag ct ON uat.cooperation_tag_id = ct.id
        WHERE ct.id = #{type}
          and uat.is_deleted = 0
    </select>
    <select id="listServiceBitmapData" resultType="com.mmc.iuav.user.entity.WebsiteInfoDO">
        SELECT uat.id,
               ci.company_name                                                  AS `name`,
               ci.address,
               ci.lon,
               ci.lat,
               ct.id                                                            AS type,
               ci.content,
               ci.score,
               st_distance_sphere(point(ci.lon, ci.lat), point(${lon}, ${lat})) AS distance
        FROM user_apply_tag uat
                 INNER JOIN cooperation_tag ct ON uat.cooperation_tag_id = ct.id
                 INNER JOIN company_member cm ON cm.user_account_id = uat.user_account_id
                 INNER JOIN company_info ci ON cm.company_info_id = ci.id
        WHERE ct.id = #{type}
          and uat.is_deleted = 0
        ORDER BY distance ASC
            LIMIT #{pageNo}, #{pageSize}
    </select>
    <select id="getWebsiteInfoByUserApplyId" resultType="com.mmc.iuav.user.entity.WebsiteInfoDO">
        select id,
               `name`,
               address,
               lat,
               lon,
               `type`,
               user_apply_tag
        from website_info
        where user_apply_tag = #{id}
    </select>


    <delete id="deleteApplyResource" parameterType="java.lang.Integer">
        DELETE
        FROM tag_apply_resource
        WHERE user_apply_tag_id = #{id}
    </delete>

    <insert id="insertApplyResource">
        INSERT INTO tag_apply_resource(user_apply_tag_id,url,`type`,create_time,update_time) VALUES
        <foreach collection="list" separator="," item="item">
            (#{item.userApplyTagId},#{item.url},#{item.type},NOW(),NOW())
        </foreach>
    </insert>

    <update id="updateCompanyInfo"
            parameterType="com.mmc.iuav.user.entity.UserApplyTagDO">
        UPDATE company_info
        <set>
            <if test=" lat != null and lat != '' ">
                lat = #{lat},
            </if>
            <if test=" lon != null and lon != '' ">
                lon = #{lon},
            </if>
            <if test=" address != null and address != '' ">
                address = #{address},
            </if>
            <if test=" brandName != null and brandName != '' ">
                brand_name = #{brandName},
            </if>
            <if test=" brandLogo != null and brandLogo != '' ">
                brand_logo = #{brandLogo},
            </if>
            update_time=NOW()
        </set>
        where id = #{companyInfoId}
    </update>

    <select id="appBrandList" resultType="com.mmc.iuav.user.entity.CompanyInfoDO">
        SELECT ua.id,
               ua.cooperation_tag_id,
               ua.user_account_id,
               ua.apply_name,
               ua.apply_phone,
               ua.apply_status,
               ua.remark,
               ua.create_time,
               ci.content,
               ci.score,
               ct.tag_name,
               ci.company_name,
               ci.brand_name,
               ci.brand_logo,
               ci.credit_code,
               ci.license_img,
               ci.address,
               ci.lat,
               ci.lon
        FROM user_apply_tag ua
                 INNER JOIN cooperation_tag ct ON ua.cooperation_tag_id = ct.id
                 INNER JOIN company_member cm ON cm.user_account_id = ua.user_account_id
                 INNER JOIN company_info ci ON cm.company_info_id = ci.id
        WHERE ua.apply_status = 1
          AND ua.is_deleted = 0
          AND ct.is_deleted = 0
          AND ci.is_deleted = 0
    </select>

    <select id="countUserApplyTagByUid" resultType="java.lang.Integer">
        select count(*)
        from user_apply_tag
        where user_account_id = #{userAccountId}
          and is_deleted = 0
          and apply_status = 1
    </select>


    <select id="appBrandMessage" resultMap="UserApplyTagResultMap">
        SELECT ua.id,
               ua.cooperation_tag_id,
               ua.user_account_id,
               ua.apply_name,
               ua.apply_phone,
               ua.apply_status,
               ua.remark,
               ua.create_time,
               ct.tag_name,
               ci.company_name,
               ci.brand_name,
               ci.brand_logo,
               ci.credit_code,
               ci.license_img,
               ci.address,
               ci.lat,
               ci.lon,
               ci.content,
               ci.score
        FROM user_apply_tag ua
                 INNER JOIN cooperation_tag ct ON ua.cooperation_tag_id = ct.id
                 INNER JOIN company_member cm ON cm.user_account_id = ua.user_account_id
                 INNER JOIN company_info ci ON cm.company_info_id = ci.id
        WHERE ua.apply_status = 1
          AND ua.user_account_id = #{userAccountId}
          AND ua.is_deleted = 0
          AND ct.is_deleted = 0
          AND ci.is_deleted = 0
    </select>

    <select id="countListCompanyInfoByCoopId" resultType="java.lang.Integer">
        SELECT count(*)
        FROM user_apply_tag uat
        INNER JOIN cooperation_tag ct ON uat.cooperation_tag_id = ct.id
        INNER JOIN company_member cm ON cm.user_account_id = uat.user_account_id
        INNER JOIN company_info ci ON cm.company_info_id = ci.id
        WHERE ct.id = #{coopId}
          and uat.is_deleted = 0 and uat.apply_status = 1 and ci.is_deleted = 0
    </select>

    <select id="listCompanyInfoByCoopId" resultType="com.mmc.iuav.user.entity.CompanyInfoDO">
        SELECT ci.id,
               ci.company_name,
               ci.address,
               ci.lon,
               ci.lat,
               ci.content,
               ci.score,
               ci.brand_name,
               ci.brand_logo,
               cbu.back_user_account_id,
               round(st_distance_sphere(point(ci.lon, ci.lat),point(#{lon}, #{lat}))/1000, 2) AS distance
        FROM user_apply_tag uat
                 INNER JOIN cooperation_tag ct ON uat.cooperation_tag_id = ct.id
                 INNER JOIN company_member cm ON cm.user_account_id = uat.user_account_id
                 INNER JOIN company_info ci ON cm.company_info_id = ci.id
                 INNER JOIN company_back_user cbu ON cbu.company_info_id = ci.id
        WHERE ct.id = #{coopId}
          and uat.is_deleted = 0 and uat.apply_status = 1 and ci.is_deleted = 0
          ORDER BY distance ASC
            LIMIT #{pageNo}, #{pageSize}
    </select>
</mapper>
