package com.zcloud.controller.filemanager;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.logs.LogAnno;
import com.zcloud.service.filemanager.MfolderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明：文件管理
 * 作者：zCloud
 * 官网：
 */
@Controller
@RequestMapping("app/mfolder")
public class AppMfolderController extends BaseController {

	@Autowired
	private MfolderService mfolderService;


	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "体系运行",instructionsOperate = "体系文件",instructionsType = "列表")
	public Object list(Page page) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		page.setPd(pd);
		List<PageData>	mfolderVal = mfolderService.list(page);					//列出Mfolder列表
		Map<String, Object> keyMap = new HashMap<>();
		List<Map<String, Object>>valList = new ArrayList<>();//返回的list
		Map<String, Object> valMap = new HashMap<>();//返回list中的map
		Map<String, Object> sonKeyVal = new HashMap<>();//
		List<Map<String, Object>> sonList = new ArrayList<>();//子list
		Map<String, Object> sonMap = new HashMap<>();//子list的map
		Map<String, Integer> valCount = new HashMap<>();//包含子数据的数量


		for(int i =0; i<mfolderVal.size(); i++) {
			PageData mf = mfolderVal.get(i);
			Integer count = 0;
			if("0".equals(mf.get("PARENT_ID").toString())) {//目录的状态
				valMap = new HashMap<>();
				valMap.put("name", mf.get("NAME"));
				valMap.put("count", "0");
				valMap.put("sonList", sonList);
				valMap.put("MFOLDER_ID", mf.get("MFOLDER_ID"));
				valList.add(valMap);
				keyMap.put(mf.getString("MFOLDER_ID").toString(), valMap);
			}else { //PARENT_ID  MFOLDER_ID
				String parentId = mf.getString("PARENT_ID");
				sonMap = new HashMap<>();
				sonMap.put("name", mf.get("NAME"));
				sonMap.put("fileSize", mf.get("FILESIZE"));
				sonMap.put("ctime", mf.get("CTIME"));
				sonMap.put("MFOLDER_ID", mf.get("MFOLDER_ID"));
				sonMap.put("filePath", mf.get("FILEPATH"));
				if(sonKeyVal.containsKey(parentId)) {
					count = valCount.get(parentId);
					sonList = (List<Map<String, Object>>) sonKeyVal.get(parentId);
				}else {
					sonList = new ArrayList<>();
					count =0;
				}
				count++;
				sonList.add(sonMap);
				valCount.put(parentId, count);
				sonKeyVal.put(parentId, sonList);
			}
		}
		//把sonlist装载带valList
		for(int i =0; i<valList.size();i++) {
			Map<String, Object> valMap2 =valList.get(i);
			String mfilderId = valMap2.get("MFOLDER_ID").toString();
			if(sonKeyVal.containsKey(mfilderId)) {
				valMap2.put("count",valCount.get(mfilderId));
				valMap2.put("sonList", sonKeyVal.get(mfilderId));
			}
		}
		map.put("varList", valList);
		map.put("result", errInfo);
		return map;
	}


}

