package sgc.portal.cd.cd0301.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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.cd.cd0301.dto.CD03010003PSearch;
import sgc.portal.cd.cd0301.persistence.CD03010003PMapper;
import sgc.portal.com.sym.mail.service.MailService;
import sgc.portal.com.sym.user.model.UserDetails;

@Service
public class CD03010003PService {

	private final CD03010003PMapper mapper;
	private final MailService mailService;
	
    public CD03010003PService(CD03010003PMapper mapper, MailService mailService) {
        this.mapper = mapper;
        this.mailService = mailService;
    }
    
    /**
     * 메일전송 작업지시서 조회
     * @param search
     * @return
     */
	public List<EasyHashMap<String, Object>> selectConWkiOdrList(CD03010003PSearch search) {
		return mapper.selectConWkiOdrList(search);
	}

	/**
	 * 작업지시서 메일전송
	 */
	@Transactional
	public List<Map<String, String>> sendMailConWkiOdr(List<CD03010003PSearch> CD03010003PS) {
		List<Map<String, String>> result = new ArrayList<Map<String, String>>();
		UserDetails userDetails = SessionUtil.getUserDetails();
		String cmpnCd = userDetails.getCmpnCd();
		String sSender = userDetails.getEmpNo();	
		String sSubject = "[SGC PORTAL] 작업지시서 내용 공유";
		String ssubSubject = "작업지시서 내용 공유";
		
		for(CD03010003PSearch cD03010003P : CD03010003PS) {
			StringBuffer sContent = new StringBuffer();
			String srecvAddress = cD03010003P.getEmail();
			sContent.append(cD03010003P.getMailCn().toString());
			//메일전송
			result.add(mailService.mailSendWorkShare(cmpnCd, sSubject, ssubSubject, sContent, sSender, srecvAddress));				
		}
		
		return result;
	}
	
}
