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 com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.common.service.impl.CrudServiceImpl;
import io.modules.item.dao.FrontUserDao;
import io.modules.item.dao.ItemDao;
import io.modules.item.dao.UserBehaviorDao;
import io.modules.item.dto.HouseDTO;
import io.modules.item.entity.HouseEntity;
import io.modules.item.entity.UserBehaviorEntity;
import io.modules.item.entity.ViewEntity;
import io.modules.item.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主表
 */
@Service
public class HouseServiceImpl extends CrudServiceImpl<ItemDao, HouseEntity, HouseDTO> implements HouseService {


    @Autowired
    private FrontUserDao frontUserDao;

    @Autowired
    private UserBehaviorDao userBehaviorDao;

    @Override
    public QueryWrapper<HouseEntity> getWrapper(Map<String, Object> params){
        String title = (String)params.get("title");
        String status = (String)params.get("status");
        String view = (String)params.get("view");
        String userId = (String)params.get("userId");

        QueryWrapper<HouseEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(title), "title", title);
        wrapper.like(StrUtil.isNotBlank(status), "status", status);
        wrapper.like(StrUtil.isNotBlank(userId), "user_id", userId);
        wrapper.orderByDesc(StrUtil.isNotBlank(view), "view");
        wrapper.orderByDesc("create_time");
        return wrapper;
    }


    @Override
    public List<HouseEntity> score() {
        LambdaQueryWrapper<HouseEntity> lwq = new LambdaQueryWrapper<>();
        lwq.orderByDesc(HouseEntity::getView);


    // 创建 Page 对象，设置当前页和每页大小
            Page<HouseEntity> page = new Page<>(1, 10);  // 第1页，每页10条

    // 执行分页查询
            IPage<HouseEntity> result = baseDao.selectPage(page, lwq);

    // 获取查询结果
            List<HouseEntity> list = result.getRecords();
        return list;
    }

    @Override
    public List<Object> query() {
        ArrayList<Object> res = new ArrayList<>();
        Long item_total = baseDao.selectCount(null);
        //查询数据总和
        HashMap<String, Object> a1 = new HashMap<>();
        a1.put("name", "商品总量");
        a1.put("value", item_total);
        a1.put("tag", "条");
        res.add(a1);

        HashMap<String, Object> a2 = new HashMap<>();
        a2.put("name", "用户总量");
        a2.put("value", frontUserDao.selectCount(null));
        a2.put("tag", "位");
        res.add(a2);
        return res;
    }

    @Override
    public  List<ViewEntity> view1() {
        List<ViewEntity>  res =   baseDao.view1();

        return res;
    }

    @Override
    public List<HouseEntity> commit() {
        LambdaQueryWrapper<HouseEntity> lwq = new LambdaQueryWrapper<>();
        lwq.orderByDesc(HouseEntity::getView);

        // 创建 Page 对象，设置当前页和每页大小
        Page<HouseEntity> page = new Page<>(1, 10);  // 第1页，每页10条

        // 执行分页查询
        IPage<HouseEntity> result = baseDao.selectPage(page, lwq);

        // 获取查询结果
        List<HouseEntity> list = result.getRecords();
        return list;
    }

    @Override
    public List<HouseEntity> listUser(Long userId) {
        LambdaQueryWrapper<UserBehaviorEntity> lwq = new LambdaQueryWrapper<>();
        lwq.eq(UserBehaviorEntity::getUserId,userId);
        lwq.eq(UserBehaviorEntity::getType,1);
        List<UserBehaviorEntity> list = userBehaviorDao.selectList(lwq);
        if (list.isEmpty()){
            return new ArrayList<>();
        }
        List<HouseEntity> res = new ArrayList<>();

        for (UserBehaviorEntity userBehaviorEntity : list) {
            HouseEntity itemEntity = baseDao.selectById(userBehaviorEntity.getItemId());
            if (itemEntity != null){
                res.add(itemEntity);
            }
        }
        return res;
    }
}
