package com.codvision.auth.controller.manager;

import com.codvision.authcore.annotation.RequiresPermissions;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.auth.entity.vo.OrgPropertyVO;
import com.codvision.auth.entity.OrgProperty;
import com.codvision.auth.service.IOrgPropertyService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codvision.webcore.annotation.ApiSecret;
import com.codvision.webcore.bean.QueryPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 组织属性表 管理模块
 *
 * @author lingee
 * @date 2023-11-10
 */
@RestController
@RequestMapping("/v1/manager/orgProperty")
@Api(tags = "平台管理端-组织属性管理模块")
public class ManagerOrgPropertyController {

    @Autowired
    private IOrgPropertyService orgPropertyService;

    @ApiOperation(value = "获取组织属性表分页列表", notes = "获取组织属性表分页列表接口")
    @PostMapping("/findAll")
    @RequiresPermissions("sys_org_property_list")
    public ResponseEntity<IPage<OrgPropertyVO>> findAll(@RequestParam(required = false) String keyword,
                                                        @ApiParam(value = "父级id", required = true) @RequestParam(required = false) Integer pid,
                                                        @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                        @RequestParam(value = "size", required = false, defaultValue = "10") int size) {
        OrgProperty queryBean = new OrgProperty();
        queryBean.setName(keyword);
        queryBean.setPid(pid);
        IPage<OrgPropertyVO> pageResult = orgPropertyService.getPageResult(new QueryPage<>(queryBean, page, size));
        return new ResponseEntity<IPage<OrgPropertyVO>>().ok().data(pageResult);
    }

    @ApiOperation(value = "新增组织属性表", notes = "新增组织属性表接口")
    @PostMapping("/add")
    @RequiresPermissions("sys_org_property_add")
    public ResponseEntity<String> saveOrgProperty(@Valid @RequestBody OrgProperty orgProperty) {
        orgPropertyService.save(orgProperty);
        return new ResponseEntity<String>().ok().message("添加成功");
    }

    @ApiOperation(value = "删除组织属性表", notes = "删除组织属性表接口")
    @PostMapping("/del")
    @RequiresPermissions("sys_org_property_del")
    public ResponseEntity<String> deleteById(@ApiParam(value = "组织属性表ID", required = true) @RequestParam Integer id) {
        orgPropertyService.removeById(id);
        return new ResponseEntity<String>().ok().message("删除成功");
    }

    @ApiOperation(value = "编辑组织属性表", notes = "编辑组织属性表接口")
    @PostMapping("/update")
    @RequiresPermissions("sys_org_property_edit")
    public ResponseEntity<String> updateOrgProperty(@RequestBody OrgProperty orgProperty) {
        orgPropertyService.updateById(orgProperty);
        return new ResponseEntity<String>().ok().message("编辑成功");
    }
}
