/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package org.springblade.modules.project.service.impl;

import org.springblade.common.cache.DictCache;
import org.springblade.common.enums.DictEnum;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.project.pojo.excel.WorkFocusExcel;
import org.springblade.modules.project.pojo.entity.WorkFocusEntity;
import org.springblade.modules.project.pojo.excel.WorkFocusExcel2;
import org.springblade.modules.project.pojo.vo.WorkFocusDataInfoVO;
import org.springblade.modules.project.pojo.vo.WorkFocusProgressDataInfoVO;
import org.springblade.modules.project.pojo.vo.WorkFocusVO;
import org.springblade.modules.project.mapper.WorkFocusMapper;
import org.springblade.modules.project.service.IWorkFocusService;
import org.springblade.modules.project.wrapper.WorkFocusWrapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * 附件表 服务实现类
 *
 * @author BladeX
 * @since 2024-04-09
 */
@Service
public class WorkFocusServiceImpl extends BaseServiceImpl<WorkFocusMapper, WorkFocusEntity> implements IWorkFocusService {

	@Override
	public IPage<WorkFocusVO> selectWorkFocusPage(IPage<WorkFocusVO> page, WorkFocusVO workfocus) {
		List<WorkFocusVO> workFocusEntities = baseMapper.selectWorkFocusPage(page, workfocus);
		return page.setRecords(workFocusEntities);
	}


	@Override
	public List<WorkFocusExcel> exportWorkFocus(Wrapper<WorkFocusEntity> queryWrapper) {
		List<WorkFocusExcel> workfocusList = baseMapper.exportWorkFocus(queryWrapper);
		//workfocusList.forEach(workfocus -> {
		//	workfocus.setTypeName(DictCache.getValue(DictEnum.YES_NO, WorkFocus.getType()));
		//});
		return workfocusList;
	}

	@Override
	public void importWorkFocus(List<WorkFocusExcel2> data, Boolean isCovered) {
		List<WorkFocusEntity> list = new ArrayList<>();

		data.forEach(regionExcel -> {
			WorkFocusEntity region = BeanUtil.copyProperties(regionExcel, WorkFocusEntity.class);
			list.add(region);
		});


		/*if (isCovered) {
			this.saveOrUpdateBatch(list);
		} else {
			this.saveBatch(list);
		}*/
	}

	@Override
	public WorkFocusDataInfoVO queryWorkFocusDataInfo(WorkFocusVO workfocus) {
		WorkFocusDataInfoVO focusDataInfoVO = new WorkFocusDataInfoVO();
		List<WorkFocusProgressDataInfoVO> data = baseMapper.queryWorkFocusDataInfo(workfocus);
		focusDataInfoVO.setTabList(data);
		focusDataInfoVO.setLastDate(DateUtil.time());
		focusDataInfoVO.setTaskCount(data.size());

		List<WorkFocusProgressDataInfoVO> data2 = data.stream().filter(o -> o.getWorkFocusStageName() != null).collect(
				Collectors.collectingAndThen(Collectors.toCollection(
						() -> new TreeSet<>(Comparator.comparing(o -> o.getWorkFocusStageName()))), ArrayList<WorkFocusProgressDataInfoVO>::new));
		focusDataInfoVO.setStagesCount(data2.size());
		return focusDataInfoVO;
	}

}
