package com.inkasso.factoring.system.mail.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.inkasso.adapter.util.BeanUtilsProxy;
import com.inkasso.factoring.common.entity.GridPage;
import com.inkasso.factoring.system.mail.dao.SystemEmailMapper;
import com.inkasso.factoring.system.mail.entity.SystemEmail;
import com.inkasso.factoring.system.mail.entity.SystemEmailQueryRequest;
import com.inkasso.factoring.system.mail.entity.SystemEmailVO;
import com.inkasso.factoring.system.mail.service.SystemEmailService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemEmailImpl extends ServiceImpl<SystemEmailMapper, SystemEmail> implements SystemEmailService {

    @Override
    public GridPage<SystemEmailVO> pageQuery(SystemEmailQueryRequest systemEmailQueryRequest) {

        LambdaQueryWrapper<SystemEmail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(SystemEmail::getTitle,systemEmailQueryRequest.getTitle());
        queryWrapper.orderByDesc(SystemEmail::getGmtCreate);
        Page<Object> objects = PageHelper.startPage(systemEmailQueryRequest.getPage(), systemEmailQueryRequest.getRows());
        List<SystemEmail> systemEmails = this.baseMapper.selectList(queryWrapper);
        List<SystemEmailVO> systemEmailVOS = null;
        if(CollectionUtil.isNotEmpty(systemEmails)){
            systemEmailVOS = BeanUtilsProxy.copyPropertiesList(systemEmails, SystemEmailVO.class);
        }


        return new GridPage<SystemEmailVO>(systemEmailQueryRequest.getRows(),systemEmailQueryRequest.getPage(),objects.getTotal(),systemEmailVOS);
    }

}
