package com.ruoyi.web.controller.ehs;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.materialClass.domain.MaterialClass;
import com.ruoyi.materialClass.domain.MaterialClassSub;
import com.ruoyi.materialClass.service.IMaterialClassSubService;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.materialClass.domain.MaterialClassMaster;
import com.ruoyi.materialClass.service.IMaterialClassMasterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物资分类一级分类Controller
 *
 * @author ruoyi
 * @date 2024-01-05
 */
@Api(tags="物资分类一级分类管理")
@RestController
@RequestMapping("/materialClass/materialClassMaster")
public class MaterialClassMasterController extends BaseController
{
    @Autowired
    private IMaterialClassMasterService materialClassMastService;
	@Autowired
	private IMaterialClassSubService materialClassSubService;
/**
 * 查询物资分类一级分类列表
 */
@PreAuthorize("@ss.hasPermi('materialClass:materialClassMaster:list')")
@GetMapping("/list")
	public TableDataInfo list(MaterialClassMaster materialClassMast)
	{
		startPage();
		List<MaterialClassMaster> list = materialClassMastService.selectMaterialClassMastList(materialClassMast);
		return getDataTable(list);
	}

	/**
	 * 导出物资分类一级分类列表
	 */
	@PreAuthorize("@ss.hasPermi('materialClass:materialClassMaster:export')")
	@Log(title = "物资分类一级分类", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, MaterialClassMaster materialClassMast)
	{
		List<MaterialClassMaster> list = materialClassMastService.selectMaterialClassMastList(materialClassMast);
		ExcelUtil<MaterialClassMaster> util = new ExcelUtil<MaterialClassMaster>(MaterialClassMaster.class);
		util.exportExcel(response, list, "物资分类一级分类数据");
	}

	/**
	 * 获取物资分类一级分类详细信息
	 */
	@PreAuthorize("@ss.hasPermi('materialClass:materialClassMaster:query')")
	@GetMapping(value = "/{classMastId}")
	public AjaxResult getInfo(@PathVariable("classMastId") String classMastId)
	{
		return success(materialClassMastService.selectMaterialClassMastByClassMastId(classMastId));
	}

    /**
     * 新增物资分类一级分类
     */
    @ApiOperation("新增物资分类一级分类")
    @PreAuthorize("@ss.hasPermi('materialClass:materialClassMaster:add')")
    @Log(title = "物资分类一级分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MaterialClassMaster materialClassMast)
    {
        return toAjax(materialClassMastService.insertMaterialClassMast(materialClassMast));
    }

    /**
     * 修改物资分类一级分类
     */
    @ApiOperation("修改物资分类一级分类")
    @PreAuthorize("@ss.hasPermi('materialClass:materialClassMaster:edit')")
    @Log(title = "物资分类一级分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MaterialClassMaster materialClassMast)
    {
        return toAjax(materialClassMastService.updateMaterialClassMast(materialClassMast));
    }

    /**
     * 删除物资分类一级分类
     */
    @ApiOperation("删除物资分类一级分类")
    @PreAuthorize("@ss.hasPermi('materialClass:materialClassMaster:remove')")
    @Log(title = "物资分类一级分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(materialClassMastService.deleteMaterialClassMastByClassMastIds(ids));
    }

	/**
	 * 删除物资分类一级分类
	 */
	@ApiOperation("获取分类的json字符串")
	@GetMapping(value = "/getMaterialClassJson/{id}")
	public String getMaterialClassJson(@PathVariable String id) throws JsonProcessingException {
		MaterialClassMaster   msm = new MaterialClassMaster();
		msm.setClassMastType(id);
		List<MaterialClassMaster> list = materialClassMastService.selectMaterialClassMastList(msm);
		List<MaterialClass> mcList = new ArrayList<>();
		for(MaterialClassMaster mcm :list){
			MaterialClass mc = new MaterialClass();
			mc.setLabel(mcm.getClassNameMast());
			mc.setValue(mcm.getClassMastId());
			MaterialClassSub mcs = new MaterialClassSub();
			mcs.setMastId(mcm.getId());
			List<MaterialClassSub> sublist = materialClassSubService.selectMaterialClassSubList(mcs);
			List<MaterialClass> mcSubList = new ArrayList<>();
			for(MaterialClassSub mcss:sublist){
				MaterialClass mcsub = new MaterialClass();
				mcsub.setLabel(mcss.getClassNameSub());
				mcsub.setValue(mcss.getClassSubId());
				mcSubList.add(mcsub);
			}
			mc.setChildren(mcSubList);
			mcList.add(mc);
		}
		//String jsion = JSONUtil.toJsonStr(list);
		ObjectMapper objectMapper = new ObjectMapper();
		String jsonStr = objectMapper.writeValueAsString(mcList);
		System.out.println(jsonStr);
		return jsonStr;
	}
	@ApiOperation("获取分类的json字符串")
	@PostMapping(value = "/setMaterialClassJson")
	public String setMaterialClassJson(MaterialClassMaster materialClassMaster) throws IOException {
		String jsonStr = materialClassMaster.getClassNameMast();
		/*for(MaterialClass mc :mcList){
			MaterialClassMaster mcm = new MaterialClassMaster();
			mcm.setClassMastType(id);
			mcm.setClassMastId(mc.getValue());
			mcm.setClassNameMast(mc.getLabel());
		}*/
		for(int m=2;m<=8;m++) {
			InputStreamReader isr = new InputStreamReader(new FileInputStream("C:\\materialClass"+m+".json"), "utf8");
			BufferedReader br = new BufferedReader(isr);
			StringBuffer resposeBuffer = new StringBuffer("");
			String lineTxt = null;
			String s = "";
			//将文件内容全部拼接到 字符串s
			while ((lineTxt = br.readLine()) != null) {
				s += lineTxt;
			}
			JSONArray array = (JSONArray) JSONUtil.parseArray(s); // 将JSON字符串转换为JSONArray对象

			for (int i = 0; i < array.size(); i++) {
				cn.hutool.json.JSONObject obj = array.getJSONObject(i);
				MaterialClassMaster mcm = new MaterialClassMaster();
				mcm.setClassMastType(String.valueOf(m));
				mcm.setClassMastId(obj.getStr("value"));
				mcm.setClassNameMast(obj.getStr("label"));
				materialClassMastService.insertMaterialClassMast(mcm);
				JSONArray subArray = obj.getJSONArray("children");
				for (int j = 0; j < subArray.size(); j++) {
					cn.hutool.json.JSONObject subObj = subArray.getJSONObject(j);
					MaterialClassSub sub = new MaterialClassSub();
					sub.setClassMastId(obj.getStr("value"));
					sub.setClassSubId(subObj.getStr("value"));
					sub.setClassNameSub(subObj.getStr("label"));
					sub.setClassMastType(mcm.getClassMastType());
					sub.setMastId(mcm.getId());
					materialClassSubService.save(sub);
				}
			}
		}
		return "ok";
	}

}
