package com.bonus.canteen.core.allocation.canteen.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonus.canteen.core.allocation.canteen.dto.AllocCanteenModifyDTO;
import com.bonus.canteen.core.allocation.canteen.model.AllocCanteen;
import com.bonus.canteen.core.allocation.canteen.param.AllocCanteenStallPageParam;
import com.bonus.canteen.core.allocation.canteen.vo.AllocCanteenStallVO;
import com.bonus.canteen.core.allocation.canteen.vo.AllocCanteenVO;
import com.bonus.canteen.core.auth.config.LeNiuDataPermission;
import com.bonus.canteen.core.auth.enums.DataPermissionTypeEnum;
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 java.util.List;

@Mapper
public interface AllocCanteenMapper extends BaseMapper<AllocCanteen> {
    List<Long> getCanteenIdListByAreaIdList(@Param("areaIdList") List<Long> areaIdList);

    @Select({"SELECT canteen_id, canteen_name FROM alloc_canteen WHERE if_del = #{ifDel} AND canteen_type = #{canteenType}"})
    List<AllocCanteenStallVO> listAllCanteenStall(@Param("ifDel") Integer ifDel, @Param("canteenType") Integer canteenType);

    @Select({"SELECT area_id FROM alloc_canteen WHERE canteen_id = #{canteenId}"})
    Long getAreaId(Long canteenId);

    @LeNiuDataPermission.List({@LeNiuDataPermission(
            alias = "t1",
            permissionType = DataPermissionTypeEnum.PERMISSION_CANTEEN
    ), @LeNiuDataPermission(
            alias = "t2",
            permissionType = DataPermissionTypeEnum.PERMISSION_AREA
    )})
    Page<AllocCanteenVO> pageCanteen(@Param("page") Page<AllocCanteenVO> page, @Param("param") AllocCanteenStallPageParam param);

    @Select({"SELECT canteen_id, area_id FROM alloc_canteen WHERE if_del = 2 AND canteen_num IS NULL ORDER BY id ASC"})
    List<AllocCanteen> listCanteenNumBlank();

    @Update({"UPDATE alloc_canteen SET canteen_num = #{canteenNum} WHERE canteen_id = #{canteenId}"})
    void updateNumById(@Param("canteenNum") String canteenNum, @Param("canteenId") Long canteenId);

    @Select({"SELECT MAX(canteen_num) FROM alloc_canteen WHERE if_del = 2 AND area_id = #{areaId}"})
    String getLatestCanteenNum(Long areaId);

    void updateByCanteenId(@Param("dto") AllocCanteenModifyDTO canteenModifyDTO, @Param("canteenId") Long canteenId);

    @Select({"SELECT if_book FROM alloc_canteen WHERE canteen_id = #{canteenId}"})
    Integer getIfBook(Long canteenId);

    @Select({"SELECT canteen_num FROM alloc_canteen WHERE canteen_id = #{canteenId}"})
    String getCanteenNumById(Long canteenId);

    @Select({"SELECT if_reserve FROM alloc_canteen WHERE canteen_id = #{canteenId}"})
    Integer getIfReserve(Long canteenId);

    @LeNiuDataPermission.List({@LeNiuDataPermission(
            alias = "t1",
            permissionType = DataPermissionTypeEnum.PERMISSION_CANTEEN
    ), @LeNiuDataPermission(
            alias = "t1",
            permissionType = DataPermissionTypeEnum.PERMISSION_AREA
    )})
    List<AllocCanteenVO> listAuthCanteen();

}
