package com.hfkj.dao;

import com.hfkj.entity.CyymallOrderDetail;
import com.hfkj.entity.CyymallOrderDetailExample.Criteria;
import com.hfkj.entity.CyymallOrderDetailExample.Criterion;
import com.hfkj.entity.CyymallOrderDetailExample;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.jdbc.SQL;

public class CyymallOrderDetailSqlProvider {

    public String countByExample(CyymallOrderDetailExample example) {
        SQL sql = new SQL();
        sql.SELECT("count(*)").FROM("cyymall_order_detail");
        applyWhere(sql, example, false);
        return sql.toString();
    }

    public String deleteByExample(CyymallOrderDetailExample example) {
        SQL sql = new SQL();
        sql.DELETE_FROM("cyymall_order_detail");
        applyWhere(sql, example, false);
        return sql.toString();
    }

    public String insertSelective(CyymallOrderDetail record) {
        SQL sql = new SQL();
        sql.INSERT_INTO("cyymall_order_detail");
        
        if (record.getOrderId() != null) {
            sql.VALUES("order_id", "#{orderId,jdbcType=INTEGER}");
        }
        
        if (record.getGoodsId() != null) {
            sql.VALUES("goods_id", "#{goodsId,jdbcType=INTEGER}");
        }
        
        if (record.getNum() != null) {
            sql.VALUES("num", "#{num,jdbcType=INTEGER}");
        }
        
        if (record.getTotalPrice() != null) {
            sql.VALUES("total_price", "#{totalPrice,jdbcType=DECIMAL}");
        }
        
        if (record.getAddtime() != null) {
            sql.VALUES("addtime", "#{addtime,jdbcType=INTEGER}");
        }
        
        if (record.getIsDelete() != null) {
            sql.VALUES("is_delete", "#{isDelete,jdbcType=SMALLINT}");
        }
        
        if (record.getPic() != null) {
            sql.VALUES("pic", "#{pic,jdbcType=VARCHAR}");
        }
        
        if (record.getIntegral() != null) {
            sql.VALUES("integral", "#{integral,jdbcType=INTEGER}");
        }
        
        if (record.getIsLevel() != null) {
            sql.VALUES("is_level", "#{isLevel,jdbcType=SMALLINT}");
        }
        
        if (record.getBatchPriceTips() != null) {
            sql.VALUES("batch_price_tips", "#{batchPriceTips,jdbcType=VARCHAR}");
        }
        
        if (record.getSupplierPrice() != null) {
            sql.VALUES("supplier_price", "#{supplierPrice,jdbcType=DECIMAL}");
        }
        
        if (record.getVgoodsId() != null) {
            sql.VALUES("vgoods_id", "#{vgoodsId,jdbcType=INTEGER}");
        }
        
        if (record.getVgoodsPrice() != null) {
            sql.VALUES("vgoods_price", "#{vgoodsPrice,jdbcType=DECIMAL}");
        }
        
        if (record.getVerifyCardSaleId() != null) {
            sql.VALUES("verify_card_sale_id", "#{verifyCardSaleId,jdbcType=INTEGER}");
        }
        
        if (record.getGiveGoodsId() != null) {
            sql.VALUES("give_goods_id", "#{giveGoodsId,jdbcType=INTEGER}");
        }
        
        if (record.getAttr() != null) {
            sql.VALUES("attr", "#{attr,jdbcType=LONGVARCHAR}");
        }
        
        return sql.toString();
    }

    public String selectByExampleWithBLOBs(CyymallOrderDetailExample example) {
        SQL sql = new SQL();
        if (example != null && example.isDistinct()) {
            sql.SELECT_DISTINCT("id");
        } else {
            sql.SELECT("id");
        }
        sql.SELECT("order_id");
        sql.SELECT("goods_id");
        sql.SELECT("num");
        sql.SELECT("total_price");
        sql.SELECT("addtime");
        sql.SELECT("is_delete");
        sql.SELECT("pic");
        sql.SELECT("integral");
        sql.SELECT("is_level");
        sql.SELECT("batch_price_tips");
        sql.SELECT("supplier_price");
        sql.SELECT("vgoods_id");
        sql.SELECT("vgoods_price");
        sql.SELECT("verify_card_sale_id");
        sql.SELECT("give_goods_id");
        sql.SELECT("attr");
        sql.FROM("cyymall_order_detail");
        applyWhere(sql, example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            sql.ORDER_BY(example.getOrderByClause());
        }
        
        return sql.toString();
    }

