
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.log.exception.ServiceException;
import org.sky.core.mp.base.BaseServiceImpl;
import org.sky.scientific.excel.XmBgrzExcel;
import org.sky.scientific.mapper.DepartmentMapper;
import org.sky.scientific.mapper.TechnicianMapper;
import org.sky.scientific.mapper.XmBgrzMapper;
import org.sky.scientific.pojo.entity.XmBgrzEntity;
import org.sky.scientific.pojo.vo.XmBgrzVO;
import org.sky.scientific.service.IXmBgrzService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 项目-变更记录 服务实现类
 *
 * @author Kd
 * @since 2025-06-29
 */
@Service
@AllArgsConstructor
public class XmBgrzServiceImpl extends BaseServiceImpl<XmBgrzMapper, XmBgrzEntity> implements IXmBgrzService {

	private final DepartmentMapper departmentMapper;
	private final TechnicianMapper technicianMapper;

	@Override
	public List<XmBgrzExcel> exportXmBgrz(Wrapper<XmBgrzEntity> queryWrapper) {
		List<XmBgrzExcel> xmBgrzList = baseMapper.exportXmBgrz(queryWrapper);
		//xmBgrzList.forEach(xmBgrz -> {
		//	xmBgrz.setTypeName(DictCache.getValue(DictEnum.YES_NO, XmBgrz.getType()));
		//});
		return xmBgrzList;
	}

    @Override
    public boolean saveBgrz(XmBgrzEntity xmBgrz) {
		if(xmBgrz.getXmId() == null){
			throw new ServiceException("请选择需要变更的项目");
		}

        return this.save(xmBgrz);
    }

	@Override
	public IPage<XmBgrzVO> selectBgrzPage(XmBgrzEntity xmBgrz, IPage<XmBgrzVO> page) {
		List<XmBgrzVO> voList = baseMapper.selectBgrzPage(page, xmBgrz);
		for (XmBgrzVO vo : voList) {
			vo.setXmsqdw(departmentMapper.getFullNameById(vo.getXmsqdwid()));
			if(vo.getZdmc().equalsIgnoreCase("XMSQDWID")){
				// 项目申请单位
				vo.setBgqdz(departmentMapper.getFullNameById(Long.parseLong(vo.getBgqdz())));
				vo.setBghdz(departmentMapper.getFullNameById(Long.parseLong(vo.getBghdz())));
			}else if(vo.getZdmc().equalsIgnoreCase("XMFZR")){
				vo.setBgqdz(technicianMapper.getNameById(Long.parseLong(vo.getBgqdz())));
				vo.setBghdz(technicianMapper.getNameById(Long.parseLong(vo.getBghdz())));
			}
		}
		return page.setRecords(voList);
	}

}
