package com.fs.statis.mapper;

import com.fs.sop.domain.SopUserLogs;
import com.fs.statis.domain.FsStatisPeriodWatch;
import com.fs.statis.dto.StatsWatchLogPageListDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Delete;

import java.time.LocalDate;
import java.util.List;

/**
 * fs_statis_period_watch表 Mapper 接口
 *
 * @author AutoGenerator
 * @since now
 */
@Mapper
public interface FsStatisPeriodWatchMapper {

    /**
     * 根据主键查询数据
     *
     * @param id 主键
     * @return 实体对象
     */
    @Select("SELECT id, period_id, period_num, not_reg_num, registered_num, reg_rate, " +
            "watch_completed_rate, offline_total, offline_not_reg_num, offline_not_watch_num, " +
            "online_total, online_rate, online_watch_completed_rate, online_watch_not_completed, " +
            "online_watch_completed, data_date " +
            "FROM fs_statis_period_watch WHERE id = #{id}")
    FsStatisPeriodWatch selectById(@Param("id") Integer id);

    /**
     * 新增数据
     *
     * @param fsStatisPeriodWatch 实体对象
     * @return 影响行数
     */
    @Insert("INSERT INTO fs_statis_period_watch (id, period_id, period_num, not_reg_num, registered_num, " +
            "reg_rate, watch_completed_rate, offline_total, offline_not_reg_num, offline_not_watch_num, " +
            "online_total, online_rate, online_watch_completed_rate, online_watch_not_completed, " +
            "online_watch_completed, data_date) " +
            "VALUES (#{id}, #{periodId}, #{periodNum}, #{notRegNum}, #{registeredNum}, #{regRate}, " +
            "#{watchCompletedRate}, #{offlineTotal}, #{offlineNotRegNum}, #{offlineNotWatchNum}, " +
            "#{onlineTotal}, #{onlineRate}, #{onlineWatchCompletedRate}, #{onlineWatchNotCompleted}, " +
            "#{onlineWatchCompleted}, #{dataDate})")
    // 如果id是自增的，可以加上 @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn="id")
    int insert(FsStatisPeriodWatch fsStatisPeriodWatch);

    /**
     * 根据主键更新数据
     *
     * @param fsStatisPeriodWatch 实体对象
     * @return 影响行数
     */
    @Update("<script>" +
            "UPDATE fs_statis_period_watch " +
            "<set>" +
            "  <if test='periodId != null'>period_id = #{periodId},</if>" +
            "  <if test='periodNum != null'>period_num = #{periodNum},</if>" +
            "  <if test='notRegNum != null'>not_reg_num = #{notRegNum},</if>" +
            "  <if test='registeredNum != null'>registered_num = #{registeredNum},</if>" +
            "  <if test='regRate != null'>reg_rate = #{regRate},</if>" +
            "  <if test='watchCompletedRate != null'>watch_completed_rate = #{watchCompletedRate},</if>" +
            "  <if test='offlineTotal != null'>offline_total = #{offlineTotal},</if>" +
            "  <if test='offlineNotRegNum != null'>offline_not_reg_num = #{offlineNotRegNum},</if>" +
            "  <if test='offlineNotWatchNum != null'>offline_not_watch_num = #{offlineNotWatchNum},</if>" +
            "  <if test='onlineTotal != null'>online_total = #{onlineTotal},</if>" +
            "  <if test='onlineRate != null'>online_rate = #{onlineRate},</if>" +
            "  <if test='onlineWatchCompletedRate != null'>online_watch_completed_rate = #{onlineWatchCompletedRate},</if>" +
            "  <if test='onlineWatchNotCompleted != null'>online_watch_not_completed = #{onlineWatchNotCompleted},</if>" +
            "  <if test='onlineWatchCompleted != null'>online_watch_completed = #{onlineWatchCompleted},</if>" +
            "  <if test='dataDate != null'>data_date = #{dataDate},</if>" +
            "</set>" +
            "WHERE id = #{id}" +
            "</script>")
    int updateById(FsStatisPeriodWatch fsStatisPeriodWatch);

    /**
     * 根据主键删除数据
     *
     * @param id 主键
     * @return 影响行数
     */
    @Delete("DELETE FROM fs_statis_period_watch WHERE id = #{id}")
    int deleteById(@Param("id") Integer id);

    /**
     * 查询所有数据
     *
     * @return 实体对象列表
     */
    List<FsStatisPeriodWatch> selectAll();

    List<FsStatisPeriodWatch> queryList(StatsWatchLogPageListDTO param);

    /**
     * 获取每个sop任务对应的营期记录
     * @return
     */
    List<SopUserLogs> selectRecords(@Param("previousDay") LocalDate previousDay);
}
