package com.sifang.modules.platform.utils;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.util.SqlBuilder;

import java.util.ArrayList;
import java.util.List;

public class AddressUtils {

    /**
     * 获取省份
    */
    public static  List<Record> getProvincesList(){
        String sql = "SELECT a.ProvinceID,a.ProvinceName,a.regionalism_code FROM s_province a ";

        return Db.find(sql);
    }

    /**
     * 获取代理商授权省份
     */
    public static List<Record> findProvinceListByTraderId(String traderId, String province) {
        SqlBuilder sql = new SqlBuilder();
        sql.line("SELECT DISTINCT(a.ProvinceID),a.ProvinceName,a.regionalism_code,");
        sql.line("(CASE WHEN b.trader_id IS NULL THEN '' ELSE b.trader_id END) AS traderId");
        sql.line("FROM s_province a");
        sql.line("LEFT JOIN crm_trader_pcd b");
        sql.line("ON b.province = a.ProvinceName");
        sql.line("WHERE b.trader_id = ?", traderId);
        if (StringUtil.isNotBlank(province)) {
            sql.line("AND a.ProvinceName = ?", province);
        }
        sql.line(" GROUP BY ProvinceID");
        return Db.find(sql.build(), sql.params());
    }

    public static  List<Record> getCityList(){
        String sql = "SELECT c.CityID,c.CityName,c.ProvinceId,c.AreaCode,c.regionalism_code FROM s_city c ";

        return Db.find(sql);
    }

    /**
     * 根据省获取市
    */
    public static List<Record> getCitysByProId(String provinceId){
        String sql = "SELECT c.CityID,c.CityName,c.ProvinceId,c.AreaCode,c.regionalism_code FROM s_city c WHERE c.ProvinceId=?";
        return Db.find(sql, provinceId);
    }

    /**
     * 获取代理商授权城市
    */
    public static List<Record> findCityListByTraderId(String traderId, String provinceId, String province, String city){
        SqlBuilder sql = new SqlBuilder();
        sql.line("SELECT DISTINCT(c.CityID),c.CityName,c.ProvinceId,c.AreaCode,c.regionalism_code,");
        if (StringUtil.isNotBlank(traderId)) {
            sql.line("(CASE WHEN b.trader_id IS NULL THEN '' ELSE b.trader_id END) AS traderId");
        } else {
            sql.line("'' AS traderId");
        }
        sql.line("FROM s_city c");
        sql.line("LEFT JOIN s_province a ON a.ProvinceID = c.ProvinceId");
        if (StringUtil.isNotBlank(traderId)) {
            sql.line("LEFT JOIN crm_trader_pcd b");
            sql.line("ON b.`province` = a.ProvinceName and b.trader_id = ?", traderId);
        }
        sql.line("WHERE 1=1");
        if (StringUtil.isNotBlank(traderId)) {
            sql.line("AND (b.city = c.CityName OR (b.`city` IS NULL AND b.`id` IS NOT NULL))");
        }
        if (StringUtil.isNotBlank(provinceId)) {
            sql.line("AND c.ProvinceId = ?", provinceId);
        }
        if (StringUtil.isNotBlank(province)) {
            sql.line("AND a.ProvinceName = ?", province);
        }
        if (StringUtil.isNotBlank(city)) {
            sql.line("AND c.CityName = ?", city);
        }
        return Db.find(sql.build(), sql.params());
    }

    /**
     * 获取区县
     */
    public static List<Record> getAreaByCityId(String cityId) {
        String sql = "SELECT d.CityId,d.DistrictID,d.DistrictName,d.regionalism_code FROM s_district d WHERE d.CityId=? ";
        return Db.find(sql, cityId);
    }

