package org.ta.pddserver.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
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.ta.pddserver.entity.AreaEntity;
import org.ta.pddserver.entity.ShelvesEntity;
import org.ta.pddserver.model.api.request.AreaListRequest;

import java.util.List;

@Mapper
public interface ShelvesMapper extends BaseMapper<ShelvesEntity> {

    @Select("SELECT count(`uid`) FROM `shelves` WHERE `state` > 0")
    Integer countUsable(AreaListRequest param);

    @Select("SELECT * FROM `shelves` WHERE  `state` > 0 ORDER BY uid DESC LIMIT #{offset}, #{size}; ")
    List<ShelvesEntity> getListByRequest(AreaListRequest param);

    @Select("SELECT * FROM `shelves` WHERE  `state` > 0 AND `code` = #{code};")
    ShelvesEntity getInfoByCode(@Param("code") String code);

    @Select("SELECT * FROM `shelves` WHERE `state` > 0 AND `uid` = #{uid}")
    ShelvesEntity getInfoByUid(@Param("uid") String uid);

    @Select("SELECT * FROM `shelves` WHERE `state` > 0 ORDER BY `uid` ASC")
    List<ShelvesEntity> getList();

    @Select("SELECT `uid`, `level1`, `level2`, `level3`, `allNum`, `lateNum`, `ined` FROM `shelves` WHERE `state` > 0 ORDER BY  `allNum`-`lateNum`-`ined` DESC, `level3` ASC, `level2` ASC, `level1` ASC LIMIT 1;")
    ShelvesEntity findUsableOrdered();

    @Update("UPDATE `shelves` SET `ined` = `ined` + 1 WHERE `level1` =#{level1} AND `level2` = #{level2} AND `level3` = #{level3};")
    int updateNum(@Param("level1") String level1, @Param("level2") int level2, @Param("level3") int level3);
}
