package com.fzm.mall.server.front.asset.sql;

import com.fzm.mall.server.front.asset.model.po.qo.OrderQO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

/**
 * @author tdz
 * @date 2021/02/22 11:26
 */

public class OrderSqlFactory {
    public String listOrder(OrderQO orderQO) {
        return new SQL() {{
            SELECT("distinct order_info.*", "shop.shop_name enterpriseName");
            FROM("order_info");
            LEFT_OUTER_JOIN("shop on order_info.merchant_id = shop.merchant_id");
            LEFT_OUTER_JOIN("order_detail on order_info.oid = order_detail.oid");
            if (0 == orderQO.getType()) {
                WHERE("order_info.is_delete = 0");
            }
            if (StringUtils.isNotBlank(orderQO.getUid())) {
                WHERE("order_info.uid = #{uid}");
            }
            if (StringUtils.isNotBlank(orderQO.getSearchKey())) {
                WHERE("(order_info.oid = #{searchKey} or order_detail.name like CONCAT('%', #{searchKey},'%'))");
            }
            if (StringUtils.isNotBlank(orderQO.getOrderState())) {
                if (orderQO.getOrderState().equals("waitCommon")) {
                    WHERE("order_info.comment_total > order_info.comment_already");
                }else if (orderQO.getOrderState().equals("afterSale")) {
                    WHERE("order_info.order_state in (1,2,3,4,5)");
                } else if (orderQO.getOrderState().equals("1")) {
                    WHERE("order_info.order_state in (1,5)");
                } else {
                    WHERE("order_info.order_state = #{orderState}");
                }
            }
            //WHERE("order_info.create_time between #{startTime} and #{endTime}");
            ORDER_BY("order_info.id desc");
        }}.toString() + " limit #{begin},#{size}";
    }

    public String countOrder(OrderQO orderQO) {
        return new SQL() {{
            SELECT("count(order_info.id)");
            FROM("order_info");
            LEFT_OUTER_JOIN("order_detail on order_info.oid = order_detail.oid");
            if (0 == orderQO.getType()) {
                WHERE("order_info.is_delete = 0");
            }
            if (StringUtils.isNotBlank(orderQO.getUid())) {
                WHERE("order_info.uid = #{uid}");
            }
            if (StringUtils.isNotBlank(orderQO.getSearchKey())) {
                WHERE("(order_info.oid = #{searchKey} or order_detail.name like CONCAT('%', #{searchKey},'%'))");
            }
            if (StringUtils.isNotBlank(orderQO.getOrderState())) {
                if (orderQO.getOrderState().equals("waitCommon")) {
                    WHERE("order_info.comment_total > order_info.comment_already");
                }else if (orderQO.getOrderState().equals("afterSale")) {
                    WHERE("order_info.order_state in (4,5,8,9)");
                } else if (orderQO.getOrderState().equals("1")) {
                    WHERE("order_info.order_state in (1,5)");
                } else {
                    WHERE("order_info.order_state = #{orderState}");
                }
            }
        }}.toString();
    }

    public String countAfterSaleOrder(OrderQO orderQO) {
        return new SQL() {{
            SELECT("count(order_refund.id)");
            FROM("order_refund");
            INNER_JOIN("order_info on order_refund.oid = order_info.oid");
            LEFT_OUTER_JOIN("order_detail on order_info.oid = order_detail.oid");
            if (StringUtils.isNotBlank(orderQO.getUid())) {
                WHERE("order_info.uid = #{uid}");
            }
        }}.toString();
    }

    public String listAfterSaleOrder(OrderQO orderQO) {
        return new SQL() {{
            SELECT("f.oid,r.id,f.uid,r.create_time,f.pay_id,f.merchant_id,f.post_fee,f.total,f.accept_digital,f.pay_type,f.order_type,f.order_state,f.delivery,f.uname,f.phone,f.address,f.is_delete,f.v_note,f.consult_discount_amount,f.consult_discount_type,f.discount_amount,f.amount,f.comment_total,f.comment_already,f.update_time","r.return_state orderState ", "shop.shop_name enterpriseName");
            FROM("order_info f");
            INNER_JOIN("order_refund r on r.oid = f.oid");
            LEFT_OUTER_JOIN("shop on f.merchant_id = shop.merchant_id");
            LEFT_OUTER_JOIN("order_detail on f.oid = order_detail.oid");
            if (StringUtils.isNotBlank(orderQO.getUid())) {
                WHERE("f.uid = #{uid}");
            }
            ORDER_BY("r.id desc");
        }}.toString() + " limit #{begin},#{size}";
    }
}
