package com.bizzan.bitrade.service;

import com.bizzan.bitrade.constant.WalletType;
import com.bizzan.bitrade.dao.WalletTransRecordDao;
import com.bizzan.bitrade.entity.WalletTransRecord;
import com.bizzan.bitrade.pagination.Criteria;
import com.bizzan.bitrade.pagination.Restrictions;
import com.bizzan.bitrade.service.Base.BaseService;
import com.bizzan.bitrade.util.DateUtil;
import com.bizzan.bitrade.vo.IWalletTransStat;
import com.bizzan.bitrade.vo.WalletTransStat;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class WalletTransRecordService extends BaseService {

    @Autowired
    private WalletTransRecordDao walletTransRecordDao;

    /**
     * 保存划转记录
     * @param record
     * @return
     */
    public WalletTransRecord save(WalletTransRecord record) {
        return walletTransRecordDao.saveAndFlush(record);
    }
    
    public Page<WalletTransRecord> queryByMember(Long uid, Integer pageNo, Integer pageSize,WalletType from,WalletType to,String startDate,String endDate) throws ParseException {
        //排序方式 (需要倒序 这样    Criteria.sort("id","createTime.desc") ) //参数实体类为字段名
        Sort orders = Criteria.sortStatic("createTime.desc");
        //分页参数
        PageRequest pageRequest = new PageRequest(pageNo-1, pageSize, orders);
        //查询条件
        Criteria<WalletTransRecord> specification = new Criteria<WalletTransRecord>();
        specification.add(Restrictions.eq("memberId", uid, false));
        if(from != null){
            specification.add(Restrictions.eq("source",from,false));
        }
        if(to != null){
            specification.add(Restrictions.eq("target",to,false));
        }
        if(StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)){
            specification.add(Restrictions.gte("createTime",DateUtil.YYYY_MM_DD_MM_HH_SS.parse(startDate+" 00:00:00"),false));
            specification.add(Restrictions.lte("createTime",DateUtil.YYYY_MM_DD_MM_HH_SS.parse(endDate+" 23:59:59"),false));
        }
        return walletTransRecordDao.findAll(specification, pageRequest);
    }
    
    public List<IWalletTransStat> statWalletTrans(List<Long> ids) {
    	return walletTransRecordDao.statWalletTrans(ids);
    }

}
