
package org.sky.scientific.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.ArrayUtils;
import org.sky.core.log.exception.ServiceException;
import org.sky.core.tool.utils.CollectionUtil;
import org.sky.core.tool.utils.StringUtil;
import org.sky.scientific.pojo.entity.YfzdEntity;
import org.sky.scientific.pojo.vo.YfzdVO;
import org.sky.scientific.excel.YfzdExcel;
import org.sky.scientific.mapper.YfzdMapper;
import org.sky.scientific.service.IYfzdService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.sky.core.mp.base.BaseServiceImpl;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 研发制度 服务实现类
 *
 * @author Kd
 * @since 2025-06-29
 */
@Service
public class YfzdServiceImpl extends BaseServiceImpl<YfzdMapper, YfzdEntity> implements IYfzdService {

	@Override
	public IPage<YfzdVO> selectYfzdPage(IPage<YfzdVO> page, YfzdVO yfzd) {
		return page.setRecords(baseMapper.selectYfzdPage(page, yfzd));
	}


	@Override
	public List<YfzdExcel> exportYfzd(Wrapper<YfzdEntity> queryWrapper) {
		List<YfzdExcel> yfzdList = baseMapper.exportYfzd(queryWrapper);
		//yfzdList.forEach(yfzd -> {
		//	yfzd.setTypeName(DictCache.getValue(DictEnum.YES_NO, Yfzd.getType()));
		//});
		return yfzdList;
	}

	@Override
	public boolean editYfzd(YfzdEntity yfzd) {
		return this.saveOrUpdate(yfzd);
	}

	@Override
	public Map<String, List<YfzdEntity>> selectYfzd(YfzdEntity yfzd) {
		if(StringUtil.isBlank(yfzd.getYearAndMonth())){
			throw new ServiceException("请选择年份");
		}
		List<String> wjlxTypeList = baseMapper.selectWJLXType();
		LambdaQueryWrapper<YfzdEntity> queryWrapper = Wrappers.<YfzdEntity>lambdaQuery().eq(YfzdEntity::getYearAndMonth, yfzd.getYearAndMonth());
		List<YfzdEntity> yfzdList = baseMapper.selectList(queryWrapper);
		Map<String, List<YfzdEntity>> collected = yfzdList.stream().collect(Collectors.groupingBy(YfzdEntity::getWjlx));
		if(collected.isEmpty()){
			collected = new HashMap<>();
		}
		for (String wjlxType : wjlxTypeList) {
			if (!collected.keySet().contains(wjlxType)) {
				collected.put(wjlxType, new ArrayList<YfzdEntity>());
			}
		}
		return collected;
	}

	@Override
	public boolean physicalDeleteByIds(List<Long> ids) {
		return baseMapper.physicalDeleteByIds(ids);
	}

}
