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.CmsCase;
import com.gxwebsoft.cms.mapper.CmsCaseMapper;
import com.gxwebsoft.cms.param.CmsCaseParam;
import com.gxwebsoft.cms.service.CmsCaseService;
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 CmsCaseServiceImpl extends ServiceImpl<CmsCaseMapper, CmsCase> implements CmsCaseService {

    @Override
    public PageResult<CmsCase> pageRel(CmsCaseParam param) {
        // 聚合场景：传入 categoryIds 时忽略单值 categoryId，避免 AND 冲突
        if (StrUtil.isNotBlank(param.getCategoryIds())) {
            param.setCategoryId(null);
        }
        PageParam<CmsCase, CmsCaseParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, create_time desc");
        QueryWrapper<CmsCase> wrapper = page.getWrapper();
        if (StrUtil.isNotBlank(param.getKeywords())) {
            wrapper.and(w -> w.like("case_name", param.getKeywords())
                    .or().like("customer", param.getKeywords())
                    .or().like("industry", param.getKeywords()));
        }
        IPage<CmsCase> result = page(page, page.getOrderWrapper(wrapper));
        return new PageResult<>(result.getRecords(), result.getTotal());
    }

    @Override
    public List<CmsCase> listRel(CmsCaseParam param) {
        PageParam<CmsCase, CmsCaseParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, create_time desc");
        QueryWrapper<CmsCase> wrapper = page.getWrapper();
        if (StrUtil.isNotBlank(param.getKeywords())) {
            wrapper.and(w -> w.like("case_name", param.getKeywords())
                    .or().like("customer", param.getKeywords())
                    .or().like("industry", param.getKeywords()));
        }
        return list(page.getOrderWrapper(wrapper));
    }

    @Override
    public CmsCase getByIdRel(Integer caseId) {
        return getById(caseId);
    }
}
