package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.OaEmailTemplateBo;
import com.ruoyi.oa.domain.vo.OaEmailTemplateVo;
import com.ruoyi.oa.domain.OaEmailTemplate;
import com.ruoyi.oa.mapper.OaEmailTemplateMapper;
import com.ruoyi.oa.service.IOaEmailTemplateService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 邮件模板Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-16
 */
@RequiredArgsConstructor
@Service
public class OaEmailTemplateServiceImpl implements IOaEmailTemplateService {

    private final OaEmailTemplateMapper baseMapper;

    /**
     * 查询邮件模板
     */
    @Override
    public OaEmailTemplateVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询邮件模板列表
     */
    @Override
    public TableDataInfo<OaEmailTemplateVo> queryPageList(OaEmailTemplateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaEmailTemplate> lqw = buildQueryWrapper(bo);
        Page<OaEmailTemplateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询邮件模板列表
     */
    @Override
    public List<OaEmailTemplateVo> queryList(OaEmailTemplateBo bo) {
        LambdaQueryWrapper<OaEmailTemplate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaEmailTemplate> buildQueryWrapper(OaEmailTemplateBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaEmailTemplate> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTemplateName()), OaEmailTemplate::getTemplateName, bo.getTemplateName());
        lqw.eq(StringUtils.isNotBlank(bo.getCategory()), OaEmailTemplate::getCategory, bo.getCategory());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), OaEmailTemplate::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getAttachments()), OaEmailTemplate::getAttachments, bo.getAttachments());
        return lqw;
    }

    /**
     * 新增邮件模板
     */
    @Override
    public Boolean insertByBo(OaEmailTemplateBo bo) {
        OaEmailTemplate add = BeanUtil.toBean(bo, OaEmailTemplate.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改邮件模板
     */
    @Override
    public Boolean updateByBo(OaEmailTemplateBo bo) {
        OaEmailTemplate update = BeanUtil.toBean(bo, OaEmailTemplate.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OaEmailTemplate entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除邮件模板
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
