/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.dm.foodCate.rest;

import cn.hutool.core.collection.CollectionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.config.FileProperties;
import me.zhengjie.modules.dm.foodCate.domain.DmFoodCate;
import me.zhengjie.modules.dm.foodCate.service.DmFoodCateService;
import me.zhengjie.modules.dm.foodCate.service.dto.DmFoodCateDto;
import me.zhengjie.modules.dm.foodCate.service.dto.DmFoodCateQueryCriteria;
import me.zhengjie.modules.dm.foodCate.service.mapstruct.DmFoodCateMapper;
import me.zhengjie.modules.system.domain.Menu;
import me.zhengjie.modules.system.service.dto.MenuDto;
import me.zhengjie.modules.system.service.dto.MenuQueryCriteria;
import me.zhengjie.utils.PageUtil;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@RestController
@RequiredArgsConstructor
@Api(tags = "foodCate管理")
@RequestMapping("/api/dmFoodCate")
public class DmFoodCateController {

    private final DmFoodCateService dmFoodService;
    private final DmFoodCateMapper dmFoodCateMapper;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('dmFoodCate:list')")
    public void download(HttpServletResponse response, DmFoodCateQueryCriteria criteria) throws IOException {
        dmFoodService.download(dmFoodService.queryAll(criteria), response);
    }

//    @GetMapping
//    @Log("查询dmFoodCate")
//    @ApiOperation("查询dmFoodCate")
//    @PreAuthorize("@el.check('dmFoodCate:list')")
//    public ResponseEntity<Object> query(DmFoodCateQueryCriteria criteria, Pageable pageable){
//        return new ResponseEntity<>(dmFoodService.queryAll(criteria,pageable),HttpStatus.OK);
//    }

    @GetMapping
    @Log("查询dmFoodCate")
    @ApiOperation("查询dmFoodCate")
    @PreAuthorize("@el.check('dmFoodCate:list')")
    public ResponseEntity<Object> query(DmFoodCateQueryCriteria criteria, Pageable pageable) throws Exception {
        return new ResponseEntity<>(dmFoodService.queryAll(criteria,true,pageable),HttpStatus.OK);
    }

    @ApiOperation("返回全部的菜品分类")
    @Log("返回全部的菜品分类")
    @GetMapping(value = "/lazy")
    @PreAuthorize("@el.check('dmFoodCate:list')")
    public ResponseEntity<Object> query(@RequestParam Integer pid){
        return new ResponseEntity<>(dmFoodService.getMenus(pid),HttpStatus.OK);
    }

    @ApiOperation("查询菜品分类:根据ID获取同级与上级数据")
    @Log("查询菜品分类:根据ID获取同级与上级数据")
    @PostMapping("/superior")
    @PreAuthorize("@el.check('dmFoodCate:list')")
    public ResponseEntity<Object> getSuperior(@RequestBody List<Integer> ids) {
        Set<DmFoodCateDto> foodCateDtos = new LinkedHashSet<>();
        if(CollectionUtil.isNotEmpty(ids)){
            for (Integer id : ids) {
                DmFoodCateDto dto = dmFoodService.findById(id);
                foodCateDtos.addAll(dmFoodService.getSuperior(dto, new ArrayList<>()));
            }
            return new ResponseEntity<>(dmFoodService.buildTree(new ArrayList<>(foodCateDtos)),HttpStatus.OK);
        }
        return new ResponseEntity<>(dmFoodService.getMenus(null),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增dmFoodCate")
    @ApiOperation("新增dmFoodCate")
    @PreAuthorize("@el.check('dmFoodCate:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody DmFoodCate resources){
        dmFoodService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改dmFoodCate")
    @ApiOperation("修改dmFoodCate")
    @PreAuthorize("@el.check('dmFoodCate:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody DmFoodCate resources){
        dmFoodService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除dmFoodCate")
    @ApiOperation("删除dmFoodCate")
    @PreAuthorize("@el.check('dmFoodCate:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Set<Integer> ids) {
        Set<DmFoodCate> foodCateSet = new HashSet<>();
        for (Integer id : ids) {
            List<DmFoodCateDto> menuList = dmFoodService.getMenus(id);
            foodCateSet.add(dmFoodService.findOne(id));
            foodCateSet = dmFoodService.getChildMenus(dmFoodCateMapper.toEntity(menuList), foodCateSet);
        }
        dmFoodService.deleteAll(foodCateSet);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}