package com.gxwebsoft.cms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.cms.entity.CmsMessage;
import com.gxwebsoft.cms.mapper.CmsMessageMapper;
import com.gxwebsoft.cms.param.CmsMessageParam;
import com.gxwebsoft.cms.service.CmsMessageService;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 留言咨询Service实现
 *
 * @author WorkBuddy
 * @since 2026-07-17
 */
@Service
public class CmsMessageServiceImpl extends ServiceImpl<CmsMessageMapper, CmsMessage> implements CmsMessageService {

    @Override
    public PageResult<CmsMessage> pageRel(CmsMessageParam param) {
        PageParam<CmsMessage, CmsMessageParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        QueryWrapper<CmsMessage> wrapper = page.getWrapper();
        if (StrUtil.isNotBlank(param.getKeywords())) {
            wrapper.and(w -> w.like("name", param.getKeywords())
                    .or().like("content", param.getKeywords())
                    .or().like("company", param.getKeywords()));
        }
        IPage<CmsMessage> result = page(page, page.getOrderWrapper(wrapper));
        return new PageResult<>(result.getRecords(), result.getTotal());
    }

    @Override
    public List<CmsMessage> listRel(CmsMessageParam param) {
        PageParam<CmsMessage, CmsMessageParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        QueryWrapper<CmsMessage> wrapper = page.getWrapper();
        if (StrUtil.isNotBlank(param.getKeywords())) {
            wrapper.and(w -> w.like("name", param.getKeywords())
                    .or().like("content", param.getKeywords())
                    .or().like("company", param.getKeywords()));
        }
        return list(page.getOrderWrapper(wrapper));
    }

    @Override
    public CmsMessage getByIdRel(Integer messageId) {
        return getById(messageId);
    }
}
