package io.modules.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.hutool.core.util.StrUtil;
import io.common.service.impl.CrudServiceImpl;
import io.modules.item.dao.UserBehaviorDao;
import io.modules.item.dto.UserBehaviorDTO;
import io.modules.item.entity.UserBehaviorEntity;
import io.modules.item.service.UserBehaviorService;
import org.springframework.stereotype.Service;

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

/**
 * 用户行为表
 *
 * @author Mark #
 * @since 1.0.0 2024-12-16
 */
@Service
public class UserBehaviorServiceImpl extends CrudServiceImpl<UserBehaviorDao, UserBehaviorEntity, UserBehaviorDTO> implements UserBehaviorService {

    @Override
    public QueryWrapper<UserBehaviorEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<UserBehaviorEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public Boolean isExists(UserBehaviorDTO dto) {
        LambdaQueryWrapper<UserBehaviorEntity> lwq = new LambdaQueryWrapper<>();
        lwq.eq(UserBehaviorEntity::getType, dto.getType());
        lwq.eq(UserBehaviorEntity::getUserId, dto.getUserId());
        lwq.eq(UserBehaviorEntity::getItemId, dto.getItemId());
        Long count = baseDao.selectCount(lwq);
        if (count == 0) {
            return false;
        }
        return true;
    }

    @Override
    public void deleteUserBehavior(UserBehaviorDTO dto) {
        LambdaQueryWrapper<UserBehaviorEntity> lwq = new LambdaQueryWrapper<>();
        lwq.eq(UserBehaviorEntity::getType, dto.getType());
        lwq.eq(UserBehaviorEntity::getUserId, dto.getUserId());
        lwq.eq(UserBehaviorEntity::getItemId, dto.getItemId());
       baseDao.delete(lwq);

    }

    @Override
    public List<Long> getTopItems() {
        QueryWrapper<UserBehaviorEntity> queryWrapper = new QueryWrapper<>();

        // 聚合查询: 统计每个 item_id 的行为次数
        queryWrapper.select("item_id", "COUNT(*) as sum")
                .groupBy("item_id")
                .orderByDesc("sum")
                .last("LIMIT 10");  // MyBatis Plus 支持原生 SQL 扩展
        List<UserBehaviorEntity> selectList = baseDao.selectList(queryWrapper);
        // 使用 Java Stream 提取 item_id
        List<Long> itemIds = selectList.stream()
                .map(UserBehaviorEntity::getItemId)  // 提取 item_id
                .collect(Collectors.toList()); // 收集到一个 List 中
        return itemIds;
    }

    @Override
    public Boolean getIsFavorite(Long itemId, Integer type) {
        LambdaQueryWrapper<UserBehaviorEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserBehaviorEntity::getItemId,itemId);
        wrapper.eq(UserBehaviorEntity::getType,type);
        List<UserBehaviorEntity> list = baseDao.selectList(wrapper);
        if (list.isEmpty()){
            return  false;
        }
        return true;
    }

}
