
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.DateUtil;
import org.sky.core.tool.utils.Func;
import org.sky.scientific.excel.YsZjfyycqdtfyExcel;
import org.sky.scientific.mapper.AssetMapper;
import org.sky.scientific.mapper.XmMapper;
import org.sky.scientific.mapper.YsZjfyycqdtfyMapper;
import org.sky.scientific.pojo.entity.AssetEntity;
import org.sky.scientific.pojo.entity.YsZjfyycqdtfyEntity;
import org.sky.scientific.pojo.vo.XmVO;
import org.sky.scientific.pojo.vo.YsZjfyycqdtfyVO;
import org.sky.scientific.service.IYsZjfyycqdtfyService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 项目预算-折旧费用与长期待摊费用 服务实现类
 *
 * @author Kd
 * @since 2025-06-29
 */
@Service
@AllArgsConstructor
public class YsZjfyycqdtfyServiceImpl extends BaseServiceImpl<YsZjfyycqdtfyMapper, YsZjfyycqdtfyEntity> implements IYsZjfyycqdtfyService {

	private final XmMapper xmMapper;
	private final AssetMapper assetMapper;

	@Override
	public IPage<YsZjfyycqdtfyVO> selectPageByXmId(IPage<YsZjfyycqdtfyVO> page, Long xmId) {
		return page.setRecords(baseMapper.selectPageByXmId(page, xmId));
	}


	@Override
	public List<YsZjfyycqdtfyExcel> exportYsZjfyycqdtfy(Wrapper<YsZjfyycqdtfyEntity> queryWrapper) {
		List<YsZjfyycqdtfyExcel> ysZjfyycqdtfyList = baseMapper.exportYsZjfyycqdtfy(queryWrapper);

		return ysZjfyycqdtfyList;
	}

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

	@Override
	public boolean updateZjfyycqdtfy(YsZjfyycqdtfyEntity ysZjfyycqdtfy) {
		if(ysZjfyycqdtfy.getXmId() == null){
			throw new ServiceException("请选择科研项目");
		}
		return this.updateById(ysZjfyycqdtfy);
	}

	@Override
	public void importZjfyycqdtfy(MultipartFile file, Long xmId) {
		if (Func.isNull(xmId)) {
			throw new ServiceException("请选择研发项目");
		}
		XmVO xmVO = xmMapper.selectByXmId(xmId);
		if (Func.isNull(xmVO)) {
			throw new ServiceException("研发项目不存在");
		}

		//查询项目开始时间对应月份的资产清单
		List<AssetEntity> dbAssetList = assetMapper.selectList(Wrappers.<AssetEntity>lambdaQuery().eq(AssetEntity::getYearAndMonth, DateUtil.format(xmVO.getXmkssj(), DateUtil.PATTERN_YYYYMM)));

		if (Func.isEmpty(dbAssetList)) {
			throw new ServiceException(xmVO.getXmkssj() + "资产清单无数据");
		}

		List<YsZjfyycqdtfyExcel> dataList = ExcelUtil.read(file, 0, 3, YsZjfyycqdtfyExcel.class);
		if(Func.isEmpty(dataList)){
			throw new ServiceException("读取导入数据失败，请检查数据");
		}
		List<YsZjfyycqdtfyEntity> dbYsList = baseMapper.selectList(Wrappers.<YsZjfyycqdtfyEntity>lambdaQuery().eq(YsZjfyycqdtfyEntity::getXmId, xmId));

		List<YsZjfyycqdtfyEntity> insertList = new ArrayList<>();
		int batchCount = 20;
		for (YsZjfyycqdtfyExcel excel : dataList) {

			if(Func.isBlank(excel.getZcmc())){
				throw new ServiceException("序号["+excel.getXh()+"]的资产名称为空");
			}
			YsZjfyycqdtfyEntity entity = new YsZjfyycqdtfyEntity();
			entity.setXmId(xmId);

			List<Long> list = dbAssetList.stream()
				.filter(asset -> Func.equals(asset.getZcmc(), excel.getZcmc()))
				.map(AssetEntity::getId).toList();
			if (Func.isEmpty(list)) {
				throw new ServiceException("资产[" + excel.getZcmc() + "]不存在");
			}

			if(dbYsList.stream().anyMatch(ys -> Func.equals(ys.getAssetId(), list.get(0)))){
				throw new ServiceException("序号["+excel.getXh()+"]的资产已在预算表中");
			}

			BeanUtil.copyProperties(excel, entity);
			entity.setAssetId(list.get(0));
			insertList.add(entity);

			if (insertList.size() % batchCount == 0 && this.saveBatch(insertList)) {
				insertList.clear();
			}
		}

		if (Func.isNotEmpty(insertList)) {
			this.saveBatch(insertList);
		}
	}

}
