package sgc.portal.cs.cs0301.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sgc.portal.base.util.EasyHashMap;
import sgc.portal.base.util.SessionUtil;
import sgc.portal.com.sym.mail.service.MailService;
import sgc.portal.com.sym.user.model.UserDetails;
import sgc.portal.cs.cs0301.dto.CS03010001USearch;
import sgc.portal.cs.cs0301.model.CS03010001U;
import sgc.portal.cs.cs0301.model.TcsDeftElpsVo;
import sgc.portal.cs.cs0301.model.TcsDeftVo;
import sgc.portal.cs.cs0301.persistence.CS03010001UMapper;

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

@Service
public class CS03010001UService {
	private final CS03010001UMapper mapper;
	private final MailService mailService;

	public CS03010001UService(CS03010001UMapper mapper, MailService mailService) {
		this.mapper = mapper;
		this.mailService = mailService;
	}

	/*하자리스트*/
	public List<EasyHashMap<String, Object>> getDeftList(CS03010001USearch search) {
		return mapper.getDeftList(search);
	}
	
	/*하자리스트 카운트*/
	public List<EasyHashMap<String, Object>> getDeftCountList(CS03010001USearch search) {
		return mapper.getDeftCountList(search);
	}

	/*하자경과리스트*/
	public List<EasyHashMap<String, Object>> getDeftElpsList(CS03010001USearch search) {
		return mapper.getDeftElpsList(search);
	}
	
	/*접수처리*/
	@Transactional
	public Map<String, String> saveDeft(List<CS03010001U> CS03010001U) {

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

		for (CS03010001U cS03010001U : CS03010001U) {

			TcsDeftVo tcsDeft = new TcsDeftVo();
			TcsDeftElpsVo tcsDeftElps = new TcsDeftElpsVo();

			tcsDeftElps.setDeftNo(cS03010001U.getDeftNo()); // 하자번호
			tcsDeftElps.setPgprNm(cS03010001U.getRcvprNm()); // 처리자 명
			tcsDeftElps.setPrcgPrStrtDt(cS03010001U.getRcptDt()); // 처리_예정_시작_일자

			mapper.insertDeftElps(tcsDeftElps); // TCS_DEFT_ELPS insert

			tcsDeft.setDeftNo(cS03010001U.getDeftNo()); // 하자번호
			tcsDeft.setRcptDt(cS03010001U.getRcptDt()); // 접수_일자
			tcsDeft.setRcvprNm(cS03010001U.getRcvprNm()); // 접수자_명
			tcsDeft.setRcvprEmpno(cS03010001U.getRcvprEmpno()); // 접수자 사원번호
			//tcsDeft.setWorkElpsSeq(tcsDeftElps.getPrcgElpsSeq()); // 작업 경과 순번
			tcsDeft.setDeftPrcgStusCd(cS03010001U.getDeftPrcgStusCd()); // 하자처리상태코드[CS009]

			mapper.updateDeft(tcsDeft); // TCS_DEFT update

		}
		return result;
	}
	
	/*SMS전송처리경과*/
	@Transactional
	public Map<String, String> saveDeftElpsSms(CS03010001U CS03010001U) {

		Map<String, String> result = new HashMap<>();
		List<TcsDeftElpsVo> TcsDeftElpsSms = CS03010001U.getTcsDeftElpsSms();

		for (TcsDeftElpsVo tcsDeftElpsSms : TcsDeftElpsSms) {
			mapper.insertDeftElpsSms(tcsDeftElpsSms);
		}

		return result;
	}

	/* 첨부파일 */
	@Transactional
	public Map<String, String> saveAflId(CS03010001U cS03010001U) {

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

		TcsDeftVo tcsDeft = cS03010001U.getTcsDeft();

		mapper.updateAflId(tcsDeft);

		return result;
	}
	
	/*작업지시서 전송 리스트 조회*/
	public List<EasyHashMap<String, Object>> getWorkWkiList(CS03010001USearch search) {
		return mapper.getWorkWkiList(search);
	}
	
	/*작업지시서전송처리경과*/
	@Transactional
	public Map<String, String> saveDeftElpsWork(CS03010001U CS03010001U) {

		Map<String, String> result = new HashMap<>();
		List<TcsDeftVo> TcsDeftList = CS03010001U.getTcsDeftList();
		List<TcsDeftElpsVo> TcsDeftElpsList = CS03010001U.getTcsDeftElpsList();
		UserDetails userDetails = SessionUtil.getUserDetails();
		
		StringBuffer sContent = new StringBuffer();
		String cmpnCd = userDetails.getCmpnCd();
		String srecvAddress = userDetails.getEmlAddr();
		String sSender = userDetails.getEmpNo();	
		String sSubject = "[SGC이앤씨] 하자보수 작업 요청";
		String ssubSubject = "하자보수 작업 요청";
		
		sContent.append(TcsDeftElpsList.get(0).getMailCn().toString());
    	
		for(int i = 0; i < TcsDeftList.size(); i++){
			srecvAddress = (String) TcsDeftList.get(i).getVendrLbprMail();
			mailService.mailSend5(cmpnCd, sSubject, ssubSubject, sContent, sSender, srecvAddress);
		}
		
		for (TcsDeftElpsVo tcsDeftElpsWork : TcsDeftElpsList) {
			mapper.insertDeftElpsWork(tcsDeftElpsWork);
		}

		return result;
	}
	
	/*완료처리 리스트 조회*/
	public List<EasyHashMap<String, Object>> getDeftCompliteList(CS03010001USearch search) {
		return mapper.getDeftCompliteList(search);
	}
	
	/*완료처리 처리경과*/
	@Transactional
	public Map<String, String> saveDeftComplite(CS03010001U CS03010001U) {

		Map<String, String> result = new HashMap<>();
		List<TcsDeftVo> TcsDeftList = CS03010001U.getTcsDeftList();
		List<TcsDeftElpsVo> TcsDeftElpsList = CS03010001U.getTcsDeftElpsList();
		
		for (TcsDeftElpsVo tcsDeftElpsList : TcsDeftElpsList) {
			mapper.insertDeftElpsComplite(tcsDeftElpsList);
			
			for (TcsDeftVo tcsDeftList : TcsDeftList) {
				if(tcsDeftList.getDeftNo().equals(tcsDeftList.getDeftNo())) {
					tcsDeftList.setCpltElpsSeq(tcsDeftElpsList.getPrcgElpsSeq());
					tcsDeftList.setCplprNm(tcsDeftElpsList.getPgprNm());
					mapper.updateDeftComplite(tcsDeftList); // TCS_DEFT update
				}
			}
		}
		return result;
	}
}