    public String selectByExample(CyymallOrderDetailExample example) {
        SQL sql = new SQL();
        if (example != null && example.isDistinct()) {
            sql.SELECT_DISTINCT("id");
        } else {
            sql.SELECT("id");
        }
        sql.SELECT("order_id");
        sql.SELECT("goods_id");
        sql.SELECT("num");
        sql.SELECT("total_price");
        sql.SELECT("addtime");
        sql.SELECT("is_delete");
        sql.SELECT("pic");
        sql.SELECT("integral");
        sql.SELECT("is_level");
        sql.SELECT("batch_price_tips");
        sql.SELECT("supplier_price");
        sql.SELECT("vgoods_id");
        sql.SELECT("vgoods_price");
        sql.SELECT("verify_card_sale_id");
        sql.SELECT("give_goods_id");
        sql.FROM("cyymall_order_detail");
        applyWhere(sql, example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            sql.ORDER_BY(example.getOrderByClause());
        }
        
        return sql.toString();
    }

    public String updateByExampleSelective(Map<String, Object> parameter) {
        CyymallOrderDetail record = (CyymallOrderDetail) parameter.get("record");
        CyymallOrderDetailExample example = (CyymallOrderDetailExample) parameter.get("example");
        
        SQL sql = new SQL();
        sql.UPDATE("cyymall_order_detail");
        
        if (record.getId() != null) {
            sql.SET("id = #{record.id,jdbcType=INTEGER}");
        }
        
        if (record.getOrderId() != null) {
            sql.SET("order_id = #{record.orderId,jdbcType=INTEGER}");
        }
        
        if (record.getGoodsId() != null) {
            sql.SET("goods_id = #{record.goodsId,jdbcType=INTEGER}");
        }
        
        if (record.getNum() != null) {
            sql.SET("num = #{record.num,jdbcType=INTEGER}");
        }
        
        if (record.getTotalPrice() != null) {
            sql.SET("total_price = #{record.totalPrice,jdbcType=DECIMAL}");
        }
        
        if (record.getAddtime() != null) {
            sql.SET("addtime = #{record.addtime,jdbcType=INTEGER}");
        }
        
        if (record.getIsDelete() != null) {
            sql.SET("is_delete = #{record.isDelete,jdbcType=SMALLINT}");
        }
        
        if (record.getPic() != null) {
            sql.SET("pic = #{record.pic,jdbcType=VARCHAR}");
        }
        
        if (record.getIntegral() != null) {
            sql.SET("integral = #{record.integral,jdbcType=INTEGER}");
        }
        
        if (record.getIsLevel() != null) {
            sql.SET("is_level = #{record.isLevel,jdbcType=SMALLINT}");
        }
        
        if (record.getBatchPriceTips() != null) {
            sql.SET("batch_price_tips = #{record.batchPriceTips,jdbcType=VARCHAR}");
        }
        
        if (record.getSupplierPrice() != null) {
            sql.SET("supplier_price = #{record.supplierPrice,jdbcType=DECIMAL}");
        }
        
        if (record.getVgoodsId() != null) {
            sql.SET("vgoods_id = #{record.vgoodsId,jdbcType=INTEGER}");
        }
        
        if (record.getVgoodsPrice() != null) {
            sql.SET("vgoods_price = #{record.vgoodsPrice,jdbcType=DECIMAL}");
        }
        
        if (record.getVerifyCardSaleId() != null) {
            sql.SET("verify_card_sale_id = #{record.verifyCardSaleId,jdbcType=INTEGER}");
        }
        
        if (record.getGiveGoodsId() != null) {
            sql.SET("give_goods_id = #{record.giveGoodsId,jdbcType=INTEGER}");
        }
        
        if (record.getAttr() != null) {
            sql.SET("attr = #{record.attr,jdbcType=LONGVARCHAR}");
        }
        
        applyWhere(sql, example, true);
        return sql.toString();
    }

    public String updateByExampleWithBLOBs(Map<String, Object> parameter) {
        SQL sql = new SQL();
        sql.UPDATE("cyymall_order_detail");
        
        sql.SET("id = #{record.id,jdbcType=INTEGER}");
        sql.SET("order_id = #{record.orderId,jdbcType=INTEGER}");
        sql.SET("goods_id = #{record.goodsId,jdbcType=INTEGER}");
        sql.SET("num = #{record.num,jdbcType=INTEGER}");
        sql.SET("total_price = #{record.totalPrice,jdbcType=DECIMAL}");
        sql.SET("addtime = #{record.addtime,jdbcType=INTEGER}");
        sql.SET("is_delete = #{record.isDelete,jdbcType=SMALLINT}");
        sql.SET("pic = #{record.pic,jdbcType=VARCHAR}");
        sql.SET("integral = #{record.integral,jdbcType=INTEGER}");
        sql.SET("is_level = #{record.isLevel,jdbcType=SMALLINT}");
        sql.SET("batch_price_tips = #{record.batchPriceTips,jdbcType=VARCHAR}");
        sql.SET("supplier_price = #{record.supplierPrice,jdbcType=DECIMAL}");
        sql.SET("vgoods_id = #{record.vgoodsId,jdbcType=INTEGER}");
        sql.SET("vgoods_price = #{record.vgoodsPrice,jdbcType=DECIMAL}");
        sql.SET("verify_card_sale_id = #{record.verifyCardSaleId,jdbcType=INTEGER}");
        sql.SET("give_goods_id = #{record.giveGoodsId,jdbcType=INTEGER}");
        sql.SET("attr = #{record.attr,jdbcType=LONGVARCHAR}");
        
        CyymallOrderDetailExample example = (CyymallOrderDetailExample) parameter.get("example");
        applyWhere(sql, example, true);
        return sql.toString();
    }

