
package org.sky.scientific.service.impl;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.sky.core.log.exception.ServiceException;
import org.sky.core.mp.base.BaseServiceImpl;
import org.sky.core.tool.utils.Func;
import org.sky.scientific.mapper.XmFinanceMapper;
import org.sky.scientific.mapper.XmMapper;
import org.sky.scientific.pojo.entity.XmEntity;
import org.sky.scientific.pojo.entity.XmFinanceEntity;
import org.sky.scientific.service.IXmFinanceService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 会计/加计扣除/高新-研发项目清单 服务实现类
 *
 * @author Kd
 * @since 2025-06-29
 */
@Slf4j
@Service
public class XmFinanceServiceImpl extends BaseServiceImpl<XmFinanceMapper, XmFinanceEntity> implements IXmFinanceService {

	@Resource
	private XmMapper xmMapper;

	@Override
	public List<XmFinanceEntity> selectXmFinanceList(XmFinanceEntity dto) {
		if (Func.isBlank(dto.getYearAndMonth())) {
			throw new ServiceException("参数yearAndMonth不能为空");
		}
		if (dto.getType() == null || !Arrays.asList(1, 2, 3).contains(dto.getType())) {
			throw new ServiceException("参数type为空或者错误");
		}
 		return baseMapper.selectXmFinanceList(dto);
	}

	@Override
	public boolean saveFinance(XmFinanceEntity entity) {
		if (Func.isBlank(entity.getYearAndMonth())) {
			throw new ServiceException("参数yearAndMonth不能为空");
		}
		if (entity.getType() == null || !Arrays.asList(1, 2, 3).contains(entity.getType())) {
			throw new ServiceException("参数type为空或者错误");
		}
//		//根据yearAndMonth查询年度项目
//		List<XmEntity> xmListByYear = xmMapper.selectXmListByYear(entity.getYearAndMonth());
//		if (Func.isEmpty(xmListByYear)) {
//			throw new ServiceException(entity.getYearAndMonth() + "年度内无研发项目可新增");
//		}
//
//		//根据yearAndMonth和type查询已有项目
// 		List<XmFinanceEntity> dbXmFinanceList = baseMapper.selectXmFinanceList(entity);
//
//		//年度研发项目
//		List<Long> xmIdList = xmListByYear.stream().map(XmEntity::getId).toList();
//
//		//已添加的研发项目
//		List<Long> XmIdFinances = dbXmFinanceList.stream().map(XmFinanceEntity::getXmId).toList();
//		//去重,差集
//		List<Long> collect = xmIdList.stream().filter(item -> !XmIdFinances.contains(item)).toList();
//
//		if (Func.isEmpty(collect)) {
//			return true;
//		}

		List<XmFinanceEntity> list = new ArrayList<>();
		for (Long xmId : entity.getXmIdList()) {
			XmFinanceEntity finance = new XmFinanceEntity();
			finance.setYearAndMonth(entity.getYearAndMonth());
			finance.setXmId(xmId);
			finance.setType(entity.getType());
			list.add(finance);
		}
		return this.saveBatch(list);
	}

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

	@Override
	public boolean fetchFinance(XmFinanceEntity entity) {
		if (Func.isBlank(entity.getYearAndMonth())) {
			throw new ServiceException("参数yearAndMonth不能为空");
		}
		if (entity.getType() == null || !Arrays.asList(1, 2, 3).contains(entity.getType())) {
			throw new ServiceException("参数type为空或者错误");
		}
		//根据yearAndMonth查询年度项目
		List<XmEntity> xmListByYear = xmMapper.selectXmListByYear(entity.getYearAndMonth());
		if (Func.isEmpty(xmListByYear)) {
			throw new ServiceException(entity.getYearAndMonth() + "年度内无研发项目可引入");
		}
		//根据yearAndMonth和type查询已有项目
 		List<XmFinanceEntity> dbXmFinanceList = baseMapper.selectXmFinanceList(entity);
		if (Func.isEmpty(dbXmFinanceList)) {
			//本年度无会计/加计扣除/高新-研发项目清单，直接保存

		}
		//年度研发项目
		List<Long> xmIdList = xmListByYear.stream().map(XmEntity::getId).toList();

		//已添加的研发项目
		List<Long> XmIdFinances = dbXmFinanceList.stream().map(XmFinanceEntity::getXmId).toList();
		//去重,差集
		List<Long> collect = xmIdList.stream().filter(item -> !XmIdFinances.contains(item)).toList();
		//保存
		if (Func.isEmpty(collect)) {
			throw new ServiceException("年度研发项目已全部引入");
		}
		List<XmFinanceEntity> insertList = new ArrayList<>();
		for (Long xmId : collect) {
			XmFinanceEntity dto = new XmFinanceEntity();
			dto.setYearAndMonth(entity.getYearAndMonth());
			dto.setType(entity.getType());
			dto.setXmId(xmId);
			insertList.add(dto);
		}
		return this.saveBatch(insertList);
	}
}
