package com.fs.sop.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fs.aiChat.param.QwSopParam;
import com.fs.aiChat.vo.QwSopImCustomerVO;
import com.fs.aiChat.vo.QwSopListVO;
import com.fs.common.annotation.DataSource;
import com.fs.common.enums.DataSourceType;
import com.fs.qw.domain.QwSopUpdateStatus;
import com.fs.qw.result.QwFilterSopCustomersResult;
import com.fs.qw.vo.QwSopRuleTimeVO;
import com.fs.qw.vo.WxSopRuleTimeVO;
import com.fs.sop.domain.QwSop;
import com.fs.sop.params.QwSopAutoByTags;
import com.fs.sop.params.QwSopAutoTime;
import com.fs.sop.params.QwSopTagsParam;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 企微sopMapper接口
 *
 * @author fs
 * @date 2024-07-31
 */
@Repository
@DataSource(DataSourceType.SOP)
public interface QwSopMapper extends BaseMapper<QwSop> {
    /**
     * 查询企微sop
     *
     * @param id 企微sop主键
     * @return 企微sop
     */
    @DataSource(DataSourceType.SOP)
    public QwSop selectQwSopById(String id);

    /**
     * 根据员工的企业微信的userid查询他最新的启动的个人sop规则
     *
     */
//    @Select("SELECT * FROM qw_sop " +
//            "WHERE " +
//            "FIND_IN_SET(#{userId},qwUserIds) " +
//            " AND company_id = #{companyId} " +
//            " AND status=1 " +
//            " AND type=#{type} " +
//            "ORDER BY " +
//            "create_time DESC " +
//            "LIMIT 1")
//    public QwSop selectQwSopByUserId(@Param("userId") Long userId,@Param("companyId") Long companyId,@Param("type") Integer type);

    /**
     * 查询企微sop列表
     *
     * @param qwSop 企微sop
     * @return 企微sop集合
     */
    @DataSource(DataSourceType.SOP)
    public List<QwSop> selectQwSopList(QwSop qwSop);

    @DataSource(DataSourceType.SOP)
    public List<QwSop> selectQwSopMyList(QwSop qwSop);

    /**
     * 新增企微sop
     *
     * @param qwSop 企微sop
     * @return 结果
     */
    @DataSource(DataSourceType.SOP)
    public int insertQwSop(@Param("data") QwSop qwSop);

    /**
     * 修改企微sop
     *
     * @param qwSop 企微sop
     * @return 结果
     */
    @DataSource(DataSourceType.SOP)
    public int updateQwSop(@Param("data") QwSop qwSop);

    @DataSource(DataSourceType.SOP)
    public int updateAutoSopTime(@Param("map") QwSopAutoTime param);

    @DataSource(DataSourceType.SOP)
    public int updateSopStatus(@Param("map") QwSopUpdateStatus param);

    /**
     * 删除企微sop
     *
     * @param id 企微sop主键
     * @return 结果
     */
    @DataSource(DataSourceType.SOP)
    public int deleteQwSopById(String id);

