package com.jwipc.sys.logic.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.exception.DaoException;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.convert.MsgTemplateConvert;
import com.jwipc.sys.dto.MsgTemplateDTO;
import com.jwipc.sys.entity.MsgTemplate;
import com.jwipc.sys.logic.MsgTemplateLogic;
import com.jwipc.sys.model.po.MsgTemplatePO;
import com.jwipc.sys.model.query.MsgTemplateQuery;
import com.jwipc.sys.service.IMsgTemplateService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author dengjsh
 * @date 2024/3/22 15:32
 * @desc 邮件模板
 */
@Service
@RequiredArgsConstructor
public class MsgTemplateLogicImpl implements MsgTemplateLogic {

    private final IMsgTemplateService msgTemplateService;
    private final MsgTemplateConvert msgTemplateConvert;
    @Override
    public PageView findByPage(MsgTemplateQuery query) {
        final Page<MsgTemplate> dbPage = PageUtil.toPage(query);
        List<MsgTemplatePO> msgTemplatePOS = msgTemplateService.findPageByQuery(dbPage, query);
        List<MsgTemplateDTO> msgTemplateDTOS = msgTemplateConvert.po2DTOList(msgTemplatePOS);
        return PageUtil.getPV(msgTemplateDTOS, dbPage.getTotal());
    }

    @Override
    public MsgTemplateDTO getById(Long id) {
        MsgTemplate msgTemplate = msgTemplateService.getById(id);
        return msgTemplateConvert.entity2Dto(msgTemplate);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(MsgTemplateDTO param) {
        MsgTemplate msgTemplate = msgTemplateConvert.dto2Entity(param);
        try {
            checkFieldLength(param);
        } catch (Exception e) {
            throw new DaoException(e.getMessage());
        }
        final LambdaQueryWrapper<MsgTemplate> queryWrapper = getQueryWrapper(param);
        MsgTemplate msgTemplateTemp =  msgTemplateService.getOne(queryWrapper);
        //判断邮件编码不能重复
        if (param.getId() == null){
            if (Objects.nonNull(msgTemplateTemp)) {
                throw new DaoException("邮件模板编码已存在!");
            }
            msgTemplate.setCreateTime(LocalDateTime.now());
            msgTemplate.setCreateId(Objects.requireNonNull(LoginUtil.getCurrentUser()).getUserId());
        } else {
            if (Objects.nonNull(msgTemplateTemp) && !msgTemplateTemp.getId().equals(param.getId())) {
                throw new DaoException("邮件模板编码已存在!");
            }
            msgTemplate.setUpdateTime(LocalDateTime.now());
            msgTemplate.setUpdateId(Objects.requireNonNull(LoginUtil.getCurrentUser()).getUserId());
        }
        msgTemplateService.saveOrUpdate(msgTemplate);
    }

    /**
     * 参数合法性验证
     * @param param
     * @throws DaoException
     */
    private void checkFieldLength(MsgTemplateDTO param) throws DaoException {
        if (param.getMsgCode().trim().length() > 50) {
            throw new DaoException("邮件模板编码长度不能超过50个字符!");
        }

        if (param.getMsgName().trim().length() > 50) {
            throw new DaoException("邮件模板名称长度不能超过50个字符!");
        }
    }

    /**
     * 删除
     *
     * @param id 参数
     */
    @Override
    public void remove(Long id) {
        msgTemplateService.removeById(id);
    }

    private LambdaQueryWrapper<MsgTemplate> getQueryWrapper(MsgTemplateDTO query)
    {
        final LambdaQueryWrapper<MsgTemplate> queryWrapper =
                new LambdaQueryWrapper<MsgTemplate>()
                        .eq(Objects.nonNull(query.getMsgCode()), MsgTemplate::getMsgCode, query.getMsgCode())
                ;
        return queryWrapper;
    }
}
