DROP PROCEDURE IF EXISTS `CALCBLUE`;
delimiter ;;
CREATE PROCEDURE `CALCBLUE`(IN `PCPID` INT)
LABEL: BEGIN
DECLARE MCALCYEAR INT;
DECLARE MISMONTH,MCALCMONTH,MISSENT TINYINT;

START TRANSACTION;
-- 日志;
DELETE FROM AR_BS_BONUS_103_CALC_EVENT ;-- WHERE CALC_PERIOD_ID >= PCPID;
CALL CALCRECORD(-1,'开始',PCPID,NOW());
COMMIT;
-- SELECT USER_ID INTO MISMONTH FROM ZR_USER;
SELECT AP.IS_MONTH,AP.CALC_MONTH,AP.CALC_YEAR,AP.IS_SENT
INTO MISMONTH,MCALCMONTH,MCALCYEAR,MISSENT
FROM AR_PERIOD AP WHERE AP.PERIOD_NUM = PCPID;

	-- 查看是否月结
-- IF(MISMONTH  =  0 OR MISSENT = 1)THEN
-- LEAVE LABEL;
-- END IF;


-- 清除数据
CALL CALCRECORD(1,'清除数据',PCPID,NOW());
COMMIT;
CALL CALCCLEAN(PCPID);
COMMIT;

-- 初始化
CALL CALCRECORD(2,'初始化',PCPID,NOW());
COMMIT;
CALL CALCINIT(PCPID,MCALCMONTH,MCALCYEAR);
COMMIT;


-- 初始业绩
-- 3.蓝星奖
CALL CALCRECORD(3,'蓝星奖',PCPID,NOW());
COMMIT;
CALL CALC10(PCPID);
COMMIT;

-- 4.新网体
CALL CALCRECORD(4,'新网体',PCPID,NOW());
COMMIT;
CALL CALC20(PCPID);
COMMIT;

-- 5.升级主任
CALL CALCRECORD(5,'升级主任',PCPID,NOW());
COMMIT;
CALL CALCUP30;
COMMIT;

-- 6.计算主任自己第一层的总业绩
CALL CALCRECORD(6,'计算主任自己第一层的总业绩',PCPID,NOW());
COMMIT;
CALL CALCTOTALGPV;
COMMIT;

-- 7.计算平级奖
CALL CALCRECORD(7,'计算平级奖',PCPID,NOW());
COMMIT;
CALL CALCLEVEL(PCPID);
COMMIT;

-- 8.积分
CALL CALCRECORD(8,'积分',PCPID,NOW());
COMMIT;
CALL CALCPOINT;
COMMIT;

-- 9.汇总
CALL CALCRECORD(9,'汇总',PCPID,NOW());
COMMIT;
CALL CALCSUMMARY;
COMMIT;

-- 10.记录总表
CALL CALCRECORD(10,'记录总表',PCPID,NOW());
COMMIT;
CALL CALCKEEP(MCALCYEAR,MCALCMONTH);
COMMIT;

-- 11.计算拨出比
CALL CALCRECORD(11,'计算拨出比',PCPID,NOW());
COMMIT;
CALL CALCSTAT(PCPID);
COMMIT;

CALL CALCRECORD(12,'结束',PCPID,NOW());
COMMIT;
END
;;