    /**
     * 批量删除企微sop
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    @DataSource(DataSourceType.SOP)
    public int deleteQwSopByIds(String[] ids);

    @DataSource(DataSourceType.SOP)
    public int deleteQwSopByIdsUpdate(@Param("ids") String[] ids);

//    @Select("SELECT * FROM qw_sop " +
//            "WHERE " +
//            "FIND_IN_SET( #{map.qwUserId}, qwUserIds ) " +
//            "AND company_id = #{map.companyId} " +
//            "AND STATUS = 1 " +
//            "AND type = 1 " +
//            "ORDER BY " +
//            "create_time DESC ")
//    List<QwSopVO> getSopList( @Param("map") QwSopParam QwSopParam);

    @DataSource(DataSourceType.SOP)
    @Select("<script>" +
            "SELECT * FROM qw_sop " +
            "<where> " +
            "FIND_IN_SET(#{map.qwUserId}, qw_user_ids) " +
            "AND corp_id = #{map.corpId} " +
            "AND STATUS = 1 " +
            "AND type = 1 " +
            "<if test='map.sendType != null'> AND send_type = #{map.sendType}</if> " +
            "</where> " +
            "ORDER BY create_time DESC " +
            "</script>")
    List<QwSopListVO> selectSopListVO(@Param("map") QwSopParam qwSopParam);

    @DataSource(DataSourceType.MASTER)
    @Select("SELECT cu.nick_name as userName , qec.* " +
            "FROM " +
            " qw_external_contact qec  left JOIN company_user cu on  qec.company_user_id=cu.user_id " +
            "WHERE " +
            "qec.corp_id = #{map.corpId} " +
            "AND qec.company_user_id = #{map.userId}")
    List<QwSopImCustomerVO> getSopCustomerList(@Param("map") QwSopParam qwSopParam);


    /**
     * 黑科技-chat 获取员工SOP规则列表
     */
//    @Select("SELECT\n" +
//            "\tqs.*,\n" +
//            "\tqst.name AS temp_name,\n" +
//            "\tqst.setting AS temp_setting,\n" +
//            "\tqst.status AS temp_status,\n" +
//            "\tqst.gap AS temp_gap,\n" +
//            "\tqst.sort AS temp_sort,\n" +
//            "\tqst.create_time AS temp_create_time,\n" +
//            "\tqst.create_by AS temp_create_by,\n" +
//            "\tqst.corp_id AS temp_company_id\n" +
//            "FROM\n" +
//            "\tqw_sop qs\n" +
//            "\tLEFT JOIN qw_sop_temp qst ON qs.temp_id = qst.id \n" +
//            "WHERE\n" +
//            "\tqs.STATUS = 1 \n" +
//            "\tAND qs.start_time <= NOW()")
    @DataSource(DataSourceType.SOP)
    List<QwSopRuleTimeVO> checkSopRuleTime();


//    @Select("<script>" +
//            "SELECT wl.user_id AS miniUserId, wl.video_id, wl.log_type, qec.* " +
//            "FROM " +
//            "<if test='map.courseStatus != 0 '> " +
//            "fs_course_watch_log wl " +
//            "LEFT JOIN qw_external_contact qec  " +
//            "on wl.qw_external_contact_id=qec.id " +
//            "</if>" +
//            "<if test='map.courseStatus == 0 '> " +
//            "qw_external_contact qec " +
//            "LEFT JOIN fs_course_watch_log wl  " +
//            "on wl.qw_external_contact_id=qec.id " +
//            "</if>" +
//            "WHERE qec.qw_user_id  in " +
//            " <foreach collection='map.userIdsSelectList'  item='item' index='index'  open='(' separator=',' close=')'> #{item} </foreach> " +
//            " <if test='map.courseStatus !=null and map.courseStatus !=\"\" and  map.courseStatus != 0'> " +
//            "and wl.log_type=#{map.courseStatus} " +
//            "</if>" +
//            "<if test='map.videoId !=null and map.videoId !=\"\" and  map.courseStatus != 0 '> " +
//            "and wl.video_id=#{map.videoId} " +
//            "</if>" +
//            "<if test ='map.filterType==2 and map.tagsIdsSelectList.size()!=0 and  map.courseStatus != 0'> " +
//            "  and ( \n" +
//            "    <foreach collection='map.tagsIdsSelectList' item='item' index='index' separator=' or '> \n" +
//            "        find_in_set( #{item} , REGEXP_REPLACE(qec.tag_ids,  '[\"\\\\[\\\\]]', '' ) ) \n" +
//            "    </foreach> \n" +
//            "    )" +
//            "</if> " +
//            "<if test=\"(map.filterType == 1 and map.tagsIdsSelectList.size() != 0) \n" +
//            "          or ( map.courseStatus == 0 and map.filterType == 1 and map.tagsIdsSelectList.size() != 0 )\">\n" +
//            "    and (\n" +
//            "        <foreach collection='map.tagsIdsSelectList' item='item' index='index' separator=' and '>\n" +
//            "            find_in_set( #{item}, REGEXP_REPLACE(qec.tag_ids, '[\"\\\\[\\\\]]', '') )\n" +
//            "        </foreach>\n" +
//            "    )\n" +
//            "</if>"+
//            "<if test ='map.outTagsIdsSelectList.size()!=0 '> " +
//            " and <foreach collection='map.outTagsIdsSelectList'  item='item' index='index'  open='( 1=2 ' separator='' close=')'> or not find_in_set(  #{item} , REGEXP_REPLACE ( qec.tag_ids, '[\"\\\\[\\\\]]', '' ) )  </foreach> " +
//            "</if> " +
//            "<if test ='map.courseStatus == 0 '> " +
//            " AND wl.qw_external_contact_id IS NULL " +
//            "</if> " +
//            "</script>")
//    public List<QwFilterSopCustomersResult> selectFilterSopCustomers(@Param("map") QwSopTagsParam qwSopTagsParam);

