
package org.sky.scientific.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
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.XmJtbExcel;
import org.sky.scientific.mapper.DepartmentMapper;
import org.sky.scientific.mapper.TechnicianMapper;
import org.sky.scientific.mapper.XmJtbMapper;
import org.sky.scientific.pojo.dto.XmJtbDTO;
import org.sky.scientific.pojo.entity.XmJtbEntity;
import org.sky.scientific.pojo.vo.XmJtbVO;
import org.sky.scientific.service.IXmJtbService;
import org.springframework.stereotype.Service;

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

/**
 * 科研项目-项目结题表 服务实现类
 *
 * @author Kd
 * @since 2025-07-01
 */
@Service
@AllArgsConstructor
public class XmJtbServiceImpl extends BaseServiceImpl<XmJtbMapper, XmJtbEntity> implements IXmJtbService {

	private final TechnicianMapper technicianMapper;
	private final DepartmentMapper departmentMapper;

	@Override
	public XmJtbVO selectByCondition(XmJtbDTO xmJtb) {
		if (Objects.isNull(xmJtb.getXmId())) {
			throw new ServiceException("请选择研发项目");
		}
		XmJtbVO vo = baseMapper.selectByCondition(xmJtb);
		if(!Func.isNull( vo)){
			vo.getXmEntity().setXmfzrxm(technicianMapper.getNameById(vo.getXmEntity().getXmfzr()));
			vo.getXmEntity().setXmsqdw(departmentMapper.getFullNameById(vo.getXmEntity().getXmsqdwid()));
		}
		return vo;
	}

	@Override
	public boolean submit(XmJtbEntity xmJtb) {
		if (Objects.isNull(xmJtb.getXmId())) {
			throw new ServiceException("请选择科研项目");
		}
		boolean result = false;
		if (this.isFieldDuplicate(XmJtbEntity::getXmId, xmJtb.getXmId())) {
			result = this.update(xmJtb, Wrappers.<XmJtbEntity>lambdaUpdate().eq(XmJtbEntity::getXmId, xmJtb.getXmId()));
		} else {
			result = this.save(xmJtb);
		}
		return result;
	}

	@Override
	public List<XmJtbExcel> exportXmJtb(Wrapper<XmJtbEntity> queryWrapper) {
		List<XmJtbExcel> xmJtbList = baseMapper.exportXmJtb(queryWrapper);
		//xmJtbList.forEach(xmJtb -> {
		//	xmJtb.setTypeName(DictCache.getValue(DictEnum.YES_NO, XmJtb.getType()));
		//});
		return xmJtbList;
	}

}
