package sgc.portal.cw.cw0607.service;

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.cw.cw0607.model.CW06070001U;
import sgc.portal.cw.cw0607.dto.CW06070001USearch;
import sgc.portal.cw.cw0607.persistence.CW06070001UMapper;
import sgc.portal.com.sym.mail.service.MailService;

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

@Service
public class CW06070001UService {
    private final CW06070001UMapper mapper;
    private final MailService mailService;

    public CW06070001UService(CW06070001UMapper mapper, MailService mailService) {
        this.mapper = mapper;
        this.mailService = mailService;
    }

    public List<EasyHashMap<String, Object>> getList(CW06070001USearch search) {
        return mapper.getList(search);
    }

    public List<EasyHashMap<String, Object>> getInitData(CW06070001USearch search) {
        return mapper.getInitData(search);
    }

    public List<EasyHashMap<String, Object>> selectInitData(CW06070001USearch search) {
        return mapper.selectInitData(search);
    }
    
    @Transactional
    public Map<String, String> save(List<CW06070001U> CW06070001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	for (CW06070001U cW06070001U : CW06070001U) {
            switch (cW06070001U.getRowStatus()) {
                case GridRowStatus.CREATED:
                    insert(cW06070001U);
                    break;
                case GridRowStatus.UPDATED:
                    update(cW06070001U);
                    break;
                case GridRowStatus.DELETED:
                    delete(cW06070001U);
                    break;
                default:
                    break;
            }
        }
    	return result;
    }

    private void insert(CW06070001U cW06070001U) {
		int seq = mapper.selectSeq(cW06070001U);
		int coid = mapper.selectCoId(cW06070001U);
		cW06070001U.setSeq(seq);
		cW06070001U.setCoId(coid);
        mapper.insert(cW06070001U);
    }

    private void update(CW06070001U cW06070001U) {
        mapper.update(cW06070001U);
    }

    private void delete(CW06070001U cW06070001U) {
        mapper.delete(cW06070001U);
    }
    
    @Transactional
    public Map<String, String> sendMail(List<CW06070001U> CW06070001U) {
    	
    	Map<String,String> result = new HashMap<>();

        for (CW06070001U cW06070001U : CW06070001U) {
    		
        	StringBuffer sContent = new StringBuffer();
    		
        	String srecvAddress 	= mapper.selectUserEmail(cW06070001U);
        	//System.out.println("===================statusCd: " + cW06070001U.getStatusCd());
        	//System.out.println("===================srecvAddress: " + srecvAddress);
    		String cmpnCd = cW06070001U.getCmpnCd();
        	String sSubject 	= mapper.selectEmailTitle(cW06070001U);
    		String ssubSubject = sSubject;
    		String sSender = cW06070001U.getEmpNo();	
    		sContent.append(mapper.selectEmailConts(cW06070001U));

        	//System.out.println("===================sSubject: " + sSubject);
        	//System.out.println("===================sContent: " + sContent);
        	
			mailService.mailSend4(cmpnCd, sSubject, ssubSubject, sContent, sSender, srecvAddress);
			
        }
        
        return result;
    }
}
