package cn.ezhizao.project.business.accessories.controller;

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

import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.ezhizao.project.business.accessories.domain.BizAccessories;
import cn.ezhizao.project.business.accessories.service.IBizAccessoriesService;

/**
 * 辅料管理Controller
 *
 * @author ezhizao
 * @date 2024-09-09
 */
@RestController
@RequestMapping("/business/accessories")
public class BizAccessoriesController extends BaseController
{
    @Resource
    private IBizAccessoriesService bizAccessoriesService;

    /**
     * 查询辅料管理列表
     */
    @Log(title = "辅料管理查询", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BizAccessories bizAccessories) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizAccessories);
        startPage();
        List<BizAccessories> list = bizAccessoriesService.getList(bizAccessories);
        return getDataTable(list);
    }

    /**
     * 导出辅料管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:accessories:export')")
    @Log(title = "辅料管理导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizAccessories bizAccessories) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizAccessories);
        List<BizAccessories> list = bizAccessoriesService.getList(bizAccessories);
        ExcelUtil<BizAccessories> util = new ExcelUtil<BizAccessories>(BizAccessories.class);
        util.exportExcel(response, list, "辅料管理数据");
    }

    /**
     * 获取辅料管理详细信息
     */
    @Log(title = "辅料管理查询详细信息", businessType = BusinessType.SELECT)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizAccessoriesService.getById(id));
    }

    /**
     * 新增辅料管理
     */
    @PreAuthorize("@ss.hasPermi('business:accessories:add')")
    @Log(title = "辅料管理新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizAccessories bizAccessories) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizAccessories);
        checkAccessories(bizAccessories);
        return toAjax(bizAccessoriesService.save(bizAccessories));
    }

    /**
     * 修改辅料管理
     */
    @PreAuthorize("@ss.hasPermi('business:accessories:edit')")
    @Log(title = "辅料管理修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizAccessories bizAccessories) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizAccessories);
        checkAccessories(bizAccessories);
        return toAjax(bizAccessoriesService.updateById(bizAccessories));
    }

    private void checkAccessories(BizAccessories bizAccessories) {
        // 判断是否有重复编码
        if (bizAccessoriesService.query().eq("accessories_code", bizAccessories.getAccessoriesCode()).ne("id", bizAccessories.getId() == null ? 0L : bizAccessories.getId()).count() > 0) {
            throw new RuntimeException("辅料编码重复，请输入其他编码");
        }
    }

    /**
     * 删除辅料管理
     */
    @PreAuthorize("@ss.hasPermi('business:accessories:remove')")
    @Log(title = "辅料管理删除", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(bizAccessoriesService.removeBatchByIds(ids));
    }
}