    /**
     * 获取代理商授权区域
     */
    public static List<Record> findAreaListByTraderId(String traderId, String province,
                                                      String cityId, String city, String area){
        SqlBuilder sql = new SqlBuilder();
        sql.line("SELECT DISTINCT(d.DistrictID),d.CityId,d.DistrictName,d.regionalism_code");
        sql.line("FROM s_district d");
        sql.line("LEFT JOIN s_city c ON c.CityID = d.CityId");
        sql.line("LEFT JOIN s_province a ON a.ProvinceID = c.ProvinceId");
        if (StringUtil.isNotBlank(traderId)) {
            sql.line("LEFT JOIN crm_trader_pcd b");
            sql.line("ON b.`province` = a.ProvinceName AND b.trader_id = ? ", traderId);
        }
        sql.line("WHERE 1=1");
        if (StringUtil.isNotBlank(traderId)) {
            sql.line("AND b.`id` IS NOT NULL ");
            sql.line("AND ( (b.`city` IS NULL) ");
            sql.line("  OR (b.`city` = c.`CityName` AND b.area IS NULL) ");
            sql.line("  OR (b.`city` = c.`CityName` AND b.area = d.DistrictName) )");
        }
        if (StringUtil.isNotBlank(cityId)) {
            sql.line("AND d.CityId = ?", cityId);
        }
        if (StringUtil.isNotBlank(city)) {
            sql.line("AND c.CityName = ?", city);
        }
        if (StringUtil.isNotBlank(province)) {
            sql.line("AND a.ProvinceName = ?", province);
        }
        if (StringUtil.isNotBlank(area)) {
            sql.line("AND d.DistrictName = ?", area);
        }
        return Db.find(sql.build(), sql.params());
    }

    /**
     * 获取街道
     */
    public static List<Record> getStreetByAreaId(String areaId) {
        String sql = "SELECT s.street_id,s.streetName FROM  `s_street` s WHERE s.DistrictID=? ";
        return Db.find(sql, areaId);
    }

    /**
     * 获取街道
     */
    public static Record getCityByCityName(String province, String city) {
        StringBuilder sf = new StringBuilder();
        sf.append(" select c.* ");
        sf.append(" from s_province p ");
        sf.append(" inner join s_city c on c.ProvinceId= p.ProvinceID ");
        sf.append(" where p.ProvinceName=? and c.CityName=? ");
        return Db.findFirst(sf.toString(), province, city);
    }



    /**
     * 根据名称省查询市
     */
    public static List<Record> getCityByProName(String provinceName) {
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT c.CityID,c.CityName,c.ProvinceId,c.AreaCode FROM s_city c ");
        sf.append(" LEFT JOIN s_province p ON p.ProvinceID = c.ProvinceId ");
        sf.append(" WHERE p.ProvinceName=? ");
        return Db.find(sf.toString(), provinceName);
    }

    public static  List<Record> getCityByProviceRegionalismCode(String regionalismCode){
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT c.CityID,c.CityName,c.ProvinceId,c.AreaCode,c.regionalism_code FROM s_city c ");
        sf.append(" LEFT JOIN s_province p ON p.ProvinceID = c.ProvinceId ");
        sf.append(" WHERE p.regionalism_code=? ");
        return Db.find(sf.toString(),regionalismCode);
    }

    /**
     * 根据名称市查询区县
     */
    public static List<Record> getAreaByCityName(String cityName){
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT d.CityId,d.DistrictID,d.DistrictName FROM  s_district d ");
        sf.append(" LEFT JOIN s_city c ON c.CityId = d.CityId ");
        sf.append(" WHERE c.CityName=? ");
        return Db.find(sf.toString(),cityName);
    }

    /**
     * 根据名称市查询区域
     */
    public static List<Record> getAreaListByProName(String cityName) {
        if (StringUtil.isBlank(cityName)) {
            return null;
        }
        String[] cityname = cityName.split(",");
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT d.CityId,d.DistrictID,d.DistrictName FROM  s_district d");
        sf.append(" LEFT JOIN s_city c ON c.CityId = d.CityId");
        sf.append(" WHERE c.CityName in(" + StringUtil.joinInSql(cityname) + ") ");
        return Db.find(sf.toString());
    }

