
package org.tio.sitexxx.service.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.jfinal.kit.Kv;
import org.tio.jfinal.kit.Ret;
import org.tio.jfinal.plugin.activerecord.Db;
import org.tio.jfinal.plugin.activerecord.Page;
import org.tio.jfinal.plugin.activerecord.SqlPara;
import org.tio.sitexxx.service.model.main.RechargeItem;
import org.tio.sitexxx.service.vo.Const;

import java.util.Map;

/**
 * 用户财务信息业务类
 * 
 *
 */
public class UserCoinService {

	private static Logger log = LoggerFactory.getLogger(UserCoinService.class);

	public static final UserCoinService me = new UserCoinService();

	final RechargeItem rechargeItemDao = new RechargeItem().dao();

	/**
	 * 用户充值记录
	 * @param uid
	 * @param queryParams
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 * 
	 */
	public Ret getUserRecharge(Integer uid, Map<String, Object> queryParams, Integer pageNumber, Integer pageSize) {
		if (uid == null) {
			log.error("uid is null");
			return Ret.fail("msg", "用户id为空");
		}
		if (pageNumber == null || pageNumber <= 0) {
			pageNumber = 1;
		}
		if (pageSize == null || pageSize <= 0) {
			pageSize = 10;
		}
		Kv params = Kv.by("uid", uid).set("status", RechargeItem.Status.SUCCESS).set("rechargetype", Const.StandardMode.RECHARGE);
		if (queryParams.get("begintime") != null) {
			String begintime = queryParams.get("begintime").toString();
			params.set("begintime", DateUtil.parse(begintime).toSqlDate());
		} else {
			params.set("begintime", DateUtil.offsetDay(new DateTime(), -7).toSqlDate());
		}
		if (queryParams.get("endtime") != null) {
			String endtime = queryParams.get("endtime").toString();
			params.set("endtime", DateUtil.offsetDay(DateUtil.parse(endtime), 1).toSqlDate());
		} else {
			params.set("endtime", DateUtil.tomorrow().toSqlDate());
		}
		SqlPara sqlPara = Db.use(Const.Db.TIO_SITE_MAIN).getSqlPara("userrecharge.getUserRecharge", params);
		Page<RechargeItem> userRechargePage = rechargeItemDao.paginate(pageNumber, pageSize, sqlPara);
		return Ret.ok().set("data", userRechargePage);
	}
}
