package cn.hmsoft.pay.data.dao;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import cn.hmsoft.jdbc.core.PlatformDaoSupport;
import cn.hmsoft.jdbc.entity.Pager;
import cn.hmsoft.jdbc.entity.QueryOrder;
import cn.hmsoft.pay.constants.PayBillStatus;
import cn.hmsoft.pay.data.model.PayBill;

@Component
public class PayBillDao extends PlatformDaoSupport<PayBill> {

	public Pager pageBill(Integer start, Integer limit, String query, QueryOrder order) {
		final StringBuilder sql = new StringBuilder();
		sql.append("select p.*,d.channel_name from pay_bill p,pay_channel_define d where d.channel_id=p.channel_id");
		if (StringUtils.isEmpty(query)) {
			return this.pageMapBySql(order, start, limit, sql.toString());
		}
		sql.append(" and (bill_no like ? channel like ?)");
		String value = this.generateLikeParamter(query);
		return super.pageMapBySql(order, start, limit, sql.toString(), value, value);
	}

	private final static String FIND_BILL_BILL_NO_SQL = "select * from pay_bill where merchant_id=? and channel_id =? and bill_no=?";
	private final static String FIND_BILL_BILL_ID_SQL = "select * from pay_bill where bill_id=?";

	/******************************
	 * 根据订单号找订单
	 */
	public List<PayBill> listByBillNo(String merchant_id, String channel_id, String bill_no) {
		return this.listBySql(FIND_BILL_BILL_NO_SQL, merchant_id, channel_id, bill_no);
	}

	/******************************
	 * 根据订单号找订单
	 */
	public PayBill findByBillNo(String merchant_id, String channel_id, String bill_no) {
		return this.findBySql(FIND_BILL_BILL_NO_SQL, merchant_id, channel_id, bill_no);
	}

	/******************************
	 * 根据订单号找订单
	 */
	public PayBill findByBillId(String bill_id) {
		return this.findBySql(FIND_BILL_BILL_ID_SQL, bill_id);
	}

	/******************************
	 * 根据订单号找订单
	 */
	public PayBill findByBillId(int bill_id) {
		return this.findBySql(FIND_BILL_BILL_ID_SQL, bill_id);
	}

	private final static String COMPLETE_BILL_BILL_NO_SQL = "update pay_bill set pay_time=? ,bill_status=? ,reply_time=?,channel_no=? where bill_id=? and bill_status=?";

	/****************************
	 * 更新订单
	 */
	public int completeBill(int bill_id, LocalDateTime pay_time, LocalDateTime reply_time, String bank_bill_no) {
		return this.updateBySql(COMPLETE_BILL_BILL_NO_SQL, pay_time, PayBillStatus.Complete, reply_time, bank_bill_no,
				bill_id, PayBillStatus.Request);
	}

	private final static String COMPLETE_BILL_BILL_NO_PARAM_SQL = "update pay_bill set bill_status=?,pay_time=?,reply_time=?,channel_no=?,bill_param=? where bill_id=? and bill_status=?";

	/****************************
	 * 更新订单
	 */
	public int completeBill(int bill_id, LocalDateTime pay_time, LocalDateTime reply_time, String bank_bill_no,
			String bill_param) {
		return this.updateBySql(COMPLETE_BILL_BILL_NO_PARAM_SQL, PayBillStatus.Complete, pay_time, reply_time,
				bank_bill_no, bill_param, bill_id, PayBillStatus.Request);
	}

	private final static String LIST_BILL_BY_CUSTOMER = "select * from pay_bill where customer_id=? and (bill_status=? or 1=?) order by reply_time desc,bill_time desc";

	public List<PayBill> listByCustomer(String customer_id, PayBillStatus status) {
		return this.listBySql(LIST_BILL_BY_CUSTOMER, customer_id, status == null ? null : status.toString(),
				status == null ? 1 : 2);
	}
}
