package com.izouma.nineth.web.collection;

import com.izouma.nineth.domain.Collection;
import com.izouma.nineth.domain.CollectionCategory;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.service.collection.CollectionCategoryService;
import com.izouma.nineth.web.BaseController;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 藏品目录表
 *
 * @author ZouYuHong
 */
@RestController
@RequestMapping("/collectionCategory")
@AllArgsConstructor
public class CollectionCategoryController extends BaseController {
    private CollectionCategoryService collectionCategoryService;

    /**
     * 查询全部目录信息
     * @param pageQuery 搜索条件
     * @return 查询结果
     */
    @PostMapping("/all")
    public Page<CollectionCategory> all(@RequestBody PageQuery pageQuery) {
        return collectionCategoryService.all(pageQuery);
    }

    @PostMapping("/categories")
    public List<CollectionCategory> categories(@RequestBody CollectionCategory params) {
        return collectionCategoryService.categories(params.getProjectId());
    }

    /**
     * 批量修改分类
     * @param category 批量修改分类
     * @return 新建的分类
     */
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/batchUpdate")
    public List<CollectionCategory> batchUpdate(@RequestBody @Validated List<CollectionCategory> category) {
        return collectionCategoryService.batchUpdate(category);
    }


    /**
     * 新建分类or 修改分类名
     * @param category 新建的分类
     * @return 新建的分类
     */
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public CollectionCategory saveOrUpdate(@RequestBody @Validated CollectionCategory category) {
        return collectionCategoryService.saveOrUpdate(category);
    }


    /**
     *
     * @param id 分类Id
     * @return 新建的分类
     */
    @GetMapping("/get/{id}")
    public CollectionCategory detail(@PathVariable Long id) {
        return collectionCategoryService.detail(id);
    }


    /**
     * 获取一个目录下全部藏品种类
     * @param categoryId 目录ID
     * @return 藏品
     */
    @GetMapping("/allCollection/{categoryId}")
    public List<Collection> allCollection(@PathVariable Long categoryId) {
        return collectionCategoryService.allCollection(categoryId);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/delete")
    public void delete(@RequestBody CollectionCategory category){
        if(category.getId() == null){
            throw new BusinessException("参数缺失");
        }
        collectionCategoryService.softDelete(category.getId());
    }}