package io.renren.modules.levelManage.controller;

import io.renren.common.utils.MinIoUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.levelManage.entity.LevelRelationEntity;
import io.renren.modules.levelManage.service.LevelRelationService;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 用于存储层级直接，具体数据的关系
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-04-08 17:49:50
 */
@RestController
@RequestMapping("/levelrelation")
public class LevelRelationController {
    @Autowired
    private LevelRelationService levelRelationService;

    @RequestMapping("/uploadFile")
    public R filesupload(@RequestParam MultipartFile[] files) throws Exception {
        try {
            for (MultipartFile file : files) {
                try{
                    MinIoUtils.uploadMultipartFile(file,"level",file.getOriginalFilename());
                } catch (Exception e) {
                    return R.error(file.getOriginalFilename()+ "上传失败，原因为:\n\t"
                    +e.getMessage()).put("analysis",0);
                }
            }
            return R.ok("上传成功").put("analysis",1);
        } catch (Exception e) {
            if(null != e.getMessage()){
                return R.error(e.getMessage());
            }
            return R.error("上传失败").put("analysis",0);
        }
    }
    @PostMapping("/analysisFiles")
    public R analysisFiles(@RequestBody List<String> fileNames) throws Exception{
        for(String fileName:fileNames){
            try {
                if(levelRelationService.tableHasBuilted(fileName)){
                    return R.error().put("msg",fileName+"中含有重复或已存在的表，无法进行层级建立");
                }
                InputStream file= MinIoUtils.getFileInputStream("level",fileName);
                SAXReader reader=new SAXReader();
                Document document= reader.read(file);
                Element root=document.getRootElement();
                levelRelationService.analysisLevel(root,0L, 1L);
            }
            catch (Exception e){
                return R.error().put("msg",fileName+"解析失败，原因为:"+e.getMessage());
            }
        }
        return R.ok().put("msg","解析成功");
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = levelRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		LevelRelationEntity levelRelation = levelRelationService.getById(id);

        return R.ok().put("levelRelation", levelRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody LevelRelationEntity levelRelation){
		levelRelationService.save(levelRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody LevelRelationEntity levelRelation){
		levelRelationService.updateById(levelRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		levelRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