    public static List<Record> getAreaByCityRegionalismCode(String regionalismCode){
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT d.CityId,d.DistrictID,d.DistrictName,d.regionalism_code FROM  s_district d ");
        sf.append(" LEFT JOIN s_city c ON c.CityId = d.CityId ");
        sf.append(" WHERE c.regionalism_code=? ");
        return Db.find(sf.toString(),regionalismCode);
    }
    /**
     * 查询所有的省市区
    */
    /**
     * Gets the province city district data.
     *
     * @return the province city district data
     */
    public static List<PCDInfo> getProvinceCityDistrictData() {
        // TODO Auto-generated method stub
        List<PCDInfo> pcdes = new ArrayList<>();

        String query = "SELECT sp.ProvinceName, sp.ProvinceId, sc.CityName, sc.CityId, sd.DistrictName, sd.DistrictId "
                + " FROM s_city sc, s_province sp, s_district sd "
                + " WHERE sp.ProvinceID=sc.ProvinceId AND sc.CityID=sd.CityId";
        List<Record> records = Db.find(query);

        if (records != null) {
            for (Record record : records) {
                PCDInfo info = new PCDInfo();
                info.setProvinceId((Long) record.get("ProvinceId"));
                info.setProvinceName(String.valueOf(record.get("ProvinceName")));
                info.setCityId((Long) record.get("CityId"));
                info.setCityName(String.valueOf(record.get("CityName")));
                info.setDistrictId((Long) record.get("DistrictId"));
                info.setDistrictName(String.valueOf(record.get("DistrictName")));
                pcdes.add(info);
            }
        }
        return pcdes;
    }


    /**
     * 根据名称省查询市
     */
    public static List<Record> getCityListByProName(String provinceName){
        if(StringUtil.isBlank(provinceName)){
            return null;
        }
        String proname[] = provinceName.split(",");
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT c.CityID,c.CityName,c.ProvinceId,c.AreaCode FROM s_city c ");
        sf.append(" LEFT JOIN s_province p ON p.ProvinceID = c.ProvinceId ");
        sf.append(" WHERE p.ProvinceName  in("+ StringUtil.joinInSql(proname) +") ");
        return Db.find(sf.toString());
    }

    public static Record getProCityArea(String proId,String cityId,String areaId){
        return Db.findFirst("SELECT p.ProvinceName,s.`CityName`,d.`DistrictName` " +
                "FROM `s_province` p  " +
                "LEFT JOIN `s_city` s ON s.ProvinceId=p.`ProvinceID` " +
                "LEFT JOIN `s_district` d ON d.`CityId`=s.`CityID` " +
                "WHERE p.regionalism_code=? AND s.`regionalism_code`=? AND d.`regionalism_code`=? ",proId,cityId,areaId);
    }

    //根据文件地址判断文件是否是图片  常见的图片格式主要有jpg、png、gif、psd、tif、bmp等格式
    public static Boolean checkIfImg(String imgSrc){
        int filenameLeng = imgSrc.lastIndexOf(".");
        String extName = imgSrc.substring(filenameLeng).toLowerCase();
        if(StringUtil.equalsAny(extName,".jpg",".jpeg",".png",".gif",".psd",".tif",".bmp")){
            return true;
        }
        return false;
    }

    public static List<Record> findProCodeList(){
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT * FROM `crm_province_code` ");
        return Db.find(sf.toString());
    }

    public static String findProCodeNameByCode(String proCode){
        StringBuilder sf = new StringBuilder();
        sf.append(" SELECT a.power_company FROM `crm_province_code` a WHERE a.code = ? LIMIT 1");
        return Db.queryStr(sf.toString(), proCode);
    }

    public static Record getProvinceCode(String name) {
        return Db.findFirst("select ProvinceID,regionalism_code from s_province where ProvinceName=?", name);
    }

    public static Record getCityCode(String name, Long provinceId) {
        return Db.findFirst("select c.CityID,IFNULL(p.`regionalism_code`,c.`regionalism_code`) AS regionalism_code from s_city c  " +
                " LEFT JOIN s_city_parnter p ON p.`CityID` = c.`CityID` " +
                " where c.CityName=? and c.ProvinceId=?", name, provinceId);
    }

    public static Long getDistrictCode(String name, Long cityId) {
        return Db.queryLong("select regionalism_code from s_district where DistrictName=? and CityId=?", name, cityId);
    }

}