    @DataSource(DataSourceType.MASTER)
    @Select("<script>" +
            "SELECT  qec.* , qu.company_user_id as cuCompanyUserId ,cu.company_id as cuCompanyId  " +
            "FROM " +
            "qw_external_contact qec " +
            "\tleft JOIN qw_user qu on qu.id=qec.qw_user_id\n" +
            "\tLEFT JOIN company_user cu on cu.user_id=qu.company_user_id " +
            "WHERE qec.status=0 and qec.corp_id =#{map.cropId} and qec.qw_user_id  in " +
            " <foreach collection='map.userIdsSelectList'  item='item' index='index'  open='(' separator=',' close=')'> #{item} </foreach> " +
            "<if test ='map.filterType==2 and map.tagsIdsSelectList!=null'> " +
            "  and ( \n" +
            "    <foreach collection='map.tagsIdsSelectList' item='item' index='index' separator=' or '> \n" +
            "        find_in_set( #{item} , REGEXP_REPLACE(qec.tag_ids,  '[\"\\\\[\\\\]]', '' ) ) \n" +
            "    </foreach> \n" +
            "    )" +
            "</if> " +
            "<if test='map.filterType == 1 and  map.tagsIdsSelectList!=null'>\n" +
            "    and (\n" +
            "        <foreach collection='map.tagsIdsSelectList' item='item' index='index' separator=' and '>\n" +
            "            find_in_set( #{item}, REGEXP_REPLACE(qec.tag_ids, '[\"\\\\[\\\\]]', '') )\n" +
            "        </foreach>\n" +
            "    )\n" +
            "</if>"+
            "<if test ='map.outTagsIdsSelectList !=null'> " +
            " and <foreach collection='map.outTagsIdsSelectList'  item='item' index='index'  open='( 1=2 ' separator='' close=')'> or not find_in_set(  #{item} , REGEXP_REPLACE ( qec.tag_ids, '[\"\\\\[\\\\]]', '' ) )  </foreach> " +
            "</if> " +
            "</script>")
    public List<QwFilterSopCustomersResult> selectFilterQwSopCustomers(@Param("map") QwSopTagsParam qwSopTagsParam);


    @DataSource(DataSourceType.SOP)
    public List<QwSopRuleTimeVO> executeSopByIds(@Param("ids") String[] ids);
    @DataSource(DataSourceType.SOP)
    public List<WxSopRuleTimeVO> executeSopWxByIds(@Param("ids") String[] ids);

//    @Select("<script> " +
//            "SELECT\n" +
//            "\tqs.*,\n" +
//            "\tqst.id AS temp_id,\n" +
//            "\tqst.name AS temp_name,\n" +
//            "\tqst.setting AS temp_setting,\n" +
//            "\tqst.status AS temp_status,\n" +
//            "\tqst.gap AS temp_gap,\n" +
//            "\tqst.sort AS temp_sort,\n" +
//            "\tqst.create_time AS temp_create_time,\n" +
//            "\tqst.create_by AS temp_create_by,\n" +
//            "\tqst.corp_id AS temp_company_id\n" +
//            "FROM\n" +
//            "\tqw_sop qs\n" +
//            "\tLEFT JOIN qw_sop_temp qst ON qs.temp_id = qst.id \n" +
//            "WHERE qs.id in " +
//            "        <foreach item=\"id\" collection=\"array\" open=\"(\" separator=\",\" close=\")\">\n" +
//            "            #{id}\n" +
//            "        </foreach> " +
//            "</script>")
//    public List<QwSopRuleTimeVO> executeSopByIds(String[] ids);

    @DataSource(DataSourceType.SOP)
    public int updateStatusQwSopById(@Param("ids") String[] ids);


