package com.bonus.sgzb.machine.controller;;

import com.bonus.sgzb.common.core.utils.poi.ExcelUtil;
import com.bonus.sgzb.common.core.web.controller.BaseController;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.common.core.web.page.TableDataInfo;
import com.bonus.sgzb.common.security.utils.SecurityUtils;
import com.bonus.sgzb.machine.damain.MaHouse;
import com.bonus.sgzb.machine.damain.MaPartType;
import com.bonus.sgzb.machine.damain.MaType;
import com.bonus.sgzb.machine.service.IPartTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

import static com.bonus.sgzb.common.core.web.domain.AjaxResult.error;

/**
 * 配件类型管理ma_part_type(MaPartType)表控制层
 *
 * @author makejava
 * @since 2023-11-27 16:44:02
 */
@RestController
@RequestMapping("maPartType")
public class MaPartTypeController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private IPartTypeService maPartTypeService;

    /**
     * 查询配件类型列表
     * @param maPartType
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(MaPartType maPartType)
    {
        startPage();
        List<MaPartType> list = maPartTypeService.selectMaPartList(maPartType);
        return getDataTable(list);
    }

    /**
     * 新增配件管理
     * @param maPartType
     * @return
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody MaPartType maPartType){
        if (!maPartTypeService.checkPaNameUnique(maPartType)) {
            return error("新增配件名称'" + maPartType.getPaName() + "'失败，配件名称已存在");
        }
        maPartType.setCreateBy(SecurityUtils.getUsername());
        return toAjax(maPartTypeService.insertMaPart(maPartType));
    }

    /**
     * 导出配件类型管理
     * @param response
     * @param maPartType
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaPartType maPartType)
    {
        List<MaPartType> list = maPartTypeService.selectMyPartTypeList(maPartType);
        ExcelUtil<MaPartType> util = new ExcelUtil<MaPartType>(MaPartType.class);
        util.exportExcel(response, list, "配件类型管理数据");
    }

    /**
     * 删除配件管理类型
     * @param paId
     * @return
     */
    @DeleteMapping("/{paId}")
    public AjaxResult delete(@PathVariable("paId") Long paId){
        if (maPartTypeService.hasChildBypaId(paId))
        {
            return warn("存在下级仓库列表,不允许删除");
        }
        return toAjax(maPartTypeService.deletePaById(paId));
    }
}

