package com.fs.qw.mapper;

import com.fs.qw.domain.QwExternalContactCrm;
import com.fs.qw.param.QwCrmCourseStudyDetailParams;
import com.fs.qw.param.QwCrmCourseStudyParams;
import com.fs.qw.vo.QwCrmCourseStudyVO;
import com.fs.qw.vo.QwExternalContactCrmVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 企微客户-绑CRMMapper接口
 *
 * @author fs
 * @date 2024-08-09
 */
public interface QwExternalContactCrmMapper
{
    /**
     * 查询企微客户-绑CRM
     *
     * @param id 企微客户-绑CRM主键
     * @return 企微客户-绑CRM
     */
    public QwExternalContactCrm selectQwExternalContactCrmById(Long id);

    /**
     * 查询企微客户-绑CRM列表
     *
     * @param qwExternalContactCrm 企微客户-绑CRM
     * @return 企微客户-绑CRM集合
     */
    @Select({"<script> " +
            "select ec.* from qw_external_contact ec " +
            "<where>  \n" +
            "            <if test=\"externalUserId != null  and externalUserId != ''\"> and ec.external_user_id = #{externalUserId}</if>\n" +
            "            <if test=\"name != null  and name != ''\"> and ec.name like concat( #{name}, '%')</if>\n" +
            "            <if test=\"avatar != null  and avatar != ''\"> and ec.avatar = #{avatar}</if>\n" +
            "            <if test=\"companyId != null \"> and ec.company_id = #{companyId}</if>\n" +
            "            <if test=\"customerId != null \"> and ec.customer_id = #{customerId}</if>\n" +
            "        </where>"+
            "order by ec.id desc"+
            "</script>"})
    public List<QwExternalContactCrmVO> selectQwExternalContactCrmListVO(QwExternalContactCrm qwExternalContactCrm);

    /**
     * 新增企微客户-绑CRM
     *
     * @param qwExternalContactCrm 企微客户-绑CRM
     * @return 结果
     */
    public int insertQwExternalContactCrm(QwExternalContactCrm qwExternalContactCrm);

    /**
     * 修改企微客户-绑CRM
     *
     * @param qwExternalContactCrm 企微客户-绑CRM
     * @return 结果
     */
    public int updateQwExternalContactCrm(QwExternalContactCrm qwExternalContactCrm);
    /**
     * 新增或修改
     */
    public int insertOrUpdateQwExternalContactCrm(QwExternalContactCrm qwExternalContactCrm);

    /**
     * 删除企微客户-绑CRM
     *
     * @param id 企微客户-绑CRM主键
     * @return 结果
     */
    public int deleteQwExternalContactCrmById(Long id);

