package com.zbf.web.controller.system;

import com.zbf.common.annotation.Log;
import com.zbf.common.core.controller.BaseController;
import com.zbf.common.core.domain.AjaxResult;
import com.zbf.common.core.page.TableDataInfo;
import com.zbf.common.enums.BusinessType;
import com.zbf.common.utils.poi.ExcelUtil;
import com.zbf.system.domain.TBaseGoods;
import com.zbf.system.service.ITBaseGoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 物料Controller
 *
 * @author sunits
 * @date 2024-06-15
 */
@RestController
@RequestMapping("/system/goods")
public class TBaseGoodsController extends BaseController {
    @Autowired
    private ITBaseGoodsService tBaseGoodsService;

    /**
     * 查询物料列表
     */
    @PreAuthorize("@ss.hasPermi('system:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(TBaseGoods tBaseGoods) {
        startPage();
        List<TBaseGoods> list = tBaseGoodsService.selectTBaseGoodsList(tBaseGoods);
        TableDataInfo dataTable = getDataTable(list);
        dataTable.setColumns(tBaseGoods.getColumnsJson());
        return dataTable;
    }

    /**
     * 导出物料列表
     */
    @PreAuthorize("@ss.hasPermi('system:goods:export')")
    @Log(title = "物料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TBaseGoods tBaseGoods) {
        List<TBaseGoods> list = tBaseGoodsService.selectTBaseGoodsList(tBaseGoods);
        ExcelUtil<TBaseGoods> util = new ExcelUtil<TBaseGoods>(TBaseGoods.class);
        util.exportExcel(response, list, "物料数据");
    }

    /**
     * 获取物料详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:goods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tBaseGoodsService.selectTBaseGoodsById(id));
    }

    /**
     * 新增物料
     */
    @PreAuthorize("@ss.hasPermi('system:goods:add')")
    @Log(title = "物料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBaseGoods tBaseGoods) {
        logger.info("请求插入物料：{}", tBaseGoods);
        if (StringUtils.isBlank(tBaseGoods.getGoodsId())) {
            logger.error("物料编号不能为空");
            throw new RuntimeException("物料编号不能为空");
        }
        if (StringUtils.isBlank(tBaseGoods.getGoodsName())) {
            logger.error("物料名称不能为空");
            throw new RuntimeException("物料名称不能为空");
        }
        if (StringUtils.isBlank(tBaseGoods.getUnit())) {
            logger.error("单位不能为空");
            throw new RuntimeException("单位不能为空");
        }
        return toAjax(tBaseGoodsService.insertTBaseGoods(tBaseGoods));
    }

    /**
     * 修改物料
     */
    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "物料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBaseGoods tBaseGoods) {
        logger.info("更改物料信息：{}", tBaseGoods);
        return toAjax(tBaseGoodsService.updateTBaseGoods(tBaseGoods));
    }

    /**
     * 删除物料
     */
    @PreAuthorize("@ss.hasPermi('system:goods:remove')")
    @Log(title = "物料", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        logger.info("删除物料信息：{}", ids);
        return toAjax(tBaseGoodsService.deleteTBaseGoodsByIds(ids));
    }

    /**
     * 启用
     */
    @PreAuthorize("@ss.hasPermi('system:goods:remove')")
    @Log(title = "物料", businessType = BusinessType.UPDATE)
    @DeleteMapping("/disable/{ids}")
    public AjaxResult disable(@PathVariable Long[] ids) {
        return toAjax(tBaseGoodsService.disabledTBaseGoodsByIds(ids));
    }

    /**
     * 禁用
     */
    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "物料", businessType = BusinessType.UPDATE)
    @DeleteMapping("/enable/{ids}")
    public AjaxResult enable(@PathVariable Long[] ids) {
        return toAjax(tBaseGoodsService.enableTBaseGoodsByIds(ids));
    }

    /**
     * 修改状态
     */
    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "物料", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody TBaseGoods tBaseGoods) {
        return toAjax(tBaseGoodsService.updateStatus(tBaseGoods));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<TBaseGoods> util = new ExcelUtil<TBaseGoods>(TBaseGoods.class);
        util.importTemplateExcel(response, "物料数据");
    }

    @Log(title = "物料", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:goods:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<TBaseGoods> util = new ExcelUtil<TBaseGoods>(TBaseGoods.class);
        List<TBaseGoods> importList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = tBaseGoodsService.importData(importList, updateSupport, operName);
        return success(message);
    }

    @Log(title = "存入RFID", businessType = BusinessType.INSERT)
    @PostMapping("/saveRfid")
    public AjaxResult saveRfid(@RequestBody TBaseGoods goods) {
        int i = tBaseGoodsService.saveRfid(goods);
        if (i > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

}

