码迷,mamicode.com
首页 > 其他好文 > 详细

项目存储过程记录

时间:2017-08-14 19:01:10      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:end   rri   primary   标识   char   eal   long   temporary   rate   

CREATE DEFINER=`sas_settle`@`%` PROCEDURE `P_CREATE_ACCOUNT_CARRIER_TASK`()
BEGIN
    DECLARE done INT DEFAULT 0;
    DECLARE v_flow_id VARCHAR(10);                        -- 流水号
    DECLARE v_prefix VARCHAR(10) DEFAULT JS; -- 业务前缀
    
    #账期任务字段
    DECLARE v_belong VARCHAR(20);            -- 帐号
    DECLARE v_parent_id VARCHAR(20);    -- 父帐号
    DECLARE v_member_name VARCHAR(50); -- 结算商家
    DECLARE v_title_type INT;    -- 结算类型
    DECLARE v_title VARCHAR(2);            -- 结算日
    DECLARE v_year VARCHAR(4);                -- 年度
    DECLARE v_month VARCHAR(2);    -- 月度
    DECLARE v_set_no_type VARCHAR(2);      -- 账户类型(0:铁运 1:非铁运)
    DECLARE v_vip_type VARCHAR(2);        -- 用户类型    1:个人;2:企业
    DECLARE v_start_date DATE DEFAULT NOW(); -- 结算起始日期
    DECLARE v_end_date DATE DEFAULT NOW(); -- 结算结束日期
    DECLARE v_last_period_end DATE ; -- 上期结算结束日期
    DECLARE v_flag INT DEFAULT 0; -- 是否生成任务
    DECLARE v_last_title_type INT;-- 上期任务的结算类型
    DECLARE v_count INT DEFAULT 0; -- 同一个用户未执行任务存在的数目

    DECLARE y_year VARCHAR(4) DEFAULT YEAR(NOW()); -- 年份
    DECLARE i INT DEFAULT 0; -- 遍历标识
    #声明游标:所有年份
        #DECLARE y_cursor CURSOR FOR 
            #SELECT y.`YEAR` FROM sas_year y;
    
    #声明游标:清算日为当天且未生成任务的用户
    DECLARE v_cursor CURSOR FOR
        SELECT
            ME.BELONG,
            me.MEMBER_NAME,
            me.PARENT_ID,
            mri.TITLE,
            mri.TITLE_TYPE,
            mri.VIP_TYPE,
            mri.ACCOUNT_TYPE,
            YEAR (NOW()) YY,
            (SELECT tt.PERIOD_END FROM sas_task_carrier_pay_order_period tt 
                WHERE tt.BELONG = me.BELONG 
                ORDER BY tt.CREATE_TIME DESC LIMIT 0,1) last_period_end,
            (SELECT tt.SET_TYPE FROM sas_task_carrier_pay_order_period tt 
                WHERE tt.BELONG = me.BELONG 
                ORDER BY tt.CREATE_TIME DESC LIMIT 0,1) last_title_type
        FROM sas_member ME
        LEFT JOIN sas_member_rule_info mri ON (
            me.PARENT_ID = mri.USER_ID
            OR me.BELONG = mri.USER_ID
        ) AND mri.ENABLE_STATUS = 1
        WHERE me.ENABLE_STATUS = 1 
         AND NOT EXISTS (
            SELECT 1
            FROM sas_task_account_carrier_period t
            WHERE t.BELONG = me.BELONG
            #结算日为当天(月结周结)
            AND CASE WHEN mri.TITLE_TYPE = 10 THEN t.SET_DATE = WEEKDAY(now()) + 1 and t.SET_TYPE=10 and DATE_SUB(str_to_date(NOW(),%Y-%m-%d),INTERVAL 1 day) =t.PERIOD_END
            WHEN mri.TITLE_TYPE = 20 THEN t.SET_DATE = DAYOFMONTH(now()) and t.SET_TYPE=20  and DATE_SUB(str_to_date(NOW(),%Y-%m-%d),INTERVAL 1 day) =t.PERIOD_END
            #一单一结
            WHEN mri.TITLE_TYPE = 1 THEN t.TASK_STATUS = 0 and t.SET_TYPE=1
            END
        ) AND EXISTS(
            SELECT 1
            FROM sas_order_reports r
            #清算报表存在没出账单、未生成账单,没生成过账单,并且是非铁路的账单
            WHERE r.belong=me.belong and r.order_set_status =10 and r.is_create_account=10 and r.have_create_account=10 and r.IS_RAILWAY!=01
        )    AND CASE WHEN mri.TITLE_TYPE = 10 OR mri.TITLE_TYPE = 20 THEN
            EXISTS(
            SELECT 1
            FROM sas_order_reports r
            #清算报表存在没出账单、未生成账单,没生成过账单,并且是非铁路的账单
            WHERE r.belong=me.belong and r.order_set_status =10 and r.is_create_account=10 and r.have_create_account=10 and r.IS_RAILWAY!=01 and r.create_time<=DATE_SUB(str_to_date(NOW(),%Y-%m-%d),INTERVAL 1 day)
            ) END;
        
        #如果是一单一结 只生成有账单,且已审核的,且未生成过付款单的
        /*AND CASE WHEN mri.TITLE_TYPE=‘1‘ THEN EXISTS (SELECT id from sas_set_account_carrier_2016 s
            where s.BELONG =me.BELONG AND s.set_status=‘20‘ AND s.IS_PAYMENT!=‘1‘) END;*/
            
    
    
    /* 声明Continue Handler: 当游标到达尾部时,mysql自动设置done=1 */     
      DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; 
    OPEN v_cursor;
    curLoop:LOOP
        FETCH v_cursor INTO v_belong, v_member_name, v_parent_id, v_title,v_title_type,
        v_vip_type,v_set_no_type,v_year,v_last_period_end,v_last_title_type;
        
        IF done=1 THEN
            LEAVE curLoop;
        END IF;
        SET v_flag = 0;
        #月结
        IF v_title_type IS NOT NULL AND v_title_type = 20 THEN
            
            #账期为当天
            IF v_title IS NOT NULL AND DAY(NOW()) = v_title THEN
                SET v_flag = 1;
                SET v_start_date = DATE_SUB(NOW(), INTERVAL 1 MONTH);
                SET v_end_date = DATE_SUB(NOW(), INTERVAL 1 DAY);
            
            #当帐期为31日时,当月只有30天情况,则本次帐期默认为30日
            ELSEIF v_title IS NOT NULL AND DAY(NOW()) = DAY(LAST_DAY(now())) AND v_title > DAY(LAST_DAY(now())) THEN
                SET v_flag = 1;
                SET v_title = DAY(LAST_DAY(now()));
                SET v_start_date = DATE_SUB(NOW(), INTERVAL 1 MONTH);
                SET v_end_date = DATE_SUB(NOW(), INTERVAL 1 DAY);
            END IF;

        #周结
        ELSEIF v_title_type IS NOT NULL AND v_title_type = 10 THEN
            #结算日为当天
            IF v_title IS NOT NULL AND v_title = WEEKDAY(NOW()) + 1 THEN
                SET v_flag = 1;
                SET v_end_date = DATE_SUB(NOW(), INTERVAL 1 DAY);
                SET v_start_date = DATE_SUB(NOW(), INTERVAL 1 WEEK);
            END IF;
        
        #一单一结
        ELSEIF v_title_type IS NOT NULL AND v_title_type= 1 THEN 
            #账期为清算报表的创建时间(后修改为账期为现在的时间)
            SELECT min(NOW()) INTO v_end_date FROM sas_order_reports r where r.belong=v_belong and r.is_create_account=10 and r.have_create_account=10;
            SET v_start_date=v_end_date;
            SET v_flag=1;
        END IF;


        #如果上期结算结束日期有值,且不是一单一结,则本期账单开始日期 = 上期结束日期 + 1天
        IF v_last_period_end IS NOT NULL AND v_last_period_end <> ‘‘ AND v_last_title_type !=1 THEN
            SET v_start_date = DATE_ADD(v_last_period_end, INTERVAL 1 DAY);
        ELSEIF  v_last_period_end IS NOT NULL AND v_last_period_end <> ‘‘ AND v_last_title_type =1 THEN
            SET v_start_date =v_last_period_end;
        END IF;
        
        #如果开始时间大于结束时间,设置开始时间等于结束时间
        IF v_start_date>v_end_date THEN
            SET v_start_date=v_end_date;
        END IF;
        
        #排除帐期为空的数据
        IF v_flag = 1 THEN
        
            #获取流水号
            CALL P_GET_SEQNO(v_prefix,4,0,2,v_flow_id);
            SET v_month = MONTH(v_start_date);
            
            #流水号转换为纯数字
            SET v_flow_id=v_flow_id+0;

            #查询任务重复的数量
            SELECT COUNT(1) into v_count FROM sas_task_account_carrier_period
            WHERE BELONG = v_belong and TASK_STATUS=0 and DATE_FORMAT(CREATE_TIME,%Y-%m-%d)=DATE_FORMAT(SYSDATE(),%Y-%m-%d);
            
            #不存在重复才生成
            IF v_count=0 THEN
            #生成任务
            INSERT INTO sas_task_account_carrier_period
            ( `YEAR`,
                `MONTH`,
                PERIOD_START,
                PERIOD_END,
                BELONG,
                PARENT_ID,
                MEMBER_NAME,
                SET_TYPE,
                SET_DATE,
                SET_NO_TYPE,
                TASK_STATUS,
                VIP_TYPE,
                CREATE_BY,
                CREATE_TIME,
                LAST_UPDATE_BY,
                LAST_UPDATE_TIME,
                ENABLE_STATUS,
                FLOW_ID
            ) VALUES (    
                v_year,
                v_month,
                v_start_date,
                v_end_date,
                v_belong,
                v_parent_id,
                v_member_name,
                v_title_type,
                v_title,
                v_set_no_type,
                0,
                v_vip_type,
                system,SYSDATE(),system,SYSDATE(),1,v_flow_id
            );
            END IF;
        END IF;
    END LOOP curLoop;

    COMMIT;
