package com.ruoyi.system.controller;

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

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.QuoteMaterialType;
import com.ruoyi.system.service.IQuoteMaterialTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目报价--物料分类Controller
 * 
 * @author ruoyi
 * @date 2025-05-13
 */
@RestController
@RequestMapping("/system/QuoteMaterialType")
public class QuoteMaterialTypeController extends BaseController
{
    @Autowired
    private IQuoteMaterialTypeService quoteMaterialTypeService;
    @Autowired
    private IServiceGoodsService serviceGoodsService;
    /**
     * 查询项目报价--物料分类列表
     */
    @PreAuthorize("@ss.hasPermi('system:QuoteMaterialType:list')")
    @GetMapping("/list")
    public TableDataInfo list(QuoteMaterialType quoteMaterialType)
    {
        startPage();
        if(quoteMaterialType.getGoodId()!=null){
            quoteMaterialType.setGoodId("\""+quoteMaterialType.getGoodId()+"\"");
        }

        List<QuoteMaterialType> list = quoteMaterialTypeService.selectQuoteMaterialTypeList(quoteMaterialType);
        for(QuoteMaterialType q:list){
            List<String> idslist = Arrays.asList(
                    q.getGoodId().replaceAll("[\\[\\]\"]", "").split(", "));
            q.setServiceName(serviceGoodsService.selectTitlesByIds(idslist));
        }
        return getDataTable(list);
    }

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

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

    /**
     * 新增项目报价--物料分类
     */
    @PreAuthorize("@ss.hasPermi('system:QuoteMaterialType:add')")
    @Log(title = "项目报价--物料分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QuoteMaterialType quoteMaterialType)
    {
        return toAjax(quoteMaterialTypeService.insertQuoteMaterialType(quoteMaterialType));
    }

    /**
     * 修改项目报价--物料分类
     */
    @PreAuthorize("@ss.hasPermi('system:QuoteMaterialType:edit')")
    @Log(title = "项目报价--物料分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QuoteMaterialType quoteMaterialType)
    {
        return toAjax(quoteMaterialTypeService.updateQuoteMaterialType(quoteMaterialType));
    }



    /**
     * 定时任务状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:QuoteMaterialType:changeStatus')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody QuoteMaterialType quoteMaterialType)
    {
        QuoteMaterialType newQuoteMaterialType = quoteMaterialTypeService.selectQuoteMaterialTypeById(quoteMaterialType.getId());
        newQuoteMaterialType.setStatus(quoteMaterialType.getStatus());
        return toAjax(quoteMaterialTypeService.updateQuoteMaterialType(newQuoteMaterialType));
    }
    /**
     * 删除项目报价--物料分类
     */
    @PreAuthorize("@ss.hasPermi('system:QuoteMaterialType:remove')")
    @Log(title = "项目报价--物料分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(quoteMaterialTypeService.deleteQuoteMaterialTypeByIds(ids));
    }
}
