package com.ruoyi.system.controller;

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.system.domain.QuoteCraft;
import com.ruoyi.system.domain.QuoteMaterialType;
import com.ruoyi.system.domain.ServiceGoods;
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.QuoteType;
import com.ruoyi.system.service.IQuoteTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目报价--工艺分类Controller
 * 
 * @author ruoyi
 * @date 2025-05-14
 */
@RestController
@RequestMapping("/system/QuoteType")
public class QuoteTypeController extends BaseController
{
    @Autowired
    private IQuoteTypeService quoteTypeService;
    @Autowired
    private IServiceGoodsService serviceGoodsService;
    /**
     * 查询项目报价--工艺分类列表
     */
    @PreAuthorize("@ss.hasPermi('system:QuoteType:list')")
    @GetMapping("/list")
    public TableDataInfo list(QuoteType quoteType) throws JsonProcessingException {
        startPage();
//        if(quoteType.getGoodsids()!=null){
//            List<String> listids = quoteType.getGoodsids();
//            if(listids.size()>0){
//                String[] result = IntStream.range(0, listids.size())
//                        .mapToObj(i -> (i == 0 ? "" : " ") + listids.get(i))
//                        .toArray(String[]::new);
//                System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&"+Arrays.toString(result));
//            }
//        }
//        if (quoteType.getGoodsids() != null && !quoteType.getGoodsids().isEmpty()) {
//            List<String> listids = quoteType.getGoodsids();
//
//            String[] result = IntStream.range(0, listids.size())
//                    .mapToObj(i -> (i == 0 ? "" : " ") + listids.get(i))
//                    .toArray(String[]::new);
//
//            ObjectMapper mapper = new ObjectMapper();
//            String json = mapper.writeValueAsString(result);
//            quoteType.setGoodId(json);
//        }
        if(quoteType.getGoodId()!=null){
            quoteType.setGoodId("\""+quoteType.getGoodId()+"\"");
        }

        List<QuoteType> list = quoteTypeService.selectQuoteTypeList(quoteType);
        for(QuoteType quoteTypeData:list){
            List<String> idslist = Arrays.asList(
                    quoteTypeData.getGoodId().replaceAll("[\\[\\]\"]", "").split(", "));
            quoteTypeData.setServiceName(serviceGoodsService.selectTitlesByIds(idslist));


        }
        return getDataTable(list);
    }

    /**
     * 导出项目报价--工艺分类列表
     */
    @PreAuthorize("@ss.hasPermi('system:QuoteType:export')")
    @Log(title = "项目报价--工艺分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QuoteType quoteType)
    {

        List<QuoteType> list = quoteTypeService.selectQuoteTypeList(quoteType);
        ExcelUtil<QuoteType> util = new ExcelUtil<QuoteType>(QuoteType.class);
        util.exportExcel(response, list, "项目报价--工艺分类数据");

    }

   /* *//**
     * 导出项目报价--工艺分类列表
     *//*
    @PreAuthorize("@ss.hasPermi('system:QuoteType:export')")
    @PostMapping("/goodsDataList")
    public AjaxResult goodsDataList(HttpServletResponse response)
    {
        List<ServiceGoods> list = serviceGoodsService.selectServiceGoodsList(new ServiceGoods());
        return toAjax(serviceGoodsService.selectServiceGoodsList(new ServiceGoods()));
    }*/
    /**
     * 获取项目报价--工艺分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:QuoteType:query')")
    @GetMapping(value = "/goodsDataList")
    public AjaxResult goodsDataList()
    {
        return success(serviceGoodsService.selectServiceGoodsList(new ServiceGoods()));
    }


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

    /**
     * 新增项目报价--工艺分类
     */
    @PreAuthorize("@ss.hasPermi('system:QuoteType:add')")
    @Log(title = "项目报价--工艺分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QuoteType quoteType)
    {
        return toAjax(quoteTypeService.insertQuoteType(quoteType));
    }

    /**
     * 修改项目报价--工艺分类
     */
    @PreAuthorize("@ss.hasPermi('system:QuoteType:edit')")
    @Log(title = "项目报价--工艺分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QuoteType quoteType)
    {
        return toAjax(quoteTypeService.updateQuoteType(quoteType));
    }
    /**
     * 定时任务状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:QuoteMaterialType:changeStatus')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody QuoteType quoteType)
    {
        QuoteType newquoteType = quoteTypeService.selectQuoteTypeById(quoteType.getId());
        newquoteType.setStatus(quoteType.getStatus());
        return toAjax(quoteTypeService.updateQuoteType(newquoteType));
    }



    /**
     * 删除项目报价--工艺分类
     */
    @PreAuthorize("@ss.hasPermi('system:QuoteType:remove')")
    @Log(title = "项目报价--工艺分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(quoteTypeService.deleteQuoteTypeByIds(ids));
    }
}
