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

import org.apache.ibatis.jdbc.SQL;

import java.util.Map;
import java.util.Set;

/**
 * @author tdz
 * @date 2021/03/04 14:51
 */

public class CartMainSqlFactory {
    public String listShoppingCart() {
        return new SQL() {{
            SELECT("goods_spu.gid", "business.uid sid", "business.name business", "goods_spu.name", "goods_spu.thumb");
            SELECT("good_count.sales", "goods_sale.accept_bty", "goods_sale.post_type");
            SELECT("goods_sale.first_num", "goods_sale.first_amount", "goods_sale.second_num", "goods_sale.second_amount");
            SELECT("goods_sale.price", "goods_sale.start_time", "goods_sale.end_time");
            SELECT("good_state.active_state", "user_shopping_cart.amount", "user_shopping_cart.number", "user_shopping_cart.coin", "user_shopping_cart.id");
            SELECT("user_shopping_cart.p1_name", "user_shopping_cart.p1_value", "user_shopping_cart.p2_name", "user_shopping_cart.p2_value");
            FROM("user_shopping_cart");
            LEFT_OUTER_JOIN("goods_spu on user_shopping_cart.gid = goods_spu.gid");
            LEFT_OUTER_JOIN("business on goods_spu.uid = business.uid");
            LEFT_OUTER_JOIN("good_state on user_shopping_cart.gid = good_state.gid");
            LEFT_OUTER_JOIN("goods_sale on user_shopping_cart.gid = goods_sale.gid");
            LEFT_OUTER_JOIN("good_count on user_shopping_cart.gid = good_count.gid");
            WHERE("user_shopping_cart.uid = #{uid}", "user_shopping_cart.is_active = 1");
            ORDER_BY("user_shopping_cart.id desc");
        }}.toString();
    }

    @SuppressWarnings("unchecked")
    public String batchDelete(Map map) {
        Set<String> ids = (Set<String>) map.get("ids");
        String join = String.join(",", ids);
        String sql = "update user_shopping_cart set is_active = 0, update_time = #{timestamp} where uid = #{uid} and id in(";
        sql += join;
        sql += ")";
        return sql;
    }
}
