package com.fs.qw.mapper;

import com.fs.qw.domain.QwTag;
import com.fs.qw.param.QwTagSearchParam;
import com.fs.qw.param.newparam.ContactTagListParam;
import com.fs.qw.vo.QwTagVO;
import com.fs.qw.vo.sidebar.ExternalContactTagVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 企微客户标签Mapper接口
 *
 * @author fs
 * @date 2024-06-20
 */
public interface QwTagMapper
{
    /**
     * 查询企微客户标签
     *
     * @param id 企微客户标签主键
     * @return 企微客户标签
     */
    public QwTag selectQwTagById(Long id);


    /**
     * 查询企微客户标签列表
     *
     * @param qwTag 企微客户标签
     * @return 企微客户标签集合
     */
    public List<QwTag> selectQwTagList(QwTag qwTag);

    /**
     * 新增企微客户标签
     *
     * @param qwTag 企微客户标签
     * @return 结果
     */
    public int insertQwTag(QwTag qwTag);

    /**
     * 修改企微客户标签
     *
     * @param qwTag 企微客户标签
     * @return 结果
     */
    public int updateQwTag(QwTag qwTag);

    /**
     * 删除企微客户标签
     *
     * @param id 企微客户标签主键
     * @return 结果
     */
    public int deleteQwTagById(Long id);

    public int deleteQwTagByGroupId(String id);


    /**
     * 批量删除企微客户标签
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteQwTagByIds(Long[] ids);
    @Select({"<script> " +
            " select t1.*,t2.corp_name from qw_tag t1 "+
            " inner join qw_company t2 on t1.corp_id = t2.corp_id " +
            "<where>\n" +
            "            <if test=\"tagId != null  and tagId != ''\"> and t1.tag_id = #{tagId}</if>\n" +
            "            <if test=\"name != null  and name != ''\"> and t1.name like concat('%', #{name}, '%')</if>\n" +
            "            <if test=\"groupId != null  and groupId != ''\"> and t1.group_id = #{groupId}</if>\n" +
            "            <if test=\"order != null  and order != ''\"> and t1.`order` = #{order}</if>\n" +
            "            <if test=\"corpId != null  and corpId != ''\"> and t1.corp_id = #{corpId}</if>\n" +
//            "            <if test=\"companyId != null \"> and t1.company_id = #{companyId}</if>\n" +
            "            <if test=\"companyId != null \">and FIND_IN_SET(#{companyId},t2.company_ids) </if>\n" +
            "</where>"+
            "</script>"})
    List<QwTagVO> selectQwTagListVO(QwTag qwTag);

    @Select("select * from qw_tag where name=#{trimTag} and corp_id=#{corpId} limit 1" )
    QwTagVO selectQwTagByName(@Param("trimTag") String trimTag, @Param("corpId")String corpId );

    @Select("<script>" +
            "SELECT distinct name FROM qw_tag WHERE tag_id IN " +
            "<foreach collection='date.tagIds' item='tagId' open='(' separator=',' close=')'>" +
            "#{tagId}" +
            "</foreach>" +
            "</script>")
    List<String> selectQwTagListNameByTagIds(@Param("date") QwTagSearchParam param);

    List<QwTag> selectQwTagListByTagIdsNew(@Param("tagIds") List<String> tagIds);

    List<QwTagVO> selectTagListByUserId(@Param("param") ContactTagListParam param,  @Param("keywords") String[] keywords);

    @Select("<script>" +
            "SELECT tag_id, name FROM qw_tag WHERE tag_id IN " +
            "<foreach collection='tagIds' item='tagId' open='(' separator=',' close=')'>" +
            "#{tagId}" +
            "</foreach>" +
            "</script>")
    List<ExternalContactTagVO> selectQwTagListByTagIds(@Param("tagIds") List<String> tagIds);

}
