package com.bonus.material.clz.controller;

import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.material.clz.domain.BmProDepart;
import com.bonus.material.clz.service.BmProDepartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author ma_sh
 * @create 2025/5/12 10:06
 */
@Api(tags = "材料站项目部管理接口")
@RestController
@RequestMapping("/bmProDepart")
@Slf4j
public class BmProDepartController extends BaseController {

    @Resource
    private BmProDepartService bmProDepartService;

    /**
     * 查询项目部列表信息
     * @param tbProDepart
     * @return
     */
    @ApiOperation(value = "分页查询项目部信息")
    @GetMapping("/list")
    public AjaxResult queryByPage(BmProDepart tbProDepart) {
        try {
            if (tbProDepart.getIsAll() != null && tbProDepart.getIsAll() == 0) {
                return AjaxResult.success(bmProDepartService.queryByPage(tbProDepart));
            }
            startPage();
            List<BmProDepart> list = bmProDepartService.queryByPage(tbProDepart);
            return AjaxResult.success(getDataTable(list));
        } catch (Exception e) {
            log.error("查询项目部数据失败", e);
            return AjaxResult.error("项目部数据查询失败");
        }
    }

    /**
     * 新增项目部
     *
     * @param tbProDepart 实体
     * @return 新增结果
     */
    @ApiOperation(value = "新增项目部数据")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BmProDepart tbProDepart) {
        try {
            return bmProDepartService.insert(tbProDepart);
        } catch (Exception e) {
            log.error("新增项目部数据失败", e);
            return AjaxResult.error("新增项目部数据失败");
        }
    }

    /**
     * 编辑项目部
     * @param tbProDepart
     * @return
     */
    @ApiOperation(value = "编辑项目部数据")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody BmProDepart tbProDepart) {
        try {
            return bmProDepartService.update(tbProDepart);
        } catch (Exception e) {
            log.error("编辑项目部数据失败", e);
            return AjaxResult.error("编辑项目部数据失败");
        }
    }

    /**
     * 删除项目部
     * @param tbProDepart
     * @return
     */
    @ApiOperation(value = "删除项目部数据")
    @PostMapping("/delete")
    public AjaxResult deleteById(@RequestBody BmProDepart tbProDepart) {
        try {
            return bmProDepartService.deleteById(tbProDepart);
        } catch (Exception e) {
            log.error("删除项目部数据失败", e);
            return AjaxResult.error("删除项目部数据失败");
        }
    }

    /**
     * 导出项目部管理列表
     * @param response
     * @param tbProDepart
     */
    @ApiOperation(value = "导出项目部管理列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, BmProDepart tbProDepart)
    {
        List<BmProDepart> list = bmProDepartService.queryByPage(tbProDepart);
        ExcelUtil<BmProDepart> util = new ExcelUtil<BmProDepart>(BmProDepart.class);
        util.exportExcel(response, list, "项目部管理数据");
    }
}
