update1.1.sql 49.9 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 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
CREATE TABLE `crm_sys_sale_type` (
  `SAL_TYPE_ID` varchar(16) COLLATE utf8_bin NOT NULL COMMENT 'CRM系统销售类型ID',
  `SAL_TYPE_NM` varchar(64) COLLATE utf8_bin NOT NULL COMMENT 'CRM系统销售类型名称',
  `VALID_FLAG` tinyint(1) NOT NULL DEFAULT '1' COMMENT '销售类型有效标志',
  `UPT_FLAG` tinyint(1) DEFAULT '1' COMMENT '有效销售单数标志',
  `REC_TIME` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `REC_USER` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`SAL_TYPE_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;


CREATE TABLE `crm_customer_guider` (
  `CST_ID` bigint(20) NOT NULL COMMENT '顾客ID',
  `SIT_ID` bigint(20) NOT NULL COMMENT '网点ID',
  `GUIDER_ID` varchar(64) COLLATE utf8_bin NOT NULL COMMENT '导购编号',
  UNIQUE KEY `CST_ID` (`CST_ID`,`SIT_ID`),
  KEY `GUIDER_ID` (`GUIDER_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='顾客导购信息表';

CREATE TABLE `crm_guider` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `GUIDER_ID` varchar(60) COLLATE utf8_bin NOT NULL COMMENT '导购ID',
  `EMP_ID` int(11) NOT NULL COMMENT '雇员ID',
  `SITE_ID` int(11) NOT NULL COMMENT '网点ID',
  `role` int(11) DEFAULT NULL COMMENT '角色id( 0 店长,1店员,2 其它)',
  `HEADER` varchar(128) COLLATE utf8_bin DEFAULT NULL COMMENT '头像',
  `role_description` int(11) DEFAULT NULL COMMENT '角色描述',
  `state` int(11) DEFAULT NULL COMMENT '状态( 0 禁用,1可用)',
  `REC_TIME` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '记录时间',
  PRIMARY KEY (`ID`),
  UNIQUE KEY `GUIDER_ID` (`GUIDER_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='导购表';



CREATE TABLE `crm_site_sale`(
	`SIT_ID` bigint(20) NULL  COMMENT '销售网点' , 
	`MONTH` int(11) NULL  COMMENT '月份' , 
	`QUANTITY` int(11) NULL  DEFAULT 0 COMMENT '数量' , 
	`END_PRICE` decimal(10,2) NULL  DEFAULT 0.00 COMMENT '结算价汇总' , 
	`FACT_PRICE` decimal(10,2) NULL  DEFAULT 0.00 COMMENT '实际金额汇总' , 
	`PAYABLE` decimal(10,2) NULL  DEFAULT 0.00 COMMENT '应付汇总' , 
	`PAID` decimal(10,2) NULL  DEFAULT 0.00 COMMENT '实付汇总' , 
	`REC_TIME` timestamp NULL  DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP COMMENT '记录时间' 
) ENGINE=InnoDB DEFAULT CHARSET='utf8' COLLATE='utf8_bin' COMMENT='网点月销售额';


CREATE TABLE `crm_site_sale_target`(
	`target_id` int(11) NOT NULL  auto_increment COMMENT '变量域' , 
	`siId` int(11) NOT NULL  COMMENT '网点ID' , 
	`MONTH` int(11) NOT NULL  COMMENT '月份' , 
	`target_sale` bigint(20) NULL  COMMENT '销售目标,单位元' , 
	`target_recruit` int(11) NULL  COMMENT '招募目标' , 
	`REC_TIME` timestamp NULL  DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP COMMENT '记录时间' , 
	`REC_USER` bigint(20) NULL  COMMENT '记录人' , 
	PRIMARY KEY (`target_id`) , 
	UNIQUE KEY `site_month`(`siId`,`MONTH`) 
) ENGINE=InnoDB DEFAULT CHARSET='utf8' COLLATE='utf8_bin' COMMENT='网点月目标';

	
CREATE TABLE `excel_crm_coupons_grant_shopper`(
	`SHP_ID` bigint(20) NOT NULL  COMMENT '雇员ID' , 
	`PER_NUM` int(20) NULL  COMMENT '每人数量' , 
	`DOC_CODE` varchar(20) COLLATE utf8_bin NOT NULL  COMMENT '单据编号' , 
	`REC_TIME` timestamp NULL  DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP COMMENT '记录时间' , 
	`SOURCE` varchar(32) COLLATE utf8_bin NULL  COMMENT '雇员来源' , 
	`GROUP_ID` varchar(32) COLLATE utf8_bin NOT NULL  COMMENT '组id,多用户操作的时的区分字段' , 
	`ROWNUM` int(10) unsigned NOT NULL  , 
	`ERROR` tinyint(4) NOT NULL  DEFAULT 0 
) ENGINE=InnoDB DEFAULT CHARSET='utf8' COLLATE='utf8_bin' COMMENT='雇员excel导入功能';


/* Create table in target */
CREATE TABLE `excel_crm_customer_tags`(
	`CST_ID` bigint(20) NOT NULL  COMMENT '顾客ID' , 
	`TAG_KEY` bigint(20) NOT NULL  COMMENT '标签名' , 
	`TAG_VAL` bigint(20) NOT NULL  COMMENT '标签值ID' , 
	`REC_TIME` timestamp NULL  DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP COMMENT '记录时间' , 
	`OP_USER` bigint(20) NULL  DEFAULT 0 COMMENT '打标签的人' , 
	`SOURCE` varchar(32) COLLATE utf8_bin NULL  COMMENT '客户来源' , 
	`GROUP_ID` varchar(32) COLLATE utf8_bin NOT NULL  COMMENT '组id,多用户操作的时的区分字段' , 
	`ROWNUM` int(10) unsigned NOT NULL  , 
	`ERROR` tinyint(4) NOT NULL  DEFAULT 0 
) ENGINE=InnoDB DEFAULT CHARSET='utf8' COLLATE='utf8_bin' COMMENT='标签excel导入功能';


/* Create table in target */
CREATE TABLE `excel_crm_point_adjust_detail`(
	`CST_ID` bigint(20) NOT NULL  COMMENT '顾客ID/手机号' , 
	`POINTS` double NOT NULL  COMMENT '调整积点值' , 
	`DAYS` int(11) NULL  COMMENT '有效天数' , 
	`ORDER_ID` varchar(32) COLLATE utf8_bin NULL  COMMENT '关联单据编号' , 
	`REMARK` varchar(200) COLLATE utf8_bin NULL  COMMENT '备注' , 
	`ADJ_ID` varchar(16) COLLATE utf8_bin NOT NULL  COMMENT '单据编号' , 
	`REC_TIME` timestamp NULL  DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP COMMENT '记录时间' , 
	`GROUP_ID` varchar(32) COLLATE utf8_bin NOT NULL  COMMENT '组id,多用户操作的时的区分字段' , 
	`ROWNUM` int(10) unsigned NOT NULL  , 
	`ERROR` tinyint(4) NOT NULL  DEFAULT 0 
) ENGINE=InnoDB DEFAULT CHARSET='utf8' COLLATE='utf8_bin' COMMENT='积点调整单excel导入功能';


/* Create table in target */
CREATE TABLE `excel_mtsite`(
	`CODE` varchar(30) COLLATE utf8_bin NOT NULL  COMMENT '名称' , 
	`NAME` varchar(120) COLLATE utf8_bin NULL  COMMENT '名称' , 
	`SITYPE` varchar(30) COLLATE utf8_bin NULL  COMMENT '组织类型' , 
	`SISALETYPE` varchar(30) COLLATE utf8_bin NULL  COMMENT '分销类型' , 
	`SIOPENDATE` timestamp NULL  COMMENT '实际开张时间' , 
	`BENAME` varchar(120) COLLATE utf8_bin NULL  COMMENT '所属经营主体' , 
	`BEBNAME` varchar(120) COLLATE utf8_bin NULL  COMMENT '结算主体' , 
	`REC_TIME` timestamp NULL  on update CURRENT_TIMESTAMP COMMENT '记录时间' , 
	`GROUP_ID` varchar(32) COLLATE utf8_bin NULL  COMMENT '组id,多用户操作的时的区分字段' , 
	`ROWNUM` int(10) unsigned NULL  , 
	`ERROR` tinyint(4) NOT NULL  DEFAULT 0 
) ENGINE=InnoDB DEFAULT CHARSET='utf8' COLLATE='utf8_bin';



CREATE TABLE `crm_guider_qrcode` (
  `GID_ID` varchar(64) COLLATE utf8_bin NOT NULL COMMENT '导购ID',
  `GZH_ID` varchar(32) COLLATE utf8_bin NOT NULL COMMENT '公众号ID',
  `QRCODE` varchar(256) COLLATE utf8_bin NOT NULL COMMENT '二维码',
  `VALID_TIME` datetime NOT NULL COMMENT '有效期',
  `REC_TIME` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '记录时间',
  UNIQUE KEY `GID_ID` (`GID_ID`,`GZH_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;



ALTER TABLE `crm_market` 
ADD COLUMN `REF_NO` varchar(64)  COLLATE utf16_general_ci NULL COMMENT '相关活动编号' after `P_LIMIT` , 
CHANGE `REC_USER` `REC_USER` bigint(20)   NULL COMMENT '记录人' after `REF_NO` , 
CHANGE `REC_TIME` `REC_TIME` timestamp   NULL COMMENT '记录时间' after `REC_USER` ;



ALTER TABLE `crm_customer` DEFAULT CHARSET='utf8mb4', COLLATE ='utf8mb4_bin' ;
/* Alter table in target */
ALTER TABLE `crm_customer_weixin` DEFAULT CHARSET='utf8mb4', COLLATE ='utf8mb4_bin' ;

replace into `crm_account_set` (`ACT_ID`, `ACT_CODE`, `ACT_NAME`, `PTF_ID`, `ENABLE`, `ACT_DESC`, `REC_TIME`, `REC_USER`, `ACT_TYPE`) 
values('1','C001','全局CRM账套','1','1',NULL,CURRENT_TIMESTAMP,NULL,'0');

replace into `crm_points_type` (`PNT_TYPE`, `PNT_NAME`, `PNT_DESC`, `IS_SYSTEM`, `IS_DEFAULT`, `REC_TIME`, `REC_USER`) 
values('LEAGUE','联盟积分','联盟积分','1','1',CURRENT_TIMESTAMP,NULL);

replace into `crm_points_set` (`PNT_ID`, `ACT_ID`, `PNT_TYPE`, `PNT_SET_NAME`, `PNT_UNIT`, `PNT_U_V`, `EXCHANGE`, `IS_FANS`, `ENABLE`, `REC_TIME`, `REC_USER`) values('1','1','LEAGUE','联盟积分账套','分','1.000000','1','0','1',CURRENT_TIMESTAMP,NULL);

replace into `crm_variable` (`VAR_SCOPE`, `VAR_CODE`, `VAR_NAME`, `VAR_DESC`, `VAR_SYSTEM`, `REC_TIME`, `REC_USER`) 
values('POINTS_CHANGE_TYPE','CRMJFIN','销售订单','销售订单','1',CURRENT_TIMESTAMP,NULL);

replace into `crm_variable` (`VAR_SCOPE`, `VAR_CODE`, `VAR_NAME`, `VAR_DESC`, `VAR_SYSTEM`, `REC_TIME`, `REC_USER`) 
values('POINTS_CHANGE_TYPE','CRMJFOT','销售订单','销售订单','1',CURRENT_TIMESTAMP,NULL);





REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('1000021', 'NAME', '入会时间', 'test_user2', '用户表2', '2', NULL, NULL, NULL, 'java.lang.String', NULL, NULL, NULL, '1', '105', '2016-05-10 14:33:34', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000001', 'ACCOUNT', '账户ID', 'test_user', '用户表', '1', '6', '10', NULL, 'java.lang.String', NULL, NULL, NULL, '1', '105', '2016-05-10 14:33:10', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000002', 'NAME', '账户名称', 'test_user', '用户表', '2', NULL, NULL, '', 'java.lang.String', '0', '^张d{3}.*', '100001', '1', '105', '2016-05-10 14:33:34', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000003', 'ENABLE', '是否可用', 'test_user', '用户表', '3', NULL, NULL, NULL, 'java.lang.Integer', NULL, NULL, '100002,100003', '1', '105', '2016-05-10 14:33:34', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000004', 'ACT_DESC', '描述', 'test_user', '用户表', '4', NULL, '32', NULL, 'java.lang.String', NULL, NULL, NULL, '1', '105', '2016-05-10 14:33:34', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000006', 'BIRTHDAY', '出生日期', 'test_user', '用户表', '5', NULL, NULL, NULL, 'java.util.Date', NULL, NULL, NULL, '1', '105', '2016-05-10 14:33:34', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000008', 'SOURCE', '客户来源', 'test_user', '用户表', '7', NULL, NULL, '1', 'java.util.List', NULL, NULL, NULL, '1', '105', '2016-05-10 14:33:34', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000009', 'CST_ID', '客户ID', 'crm_customer_tags', '打标签', '1', NULL, NULL, NULL, 'java.lang.Integer', NULL, NULL, NULL, '1', '105', '2016-07-13 19:57:49', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000019', 'TAG_KEY', '标签名', 'crm_customer_tags', '打标签', '2', NULL, NULL, NULL, 'java.lang.String', NULL, NULL, NULL, '1', '105', '2016-07-13 19:57:49', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000020', 'TAG_VAL', '标签值', 'crm_customer_tags', '打标签', '3', NULL, NULL, NULL, 'java.lang.String', NULL, NULL, NULL, '1', '105', '2016-07-13 19:57:49', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000021', 'SHP_ID', '雇员ID', 'crm_coupons_grant_shopper', '雇员发放', '1', NULL, NULL, NULL, 'java.lang.Long', NULL, '^[0-9]*$', NULL, '1', NULL, '2016-07-18 19:03:24', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000022', 'PER_NUM', '每人数量', 'crm_coupons_grant_shopper', '雇员发放', '3', NULL, NULL, NULL, 'java.lang.Integer', NULL, '^[0-9]*$', NULL, '1', NULL, '2016-07-18 19:10:56', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000023', 'DOC_CODE', '单据编号', 'crm_coupons_grant_shopper', '雇员发放', '2', NULL, NULL, NULL, 'java.lang.String', NULL, NULL, NULL, '1', NULL, '2016-07-18 19:09:18', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000024', 'CST_ID', '顾客ID', 'crm_point_adjust_detail', '积点调整单详情', '1', NULL, NULL, NULL, 'java.lang.Long', NULL, '^[0-9]*$', NULL, '1', NULL, '2016-07-20 16:13:07', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000025', 'POINTS', '调整积点值', 'crm_point_adjust_detail', '积点调整单详情', '2', NULL, NULL, NULL, 'java.lang.Double', NULL, '^-?[1-9][0-9]*$', NULL, '1', NULL, '2016-07-20 16:17:33', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000026', 'DAYS', '有效天数', 'crm_point_adjust_detail', '积点调整单详情', '3', NULL, NULL, NULL, 'java.lang.Integer', NULL, '^[0-9]*$', NULL, '1', NULL, '2016-07-20 16:19:01', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000027', 'ORDER_ID', '关联单据编号', 'crm_point_adjust_detail', '积点调整单详情', '4', NULL, NULL, NULL, 'java.lang.String', NULL, NULL, NULL, '1', NULL, '2016-07-20 16:19:50', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000028', 'REMARK', '备注', 'crm_point_adjust_detail', '积点调整单详情', '5', NULL, NULL, NULL, 'java.lang.String', NULL, NULL, NULL, '1', NULL, '2016-07-20 16:20:05', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000029', 'ADJ_ID', '单据编号', 'crm_point_adjust_detail', '积点调整单详情', '6', NULL, NULL, NULL, 'java.lang.String', NULL, NULL, NULL, '1', NULL, '2016-07-20 16:20:17', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000030', 'CODE', '编号', 'mtSite', '网点表', '1', NULL, NULL, NULL, 'java.lang.String', '0', NULL, NULL, '1', NULL, '2016-07-20 18:42:26', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000031', 'NAME', '名称', 'mtSite', '网点表', '2', NULL, NULL, NULL, 'java.lang.String', '0', NULL, NULL, '1', NULL, '2016-07-20 18:43:24', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000032', 'SITYPE', '组织类型', 'mtSite', '网点表', '3', NULL, NULL, NULL, 'java.lang.String', '0', NULL, NULL, '1', NULL, '2016-07-20 18:48:54', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000033', 'SISALETYPE', '分销类型', 'mtSite', '网点表', '4', NULL, NULL, NULL, 'java.lang.String', '0', NULL, NULL, '1', NULL, '2016-07-20 18:54:25', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000034', 'SIOPENDATE', '实际开张时间', 'mtSite', '网点表', '5', NULL, NULL, NULL, 'java.util.Date', '0', NULL, NULL, '1', NULL, '2016-07-20 18:55:26', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000035', 'BENAME', '所属经营主体', 'mtSite', '网点表', '6', NULL, NULL, NULL, 'java.lang.String', '0', NULL, NULL, '1', NULL, '2016-07-20 18:56:28', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('10000036', 'BEBNAME', '结算主体', 'mtSite', '网点表', '7', NULL, NULL, NULL, 'java.lang.String', '0', NULL, NULL, '1', NULL, '2016-07-20 18:57:24', '0', '0');
REPLACE INTO `excel_column` (`ROW_ID`, `CODE`, `NAME`, `SHEET_CODE`, `SHEET_NAME`, `SORT_CODE`, `MIN_LENGTH`, `MAX_LENGTH`, `CODE_LIST`, `DATA_TYPE`, `NULLABLE`, `PATTERN`, `RULES`, `STATUS`, `CREATED_USER`, `CREATED_TIME`, `MODIFICATION_NUM`, `DELETED_FLAG`) VALUES ('110000005111111111', 'JOIN_TIME', '入会时间', 'test_user', '用户表', '6', NULL, NULL, NULL, 'java.util.Date', NULL, NULL, NULL, '1', '105', '2016-05-10 14:33:34', '0', '0');







DELIMITER $$
DROP VIEW /*!50032 IF EXISTS */ `v_cust_buyed`$$
CREATE VIEW v_cust_buyed (buyed_id, cust_id, GUIDER_ID, site_id, site_name, emp_id, emp_name, order_id, goods_detail_id, buyed_goods_name, buyed_goods_image, buyed_goods_code, buyed_goods_price, buyed_goods_norms, buyed_time, buyed_number, created_at, updated_at) AS select `t2`.`ITM_ID` AS `buyed_id`,`t1`.`CST_ID` AS `cust_id`,`t3`.`GUIDER_ID` AS `GUIDER_ID`,`t5`.`siCode` AS `site_id`,`t5`.`siName` AS `site_name`,`t4`.`emCode` AS `emp_id`,`t4`.`emName` AS `emp_name`,`t1`.`ORDER_ID` AS `order_id`,`t2`.`SKU` AS `goods_detail_id`,`t2`.`ART_NAME` AS `buyed_goods_name`,NULL AS `buyed_goods_image`,`t2`.`ART_NUM` AS `buyed_goods_code`,`t2`.`END_PRICE` AS `buyed_goods_price`,`t2`.`SPEC` AS `buyed_goods_norms`,`t1`.`PAY_TIME` AS `buyed_time`,`t2`.`QUANTITY` AS `buyed_number`,NULL AS `created_at`,`t1`.`REC_TIME` AS `updated_at` from ((((`crm_sales_order` `t1` left join `crm_order_item` `t2` on((`t1`.`SLD_ID` = `t2`.`SLD_ID`))) left join `crm_guider` `t3` on((`t2`.`GUIDER` = `t3`.`GUIDER_ID`))) left join `mtemployee` `t4` on((`t3`.`EMP_ID` = `t4`.`emId`))) left join `mtsite` `t5` on((`t3`.`SITE_ID` = `t5`.`siId`)))$$
DELIMITER ;

/*  Create View in target  */

DELIMITER $$
DROP VIEW /*!50032 IF EXISTS */ `v_cust_emp_mapping`$$
CREATE VIEW v_cust_emp_mapping (cust_id, guider_id, site_id) AS select `t1`.`CST_ID` AS `cust_id`,`t1`.`GUIDER_ID` AS `guider_id`,`t3`.`siCode` AS `site_id` from ((`crm_customer_guider` `t1` left join `crm_guider` `t2` on((`t1`.`GUIDER_ID` = `t2`.`GUIDER_ID`))) left join `mtsite` `t3` on((`t2`.`SITE_ID` = `t3`.`siId`)))$$
DELIMITER ;

/*  Create View in target  */

DELIMITER $$
DROP VIEW /*!50032 IF EXISTS */ `v_cust_info`$$
CREATE VIEW v_cust_info (cust_id, cust_name, last_name, first_name, cust_type, cust_header, cust_viptime, cust_sex, cust_phone, cust_birthday, cust_isreceive, created_at, updated_at) AS select `t1`.`CST_ID` AS `cust_id`,`t1`.`CST_NAME` AS `cust_name`,`t1`.`LAST_NAME` AS `last_name`,`t1`.`FIRST_NAME` AS `first_name`,`t1`.`CST_TYPE` AS `cust_type`,`w`.`HEAD_IMG` AS `cust_header`,`t1`.`REG_TIME` AS `cust_viptime`,`t1`.`SEX` AS `cust_sex`,`t1`.`PHONE` AS `cust_phone`,`t1`.`BIRTHDAY` AS `cust_birthday`,1 AS `cust_isreceive`,'' AS `created_at`,`t1`.`REC_TIME` AS `updated_at` from (`crm_customer` `t1` left join `crm_customer_weixin` `w` on((`t1`.`CST_ID` = `w`.`CST_ID`))) group by `t1`.`CST_ID`$$
DELIMITER ;

/*  Create View in target  */

DELIMITER $$
DROP VIEW /*!50032 IF EXISTS */ `v_cust_point`$$
CREATE VIEW v_cust_point (point_id, cust_id, mode_id, point_type, point_value, created_at, updated_at) AS select `t1`.`PNT_ID` AS `point_id`,`t1`.`CST_ID` AS `cust_id`,`t1`.`ACT_ID` AS `mode_id`,`t1`.`PNT_TYPE` AS `point_type`,`t1`.`PNT_VAL` AS `point_value`,`t1`.`S_TIME` AS `created_at`,`t1`.`REC_TIME` AS `updated_at` from `crm_points_account` `t1`$$
DELIMITER ;

/*  Create View in target  */

DELIMITER $$
DROP VIEW /*!50032 IF EXISTS */ `v_emp`$$
CREATE VIEW v_emp (emp_id, emp_name, emp_avatar, emp_sex, emp_level, emp_job_no, emp_position, emp_company, emp_department, emp_mobile, emp_mail, emp_available_state, emp_expire_date, created_at, updated_at) AS select `a`.`emCode` AS `emp_id`,`a`.`emName` AS `emp_name`,'' AS `emp_avatar`,`a`.`emSex` AS `emp_sex`,'' AS `emp_level`,`a`.`emCode` AS `emp_job_no`,'' AS `emp_position`,`a`.`coId` AS `emp_company`,`b`.`deId` AS `emp_department`,`a`.`ptPhone` AS `emp_mobile`,`a`.`ptEmail` AS `emp_mail`,(case when (`a`.`emLeaveDate` is not null) then 0 else 1 end) AS `emp_available_state`,'' AS `emp_expire_date`,'' AS `created_at`,`a`.`ptUpTime` AS `updated_at` from (`mtemployee` `a` left join `mtemployeedept` `b` on((`a`.`emId` = `b`.`emId`)))$$
DELIMITER ;

/*  Create View in target  */

DELIMITER $$
DROP VIEW /*!50032 IF EXISTS */ `v_emp_site_mapping`$$
CREATE VIEW v_emp_site_mapping (emp_id, site_id, guider_id, site_role, site_role_description, site_emp_state, created_at, updated_at) AS select `b`.`emCode` AS `emp_id`,`c`.`siCode` AS `site_id`,`a`.`GUIDER_ID` AS `guider_id`,`a`.`role` AS `site_role`,`a`.`role_description` AS `site_role_description`,`a`.`state` AS `site_emp_state`,'' AS `created_at`,`a`.`REC_TIME` AS `updated_at` from ((`crm_guider` `a` left join `mtemployee` `b` on((`a`.`EMP_ID` = `b`.`emId`))) left join `mtsite` `c` on((`a`.`SITE_ID` = `c`.`siId`)))$$
DELIMITER ;

/*  Create View in target  */

DELIMITER $$
DROP VIEW /*!50032 IF EXISTS */ `v_goods_class`$$
CREATE VIEW v_goods_class (class_id, class_name, class_parent_id, level_num, created_at, updated_at) AS select `mt_product_class`.`CLZ_ID` AS `class_id`,`mt_product_class`.`CLZ_NAME` AS `class_name`,`mt_product_class`.`CLZ_PID` AS `class_parent_id`,`mt_product_class`.`CLZ_LEVEL` AS `level_num`,'' AS `created_at`,`mt_product_class`.`REC_TIME` AS `updated_at` from `mt_product_class`$$
DELIMITER ;

/*  Create View in target  */

DELIMITER $$
DROP VIEW /*!50032 IF EXISTS */ `v_goods_spec`$$
CREATE VIEW v_goods_spec (spec_id, p_spec, spec_field, spec_name, created_at, updated_at) AS select `a`.`SPEC_ID` AS `spec_id`,`a`.`P_SPEC` AS `p_spec`,`a`.`SPEC_FIELD` AS `spec_field`,`a`.`SPEC_NAME` AS `spec_name`,'' AS `created_at`,`a`.`REC_TIME` AS `updated_at` from `mt_product_spec` `a`$$
DELIMITER ;

/*  Create View in target  */

DELIMITER $$
DROP VIEW /*!50032 IF EXISTS */ `v_goods_spec_value`$$
CREATE VIEW v_goods_spec_value (spec_val, p_spec, value_content, created_at, updated_at) AS select `a`.`SPEC_VAL` AS `spec_val`,`a`.`P_SPEC` AS `p_spec`,`a`.`VAL_NAME` AS `value_content`,'' AS `created_at`,`a`.`REC_TIME` AS `updated_at` from `mt_product_spec_val` `a`$$
DELIMITER ;

/*  Create View in target  */

DELIMITER $$
DROP VIEW /*!50032 IF EXISTS */ `v_order`$$
CREATE VIEW v_order (order_id, cust_id, guider_id, site_id, order_current_price, order_end_price, order_goods_amount, order_class, order_state, order_time, created_at, updated_at) AS select `a`.`ORDER_ID` AS `order_id`,`a`.`CST_ID` AS `cust_id`,`b`.`GUIDER` AS `guider_id`,`c`.`siCode` AS `site_id`,`b`.`PRICE` AS `order_current_price`,`b`.`END_PRICE` AS `order_end_price`,`b`.`QUANTITY` AS `order_goods_amount`,`a`.`SLD_TYPE` AS `order_class`,'' AS `order_state`,`a`.`SLD_TIME` AS `order_time`,'' AS `created_at`,`a`.`REC_TIME` AS `updated_at` from ((`crm_sales_order` `a` left join `crm_order_item` `b` on((`b`.`SLD_ID` = `a`.`SLD_ID`))) left join `mtsite` `c` on((`a`.`SIT_ID` = `c`.`siId`)))$$
DELIMITER ;

/*  Create View in target  */

DELIMITER $$
DROP VIEW /*!50032 IF EXISTS */ `v_order_goods_mapping`$$
CREATE VIEW v_order_goods_mapping (order_id, sku_id, goods_number, goods_list_price, goods_current_price, created_at, updated_at) AS select `a`.`SLD_ID` AS `order_id`,`a`.`SKU` AS `sku_id`,`a`.`QUANTITY` AS `goods_number`,`b`.`S_RETAIL` AS `goods_list_price`,`a`.`PRICE` AS `goods_current_price`,'' AS `created_at`,`b`.`REC_TIME` AS `updated_at` from (`crm_order_item` `a` left join `mt_product_sku` `b` on((`a`.`SKU` = `b`.`SKU`)))$$
DELIMITER ;

/*  Create View in target  */

DELIMITER $$
DROP VIEW /*!50032 IF EXISTS */ `v_qrcode`$$
CREATE VIEW v_qrcode (GZH_ID, SIT_CODE, QRCODE, STAT) AS select `crm_wxgzh_site`.`GZH_ID` AS `GZH_ID`,`crm_wxgzh_site`.`SIT_CODE` AS `SIT_CODE`,`crm_wxgzh_site`.`QRCODE` AS `QRCODE`,`crm_wxgzh_site`.`STAT` AS `STAT` from `crm_wxgzh_site`$$
DELIMITER ;

/*  Create View in target  */

DELIMITER $$
DROP VIEW /*!50032 IF EXISTS */ `v_site`$$
CREATE VIEW v_site (site_id, site_name, site_image, site_address, site_longi, site_lati, site_hash, created_at, updated_at) AS select `mtsite`.`siCode` AS `site_id`,`mtsite`.`siName` AS `site_name`,'' AS `site_image`,`mtsite`.`ptAddr` AS `site_address`,`mtsite`.`siNavLng` AS `site_longi`,`mtsite`.`siNavLat` AS `site_lati`,`mtsite`.`siNavHash` AS `site_hash`,`mtsite`.`siOpenDate` AS `created_at`,`mtsite`.`ptUpTime` AS `updated_at` from `mtsite`$$
DELIMITER ;

/*  Create View in target  */

DELIMITER $$
DROP VIEW /*!50032 IF EXISTS */ `v_site_mode_mapping`$$
CREATE VIEW v_site_mode_mapping (site_id, brand_id, brand_name, mode_id, mode_name, created_at, updated_at) AS select `d`.`siCode` AS `site_id`,`a`.`BND_ID` AS `brand_id`,`c`.`brName` AS `brand_name`,`a`.`ACT_ID` AS `mode_id`,`b`.`ACT_NAME` AS `mode_name`,'' AS `created_at`,`b`.`REC_TIME` AS `updated_at` from (((`crm_account_set_site` `a` left join `crm_account_set` `b` on((`a`.`ACT_ID` = `b`.`ACT_ID`))) left join `mtbrand` `c` on((`a`.`BND_ID` = `c`.`brId`))) left join `mtsite` `d` on((`a`.`SIT_ID` = `d`.`siId`)))$$
DELIMITER ;

/*  Create View in target  */

DELIMITER $$
DROP VIEW /*!50032 IF EXISTS */ `v_site_target`$$
CREATE VIEW v_site_target (target_id, target_siteid, target_mon, target_sale, target_recruit, created_at, updated_at) AS select `a`.`target_id` AS `target_id`,`b`.`siCode` AS `target_siteid`,`a`.`MONTH` AS `target_mon`,`a`.`target_sale` AS `target_sale`,`a`.`target_recruit` AS `target_recruit`,'' AS `created_at`,`a`.`REC_TIME` AS `updated_at` from (`crm_site_sale_target` `a` left join `mtsite` `b` on((`a`.`siId` = `b`.`siId`)))$$
DELIMITER ;



DELIMITER $$
DROP PROCEDURE /*!50032 IF EXISTS */ `crm_lbs`$$
CREATE PROCEDURE `crm_lbs`(IN _1 VARCHAR(12),IN _2 VARCHAR(12),IN _3 VARCHAR(12),IN _4 VARCHAR(12),IN _5 VARCHAR(12),IN _6 VARCHAR(12),IN _7 VARCHAR(12),IN _8 VARCHAR(12),IN _9 VARCHAR(12),IN _x DOUBLE,IN _y DOUBLE,IN gzh VARCHAR(32),IN size INT,IN dis INT)
BEGIN
    SELECT DISTINCT c.siCode,c.siName,c.ptAddr,c.ptTphone,distance(c.siNavLat,c.siNavLng,_x,_y) AS distance,c.siNavLat,c.siNavLng
		FROM crm_account_set_wxgzh a,crm_account_set_site b, mtSite c
		WHERE a.ACT_ID = b.ACT_ID 
		AND b.SIT_ID = c.siId 
		AND a.GZH_ID = gzh 
		AND c.siNavLat is not null
		AND c.siNavLng is not null
		HAVING distance < 500000
		ORDER BY distance ASC;
END$$
DELIMITER ;


DELIMITER $$
DROP PROCEDURE /*!50032 IF EXISTS */ `crm_pdu_excel_crm_coupons_grant_shopper`$$
CREATE PROCEDURE `crm_pdu_excel_crm_coupons_grant_shopper`(IN `tableName` varchar(32),IN `groupId` varchar(32),OUT `errorNum` int)
BEGIN
	declare import_total_num int default 0; -- 本次导入发放额度
	declare now_total_num int default 0; -- 当前本次发放额度
	declare now_now_num int default -1; -- 当前可发放额度
	SELECT SUM(IFNULL(t1.PER_NUM,t2.PER_NUM)) into import_total_num FROM excel_crm_coupons_grant_shopper t1, crm_coupons_grant t2 WHERE t1.DOC_CODE = t2.DOC_CODE AND t1.GROUP_ID = groupId;
	SELECT DISTINCT t2.total_num into now_total_num FROM excel_crm_coupons_grant_shopper t1, crm_coupons_grant t2 WHERE t1.DOC_CODE = t2.DOC_CODE AND t1.GROUP_ID = groupId;
	SELECT DISTINCT IF(t3.MAX_NUM != -1,t3.NOW_NUM,-1) into now_now_num FROM excel_crm_coupons_grant_shopper t1, crm_coupons_grant t2, crm_coupon_rule t3 WHERE t1.DOC_CODE = t2.DOC_CODE AND t2.CPL_ID = t3.CPL_ID AND t1.GROUP_ID = groupId;

	-- 可发放额度校验
	IF now_now_num != -1
	THEN
		IF (import_total_num + now_total_num - now_now_num)>0 -- 超出可发放额度
			THEN
			UPDATE excel_crm_coupons_grant_shopper SET error = 1 WHERE group_id=groupId;
			INSERT INTO excel_error_procedure(`rownum`,`message`,`group_id`)
			(SELECT t.rownum, '超出可发放额度', t.group_id FROM excel_crm_coupons_grant_shopper t WHERE t.group_id = groupId);
		END IF;
	END IF;

	-- 查询唯一键约束
	INSERT INTO excel_error_procedure(`rownum`,`message`,`group_id`)
	(SELECT t1.rownum, '重复', t1.group_id FROM excel_crm_coupons_grant_shopper t1, crm_coupons_grant t2, crm_coupons_grant_shopper t3 WHERE
	t1.DOC_CODE = t2.DOC_CODE AND t2.GRT_ID = t3.GRT_ID AND t1.SHP_ID = t3.SHP_ID AND t1.group_id = groupId);

	-- 执行完所有检验之后,在临时表标记错误行
	UPDATE excel_crm_coupons_grant_shopper t INNER JOIN excel_error_procedure m ON ( m.ROWNUM=t.ROWNUM and m.GROUP_ID=t.GROUP_ID) 
	SET t.error = 1
	WHERE t.group_id=groupId;

	-- 完成标记后,插入正式表
	INSERT INTO crm_coupons_grant_shopper ( GRT_ID,SHP_ID,THIS_NUM) 
	SELECT DISTINCT t2.GRT_ID, t1.SHP_ID, IFNULL(t1.PER_NUM, t2.PER_NUM) THIS_NUM FROM excel_crm_coupons_grant_shopper t1 LEFT JOIN crm_coupons_grant t2 ON t1.DOC_CODE = t2.DOC_CODE WHERE t1.group_id=groupId AND t1.error=0;

	-- 返回错误行条数
	select count(*) INTO errorNum FROM excel_crm_coupons_grant_shopper WHERE group_id=groupId AND error=1;
END$$
DELIMITER ;


/* Create Procedure in target  */

DELIMITER $$
DROP PROCEDURE /*!50032 IF EXISTS */ `crm_pdu_excel_crm_customer_tags`$$
CREATE PROCEDURE `crm_pdu_excel_crm_customer_tags`(IN tableName varchar(32), IN groupId varchar(32),OUT errorNum int)
    COMMENT 'excel导入功能,针对表crm_customer_tags'
BEGIN
	-- 查询唯一键约束
	INSERT INTO excel_error_procedure(`rownum`,`message`,`group_id`)
	(SELECT t.rownum, '重复', t.group_id FROM excel_crm_customer_tags t , crm_customer_tags m WHERE  
	m.CST_ID= t.CST_ID AND m.TAG_KEY= t.TAG_KEY AND m.TAG_VAL= t.TAG_VAL AND t.group_id = groupId);
	-- 其他校验在此处添加

	-- 执行完所有检验之后,在临时表标记错误行
	UPDATE excel_crm_customer_tags t INNER JOIN excel_error_procedure m ON ( m.ROWNUM=t.ROWNUM and m.GROUP_ID=t.GROUP_ID) 
	SET t.error = 1
	WHERE t.group_id=groupId;
	-- 完成标记后,插入正式表
	INSERT INTO crm_customer_tags ( CST_ID,TAG_KEY,TAG_VAL) 
	SELECT CST_ID,TAG_KEY,TAG_VAL FROM excel_crm_customer_tags WHERE group_id=groupId AND error=0;
	-- 返回错误行条数
	select count(*) INTO errorNum FROM  excel_crm_customer_tags WHERE group_id=groupId AND error=1;
END$$
DELIMITER ;


/* Create Procedure in target  */

DELIMITER $$
DROP PROCEDURE /*!50032 IF EXISTS */ `crm_pdu_excel_crm_point_adjust_detail`$$
CREATE PROCEDURE `crm_pdu_excel_crm_point_adjust_detail`(IN tableName varchar(32), IN groupId varchar(32),OUT errorNum int)
    COMMENT 'excel导入功能,针对表crm_customer_tags'
BEGIN
	-- 查询唯一键约束
	INSERT INTO excel_error_procedure(`rownum`,`message`,`group_id`)
	(SELECT t.rownum, '重复', t.group_id FROM excel_crm_point_adjust_detail t , crm_point_adjust_detail m WHERE  
	m.CST_ID= t.CST_ID AND m.ADJ_ID= t.ADJ_ID AND t.group_id = groupId);
	-- 其他校验在此处添加

	-- 执行完所有检验之后,在临时表标记错误行
	UPDATE excel_crm_point_adjust_detail t INNER JOIN excel_error_procedure m ON ( m.ROWNUM=t.ROWNUM and m.GROUP_ID=t.GROUP_ID) 
	SET t.error = 1
	WHERE t.group_id=groupId;
	-- 完成标记后,插入正式表
	INSERT INTO crm_point_adjust_detail ( ADJ_ID,CST_ID,POINTS,ORDER_ID,ORDER_SRC,S_TIME,E_TIME,REMARK) 
	SELECT ADJ_ID,CST_ID,POINTS,ORDER_ID,'网上订单',SYSDATE(),DATE_FORMAT(SYSDATE() + INTERVAL IFNULL(DAYS,0) DAY,'%Y-%c-%d 23:59:59'),REMARK FROM excel_crm_point_adjust_detail WHERE group_id=groupId AND error=0;
	-- 返回错误行条数
	select count(*) INTO errorNum FROM  excel_crm_point_adjust_detail WHERE group_id=groupId AND error=1;
END$$
DELIMITER ;


/* Create Procedure in target  */

DELIMITER $$
DROP PROCEDURE /*!50032 IF EXISTS */ `crm_pdu_excel_test_user`$$
CREATE PROCEDURE `crm_pdu_excel_test_user`(IN tableName varchar(32), IN groupId varchar(32),OUT errorNum int)
    COMMENT 'excel导入功能,针对表test_user'
BEGIN
	-- 查询唯一键约束
	INSERT INTO excel_error_procedure(`rownum`,`message`,`group_id`)
	(SELECT t.rownum, '重复', t.group_id FROM test_user_excel t , test_user m WHERE  m.account= t.account AND t.group_id = groupId);
	-- 其他校验在此处添加

	-- 执行完所有检验之后,在临时表标记错误行
	UPDATE test_user_excel t INNER JOIN excel_error_procedure m ON ( m.ROWNUM=t.ROWNUM) 
	SET t.error = 1
	WHERE t.group_id=groupId;
	-- 完成标记后,插入正式表
	INSERT INTO test_user
	SELECT account,name,enable,act_desc,birthday,join_time,source FROM test_user_excel WHERE group_id=groupId AND error=0;
	-- 返回错误行条数
	select count(*) INTO errorNum FROM  test_user_excel WHERE group_id=groupId AND error=1;
END$$
DELIMITER ;


DELIMITER $$
DROP PROCEDURE /*!50032 IF EXISTS */ `crm_pdu_calculate_site_sale`$$
CREATE PROCEDURE `crm_pdu_calculate_site_sale`(IN targetMonth VARCHAR(32))
BEGIN

    -- start transaction;
    delete from crm_site_sale where month =targetMonth;
    INSERT INTO crm_site_sale (SIT_ID, MONTH, QUANTITY, END_PRICE, FACT_PRICE, PAYABLE, PAID) VALUES (2, targetMonth, 1, 2.00, 3.00, 4.00, 5.00);

END$$
DELIMITER ;


DELIMITER $$
DROP PROCEDURE /*!50032 IF EXISTS */ `CouponReceiveSite`$$
CREATE PROCEDURE `CouponReceiveSite`(IN cplid VARCHAR(16),IN _x DOUBLE,IN _y DOUBLE,IN offset INT,IN size INT)
BEGIN
  SELECT a.siCode,a.siName,a.ptAddr,a.ptTphone,st_distance(point(a.siNavLat,a.siNavLng),point(_x,_y))*111195 AS dist,a.siNavLat,a.siNavLng
	FROM mtSite a,crm_coupon_site_receive b
	WHERE a.siId = b.sit_id 
	AND b.cpl_id = cplid 
	AND a.siNavLat is not null
	AND a.siNavLng is not null
	ORDER BY dist ASC
	LIMIT offset,size;
END$$
DELIMITER ;


DELIMITER $$
DROP FUNCTION /*!50032 IF EXISTS */ `distance`$$
CREATE FUNCTION `distance`(_x1 DOUBLE,_y1 DOUBLE,_x2 DOUBLE,_y2 DOUBLE) RETURNS int(11)
BEGIN
		DECLARE $d INT;
		SET _x1 = RADIANS(_x1);
		SET _y1 = RADIANS(_y1);
		SET _x2 = RADIANS(_x2);
		SET _y2 = RADIANS(_y2);
		SET $d = ACOS(COS(_x1)*COS(_x2)*COS(_y1-_y2)+SIN(_x1)*SIN(_x2))*6378137;
		SET $d = ROUND($d * 10000)/10000;
		RETURN ROUND($d,1);
END$$
DELIMITER ;

DELIMITER $$
DROP PROCEDURE /*!50032 IF EXISTS */ `crm_pdu_order_delete_repeat`$$
CREATE PROCEDURE `crm_pdu_order_delete_repeat`(IN odr VARCHAR(36),IN chl VARCHAR(36))
BEGIN
   
	SET @sid = NULL;
	SELECT SLD_ID INTO @sid FROM `crm_sales_order` WHERE `SLD_CHANNEL`= chl AND `ORDER_ID`= odr LIMIT 0,1;	
	IF @sid IS NOT NULL
	THEN
		-- 删除相关数据表
		DELETE FROM `crm_order_account` WHERE `SLD_ID`= @sid;
		DELETE FROM `crm_order_address` WHERE `SLD_ID`= @sid;
		DELETE FROM `crm_order_payment` WHERE `SLD_ID`= @sid;
		-- DELETE FROM `crm_order_item_promotion` WHERE `ITM_ID` IN (SELECT `ITM_ID` FROM `crm_order_item` WHERE `SLD_ID`= @sid);
		DELETE FROM `crm_order_item` WHERE `SLD_ID`= @sid;
		DELETE FROM `crm_sales_order` WHERE `SLD_ID` = @sid;
	END IF;
END$$
DELIMITER ;

DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `crm_tgra_wxgzh_add`$$

CREATE TRIGGER `crm_tgra_wxgzh_add` AFTER INSERT ON `crm_wxgzh` 
    FOR EACH ROW BEGIN
			DELETE FROM smmsgchannel WHERE GZH_ID = new.GZH_ID;
			INSERT INTO smmsgchannel(mcName,mcDesc,mcType,GZH_ID,plId,beId,bebLimit,plCreateId)
			VALUES (new.NICK_NAME,new.NICK_NAME,3,new.GZH_ID,1,1,FALSE,1);
    END$$
DELIMITER ;

DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `crm_tgra_wxgzh_delete`$$

CREATE TRIGGER `crm_tgra_wxgzh_delete` AFTER DELETE ON `crm_wxgzh` 
    FOR EACH ROW BEGIN
			DELETE FROM smmsgchannel WHERE GZH_ID = old.GZH_ID;
    END$$
DELIMITER ;

DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `crm_tgra_site_group_wxgzh`$$
CREATE TRIGGER `crm_tgra_site_group_wxgzh` AFTER INSERT ON `crm_site_group_wxgzh`
	FOR EACH ROW BEGIN
		if new.GZH_ID is not null and new.GZH_ID != '' then
			set @act = null;
			select ACT_ID into @act from crm_site_group where GRP_ID = new.GRP_ID;
			delete from crm_account_set_wxgzh where ACT_ID=@act ;
			insert ignore into crm_account_set_wxgzh(ACT_ID,GZH_ID) values (@act,new.GZH_ID);
		end if;
  END$$
DELIMITER ;


DELIMITER $$
DROP PROCEDURE /*!50032 IF EXISTS */ `rpt_proc_cust_tag_template`$$
CREATE PROCEDURE `rpt_proc_cust_tag_template`(in tag_id int)
BEGIN
#/********************标签时间范围存入临时表***************************************/;
DROP TEMPORARY TABLE IF EXISTS tmp_tag_time;
CREATE TEMPORARY TABLE tmp_tag_time
SELECT 
	T2.CST_ID
    ,TAG_KEY id
    ,CASE WHEN S_TIME_FLAG=0 THEN S_TIME 
		  ELSE CASE S_UNIT 
					WHEN 'DAY' THEN adddate(CASE WHEN S_TIME_TYPE='currentTime' THEN curdate()
												 WHEN  S_TIME_TYPE='REG_TIME' THEN T2.IN_TIME END , INTERVAL case when S_RANGE=0 then -S_DIF ELSE S_DIF end   DAY)
					WHEN 'WEEK' THEN adddate(CASE WHEN S_TIME_TYPE='currentTime' THEN curdate()
												 WHEN  S_TIME_TYPE='REG_TIME' THEN T2.IN_TIME END , INTERVAL case when S_RANGE=0 then -S_DIF ELSE S_DIF end   WEEK)
					WHEN 'MONTH' THEN adddate(CASE WHEN S_TIME_TYPE='currentTime' THEN curdate()
												 WHEN  S_TIME_TYPE='REG_TIME' THEN T2.IN_TIME END , INTERVAL case when S_RANGE=0 then -S_DIF ELSE S_DIF end   MONTH)
					WHEN 'YEAR' THEN adddate(CASE WHEN S_TIME_TYPE='currentTime' THEN curdate()
												 WHEN  S_TIME_TYPE='REG_TIME' THEN T2.IN_TIME END , INTERVAL case when S_RANGE=0 then -S_DIF ELSE S_DIF end   YEAR)
				END 
          END  S_TIME
	 ,CASE WHEN E_TIME_FLAG=0 THEN E_TIME 
		  ELSE CASE E_UNIT 
					WHEN 'DAY' THEN adddate(CASE WHEN E_TIME_TYPE='currentTime' THEN curdate()
												 WHEN  E_TIME_TYPE='REG_TIME' THEN T2.IN_TIME END , INTERVAL case when E_RANGE=0 then -E_DIF ELSE E_DIF end   DAY)
					WHEN 'WEEK' THEN adddate(CASE WHEN E_TIME_TYPE='currentTime' THEN curdate()
												 WHEN  E_TIME_TYPE='REG_TIME' THEN T2.IN_TIME END , INTERVAL case when E_RANGE=0 then -E_DIF ELSE E_DIF end   WEEK)
					WHEN 'MONTH' THEN adddate(CASE WHEN E_TIME_TYPE='currentTime' THEN curdate()
												 WHEN  E_TIME_TYPE='REG_TIME' THEN T2.IN_TIME END , INTERVAL case when E_RANGE=0 then -E_DIF ELSE E_DIF end   MONTH)
					WHEN 'YEAR' THEN adddate(CASE WHEN E_TIME_TYPE='currentTime' THEN curdate()
												 WHEN  E_TIME_TYPE='REG_TIME' THEN T2.IN_TIME END , INTERVAL case when E_RANGE=0 then -E_DIF ELSE E_DIF end   YEAR)
				END 
          END  E_TIME
		,CST_BEHAVIOR
        ,PRO_CATE
        ,TOP_N	
FROM  crm_tag_template_popular_argument t1
	 join rpt_dim_cst_info t2 on 1=1
where
	t1.tag_key=tag_id;
    
select @s_spec:=pro_cate from  crm_tag_template_popular_argument tt where tt.tag_key=tag_id;

delete from crm_customer_tags  where  TAG_KEY= tag_id  ;

if @s_spec like 'S_SPEC%' then 
#/********************计算规格**********************/;
set @sql1 ='insert into crm_customer_tags
(CST_ID,TAG_KEY,TAG_VAL,REC_TIME,OP_USER)  
SELECT 
	CST_ID 
    ,TT.ID
    ,pro_spec  
    ,current_timestamp()
    ,rank
FROM  
(
SELECT 
	CST_ID
    ,TT.ID
	,tt.pro_spec
	,QUANTITY
    ,TOP_N
    ,@rownum:=@rownum+1 AS rownum
    ,IF(@pa=tt.CST_ID AND @pa1=tt.ID,@rank:=@rank+1,@rank:=1) AS rank
    ,@pa:=tt.CST_ID  
    ,@pa1:=tt.ID
FROM  
   (SELECT 
		tt.CST_ID
        ,T2.ID
		,sum(t1.QUANTITY) as QUANTITY
        ,MAX(TOP_N) TOP_N
        ,';
set @sql2=CONCAT(' as pro_spec
	FROM 
		crm_sales_order tt 
		join crm_order_item t1 on tt.SLD_ID=t1.SLD_ID
        join tmp_tag_time t2 on tt.CST_ID=t2.CST_ID 
        join mt_product_sku t3 on t1.SKU=t3.SKU	
	where
		date_format(tt.SLD_TIME,''%Y%m%d'') between  date_format(t2.S_TIME,''%Y%m%d'') and date_format(t2.E_TIME,''%Y%m%d'')
        AND LOCATE(''BUY'',T2.CST_BEHAVIOR)>0
         and t2.id=', @id,'
	group by 
		tt.CST_ID
        ,T2.ID
		,');
set @sql3=' order by 
		CST_ID
        ,T2.ID
		,QUANTITY desc) tt
   , (SELECT @rank:=0,@rownum:=0,@pa=NULL,@pa1=NULL) t1
   ) tt
where
	rank<=TOP_N';
set @sqlall=CONCAT(@sql1,@s_spec,@sql2,@s_spec,@sql3) ;
prepare stmt1 from @sqlall;
execute stmt1;
deallocate prepare stmt1;

else 

select @PRO_CATE:=MAX(CLZ_FIELD) FROM mt_product_class TT JOIN crm_tag_template_popular_argument T1 ON TT.GRP_ID=T1.PRO_CATE  WHERE T1.tag_key=tag_id;
set @sql_pro_1 ='insert into crm_customer_tags
(CST_ID,TAG_KEY,TAG_VAL,REC_TIME,OP_USER)  
SELECT 
	CST_ID 
    ,TT.ID
    ,pro_spec  
    ,current_timestamp()
    ,rank
FROM  
(
SELECT 
	CST_ID
    ,TT.ID
	,tt.pro_spec
	,QUANTITY
    ,TOP_N
    ,@rownum:=@rownum+1 AS rownum
    ,IF(@pa=tt.CST_ID AND @pa1=tt.ID,@rank:=@rank+1,@rank:=1) AS rank
    ,@pa:=tt.CST_ID  
    ,@pa1:=tt.ID
FROM  
   (SELECT 
		tt.CST_ID
        ,T2.ID
		,sum(t1.QUANTITY) as QUANTITY
        ,MAX(TOP_N) TOP_N
        ,';
set @sql_pro_2=CONCAT(' as pro_spec
	FROM 
		crm_sales_order tt 
		join crm_order_item t1 on tt.SLD_ID=t1.SLD_ID
        join tmp_tag_time t2 on tt.CST_ID=t2.CST_ID 
        join mt_product t3 on t1.ART_NAME=t3.P_ID	
        join mt_product_class t4 on t4.p_clz=t3.',@PRO_CATE,'
	where
		date_format(tt.SLD_TIME,''%Y%m%d'') between  date_format(t2.S_TIME,''%Y%m%d'') and date_format(t2.E_TIME,''%Y%m%d'')
        AND LOCATE(''BUY'',T2.CST_BEHAVIOR)>0
        and t2.id=', @id,'
	group by 
		tt.CST_ID
        ,T2.ID
		,')
;
set @sql_pro_3=' order by 
		CST_ID
        ,T2.ID
		,QUANTITY desc) tt
   , (SELECT @rank:=0,@rownum:=0,@pa=NULL,@pa1=NULL) t1
   ) tt
where
	rank<=TOP_N'
    ;
set @sql_pro_all=CONCAT(@sql_pro_1,@PRO_CATE,@sql_pro_2,@PRO_CATE,@sql_pro_3) ;

prepare stmt2 from @sql_pro_all;

execute stmt2;
deallocate prepare stmt2;
end if;
END$$
DELIMITER ;


DELIMITER $$
DROP PROCEDURE /*!50032 IF EXISTS */ `rpt_proc_cust_tag_template_rfm`$$
CREATE PROCEDURE `rpt_proc_cust_tag_template_rfm`(in tag_id int)
BEGIN
#/********************标签时间范围存入临时表***************************************/;
/***DROP TEMPORARY TABLE IF EXISTS tmp_tag_time_rfm;
CREATE TEMPORARY TABLE tmp_tag_time_rfm
SELECT 
	T2.CST_ID
    ,TAG_KEY id
    ,CASE WHEN S_TIME_FLAG=0 THEN S_TIME 
		  ELSE CASE S_UNIT 
					WHEN 'DAY' THEN adddate(CASE WHEN S_TIME_TYPE='currentTime' THEN curdate()
												 WHEN  S_TIME_TYPE='REG_TIME' THEN T2.IN_TIME END , INTERVAL case when S_RANGE=0 then -S_DIF ELSE S_DIF end   DAY)
					WHEN 'WEEK' THEN adddate(CASE WHEN S_TIME_TYPE='currentTime' THEN curdate()
												 WHEN  S_TIME_TYPE='REG_TIME' THEN T2.IN_TIME END , INTERVAL case when S_RANGE=0 then -S_DIF ELSE S_DIF end   WEEK)
					WHEN 'MONTH' THEN adddate(CASE WHEN S_TIME_TYPE='currentTime' THEN curdate()
												 WHEN  S_TIME_TYPE='REG_TIME' THEN T2.IN_TIME END , INTERVAL case when S_RANGE=0 then -S_DIF ELSE S_DIF end   MONTH)
					WHEN 'YEAR' THEN adddate(CASE WHEN S_TIME_TYPE='currentTime' THEN curdate()
												 WHEN  S_TIME_TYPE='REG_TIME' THEN T2.IN_TIME END , INTERVAL case when S_RANGE=0 then -S_DIF ELSE S_DIF end   YEAR)
				END 
          END  S_TIME
	 ,CASE WHEN E_TIME_FLAG=0 THEN E_TIME 
		  ELSE CASE E_UNIT 
					WHEN 'DAY' THEN adddate(CASE WHEN E_TIME_TYPE='currentTime' THEN curdate()
												 WHEN  E_TIME_TYPE='REG_TIME' THEN T2.IN_TIME END , INTERVAL case when E_RANGE=0 then -E_DIF ELSE E_DIF end   DAY)
					WHEN 'WEEK' THEN adddate(CASE WHEN E_TIME_TYPE='currentTime' THEN curdate()
												 WHEN  E_TIME_TYPE='REG_TIME' THEN T2.IN_TIME END , INTERVAL case when E_RANGE=0 then -E_DIF ELSE E_DIF end   WEEK)
					WHEN 'MONTH' THEN adddate(CASE WHEN E_TIME_TYPE='currentTime' THEN curdate()
												 WHEN  E_TIME_TYPE='REG_TIME' THEN T2.IN_TIME END , INTERVAL case when E_RANGE=0 then -E_DIF ELSE E_DIF end   MONTH)
					WHEN 'YEAR' THEN adddate(CASE WHEN E_TIME_TYPE='currentTime' THEN curdate()
												 WHEN  E_TIME_TYPE='REG_TIME' THEN T2.IN_TIME END , INTERVAL case when E_RANGE=0 then -E_DIF ELSE E_DIF end   YEAR)
				END 
          END  E_TIME
		,MERGE
        ,SITE_GROUP
        ,PRO_GROUP
        ,RECENCY
        ,FREQUENCY
        ,MONETARY
FROM  crm_tag_template_rfm_argument t1
	 join rpt_dim_cst_info t2 on 1=1
where
	t1.tag_key=tag_id ;
create table  IF NOT EXISTS rpt_tamp_tag_rfm
SELECT
	t2.id as TAG_KEY
    ,tt.CST_ID
    ,datediff(curdate(),max(date_format(tt.SLD_TIME,'%Y%m%d'))) as R_DAY
     ,count(distinct case when t2.merge=0 then tt.order_id else date_format(tt.SLD_TIME,'%Y%m%d') end  ) as F_QTY
    -- ,max(date_format(tt.SLD_TIME,'%Y%m%d'))
    ,sum(t1.QUANTITY*t1.FACT_PRICE) as M_AMT
   -- ,sum(t1.QUANTITY) as sal_qty
   -- ,count(distinct tt.order_id) as nos_qty
   -- ,count(distinct date_format(tt.SLD_TIME,'%Y%m%d') ) as sa_nos_qty
FROM 
		crm_sales_order tt 
		join crm_order_item t1 on tt.SLD_ID=t1.SLD_ID
        join tmp_tag_time_rfm t2 on tt.CST_ID=t2.CST_ID 
        join mt_product_sku t3 on t1.SKU=t3.SKU	
where
		1=2
group by 
	tt.CST_ID
    ,t2.id;
    
DELETE FROM rpt_tamp_tag_rfm WHERE TAG_KEY=tag_id;
INSERT INTO rpt_tamp_tag_rfm(TAG_KEY,CST_ID,R_DAY,F_QTY,M_AMT)
SELECT
	t2.id as TAG_KEY
    ,tt.CST_ID
    ,datediff(curdate(),max(date_format(tt.SLD_TIME,'%Y%m%d'))) as R_DAY
     ,count(distinct case when t2.merge=0 then tt.order_id else date_format(tt.SLD_TIME,'%Y%m%d') end  ) as F_QTY
    -- ,max(date_format(tt.SLD_TIME,'%Y%m%d'))
    ,sum(t1.QUANTITY*t1.FACT_PRICE) as M_AMT
   -- ,sum(t1.QUANTITY) as sal_qty
   -- ,count(distinct tt.order_id) as nos_qty
   -- ,count(distinct date_format(tt.SLD_TIME,'%Y%m%d') ) as sa_nos_qty   
FROM 
		crm_sales_order tt 
		join crm_order_item t1 on tt.SLD_ID=t1.SLD_ID
        join tmp_tag_time_rfm t2 on tt.CST_ID=t2.CST_ID 
        join mt_product_sku t3 on t1.SKU=t3.SKU	
where
		date_format(tt.SLD_TIME,'%Y%m%d') between  date_format(t2.S_TIME,'%Y%m%d') and date_format(t2.E_TIME,'%Y%m%d')
         and t2.id=tag_id
         AND TT.SLD_TYPE='0'
group by 
	tt.CST_ID
    ,t2.id; **/
  /***************计算RFM********************************/  
select @tr_day:= sum(r_day), @tf_qty:= sum(f_qty),@tm_amt:= sum(m_amt) from rpt_tamp_tag_rfm WHERE TAG_KEY=tag_id ;
SELECT @ri:=recency, @fi:= frequency,@mi:= monetary FROM crm_tag_template_rfm_argument WHERE TAG_KEY=tag_id;

DROP TEMPORARY TABLE IF EXISTS tmp_rfm;
CREATE TEMPORARY TABLE tmp_rfm
select  
	'R' AS TYPE 
    ,ROUND(@tr_day*1.0/@ri,2) AS rfm  
    ,'R1' AS rfm_nm;  
    
SET  @a=2;
-- R等分标准

while @a<=@ri do
	insert into  tmp_rfm 
	select 'R',ROUND(@tr_day/@ri*@a,2) AS R  ,CONCAT('R',@a);
	set @a=@a+1;
end while;

-- f等分标准
set @a=1;
while @a<=@fi do
	insert into  tmp_rfm 
	select 'F',ROUND(@tf_qty/@fi*@a,2) AS R  ,CONCAT('F',@a);
set @a=@a+1;
end while;

-- m等分标准
set @a=1;
while @a<=@mi do
	insert into  tmp_rfm 
	select 'M',ROUND(@tm_amt/@mi*@a,2) AS R  ,CONCAT('M',@a) ;
set @a=@a+1;
end while;

insert into crm_customer_tags
(CST_ID,TAG_KEY,TAG_VAL,REC_TIME)
-- 计算R标签
select 
	TT.CST_ID
    ,TT.TAG_KEY
    ,MIN(t1.rfm_nm) as R		
    ,current_timestamp()
from 
	(select 
		tt.* 
		,IF(@tag=tt.TAG_KEY ,@r_day:=@r_day+tt.R_DAY,@r_day:=tt.R_DAY) AS ttlR_DAY
		,@tag:=tt.TAG_KEY	
	from  
		rpt_tamp_tag_rfm tt
	, (select @tag := null,@r_day := 0 ) a
	order by 
		tt.R_DAY ) tt
	join tmp_rfm t1 on T1.TYPE='R' AND  tt.ttlR_DAY<=t1.rfm
GROUP BY 
	TT.TAG_KEY
    ,TT.CST_ID
UNION ALL
-- 计算F标签
select 
	TT.CST_ID
    ,TT.TAG_KEY
    ,F_QTY
    ,MIN(t1.rfm_nm) as R		
    ,current_timestamp()
from 
	(select 
		tt.* 
		,IF(@tag=tt.TAG_KEY ,@r_day:=@r_day+tt.F_QTY,@r_day:=tt.F_QTY) AS ttlR_DAY
		,@tag:=tt.TAG_KEY	
	from  
		rpt_tamp_tag_rfm tt
	, (select @tag := null,@r_day := 0 ) a
	order by 
		tt.F_QTY DESC ) tt
	join tmp_rfm t1 on T1.TYPE='F' AND  tt.ttlR_DAY<=t1.rfm
GROUP BY 
	TT.TAG_KEY
    ,TT.CST_ID
ORDER BY 
	F_QTY  ;
END$$
DELIMITER ;