END
CREATE DEFINER=`sas_settle`@`%` PROCEDURE `P_CREATE_ACCOUNT_CARRIER`(IN p_task_id INT, -- 任务primary key
    IN p_user_id VARCHAR(20), -- 用户id
    IN p_belong VARCHAR(20), -- 母账号
    IN p_title_type VARCHAR(2), -- 结算类型:‘10‘周结,‘20‘月结,‘1‘现结
    IN p_title VARCHAR(2), -- 结算日
    IN p_set_start DATETIME, -- 账单开始日
    IN p_set_end DATETIME, -- 账单结束日
    IN p_year VARCHAR(4), -- 账单年份
    IN p_month VARCHAR(2), -- 账单月份
    IN p_vip_type VARCHAR(2), -- 用户类型
    IN p_flow_no VARCHAR(10))
BEGIN
    -- 辅助字段
    DECLARE v_table_c varchar(50) DEFAULT sas_not_railway_account_carrier_month_pay; -- 非铁路承运商帐单表
    DECLARE v_table_c_com VARCHAR(50) DEFAULT sas_not_railway_account_carrier_month_pay_detail; -- 非铁路承运商帐单明细表
    DECLARE v_prefix VARCHAR(10) DEFAULT JS;-- 业务前缀
    DECLARE v_tmp_count int default 0; -- 临时表数据量
    DECLARE v_min_id INT DEFAULT 1;
    DECLARE v_max_id INT DEFAULT 1;
    DECLARE v_report_id  INT UNSIGNED; -- 清算报表id
    DECLARE v_exception VARCHAR(10) DEFAULT ‘‘; -- 异常处理
    