    public String updateByExample(Map<String, Object> parameter) {
        SQL sql = new SQL();
        sql.UPDATE("cyymall_order_detail");
        
        sql.SET("id = #{record.id,jdbcType=INTEGER}");
        sql.SET("order_id = #{record.orderId,jdbcType=INTEGER}");
        sql.SET("goods_id = #{record.goodsId,jdbcType=INTEGER}");
        sql.SET("num = #{record.num,jdbcType=INTEGER}");
        sql.SET("total_price = #{record.totalPrice,jdbcType=DECIMAL}");
        sql.SET("addtime = #{record.addtime,jdbcType=INTEGER}");
        sql.SET("is_delete = #{record.isDelete,jdbcType=SMALLINT}");
        sql.SET("pic = #{record.pic,jdbcType=VARCHAR}");
        sql.SET("integral = #{record.integral,jdbcType=INTEGER}");
        sql.SET("is_level = #{record.isLevel,jdbcType=SMALLINT}");
        sql.SET("batch_price_tips = #{record.batchPriceTips,jdbcType=VARCHAR}");
        sql.SET("supplier_price = #{record.supplierPrice,jdbcType=DECIMAL}");
        sql.SET("vgoods_id = #{record.vgoodsId,jdbcType=INTEGER}");
        sql.SET("vgoods_price = #{record.vgoodsPrice,jdbcType=DECIMAL}");
        sql.SET("verify_card_sale_id = #{record.verifyCardSaleId,jdbcType=INTEGER}");
        sql.SET("give_goods_id = #{record.giveGoodsId,jdbcType=INTEGER}");
        
        CyymallOrderDetailExample example = (CyymallOrderDetailExample) parameter.get("example");
        applyWhere(sql, example, true);
        return sql.toString();
    }

    public String updateByPrimaryKeySelective(CyymallOrderDetail record) {
        SQL sql = new SQL();
        sql.UPDATE("cyymall_order_detail");
        
        if (record.getOrderId() != null) {
            sql.SET("order_id = #{orderId,jdbcType=INTEGER}");
        }
        
        if (record.getGoodsId() != null) {
            sql.SET("goods_id = #{goodsId,jdbcType=INTEGER}");
        }
        
        if (record.getNum() != null) {
            sql.SET("num = #{num,jdbcType=INTEGER}");
        }
        
        if (record.getTotalPrice() != null) {
            sql.SET("total_price = #{totalPrice,jdbcType=DECIMAL}");
        }
        
        if (record.getAddtime() != null) {
            sql.SET("addtime = #{addtime,jdbcType=INTEGER}");
        }
        
        if (record.getIsDelete() != null) {
            sql.SET("is_delete = #{isDelete,jdbcType=SMALLINT}");
        }
        
        if (record.getPic() != null) {
            sql.SET("pic = #{pic,jdbcType=VARCHAR}");
        }
        
        if (record.getIntegral() != null) {
            sql.SET("integral = #{integral,jdbcType=INTEGER}");
        }
        
        if (record.getIsLevel() != null) {
            sql.SET("is_level = #{isLevel,jdbcType=SMALLINT}");
        }
        
        if (record.getBatchPriceTips() != null) {
            sql.SET("batch_price_tips = #{batchPriceTips,jdbcType=VARCHAR}");
        }
        
        if (record.getSupplierPrice() != null) {
            sql.SET("supplier_price = #{supplierPrice,jdbcType=DECIMAL}");
        }
        
        if (record.getVgoodsId() != null) {
            sql.SET("vgoods_id = #{vgoodsId,jdbcType=INTEGER}");
        }
        
        if (record.getVgoodsPrice() != null) {
            sql.SET("vgoods_price = #{vgoodsPrice,jdbcType=DECIMAL}");
        }
        
        if (record.getVerifyCardSaleId() != null) {
            sql.SET("verify_card_sale_id = #{verifyCardSaleId,jdbcType=INTEGER}");
        }
        
        if (record.getGiveGoodsId() != null) {
            sql.SET("give_goods_id = #{giveGoodsId,jdbcType=INTEGER}");
        }
        
        if (record.getAttr() != null) {
            sql.SET("attr = #{attr,jdbcType=LONGVARCHAR}");
        }
        
        sql.WHERE("id = #{id,jdbcType=INTEGER}");
        
        return sql.toString();
    }

    protected void applyWhere(SQL sql, CyymallOrderDetailExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            sql.WHERE(sb.toString());
        }
    }
}