package com.ruoyi.web.controller.asset;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.AssetType;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.IAssetTypeService;
import org.apache.commons.lang3.ArrayUtils;
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.List;

/**
 * 资产类型Controller
 *
 * @author boman
 * @date 2025-02-17
 */
@RestController
@RequestMapping("/wuYe/assetType")
public class AssetTypeController extends BaseController {
    @Autowired
    private IAssetTypeService assetTypeService;

    /**
     * 查询资产类型列表
     */
    @PreAuthorize("@ss.hasPermi('wuYe:assetType:list')")
    @GetMapping("/list")
    public AjaxResult list(AssetType assetType) {

        List<AssetType> list = assetTypeService.selectAssetTypeList(assetType);
        return success(list);
    }

    /**
     * 查询部门列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('wuYe:assetType:list')")
    @GetMapping("/list/exclude/{assetId}")
    public AjaxResult excludeChild(@PathVariable(value = "assetId", required = false) Long assetId)
    {
        List<AssetType> assetTypes = assetTypeService.selectAssetTypeList(new AssetType());
        assetTypes.removeIf(d -> d.getAssetId().intValue() == assetId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), assetId + ""));
        return success(assetTypes);
    }
    /**
     * 导出资产类型列表
     */
    @PreAuthorize("@ss.hasPermi('wuYe:assetType:export')")
    @Log(title = "资产类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AssetType assetType) {
        List<AssetType> list = assetTypeService.selectAssetTypeList(assetType);
        ExcelUtil<AssetType> util = new ExcelUtil<AssetType>(AssetType.class);
        util.exportExcel(response, list, "资产类型数据");
    }

    /**
     * 获取资产类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('wuYe:assetType:query')")
    @GetMapping(value = "/{assetId}")
    public AjaxResult getInfo(@PathVariable("assetId") Long assetId) {
        return success(assetTypeService.selectAssetTypeByAssetId(assetId));
    }

    /**
     * 新增资产类型
     */
    @PreAuthorize("@ss.hasPermi('wuYe:assetType:add')")
    @Log(title = "资产类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AssetType assetType) {
        if (!assetTypeService.checkAssetTypeNameUnique(assetType))
        {
            return error("新增资产'" + assetType.getAssetName() + "'失败，资产名称已存在");
        }
        return toAjax(assetTypeService.insertAssetType(assetType));
    }

    /**
     * 修改资产类型
     */
    @PreAuthorize("@ss.hasPermi('wuYe:assetType:edit')")
    @Log(title = "资产类型", businessType = BusinessType.UPDATE)
    @PostMapping("/put")
    public AjaxResult edit(@RequestBody AssetType assetType) {
        Long assetId = assetType.getAssetId();
        if (!assetTypeService.checkAssetTypeNameUnique(assetType))
        {
            return error("新增资产'" + assetType.getAssetName() + "'失败，资产名称已存在");
        }
        else if (assetType.getParentId().equals(assetId))
        {
            return error("修改资产'" + assetType.getAssetName() + "'失败，上级资产不能是自己");
        }
        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, assetType.getStatus()) && assetTypeService.selectNormalChildrenAssetById(assetId) > 0)
        {
            return error("该部门包含未停用的子部门！");
        }
        return toAjax(assetTypeService.updateAssetType(assetType));
    }

    /**
     * 删除资产类型
     */
    @PreAuthorize("@ss.hasPermi('wuYe:assetType:remove')")
    @Log(title = "资产类型", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{assetIds}")
    public AjaxResult remove(@PathVariable Long[] assetIds) {
        for (Long assetId : assetIds) {
            if (assetTypeService.hasChildByAssetId(assetId))
            {
                return warn("存在下级资产,不允许删除");
            }
        }
        return toAjax(assetTypeService.deleteAssetTypeByAssetIds(assetIds));
    }


    /**
     * 获取资产分类树列表
     */
    @PreAuthorize("@ss.hasPermi('wuYe:assetType:deptTree')")
    @GetMapping("/deptTree")
    public AjaxResult deptTree(AssetType assetType)
    {
        return success(assetTypeService.selectAssetTypeTreeList(assetType));
    }
}
