package cokr.xit.fims.base.service.bean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cokr.xit.fims.base.Backup;
import cokr.xit.fims.base.dao.AdminMapper;
import cokr.xit.foundation.component.AbstractBean;
import cokr.xit.foundation.data.DataObject;

@Component("adminBean")
public class AdminBean extends AbstractBean {
	@Resource(name = "adminMapper")
	private AdminMapper adminMapper;

	public List<DataObject> getAllList(String originalTable, String pkName) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("originalTable", originalTable);
		map.put("pkName", pkName);
		return adminMapper.selectAllList(map);
	}

	public List<DataObject> getAllList(String backupTable, String originalTable, String pkName) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("originalTable", originalTable);
		map.put("backupTable", backupTable);
		map.put("pkName", pkName);
		return adminMapper.selectAllList(map);
	}

	public boolean activate(Backup backup) {
		return adminMapper.insertOriginalData(backup) > 0;
	}

	public boolean deactivate(Backup backup) {
		adminMapper.deleteBackupData(backup);

		int result1 = adminMapper.insertBackupData(backup);
		int result2 = adminMapper.deleteOriginalData(backup);

		return result1 != 0
			&& result1 == result2;
	}

	public boolean copySggStng(String taskSeCd, String srcSgg, String trgtSgg) {
		Map<String, Object> param = new HashMap<>();
		param.put("taskSeCd",taskSeCd);
		param.put("srcSgg",srcSgg);
		param.put("trgtSgg",trgtSgg);

		adminMapper.copyTask(param);

		List<String> vltnCds = adminMapper.selectVltnList(param);
		if(isEmpty(vltnCds))
			return true;

		param.put("vltnCds", vltnCds);

		return adminMapper.copyVltn(param) == vltnCds.size();
	}
}