
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.Func;
import org.sky.core.tool.utils.StringUtil;
import org.sky.scientific.excel.CgQtExcel;
import org.sky.scientific.mapper.CgQtMapper;
import org.sky.scientific.mapper.XmMapper;
import org.sky.scientific.pojo.dto.CgQtDTO;
import org.sky.scientific.pojo.entity.CgQtEntity;
import org.sky.scientific.pojo.entity.XmEntity;
import org.sky.scientific.pojo.vo.CgQtVO;
import org.sky.scientific.pojo.vo.XmCGSummary;
import org.sky.scientific.service.ICgQtService;
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 CgQtServiceImpl extends BaseServiceImpl<CgQtMapper, CgQtEntity> implements ICgQtService {

	@Resource
	private XmMapper xmMapper;


	@Override
	public List<CgQtExcel> export(Wrapper<CgQtEntity> queryWrapper) {
		List<CgQtExcel> cgQtList = baseMapper.exportCgQt(queryWrapper);
		//cgQtList.forEach(cgQt -> {
		//	cgQt.setTypeName(DictCache.getValue(DictEnum.YES_NO, CgQt.getType()));
		//});
		return cgQtList;
	}

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

	@Override
	public boolean save(CgQtEntity cgQt) {

		return super.save(cgQt);
	}

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

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

    @Override
    public IPage<XmCGSummary> xmCgSummary(IPage<XmCGSummary> page, Long xmId) {
		if(Func.isNull(xmId)){
			throw new ServiceException("参数xmId不能为空");
		}
		List<XmCGSummary> list = baseMapper.xmCgSummary(page, xmId);
        return page.setRecords(list);
    }

	@Override
	public IPage<CgQtVO> selectQtPage(IPage<CgQtVO> page, CgQtDTO dto) {
		List<CgQtVO> list = baseMapper.selectQtPage(page, dto);
		return page.setRecords(list);
	}

}
