package com.sifang.modules.rechargewallet.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.rechargewallet.entity.RechargeWallet;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.persistence.Page;
import com.sifang.modules.sys.util.CrmUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class RechargeWalletDao extends BaseDao<RechargeWallet> {
    //修改后的充值记录
    public List<Record> getRecordList(Page<Record> page, Map<String, String> map) {
        User user = UserUtils.getUser();
        String siteId = CrmUtils.getSiteId(user);
        StringBuffer sb = new StringBuffer();
        sb.append(" select a.id,a.number,a.paid_id,a.pay_time,a.payment_amount,a.create_by,a.payment_type,a.invoice_application_id from crm_platform_collections a");
        sb.append(" where a.status='0' and a.site._id =? ");
        sb.append(getCondition(map));
        sb.append(" order by a.pay_time desc");
        if (page != null) {
            sb.append(" limit " + page.getPageSize() + " offset " + (page.getPageNo() - 1) * page.getPageSize());
        }
        return Db.find(sb.toString(), siteId);
    }

    public long getCount() {
        User user = UserUtils.getUser();
        String siteId = CrmUtils.getSiteId(user);
        StringBuffer sb = new StringBuffer();
        sb.append(" select count(a.id) from crm_platform_collections a");
        sb.append(" where a.status='0' and a.site_id =? ");
        sb.append(" order by a.pay_time desc");
        return Db.queryLong(sb.toString(), siteId);
    }

    /*
     * 充值记录查询条件
     */
    private String getCondition(Map<String, String> map) {
        StringBuilder sf = new StringBuilder();
        if (StringUtil.checkParamsValid(map.get("date1"))) {//
            sf.append(" and a.pay_time >= '" + (map.get("date1")) + " 00:00:00' ");
        }
        if (StringUtil.checkParamsValid(map.get("date2"))) {
            sf.append(" and a.pay_time <= '" + (map.get("date2")) + " 23:59:59' ");
        }
        if (StringUtil.checkParamsValid(map.get("paymentType"))) {
            sf.append(" AND a.payment_type = '" + (map.get("paymentType")) + " ' ");
        }
        if (StringUtil.checkParamsValid(map.get("invoiceStatus"))) {
            if ("0".equals(map.get("invoiceStatus"))) {
                sf.append(" AND a.invoice_application_id != '' ");
            } else {
                sf.append(" AND a.invoice_application_id IS NULL ");
            }

            // sf.append(" AND a.invoice_application_id = '"+(map.get("invoiceStatus"))+" ' ");
        }
        return sf.toString();
    }


}
