const system=require("../../../system");
const Dao=require("../../dao.base");
class EorderDao extends Dao{
    constructor(){
        super(Dao.getModelName(EorderDao));
    }

    /**
     * fn:记录总数
     * @param params
     * @returns {Promise<void>}
     */
    async countEorder(params){
        let sql = [];
        sql.push('SELECT COUNT(1) AS eorderCount FROM e_order WHERE 1 = 1 ');
        this.setEorderParams(sql,params);
        let list = await this.customQuery(sql.join(" "), params);
        if(!list || list.length==0){
            return {count:0};
        }
        return list[0];
    }

    setEorderParams(sql,params){
        if(params.id){
            sql.push('and id=:id');
        }
        if(params.merchant_id){
            sql.push('and merchant_id=:merchant_id');
        }
        if(params.merchant_name){
            sql.push('and merchant_name=:merchant_name');
        }
        if(params.sign_id){
            sql.push('and sign_id=:sign_id');
        }
        if(params.product_id){
            sql.push('and product_id=:product_id');
        }
        if(params.product_type){
            sql.push('and product_type=:product_type');
        }
        if(params.audit_status){
            params.pay_status="20";
            sql.push(`and audit_status=:audit_status and pay_status=:pay_status`);
        }
        if(params.live_status){
            params.pay_status="20";
            if(params.live_status=='10'){
                params.audit_status="10";
            }else if(params.live_status=="30"){
                params.audit_status = "30";
            }else {
                params.audit_status = "20";
            }
            sql.push('and live_status=:live_status and pay_status=:pay_status  and audit_status=:audit_status');
        }
        if(params.pay_status){

            sql.push('and pay_status=:pay_status');
        }
        if(params.audit_user_id){
            sql.push('and audit_user_id=:audit_user_id');
        }
        if (params.createdBegin) {
            sql.push("AND created_at >= :createdBegin");
        }
        if (params.createdEnd) {
            sql.push("AND created_at <= :createdEnd");
        }
        if (params.payBegin) {
            sql.push("AND pay_date >= :payBegin");
        }
        if (params.payEnd) {
            sql.push("AND pay_date <= :payEnd");
        }
        if (params.auditBegin) {
            sql.push("AND audit_date >= :auditBegin");
        }
        if (params.auditEnd) {
            sql.push("AND audit_date <= :auditEnd");
        }
    }

    /**
     * fn:订单分页查询
     * @param params
     * @returns {Promise<void>}
     */
    async pageEorder(params){
        let sql = [];
        sql.push('SELECT *  FROM e_order  WHERE 1 = 1 ');
        this.setEorderParams(sql,params);
        sql.push("ORDER BY created_at DESC");
        sql.push("LIMIT :startRow, :pageSize");
        let list = await this.customQuery(sql.join(" "), params);

        return list ||[];
    }

    // 回去商户下订单中可用的产品
    async getMerchantLiveProduct(merchantId) {
        let sql = [];
        sql.push("SELECT");
        sql.push("t1.id, t1.`engine_account_id`, t2.product_id, t2.price, t1.`merchant_id`, t1.product_type");
        sql.push("FROM e_order t1");
        sql.push("INNER JOIN `e_order_product` t2 ON t1.`id` = t2.`order_id`");
        sql.push("WHERE t1.`merchant_id` = :merchantId");
        sql.push("AND t1.`live_status` = '20' AND t1.`engine_account_id` <> ''");
        sql.push("ORDER BY t1.`created_at` ASC");

        return await this.customQuery(sql.join(" "), {merchantId: merchantId});
    }

    /**
     * fn:查询商户下所有订单的产产品
     * @param merchant_id
     * @returns {Promise<void>}
     */
    async getMerchantLiveAllProducts(merchant_id){
        let sql = [];
        sql.push("SELECT");
        sql.push(" t1.product_property, t1.product_id, t1.product_name");
        sql.push("FROM e_order_product t1");
        sql.push("INNER JOIN e_order t2 ON t1.order_id = t2.id");
        sql.push("WHERE t2.merchant_id = :merchant_id");
        sql.push("AND t2.live_status = '20' AND t2.engine_account_id <> ''");
        sql.push("ORDER BY t1.`created_at` ASC");
        return await this.customQuery(sql.join(" "), {merchant_id: merchant_id});
    }

}
module.exports=EorderDao;
