
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.Func;
import org.sky.scientific.excel.YsZbtsysyfyExcel;
import org.sky.scientific.mapper.XmMapper;
import org.sky.scientific.mapper.YsZbtsysyfyMapper;
import org.sky.scientific.pojo.entity.YsZbtsysyfyEntity;
import org.sky.scientific.pojo.vo.YsZbtsysyfyVO;
import org.sky.scientific.service.IYsZbtsysyfyService;
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 YsZbtsysyfyServiceImpl extends BaseServiceImpl<YsZbtsysyfyMapper, YsZbtsysyfyEntity> implements IYsZbtsysyfyService {

	private final XmMapper xmMapper;

	@Override
	public IPage<YsZbtsysyfyVO> selectYsZbtsysyfyPage(IPage<YsZbtsysyfyVO> page, YsZbtsysyfyVO ysZbtsysyfy) {
		return page.setRecords(baseMapper.selectYsZbtsysyfyPage(page, ysZbtsysyfy));
	}


	@Override
	public List<YsZbtsysyfyExcel> exportYsZbtsysyfy(Wrapper<YsZbtsysyfyEntity> queryWrapper) {
		List<YsZbtsysyfyExcel> ysZbtsysyfyList = baseMapper.exportYsZbtsysyfy(queryWrapper);
		//ysZbtsysyfyList.forEach(ysZbtsysyfy -> {
		//	ysZbtsysyfy.setTypeName(DictCache.getValue(DictEnum.YES_NO, YsZbtsysyfy.getType()));
		//});
		return ysZbtsysyfyList;
	}

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

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

	@Override
	public void importZbtsysyfy(MultipartFile file, Long xmId) {
		if (Func.isNull(xmId)) {
			throw new ServiceException("请选择研发项目");
		}

		List<YsZbtsysyfyExcel> dataList = ExcelUtil.read(file, 0, 3, YsZbtsysyfyExcel.class);
		if (Func.isEmpty(dataList)) {
			throw new ServiceException("读取导入数据失败，请检查数据");
		}

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

			YsZbtsysyfyEntity entity = new YsZbtsysyfyEntity();
			entity.setXmId(xmId);
			entity.setFyje(excel.getFyje());
			entity.setYsje(excel.getYsje());

			insertList.add(entity);

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

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

}
