package sgc.portal.qc.qc0101.service;

import org.apache.commons.lang3.RandomStringUtils;
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.auth.model.SmComHdlgAuth;
import sgc.portal.com.auth.persistence.ComBusiHandleAuthMngMapper;
import sgc.portal.com.sym.mail.persistence.MailMapper;
import sgc.portal.com.sym.mail.service.MailService;
import sgc.portal.qc.qc0101.persistence.QC01010001UMapper;
import sgc.portal.qc.qc0101.dto.QC01010001USearch;
import sgc.portal.qc.qc0101.model.QC01010001U;
import sgc.portal.qc.qc0101.model.QC01010002U;
import sgc.portal.qc.qc0101.model.QC01010003U;

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

@Service
public class QC01010001UService {

	private final QC01010001UMapper mapper;

    public QC01010001UService(QC01010001UMapper mapper) {
        this.mapper = mapper;
    }
    
    //레벨
  	//(품질)표준문서) TREE  조회
    public List<EasyHashMap<String, Object>> selectQcStndTreeList(QC01010001USearch search) {
        return mapper.selectQcStndTreeList(search);
    }
    
    //(품질)표준문서)  조회
    public List<EasyHashMap<String, Object>> selectQcStndList(QC01010001USearch search) {
        return mapper.selectQcStndList(search);
    }
    
    //(품질)표준문서)  등록수정삭제
    @Transactional
	public Map<String, String> saveQcStnd(QC01010001U qC01010001U) {
		Map<String, String> result = new HashMap<>();
		
		for (QC01010001U vo1 : qC01010001U.getDsList1()) {
			switch (vo1.getRowStatus()) {
	
			case GridRowStatus.CREATED:
				String Chevern = mapper.selectChevern(vo1);
				vo1.setDocId(Chevern);
				mapper.insertQcStnd(vo1);
				break;
	
			case GridRowStatus.UPDATED:
				mapper.updateQcStnd(vo1);
				break;
	
			case GridRowStatus.DELETED:
				mapper.deleteQcStnd(vo1);
				break;
			default:
				break;
			}
		}
		
		return result;
	}
    
    //프로세스
    //프로세스 이력 조회
    public List<EasyHashMap<String, Object>> selectQcStndPrcgHisList(QC01010001USearch search) {
        return mapper.selectQcStndPrcgHisList(search);
    }
    
    //프로세스  추가등록삭제
    @Transactional
	public Map<String, String> saveQcStndPrcg1(QC01010002U qC01010002U) {
		Map<String, String> result = new HashMap<>();
		
		for (QC01010002U vo1 : qC01010002U.getDsList1()) {
			switch (vo1.getRowStatus()) {
	
			case GridRowStatus.CREATED:
				mapper.insertQcStndPrcg(vo1);
				mapper.insertQcStndPrcgHis(vo1);
				break;
	
			case GridRowStatus.UPDATED:
				mapper.updateQcStndPrcgHis(vo1);
				mapper.updateQcStndPrcg(vo1);
				break;
	
			case GridRowStatus.DELETED:
				
				mapper.deleteQcStndPrcgHis(vo1);
				mapper.updateQcStndPrcgHisCheckY(vo1);
				
				//프로세스 PDF
				//절차서   PDF
				//지침첨부 
				//FORMAT첨부
				mapper.updateQcStndPrcgCheckAflId(vo1);
				
				int count1 = mapper.selectQcStndPrcgHisCount(vo1);
				if(count1 == 0) {
					mapper.deleteQcStndPrcg(vo1);
				}
				break;
			default:
				break;
			}
		}
		return result;
	}
    
    //프로세스  갱신
    @Transactional
	public Map<String, String> saveQcStndPrcg2(QC01010002U qC01010002U) {
		Map<String, String> result = new HashMap<>();
		
		//프로세스 PDF
		int count1 = mapper.selectPrcsAflIdCount1(qC01010002U);
		if(count1 > 0) {
			
			String cheabun1 = RandomStringUtils.randomAlphanumeric(30);
			qC01010002U.setId(cheabun1);
			mapper.updatePrcsAflIdGangSin1(qC01010002U);
			
			qC01010002U.setPrcsAflId(cheabun1);
			
		}else if(count1 == 0) {
			qC01010002U.setPrcsAflId("");
		}
		//절차서   PDF
		int count2 = mapper.selectAtvAflIdCount1(qC01010002U);
		if(count2 > 0) {
			
			String cheabun2 = RandomStringUtils.randomAlphanumeric(30);
			qC01010002U.setId(cheabun2);
			mapper.updateAtvAflIdGangSin1(qC01010002U);
			
			qC01010002U.setAtvAflId(cheabun2);
			
		}else if(count2 == 0) {
			
			qC01010002U.setAtvAflId("");
		}
		//지침첨부 
		int count3 = mapper.selectGudlAflIdCount1(qC01010002U);
		if(count3 > 0) {
			
			String cheabun3 = RandomStringUtils.randomAlphanumeric(30);
			qC01010002U.setId(cheabun3);
			mapper.updateGudlAflIdGangSin1(qC01010002U);
			
			qC01010002U.setGudlAflId(cheabun3);
			
		}else if(count3 == 0) {
			
			qC01010002U.setGudlAflId("");
		}
		//FORMAT첨부
		int count4 = mapper.selectFrmtAflIdCount1(qC01010002U);
		if(count4 > 0) {
			
			String cheabun4 = RandomStringUtils.randomAlphanumeric(30);
			qC01010002U.setId(cheabun4);
			mapper.updateFrmtAflIdGangSin1(qC01010002U);
			
			qC01010002U.setFrmtAflId(cheabun4);
			
		}else if(count4 == 0) {
			qC01010002U.setFrmtAflId("");
		}
		
		mapper.updateQcStndPrcgHisCheckRevNo(qC01010002U);
		mapper.updateQcStndPrcgHisCheckN(qC01010002U);
		mapper.updateQcStndPrcgHis2(qC01010002U);
		return result;
	}
    
