
package org.sky.scientific.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.BeanUtil;
import org.sky.core.tool.utils.CollectionUtil;
import org.sky.core.tool.utils.DateUtil;
import org.sky.core.tool.utils.StringUtil;
import org.sky.scientific.excel.XmCqdtfyExcel;
import org.sky.scientific.mapper.XmCqdtfyMapper;
import org.sky.scientific.mapper.XmMapper;
import org.sky.scientific.pojo.dto.XmCqdtfyDTO;
import org.sky.scientific.pojo.entity.XmCqdtfyEntity;
import org.sky.scientific.pojo.vo.XmCqdtfyVO;
import org.sky.scientific.service.IXmCqdtfyService;
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-24
 */
@Service
@AllArgsConstructor
public class XmCqdtfyServiceImpl extends BaseServiceImpl<XmCqdtfyMapper, XmCqdtfyEntity> implements IXmCqdtfyService {

	private final XmMapper xmMapper;

	@Override
	public IPage<XmCqdtfyVO> selectPageByCondition(IPage<XmCqdtfyVO> page, XmCqdtfyDTO dto) {
		return page.setRecords(baseMapper.selectPageByCondition(page, dto));
	}


	@Override
	public List<XmCqdtfyExcel> exportXmCqdtfy(Wrapper<XmCqdtfyEntity> queryWrapper) {
		List<XmCqdtfyExcel> xmCqdtfyList = baseMapper.exportXmCqdtfy(queryWrapper);

		return xmCqdtfyList;
	}

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

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

		List<XmCqdtfyEntity> insertList = new ArrayList<>();
		for (XmCqdtfyExcel data : dataList) {
			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() + "]的研发项目不存在，请检查模板数据");
			}

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

			insertList.add(entity);
		}

		this.saveBatch(insertList);
	}

	@Override
	public boolean submit(XmCqdtfyEntity entity) {
		return this.saveOrUpdate(entity);
	}

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

	@Override
	public boolean fetchLastMonthData(String yearAndMonth) {
		if(StringUtil.isBlank(yearAndMonth)){
			throw new ServiceException("参数yearAndMonth不能为空");
		}
		//查询本月数据，若有，则不能导入
		Long count = baseMapper.selectCount(Wrappers.<XmCqdtfyEntity>lambdaQuery().eq(XmCqdtfyEntity::getYearAndMonth, yearAndMonth));
		if(count > 0){
			throw new ServiceException("有数据，需要删除"+yearAndMonth+"的数据才能导入");
		}
		//查询上月数据
		String lastMonth = DateUtil.format(DateUtil.minusMonths(DateUtil.parse(yearAndMonth, DateUtil.PATTERN_YYYYMM), 1), DateUtil.PATTERN_YYYYMM);
		List<XmCqdtfyEntity> lastMonthDataList = baseMapper.selectList(Wrappers.<XmCqdtfyEntity>lambdaQuery().eq(XmCqdtfyEntity::getYearAndMonth, lastMonth));
		if(CollectionUtil.isEmpty(lastMonthDataList)){
			throw new ServiceException("上个月无数据");
		}
		List<XmCqdtfyEntity> insertList = new ArrayList<>();
		for (XmCqdtfyEntity entity : lastMonthDataList) {
			entity.setYearAndMonth(yearAndMonth);
			entity.setId(null);
			entity.setCreateUser(null);
			entity.setCreateDept(null);
			entity.setCreateTime(null);
			insertList.add(entity);
		}
		return this.saveBatch(insertList);
	}

}
