<?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.ylx.massage.mapper.MaTechnicianMapper">

    <resultMap type="MaTechnician" id="MaTechnicianResult">
        <result property="id"    column="id"    />
        <result property="teName"    column="te_name"    />
        <result property="teNickName"    column="te_nick_name"    />
        <result property="teSex"    column="te_sex"    />
        <result property="tePhone"    column="te_phone"    />
        <result property="teAddress"    column="te_address"    />
        <result property="teAreaCode"    column="te_area_code"    />
        <result property="teAge"    column="te_age"    />
        <result property="teAvatar"    column="te_avatar"    />
        <result property="openService"    column="openService"    />
        <result property="lifePhotos"    column="life_photos"    />
        <result property="avatar"    column="avatar"    />
        <result property="teBrief"    column="te_brief"    />
        <result property="avatar"    column="avatar"    />
        <result property="idCard"    column="id_card"    />
        <result property="healthCertificate"    column="health_certificate"    />
        <result property="qualificationCertificate"    column="qualification_certificate"    />
        <result property="noCrimeRecord"    column="no_crime_record"    />
        <result property="commitmentPdf"    column="commitment_pdf"    />
        <result property="commitmentAudio"    column="commitment_audio"    />
        <result property="commitmentVideo"    column="commitment_video"    />
        <result property="serviceState"    column="service_state"    />
        <result property="nStatus2"    column="n_status2"    />
        <result property="merchantStatus"    column="merchant_status"    />
        <result property="postState"    column="post_state"    />
        <result property="techType"    column="tech_type"    />
        <result property="teIsEnable"    column="te_is_enable"    />
        <result property="auditStatus"    column="audit_status"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDelete"    column="is_delete"    />
        <result property="nStar"    column="n_star"    />
        <result property="nNum"    column="n_num"    />
        <result property="isRecommend"    column="is_recommend"    />
    </resultMap>

    <sql id="selectMaTechnicianVo">
        select id, te_name, te_nick_name, te_sex, te_phone, te_address, te_area_code, te_age, te_avatar,
               openService, life_photos, avatar, te_brief, service_state, ns_status2, merchant_status,
               post_state, tech_type, te_is_enable, audit_status, create_user, update_user, create_time,
               update_time, is_delete, n_star, n_num, is_recommend
        from ma_technician
    </sql>

    <select id="selectMaTechnicianList" parameterType="MaTechnician" resultMap="MaTechnicianResult">
        <include refid="selectMaTechnicianVo"/>
        <where>
            <if test="teName != null  and teName != ''"> and te_name like concat('%', #{teName}, '%')</if>
            <if test="teNickName != null  and teNickName != ''"> and te_nick_name like concat('%', #{teNickName}, '%')</if>
            <if test="teSex != null "> and te_sex = #{teSex}</if>
            <if test="tePhone != null  and tePhone != ''"> and te_phone = #{tePhone}</if>
            <if test="teAddress != null  and teAddress != ''"> and te_address = #{teAddress}</if>
            <if test="openService != null "> and openService = #{openService}</if>
            <if test="teAge != null "> and te_age = #{teAge}</if>
            <if test="teAvatar != null  and teAvatar != ''"> and te_avatar = #{teAvatar}</if>
            <if test="lifePhotos != null  and lifePhotos != ''"> and life_photos = #{lifePhotos}</if>
            <if test="teBrief != null  and teBrief != ''"> and te_brief = #{teBrief}</if>
            <if test="serviceState != null "> and service_state = #{serviceState}</if>
            <if test="nsStatus2 != null "> and ns_status2 = #{nsStatus2}</if>
            <if test="merchantStatus != null  and merchantStatus != ''"> and merchant_status = #{merchantStatus}</if>
            <if test="postState != null "> and post_state = #{postState}</if>
            <if test="techType != null "> and tech_type = #{techType}</if>
            <if test="teIsEnable != null "> and te_is_enable = #{teIsEnable}</if>
            <if test="auditStatus != null "> and audit_status = #{auditStatus}</if>
            <if test="isRecommend != null "> and is_recommend = #{isRecommend}</if>
            <if test="createUser != null "> and create_user = #{createUser}</if>
            <if test="updateUser != null "> and update_user = #{updateUser}</if>
            <if test="isDelete != null "> and is_delete = #{isDelete}</if>
        </where>
    </select>

    <select id="selectMaTechnicianById" parameterType="Long" resultMap="MaTechnicianResult">
        <include refid="selectMaTechnicianVo"/>
        where id = #{id}
    </select>

    <insert id="insertMaTechnician" parameterType="MaTechnician" useGeneratedKeys="true" keyProperty="id">
        insert into ma_technician
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="teName != null and teName != ''">te_name,</if>
            <if test="teNickName != null and teNickName != ''">te_nick_name,</if>
            <if test="teSex != null">te_sex,</if>
            <if test="tePhone != null and tePhone != ''">te_phone,</if>
            <if test="teAddress != null">te_address,</if>
            <if test="teAreaCode != null and teAreaCode != ''">te_area_code,</if>
            <if test="teAge != null">te_age,</if>
            <if test="teAvatar != null and teAvatar != ''">te_avatar,</if>
            <if test="openService != null">openService,</if>
            <if test="teProject != null and teProject != ''">te_project,</if>
            <if test="lifePhotos != null and lifePhotos != ''">life_photos,</if>
            <if test="avatar != null and avatar != ''">avatar,</if>
            <if test="teBrief != null and teBrief != ''">te_brief,</if>
            <if test="serviceState != null">service_state,</if>
            <if test="nStatus2 != null">n_status2,</if>
            <if test="merchantStatus != null and merchantStatus != ''">merchant_status,</if>
            <if test="postState != null">post_state,</if>
            <if test="techType != null">tech_type,</if>
            <if test="teIsEnable != null">te_is_enable,</if>
            <if test="auditStatus != null">audit_status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDelete != null">is_delete,</if>
            <if test="nStar != null">n_star,</if>
            <if test="nNum != null">n_num,</if>
            <if test="isRecommend != null">is_recommend,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="teName != null and teName != ''">#{teName},</if>
            <if test="teNickName != null and teNickName != ''">#{teNickName},</if>
            <if test="teSex != null">#{teSex},</if>
            <if test="tePhone != null and tePhone != ''">#{tePhone},</if>
            <if test="teAddress != null">#{teAddress},</if>
            <if test="teAreaCode != null and teAreaCode != ''">#{teAreaCode},</if>
            <if test="teAge != null">#{teAge},</if>
            <if test="teAvatar != null and teAvatar != ''">#{teAvatar},</if>
            <if test="openService != null">#{openService},</if>
            <if test="teProject != null and teProject != ''">#{teProject},</if>
            <if test="lifePhotos != null and lifePhotos != ''">#{lifePhotos},</if>
            <if test="avatar != null and avatar != ''">#{avatar},</if>
            <if test="teBrief != null and teBrief != ''">#{teBrief},</if>
            <if test="serviceState != null">#{serviceState},</if>
            <if test="nStatus2 != null">#{nStatus2},</if>
            <if test="merchantStatus != null and merchantStatus != ''">#{merchantStatus},</if>
            <if test="postState != null">#{postState},</if>
            <if test="techType != null">#{techType},</if>
            <if test="teIsEnable != null">#{teIsEnable},</if>
            <if test="auditStatus != null">#{auditStatus},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDelete != null">#{isDelete},</if>
            <if test="nStar != null">#{nStar},</if>
            <if test="nNum != null">#{nNum},</if>
            <if test="isRecommend != null">#{isRecommend},</if>
         </trim>
    </insert>

    <update id="updateMaTechnician" parameterType="MaTechnician">
        update ma_technician
        <trim prefix="SET" suffixOverrides=",">
            <if test="teName != null and teName != ''">te_name = #{teName},</if>
            <if test="teNickName != null and teNickName != ''">te_nick_name = #{teNickName},</if>
            <if test="teSex != null">te_sex = #{teSex},</if>
            <if test="tePhone != null and tePhone != ''">te_phone = #{tePhone},</if>
            <if test="teAddress != null and teAddress != ''">te_address = #{teAddress},</if>
            <if test="teAreaCode != null and teAreaCode != ''">te_area_code = #{teAreaCode},</if>
            <if test="teAge != null">te_age = #{teAge},</if>
            <if test="teAvatar != null and teAvatar != ''">te_avatar = #{teAvatar},</if>
            <if test="openService != null">openService = #{openService},</if>
            <if test="lifePhotos != null and lifePhotos != ''">life_photos = #{lifePhotos},</if>
            <if test="avatar != null and avatar != ''">avatar = #{avatar},</if>
            <if test="teBrief != null and teBrief != ''">te_brief = #{teBrief},</if>
            <if test="serviceState != null">service_state = #{serviceState},</if>
            <if test="nsStatus2 != null">ns_status2 = #{nsStatus2},</if>
            <if test="merchantStatus != null and merchantStatus != ''">merchant_status = #{merchantStatus},</if>
            <if test="postState != null">post_state = #{postState},</if>
            <if test="techType != null">tech_type = #{techType},</if>
            <if test="teIsEnable != null">te_is_enable = #{teIsEnable},</if>
            <if test="auditStatus != null">audit_status = #{auditStatus},</if>
            <if test="createUser != null">create_user = #{createUser},</if>
            <if test="updateUser != null">update_user = #{updateUser},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDelete != null">is_delete = #{isDelete},</if>
            <if test="nStar != null">n_star = #{nStar},</if>
            <if test="nNum != null">n_num = #{nNum},</if>
            <if test="isRecommend != null">is_recommend = #{isRecommend},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMaTechnicianById" parameterType="Long">
        delete from ma_technician where id = #{id}
    </delete>

    <delete id="deleteMaTechnicianByIds" parameterType="String">
        delete from ma_technician where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <resultMap type="com.ylx.massage.domain.vo.MaTechnicianMerchantListVO" id="MaTechnicianMerchantListResult">
        <result property="merchantId" column="merchant_id"/>
        <result property="teName" column="te_name"/>
        <result property="teNickName" column="te_nick_name"/>
        <result property="teSex" column="te_sex"/>
        <result property="teSexName" column="te_sex_name"/>
        <result property="teAvatar" column="te_avatar"/>
        <result property="tePhone" column="te_phone"/>
        <result property="nStar" column="n_star"/>
        <result property="serviceState" column="service_state"/>
        <result property="serviceStateName" column="service_state_name"/>
        <result property="nNum" column="n_num"/>
        <result property="onlineTime" column="online_time"/>
        <result property="teProject" column="te_project"/>
        <result property="postState" column="post_state"/>
        <result property="isRecommend" column="is_recommend"/>
        <result property="techType" column="tech_type"/>
        <result property="techTypeName" column="tech_type_name"/>
        <result property="merchantStatus" column="merchant_status"/>
        <result property="merchantStatusName" column="merchant_status_name"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <resultMap type="com.ylx.massage.domain.vo.MaTechnicianMerchantDetailVO" id="MaTechnicianMerchantDetailResult">
        <result property="merchantId" column="merchant_id"/>
        <result property="teName" column="te_name"/>
        <result property="teNickName" column="te_nick_name"/>
        <result property="teSex" column="te_sex"/>
        <result property="teSexName" column="te_sex_name"/>
        <result property="tePhone" column="te_phone"/>
        <result property="openService" column="openService"/>
        <result property="serviceCategoryName" column="service_category_name"/>
        <result property="projectIds" column="project_ids"/>
        <result property="serviceProjectName" column="service_project_name"/>
        <result property="techType" column="tech_type"/>
        <result property="techTypeName" column="tech_type_name"/>
        <result property="isRecommend" column="is_recommend"/>
        <result property="isRecommendName" column="is_recommend_name"/>
    </resultMap>

    <resultMap type="com.ylx.massage.domain.vo.MaTechnicianAuditListVO" id="MaTechnicianAuditListResult">
        <result property="merchantId" column="merchant_id"/>
        <result property="teName" column="te_name"/>
        <result property="teSex" column="te_sex"/>
        <result property="teSexName" column="te_sex_name"/>
        <result property="tePhone" column="te_phone"/>
        <result property="avatar" column="avatar"/>
        <result property="teAddress" column="te_address"/>
        <result property="openServiceName" column="open_service_name"/>
        <result property="applyTime" column="apply_time"/>
        <result property="auditStatus" column="audit_status"/>
        <result property="auditStatusName" column="audit_status_name"/>
        <result property="techType" column="tech_type"/>
    </resultMap>

    <select id="selectMerchantAuditList" resultMap="MaTechnicianAuditListResult">
        SELECT
            t.id AS merchant_id,
            t.te_name,
            t.te_sex,
            CASE t.te_sex
                WHEN 0 THEN '女'
                WHEN 1 THEN '男'
                ELSE ''
            END AS te_sex_name,
            t.te_phone,
            t.avatar,
            t.te_address,
            COALESCE((
                SELECT GROUP_CONCAT(DISTINCT sc.name ORDER BY sc.sort, sc.id SEPARATOR '/')
                FROM service_category sc
                WHERE FIND_IN_SET(sc.id, t.openService)
                  AND sc.is_delete = 0
            ), '') AS open_service_name,
            t.create_time AS apply_time,
            t.audit_status,
            CASE t.audit_status
                WHEN 0 THEN '待入驻'
                WHEN 1 THEN '待审核'
                WHEN 2 THEN '审核通过'
                WHEN 3 THEN '审核驳回'
                ELSE ''
            END AS audit_status_name,
            t.tech_type
        FROM ma_technician t
        <where>
            t.is_delete = 0
            <if test="dto != null">
                <choose>
                    <when test="dto.auditStatus != null">
                        AND t.audit_status = #{dto.auditStatus}
                    </when>
                    <otherwise>
                        AND t.audit_status IN (0, 1, 2, 3)
                    </otherwise>
                </choose>
                <if test="dto.teName != null and dto.teName != ''">
                    AND t.te_name LIKE CONCAT('%', #{dto.teName}, '%')
                </if>
                <if test="dto.teNickName != null and dto.teNickName != ''">
                    AND t.te_nick_name LIKE CONCAT('%', #{dto.teNickName}, '%')
                </if>
                <if test="dto.techType != null">
                    AND t.tech_type = #{dto.techType}
                </if>
                <if test="dto.tePhone != null and dto.tePhone != ''">
                    AND t.te_phone LIKE CONCAT('%', #{dto.tePhone}, '%')
                </if>
                <if test="dto.beginApplyTime != null and dto.beginApplyTime != ''">
                    AND t.create_time &gt;= #{dto.beginApplyTime}
                </if>
                <if test="dto.endApplyTime != null and dto.endApplyTime != ''">
                    AND t.create_time &lt;= #{dto.endApplyTime}
                </if>
                <if test="dto.teSex != null">
                    AND t.te_sex = #{dto.teSex}
                </if>
            </if>
            <if test="dto == null">
                AND t.audit_status IN (0, 1, 2, 3)
            </if>
        </where>
        ORDER BY t.create_time DESC
    </select>

    <select id="selectMerchantList" resultMap="MaTechnicianMerchantListResult">
        SELECT
            t.id AS merchant_id,
            t.te_name,
            t.te_nick_name,
            t.te_sex,
            CASE t.te_sex
                WHEN 0 THEN '女'
                WHEN 1 THEN '男'
                ELSE ''
            END AS te_sex_name,
            t.te_avatar,
            t.te_phone,
            t.n_star,
            t.service_state,
            CASE t.service_state
                WHEN 0 THEN '服务中'
                WHEN 1 THEN '待接单'
                WHEN 2 THEN '休息中'
                ELSE ''
            END AS service_state_name,
            t.n_num,
            IFNULL(sign_data.online_time, 0) AS online_time,
            t.te_project,
            t.post_state,
            t.is_recommend,
            t.tech_type,
            CASE t.tech_type
                WHEN 0 THEN '正式用户'
                WHEN 1 THEN '虚拟用户'
                ELSE ''
            END AS tech_type_name,
            t.merchant_status,
            CASE t.merchant_status
                WHEN '0' THEN '正常'
                WHEN '1' THEN '限制接单'
                WHEN '2' THEN '冻结'
                WHEN '3' THEN '注销'
                ELSE ''
            END AS merchant_status_name,
            t.create_time
        FROM ma_technician t
        LEFT JOIN (
            SELECT js_id, SUM(IFNULL(online_time, 0)) AS online_time
            FROM t_sign
            WHERE is_delete = 0
            GROUP BY js_id
        ) sign_data ON sign_data.js_id = t.id
        <where>
            t.is_delete = 0
            <if test="dto != null">
                <!-- 商户姓名 -->
                <if test="dto.teName != null and dto.teName != ''">
                    AND t.te_name LIKE CONCAT('%', #{dto.teName}, '%')
                </if>
                <!-- 商户昵称 -->
                <if test="dto.teNickName != null and dto.teNickName != ''">
                    AND t.te_nick_name LIKE CONCAT('%', #{dto.teNickName}, '%')
                </if>
                <!-- 服务类目 -->
                <if test="dto.openService != null">
                    AND FIND_IN_SET(#{dto.openService}, t.openService)
                </if>
                <!-- 手机号 -->
                <if test="dto.tePhone != null and dto.tePhone != ''">
                    AND t.te_phone LIKE CONCAT('%', #{dto.tePhone}, '%')
                </if>
                <!-- 注册开始时间 -->
                <if test="dto.beginCreateTime != null and dto.beginCreateTime != ''">
                    AND t.create_time &gt;= #{dto.beginCreateTime}
                </if>
                <!-- 注册结束时间 -->
                <if test="dto.endCreateTime != null and dto.endCreateTime != ''">
                    AND t.create_time &lt;= #{dto.endCreateTime}
                </if>
                <!-- 上架状态 -->
                <if test="dto.postState != null">
                    AND t.post_state = #{dto.postState}
                </if>
                <!-- 性别 -->
                <if test="dto.teSex != null">
                    AND t.te_sex = #{dto.teSex}
                </if>
                <!-- 服务状态 -->
                <if test="dto.serviceState != null">
                    AND t.service_state = #{dto.serviceState}
                </if>
                <!-- 用户类型 -->
                <if test="dto.techType != null">
                    AND t.tech_type = #{dto.techType}
                </if>
            </if>
        </where>
        ORDER BY t.create_time DESC
    </select>

    <select id="selectMerchantDetailById" resultMap="MaTechnicianMerchantDetailResult">
        SELECT
            t.id AS merchant_id,
            t.te_name,
            t.te_nick_name,
            t.te_sex,
            CASE t.te_sex
                WHEN 0 THEN '女'
                WHEN 1 THEN '男'
                ELSE ''
            END AS te_sex_name,
            t.te_phone,
            t.openService,
            COALESCE(project_data.service_category_name, (
                SELECT GROUP_CONCAT(DISTINCT sc.name ORDER BY sc.sort, sc.id SEPARATOR '/')
                FROM service_category sc
                WHERE FIND_IN_SET(sc.id, t.openService)
                  AND sc.is_delete = 0
            ), '') AS service_category_name,
            project_data.project_ids,
            COALESCE(project_data.service_project_name, t.te_project, '') AS service_project_name,
            t.tech_type,
            CASE t.tech_type
                WHEN 0 THEN '正式商户'
                WHEN 1 THEN '虚拟商户'
                ELSE ''
            END AS tech_type_name,
            t.is_recommend,
            CASE t.is_recommend
                WHEN 1 THEN '是'
                WHEN 0 THEN '否'
                ELSE ''
            END AS is_recommend_name
        FROM ma_technician t
        LEFT JOIN (
            SELECT
                mtp.te_id,
                GROUP_CONCAT(DISTINCT p.id ORDER BY p.id SEPARATOR ',') AS project_ids,
                GROUP_CONCAT(DISTINCT p.title ORDER BY p.id SEPARATOR '/') AS service_project_name,
                GROUP_CONCAT(DISTINCT sc2.name ORDER BY sc2.sort, sc2.id SEPARATOR '/') AS service_category_name
            FROM ma_te_project mtp
            LEFT JOIN project p ON p.id = mtp.project_id AND p.is_delete = 0
            LEFT JOIN service_category sc2 ON sc2.id = p.type AND sc2.is_delete = 0
            GROUP BY mtp.te_id
        ) project_data ON project_data.te_id = t.id
        WHERE t.is_delete = 0
          AND t.id = #{id}
    </select>

     <!-- 首页按摩商户推荐列表-->
    <select id="getMerchantRecommend" resultType="com.ylx.massage.domain.vo.MerchantVo">
        SELECT
            t.id AS merchantId,
            t.te_name AS teName,
            t.n_star AS nStar,
            COALESCE(o.sales, 0) AS nNum,
            COALESCE(p.min_price, 0) AS price,
            t.avatar AS avatar,
            CASE
                WHEN t.dist_meters IS NULL THEN NULL
                WHEN t.dist_meters &lt; 1000 THEN CONCAT(ROUND(t.dist_meters, 0), '米')
                ELSE CONCAT(ROUND(t.dist_meters / 1000, 1), '公里')
                END AS distanceShow
        FROM (
                 SELECT
                     t.id,
                     t.te_name,
                     t.n_star,
                     t.avatar,
                     ROUND(ST_Distance_Sphere(POINT(a.longitude, a.latitude), POINT(#{dto.longitude}, #{dto.latitude})), 0) AS dist_meters
                 FROM ma_technician t
                          LEFT JOIN (
                     SELECT merchant_id, longitude, latitude
                     FROM t_address
                     WHERE user_type = 2 AND type = 1 AND is_delete = 0
                 ) a ON t.id = a.merchant_id
                 WHERE t.is_delete = 0
                   AND t.audit_status = 2
                   AND t.n_status2 = 0
                   AND t.merchant_status = 0
                   AND t.te_area_code = #{dto.cityCode}
             ) t
                 LEFT JOIN (
            SELECT merchant_id, COUNT(*) AS sales
            FROM t_order
            WHERE is_delete = 0 AND status = 6 AND project_type = 1
            GROUP BY merchant_id
        ) o ON t.id = o.merchant_id
                 LEFT JOIN (
            SELECT merchant_id, MIN(project_current_price) AS min_price
            FROM ma_project
            WHERE is_delete = 0 AND audit_status = 1 AND merchant_type = 1 AND project_is_enable = 1
            GROUP BY merchant_id
        ) p ON t.id = p.merchant_id
        ORDER BY COALESCE(o.sales, 0) DESC
            LIMIT 5
    </select>


</mapper>
