package top.continew.admin.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.micrometer.common.util.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.continew.admin.business.mapper.HuandaResourceMapper;
import top.continew.admin.business.model.entity.HuandaResourceDO;
import top.continew.admin.business.model.query.HuandaResourcePageQuery;
import top.continew.admin.business.model.query.HuandaResourceQuery;
import top.continew.admin.business.model.req.HuandaResourceReq;
import top.continew.admin.business.model.resp.HuandaResourceDetailResp;
import top.continew.admin.business.model.resp.HuandaResourceResp;
import top.continew.admin.business.service.HuandaResourceService;
import top.continew.starter.data.mp.base.BaseMapper;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.extension.crud.service.impl.BaseServiceImpl;

/**
 * 换哒-文件资源服务实现
 *
 * @author 王志维
 * @since 2025/12/23 16:53
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class HuandaResourceServiceImpl extends BaseServiceImpl<HuandaResourceMapper, HuandaResourceDO, HuandaResourceResp, HuandaResourceDetailResp, HuandaResourceQuery, HuandaResourceReq> implements HuandaResourceService {
    @Override
    public PageResp<HuandaResourceResp> pageList(HuandaResourceQuery query, HuandaResourcePageQuery pageQuery) {
        QueryWrapper<HuandaResourceDO> queryWrapper = this.buildQueryWrapper(query);
        //如果age，gender，region不为空，则添加相应的查询条件到queryWrapper
        if (StringUtils.isNotBlank(pageQuery.getAge())) {
            queryWrapper.eq("age", pageQuery.getAge());
        }
        if (StringUtils.isNotBlank(pageQuery.getGender())) {
            queryWrapper.eq("gender", pageQuery.getGender());
        }
        if (StringUtils.isNotBlank(pageQuery.getRegion())) {
            queryWrapper.eq("region", pageQuery.getRegion());
        }
        this.sort(queryWrapper, pageQuery);
        IPage<HuandaResourceDO> page = ((BaseMapper)this.baseMapper).selectPage(new Page((long)pageQuery
            .getPage(), (long)pageQuery.getSize()), queryWrapper);
        PageResp<HuandaResourceResp> pageResp = PageResp.build(page, this.getListClass());
        pageResp.getList().forEach(this::fill);
        return pageResp;
    }

}