package com.maintain.business.controller;

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

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.ErpSettlementMaterialVo;
import com.maintain.business.domain.bo.ErpSettlementMaterialBo;
import com.maintain.business.service.IErpSettlementMaterialService;
import com.maintain.common.core.page.TableDataInfo;

/**
 * 结算物料
 *
 * @author liushuai
 * @date 2025-04-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/settlementMaterial")
public class ErpSettlementMaterialController extends BaseController {

    private final IErpSettlementMaterialService iErpSettlementMaterialService;

    /**
     * 查询结算物料列表
     */
    @SaCheckPermission("business:settlementMaterial:list")
    @GetMapping("/list")
    public TableDataInfo<ErpSettlementMaterialVo> list(ErpSettlementMaterialBo bo, PageQuery pageQuery) {
        return iErpSettlementMaterialService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出结算物料列表
     */
    @SaCheckPermission("business:settlementMaterial:export")
    @Log(title = "结算物料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpSettlementMaterialBo bo, HttpServletResponse response) {
        List<ErpSettlementMaterialVo> list = iErpSettlementMaterialService.queryList(bo);
        ExcelUtil.exportExcel(list, "结算物料", ErpSettlementMaterialVo.class, response);
    }

    /**
     * 获取结算物料详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:settlementMaterial:query")
    @GetMapping("/{id}")
    public R<ErpSettlementMaterialVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iErpSettlementMaterialService.queryById(id));
    }

    /**
     * 新增结算物料
     */
    @SaCheckPermission("business:settlementMaterial:add")
    @Log(title = "结算物料", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpSettlementMaterialBo bo) {
        return toAjax(iErpSettlementMaterialService.insertByBo(bo));
    }

    /**
     * 修改结算物料
     */
    @SaCheckPermission("business:settlementMaterial:edit")
    @Log(title = "结算物料", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpSettlementMaterialBo bo) {
        return toAjax(iErpSettlementMaterialService.updateByBo(bo));
    }

    /**
     * 删除结算物料
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:settlementMaterial:remove")
    @Log(title = "结算物料", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iErpSettlementMaterialService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
