package com.hfkj.dao;

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

public class BsMerchantSqlProvider {

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

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

    public String insertSelective(BsMerchant record) {
        SQL sql = new SQL();
        sql.INSERT_INTO("bs_merchant");
        
        if (record.getSourceType() != null) {
            sql.VALUES("source_type", "#{sourceType,jdbcType=INTEGER}");
        }
        
        if (record.getProvinceCode() != null) {
            sql.VALUES("province_code", "#{provinceCode,jdbcType=BIGINT}");
        }
        
        if (record.getProvinceName() != null) {
            sql.VALUES("province_name", "#{provinceName,jdbcType=VARCHAR}");
        }
        
        if (record.getCityCode() != null) {
            sql.VALUES("city_code", "#{cityCode,jdbcType=BIGINT}");
        }
        
        if (record.getCityName() != null) {
            sql.VALUES("city_name", "#{cityName,jdbcType=VARCHAR}");
        }
        
        if (record.getAreaCode() != null) {
            sql.VALUES("area_code", "#{areaCode,jdbcType=BIGINT}");
        }
        
        if (record.getAreaName() != null) {
            sql.VALUES("area_name", "#{areaName,jdbcType=VARCHAR}");
        }
        
        if (record.getOilPriceZoneId() != null) {
            sql.VALUES("oil_price_zone_id", "#{oilPriceZoneId,jdbcType=INTEGER}");
        }
        
        if (record.getOilPriceZoneName() != null) {
            sql.VALUES("oil_price_zone_name", "#{oilPriceZoneName,jdbcType=VARCHAR}");
        }
        
        if (record.getChainBrandId() != null) {
            sql.VALUES("chain_brand_id", "#{chainBrandId,jdbcType=BIGINT}");
        }
        
        if (record.getChainBrandName() != null) {
            sql.VALUES("chain_brand_name", "#{chainBrandName,jdbcType=VARCHAR}");
        }
        
        if (record.getChainBrandSettleType() != null) {
            sql.VALUES("chain_brand_settle_type", "#{chainBrandSettleType,jdbcType=INTEGER}");
        }
        
        if (record.getMerNo() != null) {
            sql.VALUES("mer_no", "#{merNo,jdbcType=VARCHAR}");
        }
        
        if (record.getMerLogo() != null) {
            sql.VALUES("mer_logo", "#{merLogo,jdbcType=VARCHAR}");
        }
        
        if (record.getMerName() != null) {
            sql.VALUES("mer_name", "#{merName,jdbcType=VARCHAR}");
        }
        
        if (record.getContactsName() != null) {
            sql.VALUES("contacts_name", "#{contactsName,jdbcType=VARCHAR}");
        }
        
        if (record.getContactsTel() != null) {
            sql.VALUES("contacts_tel", "#{contactsTel,jdbcType=VARCHAR}");
        }
        
        if (record.getCustomerServiceTel() != null) {
            sql.VALUES("customer_service_tel", "#{customerServiceTel,jdbcType=VARCHAR}");
        }
        
        if (record.getAddress() != null) {
            sql.VALUES("address", "#{address,jdbcType=VARCHAR}");
        }
        
        if (record.getLongitude() != null) {
            sql.VALUES("longitude", "#{longitude,jdbcType=VARCHAR}");
        }
        
        if (record.getLatitude() != null) {
            sql.VALUES("latitude", "#{latitude,jdbcType=VARCHAR}");
        }
        
        if (record.getMerLabel() != null) {
            sql.VALUES("mer_label", "#{merLabel,jdbcType=VARCHAR}");
        }
        
        if (record.getListShow() != null) {
            sql.VALUES("list_show", "#{listShow,jdbcType=BIT}");
        }
        
        if (record.getStatus() != null) {
            sql.VALUES("`status`", "#{status,jdbcType=INTEGER}");
        }
        
        if (record.getCreateTime() != null) {
            sql.VALUES("create_time", "#{createTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getUpdateTime() != null) {
            sql.VALUES("update_time", "#{updateTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getExt1() != null) {
            sql.VALUES("ext_1", "#{ext1,jdbcType=VARCHAR}");
        }
        
        if (record.getExt2() != null) {
            sql.VALUES("ext_2", "#{ext2,jdbcType=VARCHAR}");
        }
        
        if (record.getExt3() != null) {
            sql.VALUES("ext_3", "#{ext3,jdbcType=VARCHAR}");
        }
        
        return sql.toString();
    }

    public String selectByExample(BsMerchantExample example) {
        SQL sql = new SQL();
        if (example != null && example.isDistinct()) {
            sql.SELECT_DISTINCT("id");
        } else {
            sql.SELECT("id");
        }
        sql.SELECT("source_type");
        sql.SELECT("province_code");
        sql.SELECT("province_name");
        sql.SELECT("city_code");
        sql.SELECT("city_name");
        sql.SELECT("area_code");
        sql.SELECT("area_name");
        sql.SELECT("oil_price_zone_id");
        sql.SELECT("oil_price_zone_name");
        sql.SELECT("chain_brand_id");
        sql.SELECT("chain_brand_name");
        sql.SELECT("chain_brand_settle_type");
        sql.SELECT("mer_no");
        sql.SELECT("mer_logo");
        sql.SELECT("mer_name");
        sql.SELECT("contacts_name");
        sql.SELECT("contacts_tel");
        sql.SELECT("customer_service_tel");
        sql.SELECT("address");
        sql.SELECT("longitude");
        sql.SELECT("latitude");
        sql.SELECT("mer_label");
        sql.SELECT("list_show");
        sql.SELECT("`status`");
        sql.SELECT("create_time");
        sql.SELECT("update_time");
        sql.SELECT("ext_1");
        sql.SELECT("ext_2");
        sql.SELECT("ext_3");
        sql.FROM("bs_merchant");
        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) {
        BsMerchant record = (BsMerchant) parameter.get("record");
        BsMerchantExample example = (BsMerchantExample) parameter.get("example");
        
        SQL sql = new SQL();
        sql.UPDATE("bs_merchant");
        
        if (record.getId() != null) {
            sql.SET("id = #{record.id,jdbcType=BIGINT}");
        }
        
        if (record.getSourceType() != null) {
            sql.SET("source_type = #{record.sourceType,jdbcType=INTEGER}");
        }
        
        if (record.getProvinceCode() != null) {
            sql.SET("province_code = #{record.provinceCode,jdbcType=BIGINT}");
        }
        
        if (record.getProvinceName() != null) {
            sql.SET("province_name = #{record.provinceName,jdbcType=VARCHAR}");
        }
        
        if (record.getCityCode() != null) {
            sql.SET("city_code = #{record.cityCode,jdbcType=BIGINT}");
        }
        
        if (record.getCityName() != null) {
            sql.SET("city_name = #{record.cityName,jdbcType=VARCHAR}");
        }
        
        if (record.getAreaCode() != null) {
            sql.SET("area_code = #{record.areaCode,jdbcType=BIGINT}");
        }
        
        if (record.getAreaName() != null) {
            sql.SET("area_name = #{record.areaName,jdbcType=VARCHAR}");
        }
        
        if (record.getOilPriceZoneId() != null) {
            sql.SET("oil_price_zone_id = #{record.oilPriceZoneId,jdbcType=INTEGER}");
        }
        
        if (record.getOilPriceZoneName() != null) {
            sql.SET("oil_price_zone_name = #{record.oilPriceZoneName,jdbcType=VARCHAR}");
        }
        
        if (record.getChainBrandId() != null) {
            sql.SET("chain_brand_id = #{record.chainBrandId,jdbcType=BIGINT}");
        }
        
        if (record.getChainBrandName() != null) {
            sql.SET("chain_brand_name = #{record.chainBrandName,jdbcType=VARCHAR}");
        }
        
        if (record.getChainBrandSettleType() != null) {
            sql.SET("chain_brand_settle_type = #{record.chainBrandSettleType,jdbcType=INTEGER}");
        }
        
        if (record.getMerNo() != null) {
            sql.SET("mer_no = #{record.merNo,jdbcType=VARCHAR}");
        }
        
        if (record.getMerLogo() != null) {
            sql.SET("mer_logo = #{record.merLogo,jdbcType=VARCHAR}");
        }
        
        if (record.getMerName() != null) {
            sql.SET("mer_name = #{record.merName,jdbcType=VARCHAR}");
        }
        
        if (record.getContactsName() != null) {
            sql.SET("contacts_name = #{record.contactsName,jdbcType=VARCHAR}");
        }
        
        if (record.getContactsTel() != null) {
            sql.SET("contacts_tel = #{record.contactsTel,jdbcType=VARCHAR}");
        }
        
        if (record.getCustomerServiceTel() != null) {
            sql.SET("customer_service_tel = #{record.customerServiceTel,jdbcType=VARCHAR}");
        }
        
        if (record.getAddress() != null) {
            sql.SET("address = #{record.address,jdbcType=VARCHAR}");
        }
        
        if (record.getLongitude() != null) {
            sql.SET("longitude = #{record.longitude,jdbcType=VARCHAR}");
        }
        
        if (record.getLatitude() != null) {
            sql.SET("latitude = #{record.latitude,jdbcType=VARCHAR}");
        }
        
        if (record.getMerLabel() != null) {
            sql.SET("mer_label = #{record.merLabel,jdbcType=VARCHAR}");
        }
        
        if (record.getListShow() != null) {
            sql.SET("list_show = #{record.listShow,jdbcType=BIT}");
        }
        
        if (record.getStatus() != null) {
            sql.SET("`status` = #{record.status,jdbcType=INTEGER}");
        }
        
        if (record.getCreateTime() != null) {
            sql.SET("create_time = #{record.createTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getUpdateTime() != null) {
            sql.SET("update_time = #{record.updateTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getExt1() != null) {
            sql.SET("ext_1 = #{record.ext1,jdbcType=VARCHAR}");
        }
        
        if (record.getExt2() != null) {
            sql.SET("ext_2 = #{record.ext2,jdbcType=VARCHAR}");
        }
        
        if (record.getExt3() != null) {
            sql.SET("ext_3 = #{record.ext3,jdbcType=VARCHAR}");
        }
        
        applyWhere(sql, example, true);
        return sql.toString();
    }

    public String updateByExample(Map<String, Object> parameter) {
        SQL sql = new SQL();
        sql.UPDATE("bs_merchant");
        
        sql.SET("id = #{record.id,jdbcType=BIGINT}");
        sql.SET("source_type = #{record.sourceType,jdbcType=INTEGER}");
        sql.SET("province_code = #{record.provinceCode,jdbcType=BIGINT}");
        sql.SET("province_name = #{record.provinceName,jdbcType=VARCHAR}");
        sql.SET("city_code = #{record.cityCode,jdbcType=BIGINT}");
        sql.SET("city_name = #{record.cityName,jdbcType=VARCHAR}");
        sql.SET("area_code = #{record.areaCode,jdbcType=BIGINT}");
        sql.SET("area_name = #{record.areaName,jdbcType=VARCHAR}");
        sql.SET("oil_price_zone_id = #{record.oilPriceZoneId,jdbcType=INTEGER}");
        sql.SET("oil_price_zone_name = #{record.oilPriceZoneName,jdbcType=VARCHAR}");
        sql.SET("chain_brand_id = #{record.chainBrandId,jdbcType=BIGINT}");
        sql.SET("chain_brand_name = #{record.chainBrandName,jdbcType=VARCHAR}");
        sql.SET("chain_brand_settle_type = #{record.chainBrandSettleType,jdbcType=INTEGER}");
        sql.SET("mer_no = #{record.merNo,jdbcType=VARCHAR}");
        sql.SET("mer_logo = #{record.merLogo,jdbcType=VARCHAR}");
        sql.SET("mer_name = #{record.merName,jdbcType=VARCHAR}");
        sql.SET("contacts_name = #{record.contactsName,jdbcType=VARCHAR}");
        sql.SET("contacts_tel = #{record.contactsTel,jdbcType=VARCHAR}");
        sql.SET("customer_service_tel = #{record.customerServiceTel,jdbcType=VARCHAR}");
        sql.SET("address = #{record.address,jdbcType=VARCHAR}");
        sql.SET("longitude = #{record.longitude,jdbcType=VARCHAR}");
        sql.SET("latitude = #{record.latitude,jdbcType=VARCHAR}");
        sql.SET("mer_label = #{record.merLabel,jdbcType=VARCHAR}");
        sql.SET("list_show = #{record.listShow,jdbcType=BIT}");
        sql.SET("`status` = #{record.status,jdbcType=INTEGER}");
        sql.SET("create_time = #{record.createTime,jdbcType=TIMESTAMP}");
        sql.SET("update_time = #{record.updateTime,jdbcType=TIMESTAMP}");
        sql.SET("ext_1 = #{record.ext1,jdbcType=VARCHAR}");
        sql.SET("ext_2 = #{record.ext2,jdbcType=VARCHAR}");
        sql.SET("ext_3 = #{record.ext3,jdbcType=VARCHAR}");
        
        BsMerchantExample example = (BsMerchantExample) parameter.get("example");
        applyWhere(sql, example, true);
        return sql.toString();
    }

    public String updateByPrimaryKeySelective(BsMerchant record) {
        SQL sql = new SQL();
        sql.UPDATE("bs_merchant");
        
        if (record.getSourceType() != null) {
            sql.SET("source_type = #{sourceType,jdbcType=INTEGER}");
        }
        
        if (record.getProvinceCode() != null) {
            sql.SET("province_code = #{provinceCode,jdbcType=BIGINT}");
        }
        
        if (record.getProvinceName() != null) {
            sql.SET("province_name = #{provinceName,jdbcType=VARCHAR}");
        }
        
        if (record.getCityCode() != null) {
            sql.SET("city_code = #{cityCode,jdbcType=BIGINT}");
        }
        
        if (record.getCityName() != null) {
            sql.SET("city_name = #{cityName,jdbcType=VARCHAR}");
        }
        
        if (record.getAreaCode() != null) {
            sql.SET("area_code = #{areaCode,jdbcType=BIGINT}");
        }
        
        if (record.getAreaName() != null) {
            sql.SET("area_name = #{areaName,jdbcType=VARCHAR}");
        }
        
        if (record.getOilPriceZoneId() != null) {
            sql.SET("oil_price_zone_id = #{oilPriceZoneId,jdbcType=INTEGER}");
        }
        
        if (record.getOilPriceZoneName() != null) {
            sql.SET("oil_price_zone_name = #{oilPriceZoneName,jdbcType=VARCHAR}");
        }
        
        if (record.getChainBrandId() != null) {
            sql.SET("chain_brand_id = #{chainBrandId,jdbcType=BIGINT}");
        }
        
        if (record.getChainBrandName() != null) {
            sql.SET("chain_brand_name = #{chainBrandName,jdbcType=VARCHAR}");
        }
        
        if (record.getChainBrandSettleType() != null) {
            sql.SET("chain_brand_settle_type = #{chainBrandSettleType,jdbcType=INTEGER}");
        }
        
        if (record.getMerNo() != null) {
            sql.SET("mer_no = #{merNo,jdbcType=VARCHAR}");
        }
        
        if (record.getMerLogo() != null) {
            sql.SET("mer_logo = #{merLogo,jdbcType=VARCHAR}");
        }
        
        if (record.getMerName() != null) {
            sql.SET("mer_name = #{merName,jdbcType=VARCHAR}");
        }
        
        if (record.getContactsName() != null) {
            sql.SET("contacts_name = #{contactsName,jdbcType=VARCHAR}");
        }
        
        if (record.getContactsTel() != null) {
            sql.SET("contacts_tel = #{contactsTel,jdbcType=VARCHAR}");
        }
        
        if (record.getCustomerServiceTel() != null) {
            sql.SET("customer_service_tel = #{customerServiceTel,jdbcType=VARCHAR}");
        }
        
        if (record.getAddress() != null) {
            sql.SET("address = #{address,jdbcType=VARCHAR}");
        }
        
        if (record.getLongitude() != null) {
            sql.SET("longitude = #{longitude,jdbcType=VARCHAR}");
        }
        
        if (record.getLatitude() != null) {
            sql.SET("latitude = #{latitude,jdbcType=VARCHAR}");
        }
        
        if (record.getMerLabel() != null) {
            sql.SET("mer_label = #{merLabel,jdbcType=VARCHAR}");
        }
        
        if (record.getListShow() != null) {
            sql.SET("list_show = #{listShow,jdbcType=BIT}");
        }
        
        if (record.getStatus() != null) {
            sql.SET("`status` = #{status,jdbcType=INTEGER}");
        }
        
        if (record.getCreateTime() != null) {
            sql.SET("create_time = #{createTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getUpdateTime() != null) {
            sql.SET("update_time = #{updateTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getExt1() != null) {
            sql.SET("ext_1 = #{ext1,jdbcType=VARCHAR}");
        }
        
        if (record.getExt2() != null) {
            sql.SET("ext_2 = #{ext2,jdbcType=VARCHAR}");
        }
        
        if (record.getExt3() != null) {
            sql.SET("ext_3 = #{ext3,jdbcType=VARCHAR}");
        }
        
        sql.WHERE("id = #{id,jdbcType=BIGINT}");
        
        return sql.toString();
    }

    protected void applyWhere(SQL sql, BsMerchantExample 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());
        }
    }
}