package com.wzj.soopin.im.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzj.soopin.im.domain.SysMessageTemplate;
import com.wzj.soopin.im.domain.bo.SysMessageTemplateBo;
import com.wzj.soopin.im.mapper.SysMessageTemplateMapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import com.wzj.soopin.im.domain.vo.SysMessageTemplateVo;
import com.wzj.soopin.im.service.ISysMessageTemplateService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 消息模板Service业务层处理
 *
 * @author ruoyi
 */
@RequiredArgsConstructor
@Service
public class SysMessageTemplateServiceImpl extends ServiceImpl<SysMessageTemplateMapper, SysMessageTemplate> implements ISysMessageTemplateService {

    private final SysMessageTemplateMapper templateMapper;

    @Override
    public TableDataInfo<SysMessageTemplateVo> selectTemplatePage(SysMessageTemplateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysMessageTemplate> lqw = new LambdaQueryWrapper<>();
        Page<SysMessageTemplate> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        Page<SysMessageTemplate> resultPage = templateMapper.selectPage(page, lqw);
        List<SysMessageTemplateVo> records = MapstructUtils.convert(resultPage.getRecords(), SysMessageTemplateVo.class);
        Page<SysMessageTemplateVo> voPage = new Page<>(resultPage.getCurrent(), resultPage.getSize(), resultPage.getTotal());
        voPage.setRecords(records);
        return TableDataInfo.build(voPage);
    }

    @Override
    public List<SysMessageTemplateVo> selectTemplateList(SysMessageTemplateBo bo) {
        return MapstructUtils.convert(templateMapper.selectList(bo.toWrapper()), SysMessageTemplateVo.class);
    }

    @Override
    public SysMessageTemplateVo selectTemplateById(Long id) {
        return MapstructUtils.convert(templateMapper.selectById(id), SysMessageTemplateVo.class);
    }

    @Override
    public int deleteTemplateByIds(Long[] ids) {
        return templateMapper.deleteBatchIds(List.of(ids));
    }

    @Override
    public int deleteTemplateById(Long id) {
        return templateMapper.deleteById(id);
    }

    @Override
    public List<SysMessageTemplateVo> selectTemplateListByName(String name) {
        LambdaQueryWrapper<SysMessageTemplate> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like(SysMessageTemplate::getName, name);
        }
        queryWrapper.orderByDesc(SysMessageTemplate::getCreateTime);
        return MapstructUtils.convert(templateMapper.selectList(queryWrapper), SysMessageTemplateVo.class);
    }

    /**
     * 根据模板类型编号查找模板
     */
    @Override
    public SysMessageTemplate selectByTemplateType(String templateType) {
        LambdaQueryWrapper<SysMessageTemplate> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysMessageTemplate::getType, templateType).eq(SysMessageTemplate::getStatus, 0);
        return templateMapper.selectOne(lqw);
    }
    @Override
    public SysMessageTemplate getTemplateByAction(Integer action) {
        LambdaQueryWrapper<SysMessageTemplate> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysMessageTemplate::getAction, action).eq(SysMessageTemplate::getStatus, 0);
        return templateMapper.selectOne(lqw);
    }
}
