package com.fs.qw.mapper;

import com.fs.qw.domain.QwExternalContactTransferLog;
import com.fs.qw.param.QwExternalContactTransferLogParam;
import com.fs.qw.vo.QwExternalContactTransferLogListVO;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 转接记录Mapper接口
 *
 * @author fs
 * @date 2024-06-27
 */
@Repository
public interface QwExternalContactTransferLogMapper
{
    /**
     * 查询转接记录
     *
     * @param id 转接记录主键
     * @return 转接记录
     */
    public QwExternalContactTransferLog selectQwExternalContactTransferLogById(Long id);

    /**
     * 查询转接记录列表
     *
     * @param qwExternalContactTransferLog 转接记录
     * @return 转接记录集合
     */
    public List<QwExternalContactTransferLog> selectQwExternalContactTransferLogList(QwExternalContactTransferLog qwExternalContactTransferLog);
    public QwExternalContactTransferLog selectQwExternalContactTransferLogListByCheck(QwExternalContactTransferLog qwExternalContactTransferLog);
    public QwExternalContactTransferLog selectQwExternalContactTransferLogListByCheckHand(QwExternalContactTransferLog qwExternalContactTransferLog);

    /**
     * 新增转接记录
     *
     * @param qwExternalContactTransferLog 转接记录
     * @return 结果
     */
    public int insertQwExternalContactTransferLog(QwExternalContactTransferLog qwExternalContactTransferLog);

    /**
     * 修改转接记录
     *
     * @param qwExternalContactTransferLog 转接记录
     * @return 结果
     */
    public int updateQwExternalContactTransferLog(QwExternalContactTransferLog qwExternalContactTransferLog);

    /**
     * 删除转接记录
     *
     * @param id 转接记录主键
     * @return 结果
     */
    public int deleteQwExternalContactTransferLogById(Long id);

    /**
     * 批量删除转接记录
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteQwExternalContactTransferLogByIds(Long[] ids);

//    @Select({"<script> " +
//            "select l.*,c.`name`,cu.nick_name  companyUserNickName from qw_external_contact_transfer_log l " +
//            "LEFT JOIN qw_external_contact c ON c.id=l.external_contact_id " +
//            "LEFT JOIN company_user cu ON cu.user_id = l.company_user_id "+
//            "<where>  \n" +
//            "            <if test=\"corpId != null  and corpId != ''\"> and l.corp_id = #{corpId}</if>\n" +
//            "            <if test=\"companyId != null \"> and l.company_id = #{companyId}</if>\n" +
//            "            <if test=\"companyUserId != null \"> and l.company_user_id = #{companyUserId}</if>\n" +
//            "            <if test=\"externalUserId != null  and externalUserId != ''\"> and l.external_user_id = #{externalUserId}</if>\n" +
//            "            <if test=\"companyUserNickName != null  and companyUserNickName != ''\"> and l.cu.nick_name = #{companyUserNickName}</if>\n" +
//            "            <if test=\"customerId != null \"> and l.customer_id = #{customerId}</if>\n" +
//            "            <if test=\"externalContactId != null \"> and l.external_contact_id = #{externalContactId}</if>\n" +
//            "            <if test=\"status != null  and status != ''\"> and l.status = #{status}</if>\n" +
//            "        </where>"+
//            "</script>"})
//    List<QwExternalContactTransferLogListVO> selectQwExternalContactTransferLogListVO(QwExternalContactTransferLogParam qwExternalContactTransferLog);

    @Select({"<script> " +
            "select l.*,c.`name`,qu.qw_user_name as companyUserNickName ,qur.qw_user_name as handoverQwUserName , qd.dept_name as deptName from qw_external_contact_transfer_log l " +
            "LEFT JOIN qw_external_contact c ON c.id=l.external_contact_id " +
            "LEFT JOIN qw_user qu ON qu.qw_user_id = l.takeover_user_id and qu.corp_id = l.corp_id " +
            "LEFT JOIN qw_user qur ON qur.qw_user_id = l.handover_user_id and qur.corp_id = l.corp_id " +
            "LEFT join qw_dept qd on qd.dept_id=qu.department and qd.corp_id=qu.corp_id "+
            "<where>  \n" +
            "            <if test=\"corpId != null  and corpId != ''\"> and l.corp_id = #{corpId}</if>\n" +
            "            <if test=\"companyId != null \"> and l.company_id = #{companyId}</if>\n" +
            "            <if test=\"companyUserId != null \"> and l.company_user_id = #{companyUserId}</if>\n" +
            "            <if test=\"externalUserId != null  and externalUserId != ''\"> and l.external_user_id = #{externalUserId}</if>\n" +
            "            <if test=\"companyUserNickName != null  and companyUserNickName != ''\"> and qu.qw_user_name = #{companyUserNickName}</if>\n" +
            "            <if test=\"customerId != null \"> and l.customer_id = #{customerId}</if>\n" +
            "            <if test=\"externalContactId != null \"> and l.external_contact_id = #{externalContactId}</if>\n" +
            "            <if test=\"status != null  and status != ''\"> and l.status = #{status}</if>\n" +
            "        </where> " +
            "order by l.create_time desc "+
            "</script>"})
    List<QwExternalContactTransferLogListVO> selectQwExternalContactTransferLogListVO(QwExternalContactTransferLogParam qwExternalContactTransferLog);

    @Update("UPDATE qw_external_contact_transfer_log SET status = 1  WHERE `status`=2 and TIMESTAMPDIFF(HOUR, create_time, NOW()) > 24")
    void updateQwExternalContactTransferLogByStatus();

    @Update("UPDATE qw_external_contact SET transfer_status =1 where id in (select external_contact_id from qw_external_contact_transfer_log WHERE `status`=2 and TIMESTAMPDIFF(HOUR, create_time, NOW()) > 24)")
    void updateQwExternalContactByStatus();

}
