package com.qiankun.dao;

import com.qiankun.dao.core.AbsDao;
import com.qiankun.dao.core.Hints;
import com.qiankun.entity.User;
import com.qiankun.entity.Wish;
import com.qiankun.pages.IPageList;
import com.qiankun.pages.PageListImpl;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public class WishDaoImpl extends AbsDao<Wish,String> implements WishDao {
    private static final String FIND_BY_RAINBOWPLANUSERID_SQL = " from Wish where rainbowPlanUserId = ? and activityId = ? order by createTime desc";
    private static final String FIND_BY_AVAILABLE_ACTIVITY_SQL = " from Wish where activityId = ? order by createTime desc";
    @Override
    public IPageList<Wish> findByRainbowPlanUserId(String userId, String activityId, Hints hints) {
        IPageList<Wish> wishPageList = new PageListImpl<Wish>();
        wishPageList.setRecords(find(FIND_BY_RAINBOWPLANUSERID_SQL,hints,userId,activityId));
        wishPageList.setRecordTotal(findCount(" select count(*) "+ FIND_BY_RAINBOWPLANUSERID_SQL,userId,activityId));
        return wishPageList;
    }

    @Override
    public IPageList<Wish> findAllByAvailableActivity(String activityId,Hints hints) {
        IPageList<Wish> wishPageList = new PageListImpl<Wish>();
        wishPageList.setRecords(find(FIND_BY_AVAILABLE_ACTIVITY_SQL,hints,activityId));
        wishPageList.setRecordTotal(findCount(" select count(*) "+ FIND_BY_AVAILABLE_ACTIVITY_SQL,activityId));
        return wishPageList;
    }

    @Override
    public Wish findById(String id) {
        return findUnique("from Wish where id = ?",new Hints(),id);
    }

    @Transactional
    @Override
    public Wish save(Wish entity) {
        return super.save(entity);
    }

    @Transactional
    @Override
    public void update(Wish entity) {
        super.update(entity);
    }
}
