package sgc.portal.cs.cs0102.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sgc.portal.base.util.EasyHashMap;
import sgc.portal.base.websquare.GridRowStatus;
import sgc.portal.com.com.persistence.ComAuthUserMngMapper;
import sgc.portal.cs.cs0102.persistence.CS01020001UMapper;
import sgc.portal.cw.cw0701.model.CW07010001U;
import sgc.portal.cs.cs0102.dto.CS01020001USearch;
import sgc.portal.cs.cs0102.model.CS01020001U;
import sgc.portal.cs.cs0102.model.CS01020002U;
import sgc.portal.cs.cs0102.model.CS01020003U;
import sgc.portal.cs.cs0102.model.CS01020004U;
import sgc.portal.cs.cs0102.model.CS01020005U;
import sgc.portal.cs.cs0102.model.CS01020006U;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CS01020001UService {

	private final CS01020001UMapper mapper;

	public CS01020001UService(CS01020001UMapper mapper) {
		this.mapper = mapper;
	}

	// 하자대공종 조회
	public List<EasyHashMap<String, Object>> selectTcsStdLjobtypList(CS01020001USearch search) {
		return mapper.selectTcsStdLjobtypList(search);
	}

	// 하자대공종 등록 변경
	@Transactional
	public Map<String, String> saveTcsStdLjobtyp(List<CS01020001U> CS01020001U) {

		Map<String, String> result = new HashMap<>();

		for (CS01020001U cS01020001U : CS01020001U) {

			switch (cS01020001U.getRowStatus()) {

			case GridRowStatus.CREATED:
				mapper.insertTcsStdLjobtyp(cS01020001U);
				break;

			case GridRowStatus.UPDATED:
				mapper.updateTcsStdLjobtyp(cS01020001U);
				break;

			case GridRowStatus.DELETED:

				int count = mapper.selectTcsStdLjobtypCount(cS01020001U);
				if (count > 0) {
					result.put("count", "0");
					result.put("message", "하자 공종이 존재하여 삭제할 수 없습니다.");
				} else if (count == 0) {
					mapper.deleteTcsStdLjobtyp(cS01020001U);
				}

				break;
			default:
				break;
			}
		}
		return result;
	}

	// 위치 조회
	public List<EasyHashMap<String, Object>> selectTcsStdLocList(CS01020001USearch search) {
		return mapper.selectTcsStdLocList(search);
	}

	// 위치 등록 변경
	@Transactional
	public Map<String, String> saveTcsStdLoc(List<CS01020002U> CS01020002U) {

		Map<String, String> result = new HashMap<>();

		for (CS01020002U cS01020002U : CS01020002U) {

			switch (cS01020002U.getRowStatus()) {

			case GridRowStatus.CREATED:
				mapper.insertTcsStdLoc(cS01020002U);
				break;

			case GridRowStatus.UPDATED:
				mapper.updateTcsStdLoc(cS01020002U);
				break;

			case GridRowStatus.DELETED:
				mapper.deleteTcsStdLoc(cS01020002U);
				break;
			default:
				break;
			}
		}
		return result;
	}

	// 하자공종 조회
	public List<EasyHashMap<String, Object>> selectTcsStdJobtypList(CS01020001USearch search) {
		return mapper.selectTcsStdJobtypList(search);
	}

	// 하자공종 트리 조회
	public List<EasyHashMap<String, Object>> selectTcsStdJobtypTreeList(CS01020001USearch search) {
		return mapper.selectTcsStdJobtypTreeList(search);
	}

	// 하자공종 대공종 콤보 조회
	public List<EasyHashMap<String, Object>> selectTcsStdJobtypCommList(CS01020001USearch search) {
		return mapper.selectTcsStdJobtypCommList(search);
	}

	// 하자공종 등록 변경
	@Transactional
	public Map<String, String> saveTcsStdJobtyp(List<CS01020003U> CS01020003U) {

		Map<String, String> result = new HashMap<>();

		for (CS01020003U cS01020003U : CS01020003U) {

			switch (cS01020003U.getRowStatus()) {

			case GridRowStatus.CREATED:
				mapper.insertTcsStdJobtyp(cS01020003U);
				break;

			case GridRowStatus.UPDATED:
				mapper.updateTcsStdJobtyp1(cS01020003U);
				break;

			case GridRowStatus.DELETED:
				int count = mapper.selectTcsStdJobtypCount(cS01020003U);

				if (count > 0) {
					result.put("count", "0");
					result.put("message", "하위 상종이 존재하여 삭제할 수 없습니다.");
				} else if (count == 0) {
					mapper.deleteTcsStdJobtyp(cS01020003U);
				}
				break;
			default:
				break;
			}
		}
		return result;
	}

	// 하자유형 조회
	public List<EasyHashMap<String, Object>> selectTcsStdTypeList(CS01020001USearch search) {
		return mapper.selectTcsStdTypeList(search);
	}

	// 하자유형 등록 변경
	@Transactional
	public Map<String, String> saveTcsStdType(List<CS01020004U> CS01020004U) {

		Map<String, String> result = new HashMap<>();

		for (CS01020004U cS01020004U : CS01020004U) {

			switch (cS01020004U.getRowStatus()) {

			case GridRowStatus.CREATED:
				mapper.insertTcsStdType(cS01020004U);
				break;

			case GridRowStatus.UPDATED:
				mapper.updateTcsStdType(cS01020004U);
				break;

			case GridRowStatus.DELETED:
				mapper.deleteTcsStdType(cS01020004U);
				break;
			default:
				break;
			}
		}
		return result;
	}

	// 하자원인 조회
	public List<EasyHashMap<String, Object>> selectTcsStdCausList(CS01020001USearch search) {
		return mapper.selectTcsStdCausList(search);
	}

	// 하자원인 등록 변경
	@Transactional
	public Map<String, String> saveTcsStdCaus(List<CS01020005U> CS01020005U) {

		Map<String, String> result = new HashMap<>();

		for (CS01020005U cS01020005U : CS01020005U) {

			switch (cS01020005U.getRowStatus()) {

			case GridRowStatus.CREATED:
				mapper.insertTcsStdCaus(cS01020005U);
				break;

			case GridRowStatus.UPDATED:
				mapper.updateTcsStdCaus(cS01020005U);
				break;

			case GridRowStatus.DELETED:
				mapper.deleteTcsStdCaus(cS01020005U);
				break;
			default:
				break;
			}
		}
		return result;
	}

	// 상품분류 조회
	public List<EasyHashMap<String, Object>> selectTcsProdList(CS01020001USearch search) {
		return mapper.selectTcsProdList(search);
	}

	// 상품분류 등록 변경
	@Transactional
	public Map<String, String> saveTcsProd(List<CS01020006U> CS01020006U) {

		Map<String, String> result = new HashMap<>();

		for (CS01020006U cS01020006U : CS01020006U) {

			switch (cS01020006U.getRowStatus()) {

			case GridRowStatus.CREATED:
				mapper.insertTcsProd(cS01020006U);
				break;

			case GridRowStatus.UPDATED:
				mapper.updateTcsProd(cS01020006U);
				break;

			case GridRowStatus.DELETED:
				mapper.deleteTcsProd(cS01020006U);
				break;
			default:
				break;
			}
		}
		return result;
	}
}