package com.maintain.business.controller;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.lang.tree.Tree;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.maintain.common.annotation.RepeatSubmit;
import com.maintain.common.annotation.Log;
import com.maintain.common.core.controller.BaseController;
import com.maintain.common.core.domain.PageQuery;
import com.maintain.common.core.domain.R;
import com.maintain.common.core.validate.AddGroup;
import com.maintain.common.core.validate.EditGroup;
import com.maintain.common.enums.BusinessType;
import com.maintain.common.utils.poi.ExcelUtil;
import com.maintain.business.domain.vo.ErpProjectTypeVo;
import com.maintain.business.domain.bo.ErpProjectTypeBo;
import com.maintain.business.service.IErpProjectTypeService;
import com.maintain.common.core.page.TableDataInfo;

/**
 * 维修项目类型
 *
 * @author liushuai
 * @date 2025-03-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/projectType")
public class ErpProjectTypeController extends BaseController {

    private final IErpProjectTypeService iErpProjectTypeService;

    /**
     * 查询维修项目类型列表
     */
    @SaCheckPermission("business:projectType:list")
    @GetMapping("/list")
    public TableDataInfo<ErpProjectTypeVo> list(ErpProjectTypeBo bo, PageQuery pageQuery) {
        return iErpProjectTypeService.queryPageList(bo, pageQuery);
    }

    /**
     * 维修项目类型树列表
     */
    @GetMapping("/tree")
    public R<List<Tree<Long>>> treeList(ErpProjectTypeBo bo) {
        return R.ok(iErpProjectTypeService.selectProjectTypeTreeList(bo));
    }


    /**
     * 导出维修项目类型列表
     */
    @SaCheckPermission("business:projectType:export")
    @Log(title = "维修项目类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpProjectTypeBo bo, HttpServletResponse response) {
        List<ErpProjectTypeVo> list = iErpProjectTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "维修项目类型", ErpProjectTypeVo.class, response);
    }

    /**
     * 获取维修项目类型详细信息
     *
     * @param typeId 主键
     */
    @SaCheckPermission("business:projectType:query")
    @GetMapping("/{typeId}")
    public R<ErpProjectTypeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long typeId) {
        return R.ok(iErpProjectTypeService.queryById(typeId));
    }

    /**
     * 新增维修项目类型
     */
    @SaCheckPermission("business:projectType:add")
    @Log(title = "维修项目类型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpProjectTypeBo bo) {
        return toAjax(iErpProjectTypeService.insertByBo(bo));
    }

    /**
     * 修改维修项目类型
     */
    @SaCheckPermission("business:projectType:edit")
    @Log(title = "维修项目类型", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpProjectTypeBo bo) {
        return toAjax(iErpProjectTypeService.updateByBo(bo));
    }

    /**
     * 删除维修项目类型
     *
     * @param typeIds 主键串
     */
    @SaCheckPermission("business:projectType:remove")
    @Log(title = "维修项目类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] typeIds) {
        return toAjax(iErpProjectTypeService.deleteWithValidByIds(Arrays.asList(typeIds), true));
    }
}
