package com.seer.teach.teacher.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.enums.BookIsFreeEnums;
import com.seer.teach.common.enums.BookStatusEnum;
import com.seer.teach.common.utils.PageConverterUtils;
import com.seer.teach.iot.api.UserDeviceServiceApi;
import com.seer.teach.teacher.common.req.BookListTvReq;
import com.seer.teach.teacher.common.resp.BookListTvResp;
import com.seer.teach.teacher.convert.BookConvert;
import com.seer.teach.teacher.module.entity.BooksEntity;
import com.seer.teach.teacher.module.entity.MyBooksEntity;
import com.seer.teach.teacher.module.mapper.BooksMapper;
import com.seer.teach.teacher.service.BooksService;
import com.seer.teach.teacher.service.MyBooksService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 书籍信息表 服务实现类
 * </p>
 *
 * @author Captain
 * @since 2025-06-13
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class BooksServiceImpl extends ServiceImpl<BooksMapper, BooksEntity> implements BooksService {

    private final UserDeviceServiceApi userDeviceServiceApi;

    private final MyBooksService myBooksService;

    /**
     * 获取书籍列表
     *
     * @param device 设备标识
     * @param params 查询参数对象，包含分页信息
     * @return 返回分页的书籍列表数据
     */
    @Override
    public PageListBean<BookListTvResp> list(String device, BookListTvReq params) {
        Integer userId = userDeviceServiceApi.getUserIdByDevice(device);
        // 查询用户购买的书籍ID列表
        List<Integer> collect = myBooksService.list(new LambdaQueryWrapper<MyBooksEntity>()
                        .eq(MyBooksEntity::getUserId, userId)).stream()
                .map(MyBooksEntity::getBookId).collect(Collectors.toList());
        // 查询免费书籍ID列表
        List<Integer> freeBook = list(new LambdaQueryWrapper<BooksEntity>()
                .eq(BooksEntity::getIsFree, BookIsFreeEnums.DISABLED.getCode())
                .eq(BooksEntity::getStatus, BookStatusEnum.ENABLED.getCode())).stream()
                .map(BooksEntity::getId).collect(Collectors.toList());
        // 合并用户购买书籍和免费书籍ID列表
        collect.addAll(freeBook);
        // 构造分页查询条件
        Page<BooksEntity> page = new Page<>(params.getPage(), params.getLimits());
        Page<BooksEntity> resultPage = super.page(page, new LambdaQueryWrapper<BooksEntity>()
                .in(!collect.isEmpty(), BooksEntity::getId, collect)
                .orderByDesc(BooksEntity::getCreateTime));
        // 转换查询结果为响应对象并返回
        return PageConverterUtils.convertPageListBean(resultPage, BookConvert.INSTANCE::toBookListTvResp);
    }


    /**
     * 书籍分页查询
     * @param pageParm
     * @param query
     * @return
     */
    @Override
    public IPage<BooksEntity> pageList(Page<BooksEntity> pageParm, BooksEntity query) {
        LambdaQueryWrapper<BooksEntity> wrapper = new LambdaQueryWrapper<>();
        //根据上架时间倒序排序
        wrapper.orderByDesc(BooksEntity::getShelfTime);
        if(StringUtils.isNotBlank(query.getTitle())){
            wrapper.like(BooksEntity::getTitle, query.getTitle());
        }
        if(query.getIsFree()!=null){
            wrapper.eq(BooksEntity::getIsFree, query.getIsFree());
        }
        if(query.getStatus()!=null){
            wrapper.eq(BooksEntity::getStatus, query.getStatus());
        }
        IPage<BooksEntity> resultPage = this.page(pageParm, wrapper);
        log.info("分页查询结果：{}", resultPage);
        return resultPage;
    }

}
