package com.gxwebsoft.house.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.gxwebsoft.house.mapper.HouseLikeLogMapper;
import com.gxwebsoft.house.service.HouseLikeLogService;
import com.gxwebsoft.house.entity.HouseLikeLog;
import com.gxwebsoft.house.param.HouseLikeLogParam;
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 科技小王子
 * @since 2025-03-05 13:47:14
 */
@Service
public class HouseLikeLogServiceImpl extends MPJBaseServiceImpl<HouseLikeLogMapper, HouseLikeLog> implements HouseLikeLogService {

    @Override
    public PageResult<HouseLikeLog> pageRel(HouseLikeLogParam param) {
        PageParam<HouseLikeLog, HouseLikeLogParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, create_time desc");
        List<HouseLikeLog> list = baseMapper.selectPageRel(page, param);
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<HouseLikeLog> listRel(HouseLikeLogParam param) {
        List<HouseLikeLog> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<HouseLikeLog, HouseLikeLogParam> page = new PageParam<>();
        page.setDefaultOrder("sort_number asc, create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public HouseLikeLog getByIdRel(Integer houseId, Integer userId) {
      LambdaQueryWrapper<HouseLikeLog> wr = Wrappers.lambdaQuery(HouseLikeLog.class).eq(HouseLikeLog::getHouseId, houseId).eq(HouseLikeLog::getUserId, userId);
      HouseLikeLog likeLogs = getBaseMapper().selectOne(wr);
      return likeLogs;
    }

    @Override
    public boolean add( HouseLikeLog likeLog) {
      LambdaQueryWrapper<HouseLikeLog> wr = Wrappers.lambdaQuery(HouseLikeLog.class).eq(HouseLikeLog::getHouseId, likeLog.getHouseId()).eq(HouseLikeLog::getUserId, likeLog.getUserId());

      List<HouseLikeLog> likeLogs = getBaseMapper().selectList(wr);
      if(CollectionUtils.isNotEmpty(likeLogs)) {
        baseMapper.delete(wr);
        return false;
      }else {
        int insert = baseMapper.insert(likeLog);
        return  true;
      }
    }

}
