package com.fs.qw.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fs.qw.domain.QwWorkTask;
import com.fs.qw.param.QwWorkTaskListParam;
import com.fs.qw.vo.QwWorkTaskListVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 企微任务看板Mapper接口
 *
 * @author fs
 * @date 2025-03-18
 */
public interface QwWorkTaskMapper extends BaseMapper<QwWorkTask>{
    /**
     * 查询企微任务看板
     *
     * @param id 企微任务看板主键
     * @return 企微任务看板
     */
    QwWorkTask selectQwWorkTaskById(Long id);

    /**
     * 查询企微任务看板列表
     *
     * @param qwWorkTask 企微任务看板
     * @return 企微任务看板集合
     */
    List<QwWorkTask> selectQwWorkTaskList(QwWorkTask qwWorkTask);

    /**
     * 新增企微任务看板
     *
     * @param qwWorkTask 企微任务看板
     * @return 结果
     */
    int insertQwWorkTask(QwWorkTask qwWorkTask);

    /**
     * 修改企微任务看板
     *
     * @param qwWorkTask 企微任务看板
     * @return 结果
     */
    int updateQwWorkTask(QwWorkTask qwWorkTask);

    /**
     * 删除企微任务看板
     *
     * @param id 企微任务看板主键
     * @return 结果
     */
    int deleteQwWorkTaskById(Long id);

    /**
     * 批量删除企微任务看板
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    int deleteQwWorkTaskByIds(Long[] ids);

    void insertQwWorkTaskBatch(@Param("qwWorkTasks")List<QwWorkTask> qwWorkTasks);
    @Select({"<script> " +
             "select t.*,e.`name`,qw.qw_user_name from qw_work_task t LEFT JOIN qw_external_contact e on e.id=t.ext_id  LEFT JOIN qw_user qw ON qw.id = t.qw_user_id  "+
             "where DATE(t.create_time) = CURDATE()" +
            "            <if test=\"extId != null \"> and t.ext_id = #{extId}</if>\n" +
            "            <if test=\"qwUserId != null \"> and t.qw_user_id = #{qwUserId}</if>\n" +
            "            <if test=\"type != null \"> and t.type = #{type}</if>\n" +
            "            <if test=\"status != null \"> and t.status = #{status}</if>\n" +
            "            <if test=\"score != null \"> and t.score = #{score}</if>\n" +
            "            <if test=\"sopId != null  and sopId != ''\"> and t.sop_id = #{sopId}</if>\n" +
            "            <if test=\"companyId != null \"> and t.company_id = #{companyId}</if>\n" +
            "            <if test=\"companyUserId != null \"> and t.company_user_id = #{companyUserId}</if>\n" +
            " " +
            " " +
            "order by t.score desc,t.id desc "+
            "</script>"})
    List<QwWorkTaskListVO> selectQwWorkTaskListVO(QwWorkTaskListParam qwWorkTask);
    @Select("select ext_id from qw_work_task where type=2 and DATE(create_time) = CURDATE() ")
    List<Long> selectQwWorkTaskByType();

    @Select("select id,ext_id from qw_work_task where type=2 and status=0 and DATE(create_time) = CURDATE() ")
    List<QwWorkTask> selectQwWorkTaskByTypeStatus();
    @Update({
            "<script>",
            "UPDATE qw_work_task",
            "SET status = 2",
            "WHERE id IN",
            "<foreach item='id' collection='overIds' open='(' separator=',' close=')'>",
            "   #{id}",
            "</foreach>",
            "</script>"
    })
    void updateQwWorkTaskStatus(@Param("overIds")List<Long> overIds);
}