-- 表头数据

    DECLARE v_sys_code VARCHAR(10) DEFAULT 10;-- 系统来源‘,
    DECLARE v_member_name VARCHAR(50); -- 清算对象
    DECLARE v_pay_way VARCHAR(2) DEFAULT 1; -- 账单类型
    DECLARE v_belong VARCHAR(20); -- 用户ID
    DECLARE v_set_date datetime DEFAULT NOW(); -- 账单时间
    DECLARE v_audit_time datetime; -- 审核时间
    DECLARE v_set_no  varchar(30); -- 清算账单号
    -- DECLARE v_p_order_no VARCHAR(30); -- 母订单号
    -- DECLARE v_order_no VARCHAR(30); -- 子订单号
    -- DECLARE v_trans_no VARCHAR(64); -- 运单号
    DECLARE v_pay_money NUMERIC(20,2);-- 账单金额
    DECLARE v_set_status VARCHAR(2) DEFAULT 10; -- 账单状态
    DECLARE v_pay_status VARCHAR(2) DEFAULT 10; -- 支付状态
    DECLARE v_is_print_pay VARCHAR(2) DEFAULT 10; -- 付款单
    DECLARE v_carrier_org_level_code VARCHAR(20) DEFAULT 0001; -- 承运商组织层级编码

