package com.ruoyi.system.controller;

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

import com.ruoyi.system.domain.QuoteType;
import com.ruoyi.system.service.IQuoteMaterialTypeService;
import com.ruoyi.system.service.IServiceGoodsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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 com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.QuoteMaterial;
import com.ruoyi.system.service.IQuoteMaterialService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目报价--物料信息Controller
 * 
 * @author ruoyi
 * @date 2025-05-13
 */
@RestController
@RequestMapping("/system/QuoteMaterial")
public class QuoteMaterialController extends BaseController
{
    @Autowired
    private IQuoteMaterialService quoteMaterialService;

    @Autowired
    private IServiceGoodsService serviceGoodsService;
    @Autowired
    private IQuoteMaterialTypeService quoteMaterialTypeService;
    /**
     * 查询项目报价--物料信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:QuoteMaterial:list')")
    @GetMapping("/list")
    public TableDataInfo list(QuoteMaterial quoteMaterial)
    {
        startPage();
        if(quoteMaterial.getGoodId()!=null){
            quoteMaterial.setGoodId("\""+quoteMaterial.getGoodId()+"\"");
        }
        List<QuoteMaterial> list = quoteMaterialService.selectQuoteMaterialList(quoteMaterial);
        for(QuoteMaterial QuoteMaterialData:list){
            List<String> idslist = Arrays.asList(
                    QuoteMaterialData.getGoodId().replaceAll("[\\[\\]\"]", "").split(", "));
                    QuoteMaterialData.setServiceName(serviceGoodsService.selectTitlesByIds(idslist));

            List<String> typeidslist = Arrays.asList(
                    QuoteMaterialData.getTypeId().replaceAll("[\\[\\]\"]", "").split(", "));
            QuoteMaterialData.setTypeName(quoteMaterialTypeService.selecttypesByIds(typeidslist));
        }
        return getDataTable(list);
    }

    /**
     * 导出项目报价--物料信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:QuoteMaterial:export')")
    @Log(title = "项目报价--物料信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QuoteMaterial quoteMaterial)
    {
        List<QuoteMaterial> list = quoteMaterialService.selectQuoteMaterialList(quoteMaterial);
        ExcelUtil<QuoteMaterial> util = new ExcelUtil<QuoteMaterial>(QuoteMaterial.class);
        util.exportExcel(response, list, "项目报价--物料信息数据");
    }

    /**
     * 获取项目报价--物料信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:QuoteMaterial:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(quoteMaterialService.selectQuoteMaterialById(id));
    }

    /**
     * 新增项目报价--物料信息
     */
    @PreAuthorize("@ss.hasPermi('system:QuoteMaterial:add')")
    @Log(title = "项目报价--物料信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QuoteMaterial quoteMaterial)
    {
        return toAjax(quoteMaterialService.insertQuoteMaterial(quoteMaterial));
    }

    /**
     * 修改项目报价--物料信息
     */
    @PreAuthorize("@ss.hasPermi('system:QuoteMaterial:edit')")
    @Log(title = "项目报价--物料信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QuoteMaterial quoteMaterial)
    {
        return toAjax(quoteMaterialService.updateQuoteMaterial(quoteMaterial));
    }

    /**
     * 删除项目报价--物料信息
     */
    @PreAuthorize("@ss.hasPermi('system:QuoteMaterial:remove')")
    @Log(title = "项目报价--物料信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(quoteMaterialService.deleteQuoteMaterialByIds(ids));
    }
}
