package com.ruoyi.web.controller.assets;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.assets.CommunityAssets;
import com.ruoyi.system.service.ICommunityAssetsService;
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-24
 */
@RestController
@RequestMapping("/wuYe/assets")
public class CommunityAssetsController extends BaseController {
    @Autowired
    private ICommunityAssetsService communityAssetsService;

    /**
     * 查询社区资产列表
     */
    @PreAuthorize("@ss.hasPermi('wuYe:assets:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommunityAssets communityAssets) {
        startPage();
        List<CommunityAssets> list = communityAssetsService.selectCommunityAssetsList(communityAssets);
        return getDataTable(list);
    }

    /**
     * 导出社区资产列表
     */
    @PreAuthorize("@ss.hasPermi('wuYe:assets:export')")
    @Log(title = "社区资产", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommunityAssets communityAssets) {
        List<CommunityAssets> list = communityAssetsService.selectCommunityAssetsList(communityAssets);
        ExcelUtil<CommunityAssets> util = new ExcelUtil<CommunityAssets>(CommunityAssets.class);
        util.exportExcel(response, list, "社区资产数据");
    }

    /**
     * 获取社区资产详细信息
     */
    @PreAuthorize("@ss.hasPermi('wuYe:assets:query')")
    @GetMapping(value = "/{communityAssetId}")
    public AjaxResult getInfo(@PathVariable("communityAssetId") Long communityAssetId) {
        return success(communityAssetsService.selectCommunityAssetsByCommunityAssetId(communityAssetId));
    }

    /**
     * 新增社区资产
     */
    @PreAuthorize("@ss.hasPermi('wuYe:assets:add')")
    @Log(title = "社区资产", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommunityAssets communityAssets) {
        if (!communityAssetsService.checkEquipmentNumberUnique(communityAssets)) {
            {
                return error("新增资产'" + communityAssets.getAssetName() + "'失败，资产编号已存在");
            }
        }
        return toAjax(communityAssetsService.insertCommunityAssets(communityAssets));
    }

    /**
     * 修改社区资产
     */
    @PreAuthorize("@ss.hasPermi('wuYe:assets:edit')")
    @Log(title = "社区资产", businessType = BusinessType.UPDATE)
    @PostMapping("/put")
    public AjaxResult edit(@RequestBody CommunityAssets communityAssets) {
        if (!communityAssetsService.checkEquipmentNumberUnique(communityAssets)) {
            {
                return error("新增资产'" + communityAssets.getAssetName() + "'失败，资产编号已存在");
            }
        }
        return toAjax(communityAssetsService.updateCommunityAssets(communityAssets));
    }

    /**
     * 删除社区资产
     */
    @PreAuthorize("@ss.hasPermi('wuYe:assets:remove')")
    @Log(title = "社区资产", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{communityAssetIds}")
    public AjaxResult remove(@PathVariable Long[] communityAssetIds) {
        return toAjax(communityAssetsService.deleteCommunityAssetsByCommunityAssetIds(communityAssetIds));
    }
}
