
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.XmDesignCostExcel;
import org.sky.scientific.mapper.XmDesignCostMapper;
import org.sky.scientific.mapper.XmMapper;
import org.sky.scientific.pojo.dto.XmDesignCostQuery;
import org.sky.scientific.pojo.entity.XmDesignCostEntity;
import org.sky.scientific.pojo.vo.XmDesignCostVO;
import org.sky.scientific.service.IXmDesignCostService;
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 XmDesignCostServiceImpl extends BaseServiceImpl<XmDesignCostMapper, XmDesignCostEntity> implements IXmDesignCostService {

	private final XmMapper xmMapper;

	@Override
	public IPage<XmDesignCostVO> selectXmDesignCostPage(IPage<XmDesignCostVO> page, XmDesignCostQuery param) {
		return page.setRecords(baseMapper.selectXmDesignCostPage(page, param));
	}


	@Override
	public List<XmDesignCostExcel> exportXmDesignCost(Wrapper<XmDesignCostEntity> queryWrapper) {
		List<XmDesignCostExcel> xmDesignCostList = baseMapper.exportXmDesignCost(queryWrapper);
		//xmDesignCostList.forEach(xmDesignCost -> {
		//	xmDesignCost.setTypeName(DictCache.getValue(DictEnum.YES_NO, XmDesignCost.getType()));
		//});
		return xmDesignCostList;
	}

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

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

		List<XmDesignCostEntity> insertList = new ArrayList<>();
		for (XmDesignCostExcel data : dataList) {

			if(!Func.isNull(data.getRecordDate()) && !Func.equals(yearAndMonth,DateUtil.format(data.getRecordDate(), DateUtil.PATTERN_YYYYMM))){
				throw new ServiceException("日期["+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() + "]的研发项目不存在，请检查模板数据");
			}

			XmDesignCostEntity entity = Objects.requireNonNull(BeanUtil.copyProperties(data, XmDesignCostEntity.class));
			entity.setXmId(xmId);
			entity.setYearAndMonth(yearAndMonth);

			insertList.add(entity);
		}

		this.saveBatch(insertList);
	}

	@Override
	public boolean physicalDeleteByIds(List<Long> ids) {

		return baseMapper.physicalDeleteByIds(ids) > 0;
	}

}
