package com.fjhx.mail.service.signature.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.mail.entity.signature.dto.MailMailSignatureDto;
import com.fjhx.mail.entity.signature.dto.MailSignatureSelectDto;
import com.fjhx.mail.entity.signature.po.MailSignature;
import com.fjhx.mail.entity.signature.vo.MailSignatureVo;
import com.fjhx.mail.mapper.signature.MailSignatureMapper;
import com.fjhx.mail.service.signature.MailSignatureService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.common.utils.wrapper.SqlField;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * 签名 服务实现类
 * </p>
 *
 * @author
 * @since 2023-08-24
 */
@Service
public class MailSignatureServiceImpl extends ServiceImpl<MailSignatureMapper, MailSignature> implements MailSignatureService {

    @Override
    public Page<MailSignatureVo> getPage(MailSignatureSelectDto dto) {
        IWrapper<MailSignature> wrapper = getWrapper();

        wrapper.keyword(dto.getKeyword(),
                new SqlField(MailSignature::getTemplateName),
                new SqlField(MailSignature::getContent)
        );

        //只能看自己的数据
        wrapper.eq(MailSignature::getCreateUser, SecurityUtils.getUserId());

        wrapper.orderByDesc("s", MailSignature::getId);
        Page<MailSignatureVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

    @Override
    public MailSignatureVo detail(Long id) {
        MailSignature MailSignature = this.getById(id);
        MailSignatureVo result = BeanUtil.toBean(MailSignature, MailSignatureVo.class);
        return result;
    }

    @Override
    public void add(MailMailSignatureDto mailSignatureDto) {
        this.save(mailSignatureDto);
    }

    @Override
    public void edit(MailMailSignatureDto mailSignatureDto) {
        this.updateById(mailSignatureDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

    @Override
    public List<String> getTemplateList() {
        List<String> list = this.listObject(MailSignature::getTemplateName, q -> q
                .eq(MailSignature::getCreateUser, SecurityUtils.getUserId())
                .groupBy(MailSignature::getTemplateName)
        );
        return list;
    }

}
