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.BmTeam;
import com.bonus.material.clz.service.BmTeamService;
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 13:02
 */
@Api(tags = "材料站班组管理接口")
@RestController
@RequestMapping("/bmTeam")
@Slf4j
public class BmTeamController extends BaseController {

    @Resource
    private BmTeamService bmTeamService;

    /**
     * 查询班组信息
     * @param tbTeam
     * @return
     */
    @ApiOperation(value = "分页查询班组信息")
    @GetMapping("/list")
    public AjaxResult queryByPage(BmTeam tbTeam) {
        try {
            if (tbTeam.getIsAll() != null && tbTeam.getIsAll() == 0) {
                return bmTeamService.getList(tbTeam);
            }
            startPage();
            List<BmTeam> list = bmTeamService.queryByPage(tbTeam);
            return AjaxResult.success(getDataTable(list));
        } catch (Exception e) {
            log.error("查询班组数据失败", e);
            return AjaxResult.error("班组数据查询失败");
        }
    }

    /**
     * 根据身份证号查询班组信息
     * @param tbTeam
     * @return
     */
    @ApiOperation(value = "根据身份证号查询班组信息")
    @GetMapping("/getListByIdCard")
    public AjaxResult getListByIdCard(BmTeam tbTeam) {
        try {
            return bmTeamService.getListByIdCard(tbTeam);
        } catch (Exception e) {
            log.error("查询班组数据失败", e);
            return AjaxResult.error("班组数据查询失败");
        }
    }

    @ApiOperation(value = "分页查询班组信息")
    @GetMapping("/getTeamList")
    public AjaxResult getTeamList(BmTeam tbTeam) {
        try {
            return bmTeamService.getTeamList(tbTeam);
        } catch (Exception e) {
            log.error("查询班组数据失败", e);
            return AjaxResult.error("班组数据查询失败");
        }
    }

    /**
     * 新增班组信息
     * @param tbTeam
     * @return
     */
    @ApiOperation(value = "新增班组信息")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BmTeam tbTeam) {
        try {
            return bmTeamService.insert(tbTeam);
        } catch (Exception e) {
            log.error("新增班组数据失败", e);
            return AjaxResult.error("新增班组数据失败");
        }
    }

    /**
     * 编辑班组信息
     * @param tbTeam
     * @return
     */
    @ApiOperation(value = "编辑班组信息")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody BmTeam tbTeam) {
        try {
            return bmTeamService.edit(tbTeam);
        } catch (Exception e) {
            log.error("编辑班组数据失败", e);
            return AjaxResult.error("编辑班组数据失败");
        }
    }

    /**
     * 删除班组信息
     * @param tbTeam
     * @return
     */
    @ApiOperation(value = "删除班组数据")
    @PostMapping("/delete")
    public AjaxResult deleteById(@RequestBody BmTeam tbTeam) {
        try {
            return bmTeamService.deleteById(tbTeam);
        } catch (Exception e) {
            log.error("删除班组信息失败", e);
            return AjaxResult.error("删除班组信息失败");
        }
    }

    /**
     * 导出班组管理列表
     * @param response
     * @param tbTeam
     */
    @ApiOperation(value = "导出班组管理列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, BmTeam tbTeam)
    {
        List<BmTeam> list = bmTeamService.queryByPage(tbTeam);
        ExcelUtil<BmTeam> util = new ExcelUtil<BmTeam>(BmTeam.class);
        util.exportExcel(response, list, "班组管理数据");
    }
}
