package com.iailab.module.data.plan.category.controller.admin;

import com.iailab.framework.common.pojo.CommonResult;
import com.iailab.framework.common.util.object.BeanUtils;
import com.iailab.module.data.plan.category.entity.PlanItemCategoryEntity;
import com.iailab.module.data.plan.category.service.PlanItemCategoryService;
import com.iailab.module.data.plan.category.vo.PlanItemCategoryReqVO;
import com.iailab.module.data.plan.category.vo.PlanItemCategoryRespVO;
import com.iailab.module.data.plan.category.vo.PlanItemCategorySaveReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;

import static com.iailab.framework.common.pojo.CommonResult.success;

/**
 * @author PanZhibao
 * @Description
 * @createTime 2024年11月01日
 */
@Tag(name = "数据平台 - 计划分类")
@RestController
@RequestMapping("/data/plan/category")
@Validated
public class PlanItemCategoryController {

    @Autowired
    private PlanItemCategoryService indItemCategoryService;

    @GetMapping("/list")
    @Operation(summary = "获取计划分类列表", description = "用于【计划分类】界面")
    @PreAuthorize("@ss.hasPermission('data:plan-item-category:query')")
    public CommonResult<List<PlanItemCategoryRespVO>> getList(PlanItemCategoryReqVO reqVO) {
        List<PlanItemCategoryEntity> list = indItemCategoryService.getList(reqVO);
        list.sort(Comparator.comparing(PlanItemCategoryEntity::getSort));
        return success(BeanUtils.toBean(list, PlanItemCategoryRespVO.class));
    }

    @GetMapping("/list-all-simple")
    @Operation(summary = "获取计划分类列表", description = "用于【计划分类】界面")
    @PreAuthorize("@ss.hasPermission('data:plan-item-category:query')")
    public CommonResult<List<PlanItemCategoryRespVO>> getList() {
        List<PlanItemCategoryEntity> list = indItemCategoryService.getSimpleList();
        list.sort(Comparator.comparing(PlanItemCategoryEntity::getSort));
        return success(BeanUtils.toBean(list, PlanItemCategoryRespVO.class));
    }

    @PostMapping("/create")
    @Operation(summary = "创建计划分类")
    @PreAuthorize("@ss.hasPermission('data:plan-item-category:create')")
    public CommonResult<Boolean> create(@Valid @RequestBody PlanItemCategorySaveReqVO createReqVO) {
        indItemCategoryService.create(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "修改计划分类")
    @PreAuthorize("@ss.hasPermission('data:plan-item-category:update')")
    public CommonResult<Boolean> update(@Valid @RequestBody PlanItemCategorySaveReqVO updateReqVO) {
        indItemCategoryService.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除计划分类")
    @Parameter(name = "id", description = "计划分类编号", required= true, example = "1024")
    @PreAuthorize("@ss.hasPermission('data:plan-item-category:delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") String id) {
        indItemCategoryService.delete(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取计划分类信息")
    @PreAuthorize("@ss.hasPermission('data:plan-item-category:query')")
    public CommonResult<PlanItemCategoryRespVO> get(String id) {
        PlanItemCategoryEntity entity = indItemCategoryService.get(id);
        return success(BeanUtils.toBean(entity, PlanItemCategoryRespVO.class));
    }
}