package com.maintain.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.maintain.business.domain.bo.ErpMaterialBo;
import com.maintain.business.domain.vo.ErpMaterialVo;
import com.maintain.business.listener.ErpMaterialImportListener;
import com.maintain.business.service.IErpMaterialService;
import com.maintain.common.annotation.Log;
import com.maintain.common.annotation.RepeatSubmit;
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.page.TableDataInfo;
import com.maintain.common.core.validate.AddGroup;
import com.maintain.common.core.validate.EditGroup;
import com.maintain.common.enums.BusinessType;
import com.maintain.common.excel.ExcelResult;
import com.maintain.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 物资
 *
 * @author liushuai
 * @date 2025-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/material")
public class ErpMaterialController extends BaseController {

    private final IErpMaterialService iErpMaterialService;

    /**
     * 查询物资列表
     */
    @SaCheckPermission("business:material:list")
    @GetMapping("/list")
    public TableDataInfo<ErpMaterialVo> list(ErpMaterialBo bo, PageQuery pageQuery) {
        return iErpMaterialService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询物资列表(库存大于0)
     */
    @GetMapping("/customList")
    public TableDataInfo<ErpMaterialVo> customList(ErpMaterialBo bo, PageQuery pageQuery) {
        return iErpMaterialService.customQueryPageList(bo, pageQuery);
    }

    /**
     * 查询物资列表根据仓库ID
     */
    @GetMapping("/customListByWarehouseId")
    public R<List<ErpMaterialVo>> customQueryListByWarehouseId(ErpMaterialBo bo) {
        return R.ok(iErpMaterialService.customQueryListByWarehouseId(bo));
    }

    /**
     * 查询物资列表(全部)
     */
    @GetMapping("/customListAll")
    public TableDataInfo<ErpMaterialVo> customListAll(ErpMaterialBo bo, PageQuery pageQuery) {
        return iErpMaterialService.customQueryPageListAll(bo, pageQuery);
    }

    /**
     * 物料导入模板下载
     */
    @PostMapping("/importTemplate")
    public void importTemplate(ErpMaterialBo bo, HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "物料导入", ErpMaterialVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "物料导入", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<ErpMaterialVo> result = ExcelUtil.importExcel(file.getInputStream(), ErpMaterialVo.class, new ErpMaterialImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 导出物资列表
     */
    @SaCheckPermission("business:material:export")
    @Log(title = "物资", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpMaterialBo bo, HttpServletResponse response) {
        List<ErpMaterialVo> list = iErpMaterialService.queryList(bo);
        ExcelUtil.exportExcel(list, "物资", ErpMaterialVo.class, response);
    }

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

    /**
     * 新增物资
     */
    @SaCheckPermission("business:material:add")
    @Log(title = "物资", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpMaterialBo bo) {
        return toAjax(iErpMaterialService.insertByBo(bo));
    }

    /**
     * 修改物资
     */
    @SaCheckPermission("business:material:edit")
    @Log(title = "物资", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpMaterialBo bo) {
        return toAjax(iErpMaterialService.updateByBo(bo));
    }

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