package cn.iocoder.yudao.module.basic.controller.admin.unit;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.basic.controller.admin.unit.vo.*;
import cn.iocoder.yudao.module.basic.dal.dataobject.unit.UnitDO;
import cn.iocoder.yudao.module.basic.convert.unit.UnitConvert;
import cn.iocoder.yudao.module.basic.service.unit.UnitService;

@Api(tags = "管理后台 - 单位")
@RestController
@RequestMapping("/basic/unit")
@Validated
public class UnitController {

    @Resource
    private UnitService unitService;

    @PostMapping("/create")
    @ApiOperation("创建单位")
    @PreAuthorize("@ss.hasPermission('basic:unit:create')")
    public CommonResult<Long> createUnit(@Valid @RequestBody UnitCreateReqVO createReqVO) {
        return success(unitService.createUnit(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新单位")
    @PreAuthorize("@ss.hasPermission('basic:unit:update')")
    public CommonResult<Boolean> updateUnit(@Valid @RequestBody UnitUpdateReqVO updateReqVO) {
        unitService.updateUnit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除单位")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('basic:unit:delete')")
    public CommonResult<Boolean> deleteUnit(@RequestParam("id") Long id) {
        unitService.deleteUnit(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得单位")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('basic:unit:query')")
    public CommonResult<UnitRespVO> getUnit(@RequestParam("id") Long id) {
        UnitDO unit = unitService.getUnit(id);
        return success(UnitConvert.INSTANCE.convert(unit));
    }

    @GetMapping("/list")
    @ApiOperation("获得单位列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('basic:unit:query')")
    public CommonResult<List<UnitRespVO>> getUnitList(@RequestParam("ids") Collection<Long> ids) {
        List<UnitDO> list = unitService.getUnitList(ids);
        return success(UnitConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得单位分页")
    @PreAuthorize("@ss.hasPermission('basic:unit:query')")
    public CommonResult<PageResult<UnitRespVO>> getUnitPage(@Valid UnitPageReqVO pageVO) {
        PageResult<UnitDO> pageResult = unitService.getUnitPage(pageVO);
        return success(UnitConvert.INSTANCE.convertPage(pageResult));
    }

}
