Test.java 30.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
import com.glsk.compute.fullupdatedata.DeleteSql;
import com.glsk.compute.fullupdatedata.QGQuerySql;
import com.glsk.compute.fullupdatedata.QuerySql;
import com.glsk.mapper.DatavMapper;
import com.glsk.util.DateUtils;
import com.glsk.util.MybatisUtil;
import com.glsk.util.MysqlUtils;
import com.glsk.vo.BusinessType;
import com.glsk.vo.QueryType;
import com.glsk.vo.ReqRet;
import com.glsk.vo.ResponseDTO;
import org.apache.ibatis.session.SqlSession;

import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.stream.Collectors;

/**
 * @author fangfang
 * @create 2024-04-09 16:11
 */
public class Test {
    public static String compareToToday(String day){
        int i = DateUtils.getToday().compareTo(day);
        if(i<=0){
            return  DateUtils.getYesterday();
        }else {
            return day;
        }

    }

    public static void main(String[] args) throws Exception {
        String startdate="2024-07-30";
        getqueryServiceBossRanking("2024-07-01","2024-07-30");



    }


    //销售排行
    public   static String  getquerySaleManRanking(String startdate,String enddate){
        String sql01="\n" +
                "select\n" +
                "\tmerchant_id \t,\n" +
                "\tmerchant_name\t ,\n" +
                "\t会员权益业务负责人id user_id,会员权益业务负责人姓名 employee_name,\n" +
                "\t(case when business_type=1 then '权益' when business_type=2 then '延保' end)  业务类型,\n" +
                "\tDATE_FORMAT(t.order_time,'%Y-%m-%d')statdate,\n" +
                "\tsum(total_price) 个人产值,\n" +
                "\tcount(distinct (case when t.upgrade_flag='Y'  then t.order_code else null end))升级订单数,\n" +
                "\tcount(distinct t.order_code)升级战败订单总数,now() created_date\n" +
                "from(\n" +
                "      select * from(\n" +
                "                  select ifnull(b.success_date,a.order_time)  order_time ,\n" +
                "                  a.order_code,a.user_id,a.merchant_id,a.merchant_name,a.employee_name,a.status,a.upgrade_flag,a.total_price,\n" +
                "                  a.deleted_flag,a.scene_type,a.business_type,a.updated_date\n" +
                "                  from dj_smartcarlife.dj_car_life_goods_order a\n" +
                "                  left join\n" +
                "                  (select * from dj_smartcarlife.dj_car_life_pay_order where deleted_flag = 'N' and order_code is not null) b\n" +
                "                  on a.order_code=b.order_code\n" +
                "                    where a.deleted_flag='N'\n" +
                "                    and merchant_name not like '%测试%'\n" +
                "                    and a.status in (2, 4, 6)\n" +
                "                    and a.upgrade_flag in ('Y','N')\n" +
                "                    and user_id is not null\n" +
                "                    and a.scene_type=0  -- 2.1会员权益场景\n" +
                "                    and a.business_type in (1,2)\n" +
                "                    and DATE_FORMAT(a.updated_date,'%Y-%m-%d')>='"+startdate+"'and DATE_FORMAT(a.updated_date,'%Y-%m-%d')<='"+enddate+"'\n" +
                "            )dclgo inner join (\n" +
                "                      select distinct merchant_id 门店id, quotation_director_user_id 会员权益业务负责人id,quotation_director_name 会员权益业务负责人姓名\n" +
                "                      from  dj_car_life_shop_merchant_config t where deleted_flag='N'\n" +
                "                      and quotation_director_user_id is not null and quotation_director_user_id !=\"\"\n" +
                "            )dclsmc on dclgo.merchant_id=dclsmc.门店id\n" +
                "          )t\n" +
                "      GROUP BY merchant_id,会员权益业务负责人id,DATE_FORMAT(t.order_time,'%Y-%m-%d'),business_type\n" +
                "            having merchant_id!='44205761' and 升级订单数>0";



        System.out.println("sql01="+sql01);

        return sql01;
    }
    //服务商老板排行
    public   static String  getqueryServiceBossRanking(String startdate,String enddate){
        String sql01="\n" +
                "select\n" +
                "\tmerchant_id \t,\n" +
                "\tmerchant_name\t ,\n" +
                "\t服务商负责人id user_id,服务商负责人姓名 employee_name,\n" +
                "\t(case when business_type=1 then '权益' when business_type=2 then '延保' end)  业务类型,\n" +
                "\tDATE_FORMAT(t.order_time,'%Y-%m-%d')statdate,\n" +
                "\tsum(total_price) 个人产值,\n" +
                "\tcount(distinct (case when t.upgrade_flag='Y'  then t.order_code else null end))升级订单数,\n" +
                "\tcount(distinct t.order_code)升级战败订单总数,now() created_date\n" +
                "from(\n" +
                "\t\t\tselect * from(\n" +
                "                  select ifnull(b.success_date,a.order_time)  order_time ,\n" +
                "                  a.order_code,a.user_id,a.merchant_id,a.merchant_name,a.employee_name,a.status,a.upgrade_flag,a.total_price,\n" +
                "                  a.deleted_flag,a.scene_type,a.business_type,a.updated_date\n" +
                "                  from dj_smartcarlife.dj_car_life_goods_order a\n" +
                "                  left join\n" +
                "                  (select * from dj_smartcarlife.dj_car_life_pay_order where deleted_flag = 'N' and order_code is not null) b\n" +
                "                  on a.order_code=b.order_code\n" +
                "                    where a.deleted_flag='N'\n" +
                "                    and merchant_name not like '%测试%'\n" +
                "                    and a.status in (2, 4, 6)\n" +
                "                    and a.upgrade_flag in ('Y','N')\n" +
                "                    and user_id is not null\n" +
                "                    and a.scene_type=0  -- 2.1会员权益场景\n" +
                "                    and a.business_type in (1,2)\n" +
                "                    and DATE_FORMAT(a.updated_date,'%Y-%m-%d')>='"+startdate+"'and DATE_FORMAT(a.updated_date,'%Y-%m-%d')<='"+enddate+"'\n" +
                "            )dclgo inner join (\n" +
                "                select distinct merchant_id 门店id,\n" +
                "                quotation_service_director_user_id  服务商负责人id,quotation_service_director_name 服务商负责人姓名\n" +
                "                from  dj_car_life_shop_merchant_config where deleted_flag='N' and quotation_service_director_user_id is not null and quotation_service_director_user_id !=\"\"\n" +
                "            )dclsmc on dclgo.merchant_id=dclsmc.门店id\n" +
                "          )t\n" +
                "\t\t\tGROUP BY merchant_id,服务商负责人id,DATE_FORMAT(t.order_time,'%Y-%m-%d'),business_type\n" +
                "            having merchant_id!='44205761' and 升级订单数>0";



        System.out.println("sql01="+sql01);

        return sql01;
    }






