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

import com.bonus.core.allocation.api.DzAllocCanteenAuthApi;
import com.bonus.core.allocation.canteen.service.AllocCanteenService;
import com.bonus.core.allocation.canteen.vo.AllocCanteenStallVO;
import com.bonus.core.order.common.dto.RequestHeaderDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 定制 食堂档口相关控制器
 *
 * @author shihao.li
 * @date 2024/8/23 11:24
 */
@Slf4j
@RestController
@RequestMapping("/api/v2/alloc/canteen")
@Api(tags = "lsh_食堂档口相关控制器")
public class DzAllocCanteenController {

    @Autowired
    @Lazy
    private AllocCanteenService allocCanteenService;
    @Autowired
    @Lazy
    private DzAllocCanteenAuthApi dzAllocCanteenAuthApi;


    /**
     * 查询所有食堂档口列表
     * 小程序使用
     **/
    @ApiOperation("查询所有食堂档口列表")
    @PostMapping("/list-all-canteen-stall")
    public List<AllocCanteenStallVO> listAllCanteenStall(@RequestHeader Map<String, String> headers) {
        List<AllocCanteenStallVO> allocCanteenStallList = allocCanteenService.listAllCanteenStall();
        /** 定制逻辑 **/
        RequestHeaderDTO headerDTO = RequestHeaderDTO.of(headers);
        List<Long> canteenIdList = dzAllocCanteenAuthApi.getAuthCanteenIdsByOpenId(headerDTO.getOpenid(), headerDTO.getSourceType(), null);
        // 无权限
        if (null == canteenIdList) {
            return new ArrayList<>();
        }
        // 有全部权限
        if (canteenIdList.isEmpty()) {
            return allocCanteenStallList;
        }
        // 有部分权限
        List<AllocCanteenStallVO> resultList = new ArrayList<>();
        for (AllocCanteenStallVO allocCanteenStallVO : allocCanteenStallList) {
            if (canteenIdList.contains(allocCanteenStallVO.getCanteenId())) {
                resultList.add(allocCanteenStallVO);
            }

        }
        return resultList;
    }

}
