package com.ruoyi.web.controller.warehouse.basicData;

import com.ruoyi.basicData.domain.TGoods;
import com.ruoyi.basicData.service.ITGoodsService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品详情Controller
 *
 * @author ruoyi
 * @date 2020-12-11
 */
@RestController
@RequestMapping("/basicdata/goods")
public class TGoodsController extends BaseController {
    @Autowired
    private ITGoodsService tGoodsService;

    /**
     * 查询商品详情列表
     */
//    @PreAuthorize("@ss.hasPermi('basicdata:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(TGoods tGoods) {
        startPage();
        List<TGoods> list = tGoodsService.selectTGoodsList(tGoods);
        return getDataTable(list);
    }

    /**
     * 导出商品详情列表
     */
    @PreAuthorize("@ss.hasPermi('basicdata:goods:export')")
    @Log(title = "商品详情", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TGoods tGoods) {
        List<TGoods> list = tGoodsService.selectTGoodsList(tGoods);
        ExcelUtil<TGoods> util = new ExcelUtil<TGoods>(TGoods.class);
        return util.exportExcel(list, "goods");
    }

    /**
     * 获取商品详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('basicdata:goods:query')")
    @GetMapping(value = "/{fId}")
    public AjaxResult getInfo(@PathVariable("fId") Long fId) {
        return AjaxResult.success(tGoodsService.selectTGoodsById(fId));
    }

    /**
     * 新增商品详情
     */
    @PreAuthorize("@ss.hasPermi('basicdata:goods:add')")
    @Log(title = "商品详情", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody TGoods tGoods) {
        // 检验编号、名称、地址 唯一
        if (UserConstants.NOT_UNIQUE.equals(tGoodsService.checkUFNoUnique(tGoods))) {
            return AjaxResult.error("新增商品'" + tGoods.getfNo() + "'失败，编号已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(tGoodsService.checkUFNnameUnique(tGoods))) {
            return AjaxResult.error("新增商品'" + tGoods.getfName() + "'失败，名称已存在");
        }
        return toAjax(tGoodsService.insertTGoods(tGoods));
    }

    /**
     * 修改商品详情
     */
    @PreAuthorize("@ss.hasPermi('basicdata:goods:edit')")
    @Log(title = "商品详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TGoods tGoods) {
        // 检验编号、名称、地址 唯一
        if (UserConstants.NOT_UNIQUE.equals(tGoodsService.checkUFNoUnique(tGoods))) {
            return AjaxResult.error("新增商品'" + tGoods.getfNo() + "'失败，编号已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(tGoodsService.checkUFNnameUnique(tGoods))) {
            return AjaxResult.error("新增商品'" + tGoods.getfName() + "'失败，名称已存在");
        }
        return tGoodsService.updateTGoods(tGoods);
    }

    /**
     * 删除商品详情
     */
    @PreAuthorize("@ss.hasPermi('basicdata:goods:remove')")
    @Log(title = "商品详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fIds}")
    @RepeatSubmit
    public AjaxResult remove(@PathVariable Long[] fIds) {
        return tGoodsService.deleteTGoodsByIds(fIds);
    }
}
