
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 jakarta.annotation.Resource;
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.StringUtil;
import org.sky.scientific.excel.CgRjzzqExcel;
import org.sky.scientific.mapper.CgRjzzqMapper;
import org.sky.scientific.mapper.XmMapper;
import org.sky.scientific.pojo.dto.CgRjzzqDTO;
import org.sky.scientific.pojo.entity.CgRjzzqEntity;
import org.sky.scientific.pojo.entity.XmEntity;
import org.sky.scientific.pojo.vo.CgRjzzqVO;
import org.sky.scientific.service.ICgRjzzqService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

/**
 * 科研成果-软件著作 服务实现类
 *
 * @author Kd
 * @since 2025-06-29
 */
@Service
public class CgRjzzqServiceImpl extends BaseServiceImpl<CgRjzzqMapper, CgRjzzqEntity> implements ICgRjzzqService {

	@Resource
	private XmMapper xmMapper;

	@Override
	public List<CgRjzzqExcel> export(Wrapper<CgRjzzqEntity> queryWrapper) {
		List<CgRjzzqExcel> cgRjzzqList = baseMapper.exportCgRjzzq(queryWrapper);
		//cgRjzzqList.forEach(cgRjzzq -> {
		//	cgRjzzq.setTypeName(DictCache.getValue(DictEnum.YES_NO, CgRjzzq.getType()));
		//});
		return cgRjzzqList;
	}

	@Transactional
    @Override
    public void importRjzzq(MultipartFile file) {
		List<CgRjzzqExcel> data = ExcelUtil.read(file, 0, 2, CgRjzzqExcel.class);
		if(CollectionUtil.isEmpty(data)){
			throw new ServiceException("未能从模板中读取数据，请检查模板格式是否正确");
		}
		for (CgRjzzqExcel excel : data) {
			CgRjzzqEntity zlDTO = Objects.requireNonNull(BeanUtil.copyProperties(excel, CgRjzzqDTO.class));
			if (!StringUtil.isAllBlank(excel.getXmbh(), excel.getXmmc())) {
				Long xmId = xmMapper.getXmIdByXmmcOrXmbh(null, excel.getXmmc(), excel.getXmbh());
				if (xmId == null) {
					throw new ServiceException("项目不存在，导入失败，请检查数据");
				}
				zlDTO.setXmId(xmId);
			}
			this.save(zlDTO);
		}
    }

	@Override
	public boolean save(CgRjzzqEntity cgRjzzq) {

		return super.save(cgRjzzq);
	}

	@Override
	public boolean update(CgRjzzqEntity cgRjzzq) {
		CgRjzzqEntity dbRjzzq = this.getById(cgRjzzq.getId());
		if (!Objects.equals(dbRjzzq.getXmbh(), cgRjzzq.getXmbh())) {
			XmEntity dbXm = xmMapper.selectOne(Wrappers.<XmEntity>lambdaQuery().eq(XmEntity::getXmbh, cgRjzzq.getXmbh()));
			cgRjzzq.setXmId(dbXm != null ? dbXm.getId() : null);
			cgRjzzq.setXmmc(dbXm != null ? dbXm.getXmmc() : null);
		}
		return updateById(cgRjzzq);
	}

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

	@Override
	public IPage<CgRjzzqVO> selectRjzzqPage(IPage<CgRjzzqVO> page, CgRjzzqDTO dto) {
		return baseMapper.selectRjzzqPage(page, dto);
	}

}
