/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.business.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.utils.DateFormatUtils;
import org.apache.commons.lang.StringUtils;
import org.springblade.business.entity.ImageClassificationFile;
import org.springblade.business.vo.ImageClassificationFileVO;
import org.springblade.business.mapper.ImageClassificationFileMapper;
import org.springblade.business.service.IImageClassificationFileService;
import org.springblade.business.vo.TreeVo;
import org.springblade.common.utils.CommonUtil;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.*;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2022-05-24
 */
@Service
public class ImageClassificationFileServiceImpl extends BaseServiceImpl<ImageClassificationFileMapper, ImageClassificationFile> implements IImageClassificationFileService {

	@Override
	public Integer queryCurrentClassifyAllFileCount(String projectId, String contractId, Long classifyId) {
		return this.baseMapper.queryCurrentClassifyAllFileCount(projectId, contractId, classifyId);
	}

	@Override
	public List<TreeVo> getYearDateTree(String classifyId, String projectId, String contractId) {
		//获取时间
		List<Date> shootingTimes = this.baseMapper.selectShootingTimeByClassifyAndProjectId(classifyId, projectId, contractId);

		//最终集合
		List<TreeVo> result = new ArrayList<>();
		//年
		Map<String,List<String>> yearMap = new HashMap<>();
		for(Date date : shootingTimes){
			//转换为字符串后拆分获取年月日
			String dateStr = DateFormatUtils.format(date, "yyyy-MM-dd");
			String[] dateArray = dateStr.split("-");
			List<String> monthList;
			if(yearMap.containsKey(dateArray[0] + "年")){
				//存在，获取集合
				monthList = yearMap.get(dateArray[0] + "年");
			} else {
				//不存在，创建
				monthList = new ArrayList<>();
			}
			monthList.add(dateArray[1] + "-" + dateArray[2]);
			yearMap.put(dateArray[0] + "年", monthList);
		}

		//循环年
		for(Map.Entry<String,List<String>> yearMaps : yearMap.entrySet()){
			String year = yearMaps.getKey().replace("年", "");
			List<String> monthList = yearMaps.getValue();

			//月集合
			List<TreeVo> monthResult = new ArrayList<>();

			//月
			TreeVo monthMap = new TreeVo();

			//循环月份
			String month = monthList.get(0).split("-")[0];
			int index = 0;
			for(String monthDay : monthList){
				//拆分
				String[] monthDays = monthDay.split("-");

				if(!month.equals(monthDays[0])){
					month = monthDays[0];
					monthResult.add(monthMap);
					monthMap = new TreeVo();
				}
				monthMap.setName(month + "月");
				monthMap.setHierarchy(year + "-" + month);
				monthMap.getTreeList().add(new TreeVo(monthDays[1] + "日", new ArrayList<>(), year + "-" + month + "-" + monthDays[1]));
				index ++;
				if(index == monthList.size()){
					monthResult.add(monthMap);
				}
			}
			//年
			TreeVo yearResult = new TreeVo(year, monthResult, year);
			result.add(yearResult);
		}

		return result;
	}

	@Override
	public IPage<ImageClassificationFileVO> selectImageClassificationFilePage(IPage<ImageClassificationFileVO> page, ImageClassificationFileVO imageClassificationFile) {
		long current = (page.getCurrent() - 1L) * page.getSize();
		if(StringUtils.isNotEmpty(imageClassificationFile.getWbsIdsStr())){
			//查询下级节点信息
			imageClassificationFile.setWbsIds(JSONArray.parseArray(JSONObject.toJSONString(imageClassificationFile.getWbsIdsStr().split(",")), String.class));
		}
		//获取数据
		List<ImageClassificationFile> fileVOS = this.baseMapper.selectImageClassificationFilePage(current, page.getSize(), imageClassificationFile);
		if(fileVOS != null && fileVOS.size() > 0){
			//分组
			List<List<ImageClassificationFile>> group = CommonUtil.getBatchSize(fileVOS, new Integer(String.valueOf(page.getSize())));

			//获取数据并设置分页信息
			List<ImageClassificationFile> result = group.get(new Integer(String.valueOf(page.getCurrent())) - 1);
			//转换类型
			List<ImageClassificationFileVO> resultVo = JSONArray.parseArray(JSONObject.toJSONString(result), ImageClassificationFileVO.class);

			//处理文件大小单位
			resultVo.forEach(vo -> {
				//获取文件大小
				String fileSize = vo.getFileSize();
				if(StringUtils.isNotEmpty(fileSize) && CommonUtil.checkIsBigDecimal(fileSize)){
					long size = new Long(fileSize);
					if(size < 1024L){
						vo.setFileSize(vo.getFileSize() + "B");
					} else {
						vo.setFileSize((size / 1024) + "K");
					}
				}
			});

			page.setTotal(fileVOS.size());
			page.setRecords(resultVo);
		}

		return page;
	}

}
