package com.neusoft.smart.pos.inventory.controller;

import com.neusoft.smart.pos.dto.inventory.request.CreateCategoryRequest;
import com.neusoft.smart.pos.dto.inventory.request.GetCategoriesRequest;
import com.neusoft.smart.pos.dto.inventory.request.ReorderRequest;
import com.neusoft.smart.pos.dto.inventory.request.UpdateCategoryRequest;
import com.neusoft.smart.pos.dto.inventory.response.CategoryResponse;
import com.neusoft.smart.pos.dto.inventory.response.InventoryCategoryTreeResponse;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.inventory.service.InventoryGoodsCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "商品管理分类rest接口", description = "商品管理分类rest接口")
@RequestMapping(path = "/goods/category")
@Validated
public class InventoryGoodsCategoryController {
    
    @Autowired
    InventoryGoodsCategoryService categoryService;
    
    @ApiOperation(value = "类别列表接口")
    @GetMapping("")
    public ResponseData<List<CategoryResponse>> getCategories(GetCategoriesRequest request) {
        return new ResponseData<>(categoryService.selectCategories(request));
    }
    
    
    @ApiOperation(value = "创建类别接口")
    @PostMapping("")
    public ResponseData<CategoryResponse> createCategory(@RequestBody CreateCategoryRequest request) {
        return new ResponseData<>(categoryService.insertCategoryWithRoleCheck(request));
    }
    
    @ApiOperation(value = "更行类别接口")
    @PutMapping("/{id}")
    public ResponseData<Integer> updateCategory(@PathVariable Integer id, @RequestBody UpdateCategoryRequest request) {
        categoryService.updateCategory(id, request);
        return new ResponseData<>(1);
    }
    
    @ApiOperation(value = "类别重排接口")
    @PostMapping("/re-order")
    public ResponseData<Integer> reorder(@RequestBody List<ReorderRequest> request) {
        Map<Integer, Integer> orderMap = new HashMap<>();
        for (ReorderRequest item : request) {
            orderMap.put(item.getId(), item.getOrder());
        }
        categoryService.reorder(orderMap);
        return new ResponseData<>(1);
    }
    
    @ApiOperation(value = "餐饮类别删除接口")
    @DeleteMapping("/catering/{id}")
    public ResponseData<Integer> deleteCateringCategory(@PathVariable Integer id) {
        categoryService.deleteCateringCategory(id);
        return new ResponseData<>(1);
    }
    
    @ApiOperation(value = "树形类别列表接口")
    @GetMapping("/tree")
    public ResponseData<List<InventoryCategoryTreeResponse>> getCategoriesTree(GetCategoriesRequest request) {
        return new ResponseData<>(categoryService.getCategoryTree(request));
    }
}
