package cn.com.ty.lift.ud.question.mapper;


import cn.com.ty.lift.ud.question.controller.query.QuestionBankAppQuery;
import cn.com.ty.lift.ud.question.controller.query.QuestionBankQuery;
import cn.com.ty.lift.ud.question.mapper.entity.QuestionBankEntity;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * 题库 -Repository
 *
 * @author Xson
 * @date 2020-04-25
 */
public interface QuestionBankMapper extends BaseMapper<QuestionBankEntity> {

    /**
     * 批量上架或者下架
     *
     * @param ids
     * @param status
     */
    @Update(value = "update question_bank set statuz=#{status} where id in (#{ids})")
    void updateByIds(List<Long> ids, Integer status);


    /**
     * 修改当前问题热门状态为热门或者普通
     *
     * @param id
     * @param flag
     */
    @Update(value = "update question_bank set hot_flag= #{flag} where id = #{id} ")
    void toUpOrDown(@Param("id") Long id,@Param("flag")Integer flag);

    /**
     * 题库审批，修改审批状态为1，默认有效期6个月
     *
     * @param id
     */
    @Update(value = "update question_bank set examine_flag= 1,valid_period = 6 ," +
            "statuz = 1, not_examine_reason = '' where id = #{id}")
    void examineById(Long id);

    /**
     * 后台分页查询
     *
     * @param condition
     * @param page
     * @return
     */
    List<QuestionBankEntity> selectByQuery(@Param("ew") Wrapper condition, @Param("page") QuestionBankQuery page);

    /**
     * 查询总数量
     *
     * @param condition
     * @return
     */
    int countByQuery(@Param("ew") Wrapper condition);

    /**
     * 新增
     *
     * @param qbEntity
     */
    void insertSelective(QuestionBankEntity qbEntity);

    /**
     * 点赞数量加 1
     * @param id
     */
    @Update(value = "update question_bank set like_num= like_num + 1 where id = #{id}")
    void like(Long id);

    /**
     * 收藏数量加 1
     * @param id
     */
    @Update(value = "update question_bank set favorite_num= favorite_num + 1 where id = #{id}")
    void collect(Long id);

    /**
     * 浏览数量加 1
     * @param id
     */
    @Update(value = "update question_bank set browse_num= browse_num + 1 where id = #{id}")
    void see(Long id);

    /**
     * 打赏数量加 1
     * @param id
     * @param reward
     */
    @Update(value = "update question_bank set reward= reward + #{reward},tip_num = tip_num + 1  where id = #{id}")
    void updateReward(@Param("id") Long id, @Param("reward")BigDecimal reward);

    /**
     * 根据id查详情
     * @param id
     * @return
     */
    QuestionBankEntity getById(@Param("id") Long id);

    /**
     * 审批驳回
     * @param id
     * @param notExamineReason
     */
    @Update("update question_bank set examine_flag = 2 ,not_examine_reason = #{notExamineReason} where id = #{id} ")
    void updateInfoById(@Param("id") Long id , @Param("notExamineReason")String notExamineReason);

    /**
     * 收藏数量减 1
     * @param questionId
     */
    @Update(value = "update question_bank set favorite_num= favorite_num - 1 where id = #{questionId}")
    void noCollect(Long questionId);

    /**
     * 点赞数量减 1
     * @param questionId
     */
    @Update(value = "update question_bank set like_num= like_num - 1 where id = #{questionId}")
    void noLike(Long questionId);

    /**
     * app分页查询
     *
     * @param condition
     * @param page
     * @return
     */
    List<QuestionBankEntity> selectAppByQuery(@Param("ew") Wrapper condition, @Param("page") QuestionBankAppQuery page);

    /**
     * 查询总数量
     *
     * @param condition
     * @return
     */
    int countAppByQuery(@Param("ew") Wrapper condition);
}
