
package org.sky.scientific.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.sky.core.excel.util.ExcelUtil;
import org.sky.core.log.exception.ServiceException;
import org.sky.core.mp.base.BaseServiceImpl;
import org.sky.core.tool.utils.*;
import org.sky.scientific.excel.XmOtherCostExcel;
import org.sky.scientific.mapper.XmMapper;
import org.sky.scientific.mapper.XmOtherCostMapper;
import org.sky.scientific.pojo.dto.XmOtherCostQuery;
import org.sky.scientific.pojo.entity.XmOtherCostEntity;
import org.sky.scientific.service.IXmOtherCostService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 项目-其他费用-基本信息台账 服务实现类
 *
 * @author Kd
 * @since 2025-07-28
 */
@Service
@AllArgsConstructor
public class XmOtherCostServiceImpl extends BaseServiceImpl<XmOtherCostMapper, XmOtherCostEntity> implements IXmOtherCostService {

	private final XmMapper xmMapper;

	@Override
	public IPage<XmOtherCostEntity> selectPage(IPage<XmOtherCostEntity> page, XmOtherCostQuery param) {
		return page.setRecords(baseMapper.selectXmOtherCostPage(page, param));
	}


	@Override
	public List<XmOtherCostExcel> export(Wrapper<XmOtherCostEntity> queryWrapper) {
		List<XmOtherCostExcel> list = baseMapper.exportXmOtherCost(queryWrapper);
		return list;
	}

	@Override
	@Transactional
	public void importData(MultipartFile file, String yearAndMonth) {
		if (StringUtil.isBlank(yearAndMonth)) {
			throw new ServiceException("参数yearAndMonth不能为空");
		}

		List<XmOtherCostExcel> dataList = ExcelUtil.read(file, XmOtherCostExcel.class);
		if (CollectionUtil.isEmpty(dataList)) {
			throw new ServiceException("导入数据为空");
		}

		List<XmOtherCostEntity> insertList = new ArrayList<>();
		for (XmOtherCostExcel data : dataList) {
			XmOtherCostEntity entity = Objects.requireNonNull(BeanUtil.copyProperties(data, XmOtherCostEntity.class));

			//TODO 根据四级科目查询科目id，且三级科目必须要其他费用，如果不存在则报错
			Long subjectId = null;
//				xmMapper.getSubjectIdBySubjectName(data.getSubjectName());
			if (Func.isNull(subjectId)) {
				throw new ServiceException("序号[" + data.getXh() + "]的费用内容[" + data.getSubjectName() + "]不是其他费用的四级科目");
			}

			if (!Func.equals(yearAndMonth, DateUtil.format(data.getRecordDate(), DateUtil.PATTERN_YYYYMM))) {
				throw new ServiceException("序号[" + data.getXh() + "]的日期[" + data.getRecordDate() + "]不属于[" + yearAndMonth + "]");
			}

			if (StringUtil.isAllBlank(data.getXmmc(), data.getXmbh())) {
				throw new ServiceException("序号[" + data.getXh() + "]的研发项目编号和名称都为空，请重新填写数据");
			}
			//根据项目名称或编号查询xmId
			Long xmId = xmMapper.getXmIdByXmmcOrXmbh(yearAndMonth, data.getXmmc(), data.getXmbh());
			if (xmId == null) {
				throw new ServiceException("序号[" + data.getXh() + "]的研发项目不存在，请检查模板数据");
			}

			entity.setXmId(xmId);
			entity.setYearAndMonth(yearAndMonth);
			entity.setSubjectId(subjectId);
			insertList.add(entity);
		}

		this.saveBatch(insertList);
	}

	@Override
	public boolean submit(XmOtherCostEntity entity) {
		if (StringUtil.isBlank(entity.getYearAndMonth())) {
			throw new ServiceException("参数yearAndMonth不能为空");
		}
		return this.saveOrUpdate(entity);
	}

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

}