    public static void queryBoutiqueMerchantData (String startdate,String enddate ) throws Exception {

//        // 数据库连接信息
//        String url = "jdbc:mysql://192.168.3.227:3306/dj_smartcarlife";
//        String username = "dev_admin";
//        String password = "1f7e1ed2";
//
//
//        queryBoutiqueMerchantData("1990-07-29","2024-07-29");
//
        Connection connQuery = MysqlUtils.getConnection("dj_smartcarlife");
        Connection connInsert = MysqlUtils.getConnection("insert");
        Connection connQuery02 = MysqlUtils.getConnection("scrm_account");



        //查询近期内有变动的订单所涉及的订单日期
        String queryUpdateOrderDate = QuerySql.getqueryUpdateDateQG(startdate, enddate,connInsert);
        ResultSet queryUpdateOrderDateRS = connQuery.prepareStatement(queryUpdateOrderDate).executeQuery(queryUpdateOrderDate);

        ArrayList<String> OrderDateList =new ArrayList<>();
        while (queryUpdateOrderDateRS.next()){
            OrderDateList.add(queryUpdateOrderDateRS.getString("order_time"));
        }
        String OrderDateStr = OrderDateList.stream().map(String::valueOf).collect(Collectors.joining("','", "('", "')"));



        //获取sql
        String sql01 = QGQuerySql.getQGqueryMerchantDatasql01(startdate, enddate,OrderDateStr,connInsert);
        ResultSet rs00 = connQuery.prepareStatement(sql01).executeQuery(sql01);
        ArrayList<String> merchantNoList =new ArrayList<>();
        while (rs00.next()){
            merchantNoList.add(rs00.getString("4s店商户号"));
        }
        String merchantNo = merchantNoList.stream().map(String::valueOf).collect(Collectors.joining("','", "('", "')"));


        //获取账户中心的省市信息
        String sql02 = QGQuerySql.getqueryMerchantDatasql02(startdate, enddate,merchantNo);
        //获取账户中心的门店名称信息
        String sql03 = QuerySql.getqueryMerchantDatasql03(startdate, enddate,merchantNo);

        ResultSet rs02 = connQuery02.prepareStatement(sql02).executeQuery(sql02);
        ResultSet rs03 = connQuery02.prepareStatement(sql03).executeQuery(sql03);


        //预编译
        PreparedStatement st = connQuery.prepareStatement(sql01);
        // 执行查询并获取结果
        ResultSet rs = st.executeQuery(sql01);

        //品牌
//        List<Map<String, String>> merBrandMap = PlatformMerchantBrand.queryBrandByMerchantNo();



        //插入sql
        String insertsql = "INSERT INTO  business_analysis_boutique_overview (stat_date, merchant_id, merchant_name, business_type, item_name, output, ordernum, total_ordernum, to_shop_film_count, buynum, quotation_director_name,service_boss,created_date,province_code,province_name,city_code,city_name) " +
                " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) " +
                "ON DUPLICATE KEY UPDATE " +
                "stat_date=VALUES(stat_date), " +
                "merchant_id=VALUES(merchant_id), " +
                "merchant_name=VALUES(merchant_name), " +
                "business_type=VALUES(business_type), " +
                "item_name=VALUES(item_name), " +
                "output=VALUES(output), " +
                "ordernum=VALUES(ordernum), " +
                "total_ordernum=VALUES(total_ordernum), " +
                "to_shop_film_count=VALUES(to_shop_film_count), " +
                "buynum=VALUES(buynum), " +
                "quotation_director_name=VALUES(quotation_director_name), " +
                "service_boss=VALUES(service_boss), " +
                "created_date=VALUES(created_date)," +
                "province_code=VALUES(province_code)," +
                "province_name=VALUES(province_name)," +
                "city_code=VALUES(city_code)," +
                "city_name=VALUES(city_name)";

        PreparedStatement ps = connInsert.prepareStatement(insertsql);
        // 开启批处理模式
        connInsert.setAutoCommit(false);
        // 计数器
        int batchCount = 0;


        String created_date="";
        // 遍历结果集
        while (rs.next()) {
            created_date=rs.getString("created_date");
            String merchant_id=rs.getString("4s店商户号");


            ps.setString(1, rs.getString("statdate"));
            ps.setString(2, rs.getString("4s店商户号"));
            ps.setString(3, rs.getString("服务门店"));
            ps.setString(4, "轻改");
            ps.setString(5, null);
            ps.setString(6, rs.getString("个人产值"));
            ps.setString(7, rs.getString("升级订单数"));
            ps.setString(8, rs.getString("升级战败订单总数"));
            ps.setString(9, rs.getString("到店贴膜量"));
            ps.setString(10, rs.getString("新车交付数"));
            ps.setString(11, rs.getString("业务负责人姓名"));
            ps.setString(12, rs.getString("服务商负责人姓名"));
            ps.setString(13, rs.getString("created_date"));
            ps.setString(14, null);
            ps.setString(15, null);
            ps.setString(16, null);
            ps.setString(17, null);



            while (rs02.next()){
                if(merchant_id.equals(rs02.getString("merchantNo"))){
                    ps.setString(14, rs02.getString("province_code"));
                    ps.setString(15, rs02.getString("province_name"));
                    ps.setString(16, rs02.getString("city_code"));
                    ps.setString(17, rs02.getString("city_name"));
                    break;
                }
            }
            rs02.beforeFirst();


            while (rs03.next()){
                if(merchant_id.equals(rs03.getString("merchantNo"))){
                    ps.setString(3, rs03.getString("merchant_name"));
                    break;
                }
            }
            rs03.beforeFirst();





//
//            //6.执行操作
//            ps.executeUpdate();


            // 添加到批处理
            ps.addBatch();
            // 更新计数器
            batchCount++;

            // 如果攒够了500条,执行批处理并提交
            if (batchCount >= 500) {
                ps.executeBatch(); // 执行批处理
                ps.clearBatch(); // 清空批处理(可选,但推荐)
                connInsert.commit(); // 提交事务
                batchCount = 0; // 重置计数器
            }

        }


        // 循环结束后,如果还有剩余的SQL语句(即最后不足500条),执行并提交它们
        if (batchCount > 0) {
            ps.executeBatch();
            connInsert.commit();
        }




        //业务已删除数据处理
        String deleteDataSql = DeleteSql.getdeletesql09(startdate, enddate,connInsert);
        ResultSet deleteDataRs = connQuery.prepareStatement(deleteDataSql).executeQuery(deleteDataSql);
        ArrayList<String> dataList =new ArrayList<>();
        while (deleteDataRs.next()){
            dataList.add(deleteDataRs.getString("effectivedata"));
        }

        String wheremerchants=" ";
        if(!dataList.isEmpty()){
            String dataS = dataList.stream().map(String::valueOf).collect(Collectors.joining("','", "('", "')"));
            wheremerchants=" and  concat(merchant_id,stat_date,business_type)   not in"+dataS;

            String deleteSql="delete from business_analysis_boutique_overview where  1=1 \n" +
                    " "+wheremerchants+"\n";
            connInsert.prepareStatement(deleteSql).executeUpdate();
        }





        String deleteSql02="delete from business_analysis_boutique_overview  where merchant_id in (select distinct merchant_id from business_analysis_delete_merchant);";
        connInsert.prepareStatement(deleteSql02).executeUpdate();




        String update01="update business_analysis_boutique_overview a,\n" +
                "(select distinct province_code,province_name from business_analysis_province_city_district) b\n" +
                "set a.province_name=b.province_name\n" +
                "where a.province_code=b.province_code";
        String update02="update business_analysis_boutique_overview a,\n" +
                "(select distinct city_code,city_name from business_analysis_province_city_district) b\n" +
                "set a.city_name=b.city_name \n" +
                "where a.city_code=b.city_code";

        String update03="update business_analysis_boutique_overview a, \n" +
                "      (select distinct merchant_id,merchant_name from business_analysis_boutique_overview  where  created_date=(\n" +
                "\t\t\tselect max(created_date)from business_analysis_boutique_overview \n" +
                "\t\t\t)) b \n" +
                "set a.merchant_name=b.merchant_name\n" +
                "where a.merchant_id=b.merchant_id";
        connInsert.prepareStatement(update03).executeUpdate();

        connInsert.prepareStatement(update01).executeUpdate();
        connInsert.prepareStatement(update02).executeUpdate();


        // 关闭结果集、Statement和连接
        rs.close();
        rs02.close();
        rs00.close();
        //关闭PreparedStatement
        ps.close();
        st.close();
        //关闭连接
        connQuery.close();
        connInsert.close();

    }






