package org.dromara.controller.assets;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.domain.assets.bo.CommunityAssetsBo;
import org.dromara.domain.assets.vo.CommunityAssetsVo;
import org.dromara.service.ICommunityAssetsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 社区资产
 *
 * @author boman
 * @date 2025-04-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wuYe/assets")
public class CommunityAssetsController extends BaseController {

    private final ICommunityAssetsService communityAssetsService;

    /**
     * 查询社区资产列表
     */
    @SaCheckPermission("wuYe:assets:list")
    @GetMapping("/list")
    public TableDataInfo<CommunityAssetsVo> list(CommunityAssetsBo bo, PageQuery pageQuery) {
        return communityAssetsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出社区资产列表
     */
    @SaCheckPermission("wuYe:assets:export")
    @Log(title = "社区资产", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CommunityAssetsBo bo, HttpServletResponse response) {
        List<CommunityAssetsVo> list = communityAssetsService.queryList(bo);
        ExcelUtil.exportExcel(list, "社区资产", CommunityAssetsVo.class, response);
    }

    /**
     * 获取社区资产详细信息
     *
     * @param communityAssetId 主键
     */
    @SaCheckPermission("wuYe:assets:query")
    @GetMapping("/{communityAssetId}")
    public R<CommunityAssetsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long communityAssetId) {
        return R.ok(communityAssetsService.queryById(communityAssetId));
    }

    /**
     * 新增社区资产
     */
    @SaCheckPermission("wuYe:assets:add")
    @Log(title = "社区资产", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CommunityAssetsBo bo) {
        if (!communityAssetsService.checkEquipmentNumberUnique(bo)) {
            {
                return R.fail("新增资产'" + bo.getAssetName() + "'失败，资产编号已存在");
            }
        }
        return toAjax(communityAssetsService.insertByBo(bo));
    }

    /**
     * 修改社区资产
     */
    @SaCheckPermission("wuYe:assets:edit")
    @Log(title = "社区资产", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CommunityAssetsBo bo) {
        return toAjax(communityAssetsService.updateByBo(bo));
    }

    /**
     * 删除社区资产
     *
     * @param communityAssetIds 主键串
     */
    @SaCheckPermission("wuYe:assets:remove")
    @Log(title = "社区资产", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{communityAssetIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] communityAssetIds) {
        return toAjax(communityAssetsService.deleteWithValidByIds(List.of(communityAssetIds), true));
    }
}
