package com.simuwang.manage.service.impl;

import com.simuwang.base.common.support.MybatisPage;
import com.simuwang.base.common.util.DateUtils;
import com.simuwang.base.common.util.StringUtil;
import com.simuwang.base.mapper.EmailTemplateMappingMapper;
import com.simuwang.base.pojo.dos.EmailTemplateInfoDO;
import com.simuwang.base.pojo.dos.EmailTemplateMappingDO;
import com.simuwang.base.pojo.dto.query.EmailTemplateSettingPageQuery;
import com.simuwang.base.pojo.vo.EmailTemplateMappingVO;
import com.simuwang.base.pojo.vo.IdListVO;
import com.simuwang.base.pojo.vo.StatusVO;
import com.simuwang.manage.service.EmailTemplateMappingService;
import com.simuwang.shiro.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * FileName: EmailTemplateMappingServiceImpl
 * Author:   chenjianhua
 * Date:     2024/9/25 20:25
 * Description: ${DESCRIPTION}
 */
@Service
public class EmailTemplateMappingServiceImpl implements EmailTemplateMappingService {
    @Autowired
    private EmailTemplateMappingMapper emailTemplateMappingMapper;

    @Override
    public MybatisPage<EmailTemplateMappingVO> searchTemplateSettingList(EmailTemplateSettingPageQuery emailTemplateSettingPageQuery) {
        List<EmailTemplateMappingDO> emailTemplateMappingDOList = emailTemplateMappingMapper.searchTemplateSettingList(emailTemplateSettingPageQuery);
        List<EmailTemplateMappingVO> emailTemplateMappingVOList = emailTemplateMappingDOList.stream().map(EmailTemplateMappingDO::toVO).collect(Collectors.toList());
        long total = emailTemplateMappingMapper.countTemplateSettingList(emailTemplateSettingPageQuery);
        return MybatisPage.of(total,emailTemplateMappingVOList);
    }

    @Override
    public void deleteTemplateSetting(IdListVO idListVO) {
        Integer userId = UserUtils.getLoginUser().getUserId();
        emailTemplateMappingMapper.deleteTemplateSetting(idListVO.getIdList(),userId);
    }

    @Override
    public void saveTemplateSetting(EmailTemplateMappingVO emailTemplateMappingVO) {
        EmailTemplateMappingDO emailTemplateMappingDO = toEmailTemplateMappingDO(emailTemplateMappingVO);
        if(StringUtil.isNull(emailTemplateMappingDO.getId())){
            emailTemplateMappingMapper.saveEmailTemplateMappingDO(emailTemplateMappingDO);
        }else{
            emailTemplateMappingMapper.updateEmailTemplateMappingDO(emailTemplateMappingDO);
        }
    }


    @Override
    public void controlTemplateConfig(StatusVO statusVO) {
        EmailTemplateMappingDO emailTemplateMappingDO = emailTemplateMappingMapper.searchTemplateById(statusVO.getId());
        emailTemplateMappingDO.setStatus(statusVO.getStatus());
        Integer userId = UserUtils.getLoginUser().getUserId();
        emailTemplateMappingDO.setUpdaterId(userId);
        emailTemplateMappingDO.setUpdateTime(DateUtils.getNowDate());
        emailTemplateMappingMapper.updateEmailTemplateMappingDO(emailTemplateMappingDO);
    }

    @Override
    public EmailTemplateMappingVO searchTemplateSetting(Integer id) {
        EmailTemplateMappingDO emailTemplateMappingDO = emailTemplateMappingMapper.searchTemplateById(id);
        EmailTemplateMappingVO vo = emailTemplateMappingDO.toVO();
        return vo;
    }

    private EmailTemplateMappingDO toEmailTemplateMappingDO(EmailTemplateMappingVO vo) {
        Integer userId = UserUtils.getLoginUser().getUserId();
        EmailTemplateMappingDO emailTemplateMappingDO = new EmailTemplateMappingDO();
        emailTemplateMappingDO.setEmail(vo.getEmail());
        emailTemplateMappingDO.setId(vo.getId());
        emailTemplateMappingDO.setTemplateId(vo.getTemplateId());
        emailTemplateMappingDO.setTemplateName(vo.getTemplateName());
        emailTemplateMappingDO.setStatus(vo.getStatus());
        emailTemplateMappingDO.setTitle(vo.getTitle());
        emailTemplateMappingDO.setCompanyName(vo.getCompanyName());
        emailTemplateMappingDO.setCompanyId(vo.getCompanyId());
        emailTemplateMappingDO.setUpdateTime(DateUtils.getNowDate());
        emailTemplateMappingDO.setIsvalid(1);
        emailTemplateMappingDO.setUpdaterId(userId);
        if(StringUtil.isNull(emailTemplateMappingDO.getId())){
            emailTemplateMappingDO.setCreateTime(DateUtils.getNowDate());
            emailTemplateMappingDO.setCreatorId(userId);
        }
        return emailTemplateMappingDO;
    }
}