    public ReqRet.OutputTrendRet queryOutputTrend(String itemname, BusinessType business_type, QueryType queryType, String startDay, String endDay) {
        ReqRet.OutputTrendRet ret=new ReqRet.OutputTrendRet();
        SqlSession querySession = null;
        try{
            querySession = MybatisUtil.getsqlSession("query");
        }catch (Exception e){
            e.printStackTrace();
        }
        DatavMapper datavMapper  = querySession.getMapper(DatavMapper.class);



        String whereitemname=" ";
        if(itemname !=null && itemname.equals("赛力斯项目")){
            whereitemname ="and item_name='"+itemname+"' ";
        }

        String wherebusiness_type=" ";
        if(wherebusiness_type !=null){
            String  business_typename="";
            if(business_type.equals(BusinessType.quanyi)){
                business_typename="权益";
            }else if(business_type.equals(BusinessType.yanbao)){
                business_typename="延保";
            }

            wherebusiness_type= "and business_type='"+business_typename+"'";

        }


        String finalsql="";
        String statDatesql="";
        if(queryType.equals(QueryType.day) || queryType.equals(QueryType.self)){
            statDatesql="SELECT a.selected_date stat_date  FROM\n" +
                    "    (\n" +
                    "        SELECT adddate('1970-01-01',t4.i * 10000 + t3.i * 1000 + t2.i * 100 + t1.i * 10 + t0.i) selected_date\n" +
                    "        FROM\n" +
                    "            ( SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4\n" +
                    "              UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t0,\n" +
                    "            ( SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4\n" +
                    "              UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t1,\n" +
                    "            ( SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4\n" +
                    "              UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t2,\n" +
                    "            ( SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4\n" +
                    "              UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t3,\n" +
                    "            ( SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4\n" +
                    "              UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t4\n" +
                    "    ) a\n" +
                    "WHERE selected_date BETWEEN DATE_SUB('"+startDay+"',INTERVAL 8 day) AND '"+endDay+"'\n" +
                    "order by stat_date desc  limit 8 ";


            String wherestat_date=" ";
            if(startDay !=null ){
                wherestat_date= "and stat_date  >=DATE_SUB('"+startDay+"',INTERVAL 10 day) and stat_date  <='"+endDay+"'";
            }
            String tmpsql="\tselect stat_date,output   \n" +
                    "\tfrom business_analysis_overview  \n" +
                    "\twhere 1=1\n" +
                    "\t"+whereitemname+" \n" +
                    "\t"+wherebusiness_type+"\n" +
                    "\t"+wherestat_date+"\n";
            finalsql="select stat_date stat_date, CONCAT(FORMAT(sum(output),2))total_output  from ( \n"+tmpsql+")t group by stat_date \n" +
                    "  order by stat_date desc  limit 8";
        }else if(queryType.equals(QueryType.week)){
            statDatesql="SELECT  DATE_FORMAT(selected_date,'%x-%v')  stat_date,\n" +
                    "DATE_SUB(selected_date, INTERVAL WEEKDAY(selected_date) DAY) AS monday,\n" +
                    "DATE_ADD(DATE_SUB(selected_date, INTERVAL WEEKDAY(selected_date) DAY), INTERVAL 6 DAY) AS sunday\n" +
                    "   FROM\n" +
                    "    (\n" +
                    "        SELECT adddate('1970-01-01',t4.i * 10000 + t3.i * 1000 + t2.i * 100 + t1.i * 10 + t0.i) selected_date\n" +
                    "        FROM\n" +
                    "            ( SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4\n" +
                    "              UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t0,\n" +
                    "            ( SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4\n" +
                    "              UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t1,\n" +
                    "            ( SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4\n" +
                    "              UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t2,\n" +
                    "            ( SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4\n" +
                    "              UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t3,\n" +
                    "            ( SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4\n" +
                    "              UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t4\n" +
                    "    ) a\n" +
                    "WHERE selected_date BETWEEN DATE_SUB('"+startDay+"',INTERVAL 7 week)AND  '"+endDay+"'\n" +
                    "group by  DATE_FORMAT(selected_date,'%x-%v') \n" +
                    "order by stat_date  desc";

            String wherestat_date=" ";
            if(startDay !=null ){
                wherestat_date= "and stat_date  >=DATE_SUB('"+startDay+"',INTERVAL 10 week) and stat_date  <='"+endDay+"'";
            }
            String tmpsql="\tselect stat_date,output   \n" +
                    "\tfrom business_analysis_overview  \n" +
                    "\twhere 1=1\n" +
                    "\t"+whereitemname+" \n" +
                    "\t"+wherebusiness_type+"\n" +
                    "\t"+wherestat_date+"\n";
            finalsql="select DATE_FORMAT(stat_date,'%x-%v')stat_date, CONCAT(FORMAT(sum(output),2))total_output  ,\n" +
                    "WEEKDAY(stat_date), DATE_SUB(stat_date, INTERVAL WEEKDAY(stat_date) DAY) AS monday,\n" +
                    "DATE_ADD(DATE_SUB(stat_date, INTERVAL WEEKDAY(stat_date) DAY), INTERVAL 6 DAY) AS sunday\n" +
                    " from (\n"
                    +tmpsql+
                    ")t  group by  DATE_FORMAT(stat_date,'%x-%v')  order by stat_date desc  limit 8";

        }else if(queryType.equals(QueryType.month)){
            statDatesql="SELECT  DATE_FORMAT(selected_date,'%Y-%m')   stat_date \n" +
                    "   FROM\n" +
                    "    (\n" +
                    "        SELECT adddate('1970-01-01',t4.i * 10000 + t3.i * 1000 + t2.i * 100 + t1.i * 10 + t0.i) selected_date\n" +
                    "        FROM\n" +
                    "            ( SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4\n" +
                    "              UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t0,\n" +
                    "            ( SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4\n" +
                    "              UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t1,\n" +
                    "            ( SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4\n" +
                    "              UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t2,\n" +
                    "            ( SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4\n" +
                    "              UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t3,\n" +
                    "            ( SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4\n" +
                    "              UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t4\n" +
                    "    ) a\n" +
                    "WHERE selected_date BETWEEN DATE_SUB('"+startDay+"',INTERVAL 7 month)AND  '"+endDay+"'\n" +
                    "group by  DATE_FORMAT(selected_date,'%Y-%m') \n" +
                    "order by stat_date  desc\n";



            String wherestat_date=" ";
            if(startDay !=null ){
                wherestat_date= "and stat_date  >=DATE_SUB('"+startDay+"',INTERVAL 10 month) and stat_date  <='"+endDay+"'";
            }

            String tmpsql="\tselect stat_date,output   \n" +
                    "\tfrom business_analysis_overview  \n" +
                    "\twhere 1=1\n" +
                    "\t"+whereitemname+" \n" +
                    "\t"+wherebusiness_type+"\n" +
                    "\t"+wherestat_date+"\n";
            finalsql="select DATE_FORMAT(stat_date,'%Y-%m')stat_date, CONCAT(FORMAT(sum(output),2))total_output    \n" +
                    " from (\n"
                    +tmpsql+
                    ")t  group by DATE_FORMAT(stat_date,'%Y-%m')  order by stat_date desc  limit 8\n";
        }

        System.out.println("statDatesql=  "+statDatesql);
        System.out.println("finalsql=  "+finalsql);

        List<Map> statDateList = datavMapper.sqlBySelf(statDatesql);
        List<Map> mapList = datavMapper.sqlBySelf(finalsql);


        List<ResponseDTO.OutputTrend> detailList =new ArrayList<>();

        if(queryType.equals(QueryType.week)){


            for(int i=statDateList.size()-1; i>=0;i--){
                if(statDateList.get(i)!=null){

                    ResponseDTO.OutputTrend data=new ResponseDTO.OutputTrend();
                    data.setOutput("0");//默认0
                    if(i==0){
                        data.setStattime("当前周");

                    }else {
                        data.setStattime("前"+i+"周");
                    }

                    for (Map map : mapList) {
                        if(statDateList.get(i).get("stat_date").equals(String.valueOf(map.get("stat_date")))){
                            data.setOutput(String.valueOf(map.get("total_output")).replace(",",""));  //如有值改成对应值
                        }
                    }

                    detailList.add(data);
                }
            }

//            for(int i=mapList.size()-1; i>=0;i--){
//                if(mapList.get(i)!=null){
//                    ResponseDTO.OutputTrend data=new ResponseDTO.OutputTrend();
//                    data.setOutput(String.valueOf(mapList.get(i).get("total_output")).replace(",",""));
//                     if(i==0){
//                         data.setStattime("当前周");
//
//                     }else {
//                         data.setStattime("前"+i+"周");
//
//                     }
//                    detailList.add(data);
//                }
//            }



        }else  if(queryType.equals(QueryType.day) ||  queryType.equals(QueryType.self)){


            for(int i=statDateList.size()-1; i>=0;i--){
                if(statDateList.get(i)!=null){
                    ResponseDTO.OutputTrend data=new ResponseDTO.OutputTrend();
                    data.setOutput("0");//设置默认值
                    String[] split=DateUtils.convTime8(String.valueOf(statDateList.get(i).get("stat_date"))).split("-");
                    data.setStattime(split[split.length - 1]+"号");

                    for (Map map : mapList) {
                        if(statDateList.get(i).get("stat_date").equals(String.valueOf(map.get("stat_date")))){
                            data.setOutput(String.valueOf(map.get("total_output")).replace(",",""));
                        }
                    }

                    detailList.add(data);
                }
            }


//            for(int i=mapList.size()-1; i>=0;i--){
//                if(mapList.get(i)!=null){
//                    ResponseDTO.OutputTrend data=new ResponseDTO.OutputTrend();
//                    data.setOutput(String.valueOf(mapList.get(i).get("total_output")).replace(",",""));
//                    String[] split=DateUtils.convTime8(String.valueOf(mapList.get(i).get("stat_date"))).split("-");
//                    data.setStattime(split[split.length - 1]+"号");
//                    detailList.add(data);
//                }
//            }


        }else if(queryType.equals(QueryType.month)){

            for(int i=statDateList.size()-1; i>=0;i--){
                if(statDateList.get(i)!=null){
                    ResponseDTO.OutputTrend data=new ResponseDTO.OutputTrend();
                    data.setOutput("0");//设置默认值
                    String[] split=DateUtils.convTime9(String.valueOf(statDateList.get(i).get("stat_date"))).split("-");
                    data.setStattime(split[split.length - 1]+"月");

                    for (Map map : mapList) {
                        if(statDateList.get(i).get("stat_date").equals(String.valueOf(map.get("stat_date")))){
                            data.setOutput(String.valueOf(map.get("total_output")).replace(",",""));
                        }
                    }



                    detailList.add(data);
                }
            }


//            for(int i=mapList.size()-1; i>=0;i--){
//                    if(mapList.get(i)!=null){
//                        ResponseDTO.OutputTrend data=new ResponseDTO.OutputTrend();
//                        data.setOutput(String.valueOf(mapList.get(i).get("total_output")).replace(",",""));
//                        String[] split=DateUtils.convTime9(String.valueOf(mapList.get(i).get("stat_date"))).split("-");
//                        data.setStattime(split[split.length - 1]+"月");
//                        detailList.add(data);
//                    }
//                }

        }



        ret.setCode(0);
        ret.setMessage("success");
        ret.setList(detailList);

        return ret;

    }

}