package com.fs.qw.mapper;

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

import java.util.List;
import java.util.Map;

/**
 * 企微任务看板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.*,cu.nick_name,e.`description`,e.name,qw.qw_user_name qwUserName,e.last_watch_time 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  "+
            "  LEFT JOIN company_user cu on t.company_user_id=cu.user_id \n" +
            "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=\"title != null  and title != ''\"> and t.title like CONCAT('%',#{title},'%') </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" +
            "<if test = 'deptId != null    '>   AND (cu.dept_id = #{deptId} OR cu.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{deptId}, ancestors) )) </if>" +
            "             <if test=\"qwUserName != null and qwUserName != ''\"> and qw.qw_user_name = #{qwUserName}</if>\n" +
            "            <if test=\"cuDeptIdList != null and !cuDeptIdList.isEmpty() and  userType != '00' \">" +
            "               AND cu.dept_id IN " +
            "                   <foreach collection='cuDeptIdList' item='item' open='(' separator=',' close=')'> " +
            "                       #{item} " +
            "                   </foreach> " +
            "            </if>" +
            "order by t.score desc,e.last_watch_time ,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);

    @Select({"<script> " +
            "select t.qw_user_id,qw.qw_user_name,ANY_VALUE(cu.nick_name) companyUserName,DATE(t.create_time) createTime,\n" +
            "\t\tSUM(CASE WHEN t.status = 0 THEN 1 ELSE 0 END) AS status0,\n" +
            "    SUM(CASE WHEN t.status = 1 THEN 1 ELSE 0 END) AS status1,\n" +
            "    SUM(CASE WHEN t.status = 2 THEN 1 ELSE 0 END) AS status2,\n" +
            "    SUM(CASE WHEN t.status = 3 THEN 1 ELSE 0 END) AS status3\n" +
            "\t\tfrom qw_work_task t  LEFT JOIN qw_user qw ON qw.id = t.qw_user_id LEFT JOIN company_user cu on cu.user_id=t.company_user_id  where  t.company_id=#{companyId} " +
            "    <if test=\"companyUserId != null \"> and t.company_user_id = #{companyUserId}</if>\n" +
            "    <if test=\"companyUserName != null and companyUserName != ''\"> and cu.nick_name = #{companyUserName}</if>\n" +
            "    <if test=\"qwUserName != null and qwUserName != ''\"> and qw.qw_user_name = #{qwUserName}</if>\n" +
            " " +
            "    <if test=\"sTime != null \">  and DATE(t.create_time) &gt;= DATE(#{sTime})</if>\n" +
            "    <if test=\"eTime != null \">  and DATE(t.create_time) &lt;= DATE(#{eTime})</if>\n" +
            " " +
            " GROUP BY t.qw_user_id,DATE(t.create_time) "+
            "</script>"})
    List<QwWorkTaskAllListVO> selectQwWorkTaskAllListVO(QwWorkTaskListParam qwWorkTask);

    List<QwWorkTask> selectQwWorkTaskByExtIdAndQwUserId(QwUserVoiceLog qwUserVoiceLog);

    List<QwWorkTask> selectQwWorkTaskListByMap(@Param("params") Map<String, Object> params);

    List<UserVOs> getUserList(@Param("userId") Long userId);

    List<UserVOs> getQwUserList(@Param("userId") Long userId, @Param("qwUserId") String qwUserId);
}
