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.AllocStallModifyDTO;
import com.bonus.canteen.core.allocation.canteen.model.AllocStall;
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.AllocStallVO;
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 AllocStallMapper extends BaseMapper<AllocStall> {
    @Select({"SELECT stall_id, stall_name FROM alloc_stall WHERE if_del = 2 AND canteen_id = #{canteenId}"})
    List<AllocCanteenStallVO.StallVO> listAllCanteenStall(@Param("canteenId") Long canteenId);

    @Select({"SELECT t2.business_state FROM alloc_stall t1 INNER JOIN alloc_canteen t2 ON t1.canteen_id = t2.canteen_id  WHERE stall_id = #{stallId}"})
    Integer getCanteenStateByStallId(Long stallId);

    @LeNiuDataPermission.List({@LeNiuDataPermission(
            alias = "t1",
            permissionType = DataPermissionTypeEnum.PERMISSION_STALL
    ), @LeNiuDataPermission(
            alias = "t6",
            permissionType = DataPermissionTypeEnum.PERMISSION_AREA
    )})
    Page<AllocStallVO> pageStall(@Param("page") Page<AllocStallVO> page, @Param("param") AllocCanteenStallPageParam param);

    @Select({"SELECT stall_id, canteen_id FROM alloc_stall WHERE if_del = 2 AND stall_num IS NULL ORDER BY id ASC"})
    List<AllocStall> listStallNumBlank();

    @Update({"UPDATE alloc_stall SET stall_num = #{stallNum} WHERE stall_id = #{stallId}"})
    void updateNumById(@Param("stallNum") String stallNum, @Param("stallId") Long stallId);

    @Select({"SELECT MAX(stall_num) FROM alloc_stall WHERE if_del = 2 AND canteen_id = #{canteenId}"})
    String getLatestStallNum(Long canteenId);

    @Select({"SELECT canteen_id FROM alloc_stall WHERE stall_id = #{stallId}"})
    Long getCanteenId(@Param("stallId") Long stallId);

    void updateByStallId(@Param("dto") AllocStallModifyDTO modifyDTO, @Param("stallId") Long stallId);

    @Select({"SELECT stall_num FROM alloc_stall WHERE stall_id = #{stallId}"})
    String getStallNumById(Long stallId);

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