-- 明细数据
  -- DECLARE v_set_no VARCHAR(30); -- 清算账单号,
  DECLARE v_order_no VARCHAR(30); -- 子订单号,
  DECLARE v_trans_no VARCHAR(30); -- 运单号,
  DECLARE v_p_order_no VARCHAR(30); -- 母订单号,
  DECLARE v_year VARCHAR(4) DEFAULT year(NOW()); -- 会计年度,
  DECLARE v_month VARCHAR(2) DEFAULT month(NOW()); -- 会计月度,
  DECLARE v_order_time datetime; -- 订单生成时间,
  DECLARE v_order_money NUMERIC(20,2); -- 订单金额,
  DECLARE v_claiming_value NUMERIC(20,2); -- 声明价值(元),
  DECLARE v_insurance_fee NUMERIC(20,2); -- 保价费,
  DECLARE v_premium NUMERIC(20,2); -- 保价差额,
  DECLARE v_basic_price NUMERIC(20,2); -- 基本运费,
  DECLARE v_rise_price NUMERIC(20,2); -- 上浮运费,
  DECLARE v_platform_buckle_point NUMERIC(20,3); -- 平台扣点,
  DECLARE v_platform_buckle_point_fee NUMERIC(20,2); -- 平台扣点费,
  DECLARE v_rebates_rate NUMERIC(20,3); -- 返点费率,
  DECLARE v_rebates_rate_fee NUMERIC(20,2); -- 返 点 费(母账号),
  DECLARE v_arbitrage_code VARCHAR(30); -- 仲裁单号,
  DECLARE v_carrier_rec_pay NUMERIC(20,2); -- 承运商收赔费,
  DECLARE v_supply_pay NUMERIC(20,2); -- 发货方应付,
  DECLARE v_carrier_verification_freight NUMERIC(20,2); -- 已核销运费(承运商),
  DECLARE v_carrier_denghexiao_money NUMERIC(20,2); -- 等核销金额(承运商),
  DECLARE v_carrier_com_rec NUMERIC(20,2); -- 公司应收(承运商),
  DECLARE v_reverse_money NUMERIC(20,2) DEFAULT 0; -- 冲销金额,
  DECLARE v_platform_discount_rate NUMERIC(20,2); -- 平台扣点折扣率,
  DECLARE v_platform_discount_fee NUMERIC(20,2); -- 平台扣点折扣费,
  DECLARE v_actual_platform_discount_fee NUMERIC(20,2); -- 实际平台扣点费,
  DECLARE v_create_by VARCHAR(20) DEFAULT system; -- 创建人
  DECLARE v_create_time DATETIME DEFAULT NOW(); -- 创建时间
  DECLARE v_enable_status INT DEFAULT 1; -- 数据有效性:默认有效
    
  DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET v_exception = error;
  -- 启动事务
  -- START TRANSACTION; 
  
  -- 创建临时表,用于临时存储承运商账单明细表数据
  DROP TEMPORARY TABLE IF EXISTS account_detail_tmp;
  CREATE TEMPORARY TABLE account_detail_tmp(
      id INT UNSIGNED NOT NULL AUTO_INCREMENT, -- 自增ID
      belong VARCHAR(20), -- 用户ID
      member_name VARCHAR(50), -- 清算对象
      -- set_no VARCHAR(30), -- 清算单号,
      order_no VARCHAR(30), -- 子订单号,
      p_order_no VARCHAR(30), -- 母订单号,
      trans_no VARCHAR(30), -- 运单号,
      -- year VARCHAR(4), -- 会计年度,
      -- month VARCHAR(2), -- 会计月度,
      order_time datetime, -- 订单生成时间,
      order_money NUMERIC(20,2), -- 订单金额,
      claiming_value NUMERIC(20,2), -- 声明价值(元),
      insurance_fee NUMERIC(20,2), -- 保价费,
      premium NUMERIC(20,2), -- 保价差额,
      basic_price NUMERIC(20,2), -- 基本运费,
      rise_price NUMERIC(20,2), -- 上浮运费,
      platform_buckle_point NUMERIC(20,3), -- 平台扣点,
      platform_buckle_point_fee NUMERIC(20,2), -- 平台扣点费,
      rebates_rate NUMERIC(20,3), -- 返点费率,
      rebates_rate_fee NUMERIC(20,2), -- 返 点 费(母账号),
      arbitrage_code VARCHAR(30), -- 仲裁单号,
      carrier_rec_pay NUMERIC(20,2), -- 承运商收赔费,
      supply_pay NUMERIC(20,2), -- 发货方应付,
      carrier_verification_freight NUMERIC(20,2), -- 已核销运费(承运商),
      carrier_denghexiao_money NUMERIC(20,2), -- 等核销金额(承运商),
      carrier_com_rec NUMERIC(20,2), -- 公司应收(承运商),
      platform_discount_rate NUMERIC(20,2), -- 平台扣点折扣率,
      platform_discount_fee NUMERIC(20,2), -- 平台扣点折扣费,
      actual_platform_discount_fee NUMERIC(20,2), -- 实际平台扣点费,
      report_id  INT , -- 清算报表id
      reverse_money NUMERIC(20,2), -- 冲销金额,
      -- create_by VARCHAR(20) DEFAULT ‘system‘, -- 创建人
      -- create_time DATETIME DEFAULT NOW(), -- 创建时间
      -- enable_status INT DEFAULT 1, -- 数据有效性:默认有效
      primary key(id)
  );
      
    IF p_belong IS NOT NULL AND p_belong <> ‘‘ THEN
        SET v_belong = p_belong;
    ELSEIF p_user_id IS NOT NULL AND p_user_id <> ‘‘ THEN
        SET v_belong = p_user_id;
    END IF;
  
    
    #将清算周期内清算报表的数据插入到临时表
    #为了防止漏单,数据取清算周期最晚时间之前所有没生成过账单的清算报表数据,不需区分清算周期(月结、周结、一单一结)
    SET @insert_tmp_table = CONCAT(insert into account_detail_tmp(belong,member_name,order_no,p_order_no,trans_no,,
             order_time,order_money,claiming_value,insurance_fee,premium,basic_price,rise_price,platform_buckle_point,,
             platform_buckle_point_fee,rebates_rate,rebates_rate_fee,arbitrage_code,carrier_rec_pay,supply_pay,,
             carrier_verification_freight,carrier_denghexiao_money,carrier_com_rec,platform_discount_rate,,
             platform_discount_fee,actual_platform_discount_fee,report_id,reverse_money) ,
             select r.BELONG,,
             (select CARRIER_NAME FROM sas_order_reports WHERE BELONG=,v_belong,  order by id desc limit 1 ),,
             r.ORDER_NO,r.P_ORDER_NO,r.WAYBILL_NO,,
             r.ORDER_TIME,r.ORDER_MONEY,r.CLAIMING_VALUE,r.INSURANCE_FEE,r.PREMIUM,r.BASIC_PRICE,r.RISE_PRICE,r.PLATFORM_BUCKLE_POINT,,
             r.PLATFORM_BUCKLE_POINT_FEE,r.REBATES_RATE,r.REBATES_RATE_FEE,r.ARBITRAGE_CODE,r.CARRIER_REC_PAY,r.SUPPLY_PAY,,
             r.CARRIER_VERIFICATION_FREIGHT,r.CARRIER_DENGHEXIAO_MONEY,r.CARRIER_COM_REC,r.PLATFORM_DISCOUNT_RATE,,
             r.PLATFORM_DISCOUNT_FEE,r.ACTUAL_PLATFORM_DISCOUNT_FEE,r.ID,r.REVERSE_MONEY from sas_order_reports r where r.belong = ,v_belong,
             and r.ORDER_SET_STATUS !=20 and r.IS_CREATE_ACCOUNT !=20 and r.HAVE_CREATE_ACCOUNT!=20 and r.IS_RAILWAY!=\01\ and ,
             str_to_date(r.create_time,\%Y-%m-%d\) <= str_to_date(\‘‘,p_set_end,\,\%Y-%m-%d\) order by r.id);
    PREPARE insert_tmp_table_stmt FROM @insert_tmp_table;
    EXECUTE insert_tmp_table_stmt;
    
    DEALLOCATE PREPARE insert_tmp_table_stmt;
    
    #查询临时表数据数量
    SELECT COUNT(1) INTO v_tmp_count FROM account_detail_tmp;
            SELECT v_tmp_count;
    
    #如果临时表有数据,正常账单
    IF v_tmp_count>0 THEN
        
        #如果是一单一结那一条账单对应一条订单
        IF p_title_type=1 THEN
        
            #取出临时表的最小id和最大id,用于循环插入明细表
            SELECT min(id) INTO v_min_id FROM account_detail_tmp;
            SELECT max(id) INTO v_max_id FROM account_detail_tmp;
            
            #从临时表查询数据,保存到明细表
            WHILE v_min_id - 1 < v_max_id DO
                
                #获取订单总金额及公共信息(承运商、belong等)用于生成账单表头
                select SUM(carrier_denghexiao_money),member_name,belong,SUM(IFNULL(reverse_money,0)) into v_pay_money,v_member_name,v_belong,v_reverse_money from account_detail_tmp  WHERE id=v_min_id;
                SET v_pay_money = v_pay_money - v_reverse_money;
                #清算账单号
                IF p_flow_no IS NULL THEN
                    CALL P_GET_SEQNO(v_prefix,4,0,2,p_flow_no);
                END IF;
                
                SET v_set_no = CONCAT(v_prefix, DATE_FORMAT(NOW(),%Y%m%d%H%i%S), LPAD(v_min_id, 4, 0));

                #生成承运商账单(非铁路)表头
                SET @insert_head_sql=CONCAT(insert into , v_table_c,  (SYS_CODE,SET_NO,BELONG,MEMBER_NAME,SET_DAY,,
                                     SET_TYPE,SET_START,SET_END,YEAR,MONTH,PAY_WAY,SET_DATE,PAY_MONEY,SET_STATUS,PAY_STATUS,,
                                     IS_PRINT_PAY,CARRIER_ORG_LEVEL_CODE,CREATE_BY,CREATE_TIME,ENABLE_STATUS) 
                                     values(\‘‘,v_sys_code,\,\‘‘,v_set_no,\,\‘‘,v_belong,\,\‘‘,v_member_name,\,\‘‘,p_title,
                                     \,\‘‘,p_title_type,\,\‘‘,p_set_start,\,\‘‘,p_set_end,\,\‘‘,v_year,\,\‘‘,v_month,\,\‘‘,v_pay_way,\,\‘‘,v_set_date,\,\‘‘,v_pay_money,\,\‘‘,v_set_status,\,\‘‘,v_pay_status,
                                     \,\‘‘,v_is_print_pay,\,\‘‘,v_carrier_org_level_code,\,\‘‘,v_create_by,\,\‘‘,v_create_time,\,\‘‘,v_enable_status,\));
                PREPARE insert_head_stmt FROM @insert_head_sql;
                EXECUTE insert_head_stmt;
                DEALLOCATE PREPARE insert_head_stmt;
                
                #账单id赋值
            SELECT report_id INTO v_report_id FROM account_detail_tmp WHERE id=v_min_id;
                #生成承运商账单明细(非铁路)
                    SET @insert_detail_sql=CONCAT(insert into ,v_table_c_com,
                         (SET_NO,ORDER_NO,TRANS_NO,P_ORDER_NO,YEAR,MONTH,ORDER_TIME,ORDER_MONEY,CLAIMING_VALUE,,
                         INSURANCE_FEE,PREMIUM,BASIC_PRICE,RISE_PRICE,PLATFORM_BUCKLE_POINT,PLATFORM_BUCKLE_POINT_FEE,,
                         REBATES_RATE,REBATES_RATE_FEE,ARBITRAGE_CODE,CARRIER_REC_PAY,SUPPLY_PAY,CARRIER_VERIFICATION_FREIGHT,,
                         CARRIER_DENGHEXIAO_MONEY,CARRIER_COM_REC,CREATE_BY,CREATE_TIME,ENABLE_STATUS,,
                         PLATFORM_DISCOUNT_RATE,PLATFORM_DISCOUNT_FEE,ACTUAL_PLATFORM_DISCOUNT_FEE),
                         select \‘‘,v_set_no,\,order_no,trans_no,p_order_no,,\‘‘,v_year,\,\‘‘,v_month,\,order_time,order_money,claiming_value,,
                         insurance_fee,premium,basic_price,rise_price,platform_buckle_point,platform_buckle_point_fee,,
                         rebates_rate,rebates_rate_fee,arbitrage_code,carrier_rec_pay,supply_pay,carrier_verification_freight,,
                         carrier_denghexiao_money,carrier_com_rec,,\‘‘,v_create_by,\,\‘‘,v_create_time,\,\‘‘,v_enable_status,
                         \, platform_discount_rate,platform_discount_fee,actual_platform_discount_fee from account_detail_tmp where id=, v_min_id);
                    
                PREPARE insert_detail_stmt FROM @insert_detail_sql;
                EXECUTE insert_detail_stmt;
                
                #更新清算报表状态为已生成账单、已生成过账单、已出账单
                SET @update_report_sql=CONCAT(update sas_order_reports set IS_CREATE_ACCOUNT =\20\, HAVE_CREATE_ACCOUNT=\20\,ORDER_SET_STATUS=\20\, LAST_UPDATE_TIME=SYSDATE(),LAST_UPDATE_BY=\system\  where id=,v_report_id);
                PREPARE update_report_stmt FROM @update_report_sql;
                EXECUTE update_report_stmt;
                
                SET v_min_id = v_min_id + 1;
            
            
            END WHILE;
            
                #释放资源
                DEALLOCATE PREPARE insert_detail_stmt;
                DEALLOCATE PREPARE update_report_stmt;
            
        
        #月结或周结一个账单对应多个订单
        ELSEIF p_title_type !=1 THEN
    
            
            #明细表
            #取出临时表的最小id和最大id,用于循环插入明细表
            SELECT min(id) INTO v_min_id FROM account_detail_tmp;
            SELECT max(id) INTO v_max_id FROM account_detail_tmp;