    public List<EasyHashMap<String, Object>> selectQcStndPrcgGudlAflIdList(QC01010001USearch search) {
        return mapper.selectQcStndPrcgGudlAflIdList(search);
    }
    
    public List<EasyHashMap<String, Object>> selectQcStndPrcgHisFrmtAflIdList(QC01010001USearch search) {
        return mapper.selectQcStndPrcgHisFrmtAflIdList(search);
    }
    
    @Transactional
	public Map<String, String> updateGudlAflId1(QC01010002U qC01010002U) {
		Map<String, String> result = new HashMap<>();
		
		mapper.updateGudlAflIdHis(qC01010002U);
		mapper.updateGudlAflId(qC01010002U);
		return result;
	}
    
    @Transactional
	public Map<String, String> updateGudlAflId2(QC01010002U qC01010002U) {
		Map<String, String> result = new HashMap<>();
		
		int count1 = mapper.selectGudlAflIfileIdCount(qC01010002U);
		if(count1 == 0) {
			qC01010002U.setGudlAflId("");
			mapper.updateGudlAflIdHis(qC01010002U);
			mapper.updateGudlAflId(qC01010002U);
		}
		return result;
	}
    
    @Transactional
	public Map<String, String> updateFrmtAflId1(QC01010002U qC01010002U) {
		Map<String, String> result = new HashMap<>();
		
		mapper.updateFrmtAflIdHis(qC01010002U);
		mapper.updateFrmtAflId(qC01010002U);
		return result;
	}
    
    @Transactional
	public Map<String, String> updateFrmtAflId2(QC01010002U qC01010002U) {
		Map<String, String> result = new HashMap<>();
		
		int count1 = mapper.selectFrmtAflIfileIdCount(qC01010002U);
		if(count1 == 0) {
			qC01010002U.setFrmtAflId("");
			mapper.updateFrmtAflIdHis(qC01010002U);
			mapper.updateFrmtAflId(qC01010002U);
		}
		return result;
	}
    
    
    
    
    //지침
    //지침 이력 조회
    public List<EasyHashMap<String, Object>> selectQcStndGudlList(QC01010001USearch search) {
        return mapper.selectQcStndGudlList(search);
    }
    
    //지침  추가등록삭제
    @Transactional
	public Map<String, String> saveQcStndGudl1(QC01010003U qC01010003U) {
		Map<String, String> result = new HashMap<>();
		
		for (QC01010003U vo1 : qC01010003U.getDsList1()) {
			switch (vo1.getRowStatus()) {
	
			case GridRowStatus.CREATED:
				mapper.insertQcStndGudl(vo1);
				break;
	
			case GridRowStatus.UPDATED:
				mapper.updateQcStndGudl(vo1);
				break;
	
			case GridRowStatus.DELETED:
				mapper.deleteQcStndGudlHis(vo1);
				mapper.deleteQcStndGudl(vo1);
				break;
			default:
				break;
			}
		}
		return result;
	}
    
    //지침  갱신
    @Transactional
	public Map<String, String> saveQcStndGudl2(QC01010003U qC01010003U) {
		Map<String, String> result = new HashMap<>();
		
		int count1 = mapper.selectGudlAflIdCount11(qC01010003U);
		if(count1 > 0) {
			
			String cheabun1 = RandomStringUtils.randomAlphanumeric(30);
			qC01010003U.setId(cheabun1);
			mapper.updateGudlAflIdGangSin11(qC01010003U);
			qC01010003U.setGudlAflId(cheabun1);
		}else if(count1 == 0) {
			qC01010003U.setGudlAflId("");
		}
		
		int count2 = mapper.selectGudlRelAflIdCount1(qC01010003U);
		if(count2 > 0) {
			
			String cheabun2 = RandomStringUtils.randomAlphanumeric(30);
			qC01010003U.setId(cheabun2);
			mapper.updateGudlRelAflIdGangSin1(qC01010003U);
			qC01010003U.setGudlRelAflId(cheabun2);
		}else if(count1 == 0) {
			qC01010003U.setGudlRelAflId("");
		}
		
		mapper.updateQcStndGudlHis2(qC01010003U);
		mapper.updateQcStndGudl2(qC01010003U);
		return result;
	}
    
    public List<EasyHashMap<String, Object>> selectQcStndGudlGudlRelAflIdList(QC01010001USearch search) {
        return mapper.selectQcStndGudlGudlRelAflIdList(search);
    }
    
    public List<EasyHashMap<String, Object>> selectQcStndGudlHisList(QC01010001USearch search) {
        return mapper.selectQcStndGudlHisList(search);
    }
    
    public List<EasyHashMap<String, Object>> selectQcStndGudlHisGudlRelAflIdList(QC01010001USearch search) {
        return mapper.selectQcStndGudlHisGudlRelAflIdList(search);
    }
    
    @Transactional
	public Map<String, String> updateGudlRelAflId1(QC01010003U qC01010003U) {
		Map<String, String> result = new HashMap<>();
		
		mapper.updateGudlRelAflId(qC01010003U);
		return result;
	}
    
    @Transactional
	public Map<String, String> updateGudlRelAflId2(QC01010003U qC01010003U) {
		Map<String, String> result = new HashMap<>();
		
		int count1 = mapper.selectGudlRelAflIdCount(qC01010003U);
		if(count1 == 0) {
			qC01010003U.setGudlRelAflId("");
			mapper.updateGudlRelAflId(qC01010003U);
		}
		return result;
	}
}