    @DataSource(DataSourceType.SOP)
    List<QwSop> selectStatusQwSopById(@Param("ids") String[] ids);


//    @Select("<script> " +
//            "SELECT\n" +
//            "\tqs.*,\n" +
//            "\tqst.name AS temp_name,\n" +
//            "\tqst.setting AS temp_setting,\n" +
//            "\tqst.status AS temp_status,\n" +
//            "\tqst.gap AS temp_gap,\n" +
//            "\tqst.sort AS temp_sort,\n" +
//            "\tqst.create_time AS temp_create_time,\n" +
//            "\tqst.create_by AS temp_create_by,\n" +
//            "\tqst.corp_id AS temp_company_id\n" +
//            "FROM\n" +
//            "\tqw_sop qs\n" +
//            "\tLEFT JOIN qw_sop_temp qst ON qs.temp_id = qst.id \n" +
//            "WHERE FIND_IN_SET(#{map.qwUserId},qs.qw_user_ids) " +
//            "and qs.corp_id=#{map.corpId} " +
//            "and qst.status = 1 " +
//            "and (\n" +
//            "        qs.STATUS IN (2, 3, 4)\n" +
//            "    ) " +
//            "and qs.is_auto_sop = 1 " +
//            "and qs.send_type = 2 " +
//            "    and (\n" +
//            "        <foreach collection='map.tagsIdsSelectList' item='item' index='index' separator=' and '> \n" +
//            "            find_in_set( #{item}, REGEXP_REPLACE(qs.tags, '[\"\\\\[\\\\]]', '') )\n" +
//            "        </foreach>\n" +
//            "    )\n" +
//            "</script>")


    @DataSource(DataSourceType.SOP)
    List<QwSopRuleTimeVO> selectQwSopByQwUserId(@Param("map") QwSopAutoByTags qwSopAutoByTags);

    @DataSource(DataSourceType.SOP)
    List<QwSopRuleTimeVO> selectQwSopAutoByTagsByForeach(@Param("map") QwSopAutoByTags qwSopAutoByTags);

    @DataSource(DataSourceType.SOP)
    List<QwSopRuleTimeVO> selectQwSopAutoByTagsByForeachNotAuto(@Param("map") QwSopAutoByTags qwSopAutoByTags);

    @DataSource(DataSourceType.SOP)
    List<QwSopRuleTimeVO> selectQwSopAutoByTagsByForeachRemoveNotAuto(@Param("map") QwSopAutoByTags qwSopAutoByTags);

//    @Select("SELECT\n" +
//            "\tqs.*,\n" +
//            "\tqst.name AS temp_name,\n" +
//            "\tqst.setting AS temp_setting,\n" +
//            "\tqst.status AS temp_status,\n" +
//            "\tqst.gap AS temp_gap,\n" +
//            "\tqst.sort AS temp_sort,\n" +
//            "\tqst.create_time AS temp_create_time,\n" +
//            "\tqst.create_by AS temp_create_by,\n" +
//            "\tqst.corp_id AS temp_company_id\n" +
//            "FROM\n" +
//            "\tqw_sop qs\n" +
//            "\tLEFT JOIN qw_sop_temp qst ON qs.temp_id = qst.id \n " +
//            "where qs.id= #{id} ")
    @DataSource(DataSourceType.SOP)
    QwSopRuleTimeVO selectQwSopByClickHouseId(@Param("id") String id);

    @DataSource(DataSourceType.SOP)
    void updateMinSendStatus(@Param("id") String id);

    @DataSource(DataSourceType.SOP)
    void updateMaxSendStatus(@Param("id") String id);

    @DataSource(DataSourceType.SOP)
    List<QwSop> selectAiChatQwSopList(QwSop qwSop);


    @DataSource(DataSourceType.SOP)
    List<QwSop> selectChatQwSopList();

    @DataSource(DataSourceType.SOP)
    List<QwSopRuleTimeVO> selectQwAiSopAutoByTagsByForeach(@Param("map") QwSopAutoByTags qwSopAutoByTags);
    @DataSource(DataSourceType.SOP)
    List<QwSop> selectByTemplateId(@Param("tempId") String tempId);

    List<QwSop> selectWxSop();
    @DataSource(DataSourceType.SOP)
    List<QwSop> selectQwSopByIsRating();
    @DataSource(DataSourceType.SOP)
    @Select("select * FROM qw_sop where is_rating = 1 and send_type in(2,3) and min_conversion_day is not null and max_conversion_day is not null order by create_time desc")
    List<QwSop> selectQwSopByIsRatingNotNull();
}
