package com.fs.company.controller;

import com.fs.common.annotation.Log;
import com.fs.common.config.LoginContextManager;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanyProduct;
import com.fs.company.service.ICompanyProductService;
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 fs
 * @date 2025-07-01
 */
@RestController
@RequestMapping("/company/product")
public class CompanyProductController extends BaseController
{
    @Autowired
    private ICompanyProductService companyProductService;

    /**
     * 查询产品列表
     */
    @PreAuthorize("@ss.hasPermi('company:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyProduct companyProduct)
    {
        startPage();
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        companyProduct.setCompanyId(currentCompanyId);
        List<CompanyProduct> list = companyProductService.selectCompanyProductList(companyProduct);
        return getDataTable(list);
    }

    /**
     * 导出产品列表
     */
    @PreAuthorize("@ss.hasPermi('company:product:export')")
    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyProduct companyProduct)
    {
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        companyProduct.setCompanyId(currentCompanyId);
        List<CompanyProduct> list = companyProductService.selectCompanyProductList(companyProduct);
        ExcelUtil<CompanyProduct> util = new ExcelUtil<CompanyProduct>(CompanyProduct.class);
        return util.exportExcel(list, "产品数据");
    }

    /**
     * 获取产品详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(companyProductService.selectCompanyProductById(id));
    }

    /**
     * 新增产品
     */
    @PreAuthorize("@ss.hasPermi('company:product:add')")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanyProduct companyProduct)
    {
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        companyProduct.setCompanyId(currentCompanyId);
        return toAjax(companyProductService.insertCompanyProduct(companyProduct));
    }

    /**
     * 修改产品
     */
    @PreAuthorize("@ss.hasPermi('company:product:edit')")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompanyProduct companyProduct)
    {
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        companyProduct.setCompanyId(currentCompanyId);
        return toAjax(companyProductService.updateCompanyProduct(companyProduct));
    }

    /**
     * 删除产品
     */
    @PreAuthorize("@ss.hasPermi('company:product:remove')")
    @Log(title = "产品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(companyProductService.deleteCompanyProductByIds(ids));
    }
}