    /**
     * 批量删除企微客户-绑CRM
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteQwExternalContactCrmByIds(Long[] ids);

    @Select("<script>" +
            "SELECT qecr.external_user_name, qecr.external_user_id, qecr.id as  contact_crm_id, qecr.avatar, " +
            "qecr.customer_id, qecr.company_id, cc.mobile, fucs.study_id, fucs.course_id, fucs.user_id, fucs.period_total, " +
            "fucs.period_study, fucs.status, fucs.create_time as study_create_time, fuc.course_name " +
            "FROM " +
            "qw_external_contact_crm qecr " +
            "LEFT JOIN crm_customer cc ON qecr.customer_id = cc.customer_id " +
            "LEFT JOIN fs_user_course_study fucs on fucs.user_id=cc.user_id " +
            "left join fs_user_course  fuc on fucs.course_id=fuc.course_id " +
            "<where>  " +
                "<if test = ' maps.externalUserName!=null and maps.externalUserName != \"\" '> " +
                "and qecr.external_user_name like concat('%', #{maps.externalUserName}, '%') " +
                "</if>" +
                "<if test = ' maps.externalUserId!=null and maps.externalUserId != \"\" '> " +
                "and qecr.external_user_id =#{maps.externalUserId} " +
                "</if>" +
                "<if test = ' maps.userId!=null and maps.userId != \"\" '> " +
                "and fucs.user_id =#{maps.userId} " +
                "</if>" +
                "<if test = ' maps.courseName!=null and maps.courseName != \"\" '> " +
                "and fuc.course_name like concat('%', #{maps.courseName}, '%') " +
                "</if>" +
                "<if test = ' maps.mobile!=null and maps.mobile != \"\" '> " +
                "and cc.mobile like concat('%', #{maps.mobile}, '%') " +
                "</if>" +
                "<if test = ' maps.companyId!=null and maps.companyId != \"\" '> " +
                "and qecr.company_id = #{maps.companyId}" +
                "</if>" +
                "<if test = ' maps.status !=null and maps.status!=0 and maps.status!=99 '> " +
                "and fucs.status = #{maps.status} " +
                "</if>" +
                "<if test = ' maps.status==0 '> " +
                "and fucs.study_id is null " +
                "</if>" +
            "</where>"+
            "</script>")
    public List<QwCrmCourseStudyVO> getQwCrmCourseStudyList(@Param("maps") QwCrmCourseStudyParams qwFsUserParam);


    @Select("<script>" +
            "SELECT qecr.external_user_name, qecr.external_user_id, qecr.id as  contact_crm_id, qecr.avatar, qecr.customer_id, qecr.company_id, cc.mobile, " +
            "fucs.study_id, fucs.course_id, fucs.user_id, fucs.period_total, fucs.period_study, fucs.status, fucs.create_time as study_create_time, " +
            "fucsl.log_id , fucsl.course_name , fucsl.video_id , fucsl.video_name , fucsl.current_times, fucsl.create_time as log_create_time, " +
            "fucsl.times,fucsl.rate , fuccv.course_sort " +
            "FROM " +
            "qw_external_contact_crm qecr " +
            "LEFT JOIN crm_customer cc ON qecr.customer_id = cc.customer_id " +
            "LEFT JOIN fs_user_course_study fucs on fucs.user_id=cc.user_id " +
            "LEFT JOIN fs_user_course_study_log fucsl on fucsl.study_id=fucs.study_id " +
            "LEFT JOIN fs_user_course_video fuccv on fuccv.video_id=fucsl.video_id " +
            "<where>  " +
            "<if test = ' maps.externalUserName!=null and maps.externalUserName != \"\" '> " +
            "and qecr.external_user_name like concat('%', #{maps.externalUserName}, '%') " +
            "</if>" +
            "<if test = ' maps.externalUserId!=null and maps.externalUserId != \"\" '> " +
            "and qecr.external_user_id =#{maps.externalUserId} " +
            "</if>" +
            "<if test = ' maps.companyId!=null and maps.companyId != \"\" '> " +
            "and qecr.company_id = #{maps.companyId}" +
            "</if>" +
            "<if test = ' maps.courseName!=null and maps.courseName != \"\" '> " +
            "and fucsl.course_name like concat( #{maps.courseName}, '%') " +
            "</if>" +
            "<if test = ' maps.studyId!=null and maps.studyId != \"\" '> " +
            "and fucs.study_id =#{maps.studyId} " +
            "</if>" +
            "<if test = ' maps.userId!=null and maps.userId != \"\" '> " +
            "and fucs.user_id =#{maps.userId} " +
            "</if>" +
            "<if test = ' maps.mobile!=null and maps.mobile != \"\" '> " +
            "and cc.mobile like concat('%', #{maps.mobile}, '%') " +
            "</if>" +
            "<if test = ' maps.companyId!=null and maps.companyId != \"\" '> " +
            "and qecr.company_id = #{maps.companyId}" +
            "</if>" +
            "<if test = ' maps.status !=null and maps.status!=0 '> " +
            "and fucs.status = #{maps.status} " +
            "</if>" +
            "<if test = ' maps.status==0 '> " +
            "and fucs.study_id is null " +
            "</if>" +
            "</where>"+
            "</script>")
    public List<QwCrmCourseStudyVO> getQwCrmCourseStudyDetail(@Param("maps") QwCrmCourseStudyDetailParams studyDetailParams);
    @Select("select openid from qw_external_contact_crm where external_user_id =#{qwExtId} and  openid is not null limit 1")
    String selectQwExternalContactCrmByExtId(String qwExtId);
}