#获取订单总金额及公共信息(承运商、belong等)用于生成账单表头
            select SUM(carrier_denghexiao_money),member_name,belong,SUM(IFNULL(reverse_money,0)) into v_pay_money,v_member_name,v_belong,v_reverse_money from account_detail_tmp;
            SET v_pay_money = v_pay_money - v_reverse_money;
            SELECT v_reverse_money,v_pay_money;
            #清算账单号
            IF p_flow_no IS NULL THEN
                CALL P_GET_SEQNO(v_prefix,4,0,2,p_flow_no);
            END IF;
            
            SET v_set_no = CONCAT(v_prefix, DATE_FORMAT(NOW(),%Y%m%d%H%i%S), LPAD(p_flow_no, 4, 0));

            #生成承运商账单(非铁路)表头
            SET @insert_head_sql=CONCAT(insert into , v_table_c,  (SYS_CODE,SET_NO,BELONG,MEMBER_NAME,SET_DAY,,
                                 SET_TYPE,SET_START,SET_END,YEAR,MONTH,PAY_WAY,SET_DATE,PAY_MONEY,SET_STATUS,PAY_STATUS,,
                                 IS_PRINT_PAY,CARRIER_ORG_LEVEL_CODE,CREATE_BY,CREATE_TIME,ENABLE_STATUS) 
                                 values(\‘‘,v_sys_code,\,\‘‘,v_set_no,\,\‘‘,v_belong,\,\‘‘,v_member_name,\,\‘‘,p_title,
                                 \,\‘‘,p_title_type,\,\‘‘,p_set_start,\,\‘‘,p_set_end,\,\‘‘,v_year,\,\‘‘,v_month,\,\‘‘,v_pay_way,\,\‘‘,v_set_date,\,\‘‘,v_pay_money,\,\‘‘,v_set_status,\,\‘‘,v_pay_status,
                                 \,\‘‘,v_is_print_pay,\,\‘‘,v_carrier_org_level_code,\,\‘‘,v_create_by,\,\‘‘,v_create_time,\,\‘‘,v_enable_status,\));
            PREPARE insert_head_stmt FROM @insert_head_sql;
            EXECUTE insert_head_stmt;
            DEALLOCATE PREPARE insert_head_stmt;
            
            #从临时表查询数据,保存到明细表
            WHILE v_min_id - 1 < v_max_id DO
                    
            #账单id赋值
            SELECT report_id INTO v_report_id FROM account_detail_tmp WHERE id=v_min_id;
                #生成承运商账单明细(非铁路)
                    SET @insert_detail_sql=CONCAT(insert into ,v_table_c_com,
                         (SET_NO,ORDER_NO,TRANS_NO,P_ORDER_NO,YEAR,MONTH,ORDER_TIME,ORDER_MONEY,CLAIMING_VALUE,,
                         INSURANCE_FEE,PREMIUM,BASIC_PRICE,RISE_PRICE,PLATFORM_BUCKLE_POINT,PLATFORM_BUCKLE_POINT_FEE,,
                         REBATES_RATE,REBATES_RATE_FEE,ARBITRAGE_CODE,CARRIER_REC_PAY,SUPPLY_PAY,CARRIER_VERIFICATION_FREIGHT,,
                         CARRIER_DENGHEXIAO_MONEY,CARRIER_COM_REC,CREATE_BY,CREATE_TIME,ENABLE_STATUS,,
                         PLATFORM_DISCOUNT_RATE,PLATFORM_DISCOUNT_FEE,ACTUAL_PLATFORM_DISCOUNT_FEE),
                         select \‘‘,v_set_no,\,order_no,trans_no,p_order_no,,\‘‘,v_year,\,\‘‘,v_month,\,order_time,order_money,claiming_value,,
                         insurance_fee,premium,basic_price,rise_price,platform_buckle_point,platform_buckle_point_fee,,
                         rebates_rate,rebates_rate_fee,arbitrage_code,carrier_rec_pay,supply_pay,carrier_verification_freight,,
                         carrier_denghexiao_money,carrier_com_rec,,\‘‘,v_create_by,\,\‘‘,v_create_time,\,\‘‘,v_enable_status,
                         \, platform_discount_rate,platform_discount_fee,actual_platform_discount_fee from account_detail_tmp where id=, v_min_id);
                    
                PREPARE insert_detail_stmt FROM @insert_detail_sql;
                EXECUTE insert_detail_stmt;
                DEALLOCATE PREPARE insert_head_stmt;
                
                #更新清算报表状态为已生成账单、已生成过账单、已出账单
                SET @update_report_sql=CONCAT(update sas_order_reports set IS_CREATE_ACCOUNT =\20\, HAVE_CREATE_ACCOUNT=\20\,ORDER_SET_STATUS=\20\, LAST_UPDATE_TIME=SYSDATE(),LAST_UPDATE_BY=\system\  where id=,v_report_id);
                PREPARE update_report_stmt FROM @update_report_sql;
                EXECUTE update_report_stmt;
                
                SET v_min_id = v_min_id + 1;
            END WHILE;
            
                #释放资源
                DEALLOCATE PREPARE insert_detail_stmt;
                DEALLOCATE PREPARE update_report_stmt;
        END IF;
    END IF;
    
        #删除临时表
        TRUNCATE TABLE account_detail_tmp;
        DROP TEMPORARY TABLE IF EXISTS account_detail_tmp;
    
        #更新任务状态为“已处理”
        UPDATE sas_task_account_carrier_period 
        SET TASK_STATUS = 1, LAST_UPDATE_TIME = SYSDATE(), LAST_UPDATE_BY = system
        WHERE ID = p_task_id;
    IF v_exception<>‘‘ THEN    /*异常 :回滚*/
        ROLLBACK;
    ELSE
    #提交事务
        COMMIT;
    END IF;
END

 

项目存储过程记录

标签:end   rri   primary   标识   char   eal   long   temporary   rate   

原文地址:http://www.cnblogs.com/againn/p/7359215.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!