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/04/13 21:00
 */

public class DeliverySqlFactory {
    public String afterSaleDelivery(OrderQO orderQO) {
        return new SQL() {{
            SELECT("d.order_delivery_id deliveryId", "p.type", "d.uid", "d.merchant_id", "m.shop_name enterpriseName");
            SELECT("d.goods_id", "d.sku_id", "d.name", "d.coin", "d.number", "p.thumb");
            SELECT("d.uname", "d.phone", "d.address", "d.note");
            SELECT("f.create_time", "d.delivery_time", "d.finish_time");
            SELECT("d.express_type", "d.express_no", "f.state deliveryState");
            SELECT("d.buyer_state", "d.seller_state");
            FROM("order_delivery d");
            INNER_JOIN("order_delivery_refund f on f.order_delivery_id = d.order_delivery_id");
            LEFT_OUTER_JOIN("shop m on d.merchant_id = m.merchant_id");
            LEFT_OUTER_JOIN("goods_spu p on d.goods_id = p.goods_id");
            LEFT_OUTER_JOIN("goods_sku k on d.goods_id = k.goods_id and d.coin = k.coin_name and k.status = 1");
            if (StringUtils.isNotBlank(orderQO.getUid())) {
                WHERE("d.uid = #{uid}");
            }
            if (orderQO.getType() == 0) {
                ORDER_BY("f.create_time desc");
            } else {
                ORDER_BY("f.update_time desc");
            }
        }}.toString() + " limit #{begin},#{size}";
    }

    public String countAfterSaleDelivery(OrderQO orderQO) {
        return new SQL() {{
            SELECT("count(id)");
            FROM("order_delivery_refund d");
            if (StringUtils.isNotBlank(orderQO.getUid())) {
                WHERE("d.uid = #{uid}");
            }
        }}.toString();
    }

    public String listDelivery(OrderQO orderQO) {
        return new SQL() {{
            SELECT("d.order_delivery_id deliveryId", "d.uid", "d.merchant_id", "m.shop_name enterpriseName");
            SELECT("d.goods_id", "d.sku_id", "d.name", "d.coin", "d.number", "p.thumb");
            SELECT("d.uname", "d.phone", "d.address", "d.note");
            SELECT("d.create_time", "d.delivery_time", "d.finish_time");
            SELECT("d.express_type", "d.express_no", "d.delivery_state");
            SELECT("d.buyer_state", "d.seller_state");
            FROM("order_delivery d");
            LEFT_OUTER_JOIN("shop m on d.merchant_id = m.merchant_id");
            LEFT_OUTER_JOIN("goods_spu p on d.goods_id = p.goods_id");
            LEFT_OUTER_JOIN("goods_sku k on d.goods_id = k.goods_id and d.coin = k.coin_name and k.status = 1");
            if (0 == orderQO.getType()) {
                WHERE("d.is_delete = 0");
            }
            if (StringUtils.isNotBlank(orderQO.getUid())) {
                WHERE("d.uid = #{uid}");
            }
            if (StringUtils.isNotBlank(orderQO.getSearchKey())) {
                WHERE("(d.order_delivery_id = #{searchKey} or d.name like CONCAT('%', #{searchKey},'%'))");
            }
            if (StringUtils.isNotBlank(orderQO.getAssessState())) {
                WHERE("d.delivery_state = 2");
                if (orderQO.getType() == 0) {
                    WHERE("d.buyer_state = #{assessState}");
                } else {
                    WHERE("d.seller_state = #{assessState}");
                }
            } else if (StringUtils.isNotBlank(orderQO.getOrderState())) {
                if (orderQO.getOrderState().equals("6")) {
                    WHERE("d.delivery_state in (2,3,5,6,7,8)");
                } else if (orderQO.getOrderState().equals("afterSale")) {
                    WHERE("d.delivery_state in (1,2,3,4,5,7,8)");
                } else if (orderQO.getOrderState().equals("4")) {
                    WHERE("d.delivery_state in (4,8)");
                } else {
                    WHERE("d.delivery_state = #{orderState}");
                }
            }
            if (orderQO.getType() == 0) {
                ORDER_BY("d.create_time desc");
            } else {
                ORDER_BY("d.update_time desc");
            }
        }}.toString() + " limit #{begin},#{size}";
    }

    public String countDelivery(OrderQO orderQO) {
        return new SQL() {{
            SELECT("count(id)");
            FROM("order_delivery d");
            if (StringUtils.isNotBlank(orderQO.getUid())) {
                WHERE("d.uid = #{uid}");
            }
            if (0 == orderQO.getType()) {
                WHERE("d.is_delete = 0");
            }
            if (StringUtils.isNotBlank(orderQO.getAssessState())) {
                WHERE("d.delivery_state = 2");
                if (orderQO.getType() == 0) {
                    WHERE("d.buyer_state = #{assessState}");
                } else {
                    WHERE("d.seller_state = #{assessState}");
                }
            } else if (StringUtils.isNotBlank(orderQO.getOrderState())) {
                if (orderQO.getOrderState().equals("6")) {
                    WHERE("d.delivery_state in (2,3,5,6,7,8)");
                } else if (orderQO.getOrderState().equals("afterSale")) {
                    WHERE("d.delivery_state in (4,5,7,8)");
                } else if (orderQO.getOrderState().equals("4")) {
                    WHERE("d.delivery_state in (4,8)");
                } else {
                    WHERE("d.delivery_state = #{orderState}");
                }
            }
            if (StringUtils.isNotBlank(orderQO.getSearchKey())) {
                WHERE("(d.order_delivery_id = #{searchKey} or d.name like CONCAT('%', #{searchKey},'%'))");
            }
        }}.toString();
    }
}
