package com.ktg.iscs.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.pojo.CommonResult;
import com.ktg.common.utils.StringUtils;
import com.ktg.common.utils.poi.ExcelUtil;
import com.ktg.iscs.domain.IsUnit;
import com.ktg.iscs.service.IIsUnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 单位管理Controller
 *
 * @author cgj
 * @date 2024-12-18
 */
@Api(tags = "单位管理")
@RestController
@RequestMapping("/iscs/unit")
public class IsUnitController extends BaseController
{
    @Autowired
    private IIsUnitService isUnitService;

    /**
     * 查询单位管理分页
     */
    @ApiOperation("查询单位管理-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "isUnit", description = "实体参数")
    })
    @PreAuthorize("@ss.hasPermi('iscs:unit:list')")
    @GetMapping("/getIsUnitPage")
    public CommonResult<Page<IsUnit>> getIsUnitPage(Page<IsUnit> page, IsUnit isUnit)
    {
        Page<IsUnit> result = isUnitService.getIsUnitPage(page, isUnit);
        return CommonResult.success(result);
    }

    /**
     * 导出单位管理列表
     */
    @ApiOperation("导出单位管理列表")
    @Parameter(name = "isUnit", description = "实体参数")
    @PreAuthorize("@ss.hasPermi('iscs:unit:export')")
    @Log(title = "单位管理", businessType = BusinessType.EXPORT)
    @PostMapping("/exportIsUnit")
    public void exportIsUnit(HttpServletResponse response, IsUnit isUnit)
    {
        Page<IsUnit> page = new Page<>();
        page.setSize(-1);
        page.setCurrent(1);
        List<IsUnit> list = isUnitService.page(page, Wrappers.<IsUnit>lambdaQuery()
                .orderByDesc(IsUnit::getUnitId)).getRecords();
        ExcelUtil<IsUnit> util = new ExcelUtil<IsUnit>(IsUnit.class);
        util.exportExcel(response, list, "单位管理数据");
    }

    /**
     * 获取单位管理详细信息
     */
    @ApiOperation("获取单位管理详细信息")
    @Parameter(name = "unitId", description = "unitId")
    @PreAuthorize("@ss.hasPermi('iscs:unit:query')")
    @GetMapping(value = "/selectIsUnitById")
    public CommonResult<IsUnit> selectIsUnitById(Long unitId)
    {
        return CommonResult.success(isUnitService.getById(unitId));
    }

    /**
     * 新增单位管理
     */
    @ApiOperation("新增单位管理")
    @PreAuthorize("@ss.hasPermi('iscs:unit:add')")
    @Log(title = "单位管理", businessType = BusinessType.INSERT)
    @PostMapping("/insertIsUnit")
    public CommonResult<Boolean> insertIsUnit(@RequestBody @Parameter(name = "isUnit", description = "新增数据类,放到body") IsUnit isUnit)
    {
        // 断言校验
        Assert.isTrue(StringUtils.isNotBlank(isUnit.getUnitName()), "单位名称不可为空!");
        IsUnit one =isUnitService.getById(isUnit.getParentId());
        String ancestors;
        if (one != null) {
            ancestors = one.getAncestors() + "," + isUnit.getParentId();
        } else {
            ancestors = "0";
        }
        isUnit.setAncestors(ancestors);
        return CommonResult.success(isUnitService.save(isUnit));
    }

    /**
     * 修改单位管理
     */
    @ApiOperation("修改单位管理")
    @PreAuthorize("@ss.hasPermi('iscs:unit:edit')")
    @Log(title = "单位管理", businessType = BusinessType.UPDATE)
    @PostMapping("/updateIsUnit")
    public CommonResult<Boolean> updateIsUnit(@RequestBody @Parameter(name = "isUnit", description = "修改数据类,放到body") IsUnit isUnit)
    {

        return CommonResult.success(isUnitService.updateById(isUnit));
    }

    /**
     * 删除单位管理
     */
    @ApiOperation("删除单位管理")
    @PreAuthorize("@ss.hasPermi('iscs:unit:remove')")
    @Log(title = "单位管理", businessType = BusinessType.DELETE)
	@PostMapping("/deleteIsUnitByUnitIds")
    public CommonResult<Boolean> deleteIsUnitByUnitIds(String unitIds)
    {
        Assert.notBlank(unitIds, "请选择需要删除的数据!");
        Long[] longIds = Convert.toLongArray(unitIds);
        return CommonResult.success(isUnitService.removeBatchByIds(Arrays.asList(longIds)));
    }
}
