
package org.sky.scientific.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.YsWxzctxfyExcel;
import org.sky.scientific.mapper.AssetMapper;
import org.sky.scientific.mapper.XmMapper;
import org.sky.scientific.mapper.YsWxzctxfyMapper;
import org.sky.scientific.pojo.entity.AssetEntity;
import org.sky.scientific.pojo.entity.YsWxzctxfyEntity;
import org.sky.scientific.pojo.vo.XmVO;
import org.sky.scientific.pojo.vo.YsWxzctxfyVO;
import org.sky.scientific.service.IYsWxzctxfyService;
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 YsWxzctxfyServiceImpl extends BaseServiceImpl<YsWxzctxfyMapper, YsWxzctxfyEntity> implements IYsWxzctxfyService {

	private final AssetMapper assetMapper;
	private final XmMapper xmMapper;

	@Override
	public IPage<YsWxzctxfyVO> selectYsWxzctxfyPage(IPage<YsWxzctxfyVO> page, YsWxzctxfyVO ysWxzctxfy) {
		return page.setRecords(baseMapper.selectYsWxzctxfyPage(page, ysWxzctxfy));
	}


	@Override
	public List<YsWxzctxfyExcel> exportYsWxzctxfy(Wrapper<YsWxzctxfyEntity> queryWrapper) {
		List<YsWxzctxfyExcel> ysWxzctxfyList = baseMapper.exportYsWxzctxfy(queryWrapper);
		//ysWxzctxfyList.forEach(ysWxzctxfy -> {
		//	ysWxzctxfy.setTypeName(DictCache.getValue(DictEnum.YES_NO, YsWxzctxfy.getType()));
		//});
		return ysWxzctxfyList;
	}

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

	@Override
	public boolean updateWxzctxfy(YsWxzctxfyEntity ysWxzctxfy) {
		if (ysWxzctxfy.getId() == null) {
			throw new ServiceException("id不能为空");
		}
		if (ysWxzctxfy.getXmId() == null) {
			throw new ServiceException("请选择科研项目");
		}
		LambdaQueryWrapper<YsWxzctxfyEntity> wrapper = Wrappers.<YsWxzctxfyEntity>lambdaQuery()
			.eq(YsWxzctxfyEntity::getXmId, ysWxzctxfy.getXmId())
			.eq(YsWxzctxfyEntity::getAssetId, ysWxzctxfy.getAssetId());
		YsWxzctxfyEntity dbYs = baseMapper.selectOne(wrapper);
		if (dbYs != null && !Func.equals(dbYs.getId(), ysWxzctxfy.getId())) {
			throw new ServiceException("资产已在预算中");
		}
		return this.updateById(ysWxzctxfy);
	}

	@Override
	public void importWxzctxfy(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<YsWxzctxfyExcel> dataList = ExcelUtil.read(file, 0, 3, YsWxzctxfyExcel.class);
		if (Func.isEmpty(dataList)) {
			throw new ServiceException("读取导入数据失败，请检查数据");
		}
		List<YsWxzctxfyEntity> dbYsList = baseMapper.selectList(Wrappers.<YsWxzctxfyEntity>lambdaQuery().eq(YsWxzctxfyEntity::getXmId, xmId));

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

			if (Func.isBlank(excel.getZcmc())) {
				throw new ServiceException("序号[" + excel.getXh() + "]的资产名称为空");
			}
			YsWxzctxfyEntity entity = new YsWxzctxfyEntity();
			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);
		}
	}

	@Override
	public boolean saveWxzctxfy(YsWxzctxfyEntity ysWxzctxfy) {

		if (ysWxzctxfy.getXmId() == null) {
			throw new ServiceException("请选择科研项目");
		}
		LambdaQueryWrapper<YsWxzctxfyEntity> wrapper = Wrappers.<YsWxzctxfyEntity>lambdaQuery()
			.eq(YsWxzctxfyEntity::getXmId, ysWxzctxfy.getXmId())
			.eq(YsWxzctxfyEntity::getAssetId, ysWxzctxfy.getAssetId());
		YsWxzctxfyEntity dbYs = baseMapper.selectOne(wrapper);
		if (dbYs != null) {
			throw new ServiceException("资产已在预算中");
		}
		return this.save(ysWxzctxfy);
	}

}
