package org.springblade.modules.xpgxb.excel;

import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springblade.common.cache.DictCache;
import org.springblade.core.excel.support.ExcelImporter;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.xpgxb.entity.XpgxbEntity;
import org.springblade.modules.xpgxb.service.IXpgxbService;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.List;

@RequiredArgsConstructor
public class XpgxImporter implements ExcelImporter<XpgxbExcel> {

	private final IXpgxbService service;
	private final IDeptService deptService;

	@Override
	public void save(List<XpgxbExcel> data) {
		data.forEach(xpgxbExcel -> {

			if(StringUtils.isNotEmpty(xpgxbExcel.getXpmjssdw())){
				String deptName = xpgxbExcel.getXpmjssdw();
				String sssdwId = deptService.getDeptIdByDeptname(deptName);
				xpgxbExcel.setXpmjssdwdm(sssdwId);
			}
			if(StringUtils.isNotEmpty(xpgxbExcel.getXpfj())){
				String deptName = xpgxbExcel.getXpfj();
				String fjId = deptService.getDeptIdByDeptname(deptName);
				xpgxbExcel.setXpfjdm(fjId);
			}
			if(StringUtils.isNotEmpty(xpgxbExcel.getXppcs())){
				String deptName = xpgxbExcel.getXppcs();
				String pcsId = deptService.getDeptIdByDeptname(deptName);
				xpgxbExcel.setXppcsdm(pcsId);
			}
			if(StringUtils.isNotEmpty( xpgxbExcel.getXpjws())){
				String deptName = xpgxbExcel.getXpjws();
				String jwsId = deptService.getDeptIdByDeptname(deptName);
				xpgxbExcel.setXpjwsid(jwsId);
			}
//			String rylb = DictCache.getKey("ry_type", noticeExcel.getRylbMc());
//			noticeExcel.setRylb(Func.toInt(rylb));
//			String sex = DictCache.getKey("sex", noticeExcel.getSex());
//			noticeExcel.setXb(Func.toInt(sex));
//			String zw = DictCache.getKey("ry_zw", noticeExcel.getZwMc());
//			noticeExcel.setZw(Func.toInt(zw));
			XpgxbEntity xpgxbEntity = BeanUtil.copy(xpgxbExcel, XpgxbEntity.class);
			service.save(xpgxbEntity);
		});
	}
}
