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


import cn.com.ty.lift.ud.operation.mapper.entity.CommissionSettingEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 抽成设置  -Repository
 * 
 * @author Xson
 * @date 2020-05-15
 */
public interface CommissionSettingMapper extends BaseMapper<CommissionSettingEntity> {

    /**
     * 查询最新的版本
     * @return
     */
    @Select("select * from commission_setting where version_id =(select max(version_id) from commission_setting" +
            " where examine_flag = 1) ")
    CommissionSettingEntity getLatestVersion();


    /**
     * 审批通过
     * @param id
     */
    @Update("update commission_setting set examine_flag = 1 , not_examine_reason = '' where id = #{id} ")
    void examineById(Long id);

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

    /**
     * 设置版本号
     * @param versionId
     * @param id
     * @return
     */
    @Update("update commission_setting set version_id = #{version_id} , examine_flag = 1," +
            " not_examine_reason = '' where id = #{id} ")
    int updateVersionId(@Param("version_id") Integer versionId, @Param("id")Long id);

    /**
     * 查询所有待审核的
     * @return
     */
    @Select("select * from commission_setting where examine_flag = 0")
    List<CommissionSettingEntity> getAllStay();

    /**
     * 查询历史版版本（最新版本的上一个版本）
     * @return
     */
    @Select("select * from commission_setting where version_id<(select max(version_id) from commission_setting) " +
            " order by version_id Desc limit 0,1")
    CommissionSettingEntity getHistoryVersion();

    /**
     * 查询所有待提交的
     * @return
     */
    @Select("select * from commission_setting where examine_flag = 3")
    List<CommissionSettingEntity>  getAllCommit();

    /**
     * 提交
     * @param id
     * @return
     */
    @Update("update commission_setting set examine_flag = 0  where id = #{id} ")
    int submit(Long id);
}
