<?xml version="1.0" encoding="utf-8"?>

<databaseChangeLog
		xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
	<changeSet author="maoying" id="1550832246001-1" runAlways="true" >
		<createView  replaceIfExists="true" viewName="v_msg_subscribe_check">
			SELECT
			`user_id`,
			MAX(
			CASE
			WHEN  msg_type='checkEmail' THEN
			attribute1
			END
			) AS 'check_email',
			MAX(
			CASE
			WHEN  msg_type='checkType' THEN
			attribute1
			END
			) AS 'check_type',
			MAX(
			CASE
			WHEN msg_type='route' THEN
			attribute1
			END
			) AS 'route'
			FROM
			p_msg_subscribe
			GROUP BY `user_id`;
		</createView>
	</changeSet>
	<changeSet author="suhuiguang" id="1553233487001-1" runAlways="true">
		<createView  replaceIfExists="true" viewName="vcheck">
			SELECT
			`a`.`id` AS `id`,
			`a`.`org_code` AS `org_code`,
			`a`.`point_id` AS `point_id`,
			`b`.`name` AS `point_name`,
			`b`.`is_fixed` AS `is_fixed`,
			`b`.`point_no` AS `point_no`,
			`a`.`user_id` AS `user_id`,
			<!-- `c`.`name` AS `user_name`, -->
			date_format(
			`a`.`check_time`,
			'%Y-%m-%d %H:%i:%s'
			) AS `check_time`,
			date_format(
			`a`.`upload_time`,
			'%Y-%m-%d %H:%i:%s'
			) AS `upload_time`,
			(
			CASE
			WHEN  `a`.`plan_task_detail_id` = 0 OR `a`.`plan_task_detail_id` is null
			THEN  '0'
			WHEN  `a`.`plan_task_detail_id` != 0 AND  `a`.`is_ok` != 3
			THEN  '1'
			WHEN  `a`.`is_ok` = 3
			THEN  '2'
			END
			) AS `Finish_Status`,
			`a`.`is_ok`,
			`a`.`score` AS `score`,
			`a`.`route_id` AS `route_id`,
			`d`.`name` AS `route_name`,
			`a`.`check_mode` AS `check_mode`,
			`a`.`plan_id` AS `plan_id`,
			`e`.`name` AS `plan_name`,
			`b`.`catalog_id` AS `catalog_id`,
			`a`.`plan_task_id` AS `plan_task_id`,
			`a`.`error` AS `error`,
			`a`.`remark` AS `remark`,
			<!-- sd.department_name, -->
			`e`.`dept_id` AS department_id
			FROM
			`p_check` `a`
			LEFT JOIN `p_point` `b` ON `a`.`point_id` = `b`.`id`
			<!-- 	LEFT JOIN `s_user` `c` ON `a`.`user_id` = `c`.`id` -->
			<!-- 		LEFT JOIN s_department sd ON sd.id = c.department_id
             -->		LEFT JOIN `p_route` `d` ON `a`.`route_id` = `d`.`id`
			LEFT JOIN `p_plan` `e` ON `a`.`plan_id` = `e`.`id`
			LEFT JOIN `p_catalog_tree` `f` ON `b`.`catalog_id` = `f`.`id`
		</createView>
	</changeSet>
	<changeSet author="maoying" id="1610421278000-1" runAlways="true">
		<comment>check_insert 触发器创建</comment>
		<sql>
			DROP TRIGGER IF EXISTS `trigger_check_insert`;
		</sql>
	</changeSet>
	<changeSet author="maoying" id="1610421278000-2" runAlways="true">
		<comment>check_insert 触发器创建</comment>
		<sql endDelimiter="#">
			CREATE TRIGGER `trigger_check_insert` AFTER INSERT ON `p_check` FOR EACH ROW
			BEGIN
			UPDATE p_plan_task_detail SET status =  NEW.is_ok WHERE id = NEW.plan_task_detail_id;
			UPDATE p_point SET `status` = NEW.is_ok,error = NEW.error,check_time = NEW.check_time WHERE id = NEW.point_id;
			END#
		</sql>
	</changeSet>
	<changeSet author="maoying" id="1610421278000-3" runAlways="true">
		<comment>check_update 触发器创建</comment>
		<sql>
			DROP TRIGGER IF EXISTS `trigger_check_update`;
		</sql>
	</changeSet>
	<changeSet author="maoying" id="1610421278000-4" runAlways="true">
		<comment>check_update 触发器创建</comment>
		<sql endDelimiter="#">
			CREATE TRIGGER `trigger_check_update` AFTER UPDATE ON `p_check` FOR EACH ROW
			BEGIN
			UPDATE p_plan_task_detail SET status =  NEW.is_ok WHERE id = NEW.plan_task_detail_id;
			UPDATE p_point SET `status` = NEW.is_ok,error = NEW.error,check_time = NEW.check_time WHERE id = NEW.point_id;
			END#
		</sql>
	</changeSet>
	<!--
   <changeSet author="maoying" id="1554111363112-6" runOnChange="true">
       <comment>plan_task_detail_update 触发器创建</comment>
       <sql endDelimiter="#">
           DROP TRIGGER IF EXISTS `trigger_plan_task_detail_update`;
           CREATE TRIGGER `trigger_plan_task_detail_update` AFTER UPDATE ON `p_plan_task_detail` FOR EACH ROW
         BEGIN
            DECLARE status VARCHAR (50) CHARACTER SET utf8;
           DECLARE unQualifiedCount INT (4);
           DECLARE omissionCount INT (4);
           SELECT count(1) into unQualifiedCount FROM p_plan_task_detail WHERE point_id = NEW.point_id AND status = '2';
           SELECT count(1) into omissionCount FROM p_plan_task_detail WHERE point_id = NEW.point_id AND status = '3';
           SET status = NEW. status;
           IF status = '2' THEN
               UPDATE p_point SET `status` = status WHERE id = NEW.point_id;
           END IF;
           IF status = '3' AND unQualifiedCount = 0 THEN
                   UPDATE p_point SET `status` = status WHERE id = NEW.point_id;
           END IF;
           IF status = '1' AND unQualifiedCount = 0 AND omissionCount = 0 THEN
                   UPDATE p_point SET `status` = status WHERE id = NEW.point_id;
           END IF;
         END#
       </sql>
   </changeSet> -->
	<changeSet author="maoying" id="1610421278000-5" runAlways="true">
		<comment>plan_task_detail_insert 触发器创建</comment>
		<sql>
			DROP TRIGGER IF EXISTS `trigger_plan_task_detail_insert`;
		</sql>
	</changeSet>
	<changeSet author="maoying" id="1610421278000-6" runAlways="true">
		<comment>plan_task_detail_insert 触发器创建</comment>
		<sql endDelimiter="#">
			CREATE TRIGGER `trigger_plan_task_detail_insert` AFTER INSERT ON `p_plan_task_detail` FOR EACH ROW
			BEGIN
			DECLARE pointId bigint(20);
			DECLARE pointStatus varchar (50);
			SET  pointId = NEW.point_id;
			SELECT status into pointStatus FROM p_point WHERE id = pointId;
			IF pointStatus = '0' THEN
			UPDATE p_point SET status = '1' WHERE id = pointId;
			END IF;
			END#
		</sql>
	</changeSet>

	<changeSet author="suhuiguang" id="1554190356001-1" runAlways="true">
		<createView  replaceIfExists="true" viewName="v_point_last_check">
			SELECT
			`a`.`point_id` AS `point_id`,
			`a`.`check_time` AS `check_time`,
			(
			CASE `a`.`is_ok`
			WHEN '2' THEN
			(
			SELECT
			group_concat(`c`.`name` SEPARATOR ';')
			FROM
			(
			`p_check_input` `b`
			JOIN `p_input_item` `c`
			)
			WHERE
			(
			(`b`.`check_id` = `a`.`id`)
			AND (`b`.`input_id` = `c`.`id`)
			AND (`b`.`is_ok` = '2')
			)
			)
			WHEN '3' THEN
			'漏检'
			END
			) AS `errorMsg`
			FROM
			`p_check` `a`
			WHERE
			(
			`a`.`id` = (
			SELECT
			max(`pc`.`id`)
			FROM
			`p_check` `pc`
			WHERE
			(
			`a`.`point_id` = `pc`.`point_id`
			)
			)
			)
		</createView>
	</changeSet>

	<changeSet author="tianbo" id="1554691494001-1" runAlways="true">
		<createView replaceIfExists="true" viewName="v_check_info">
			SELECT
			sss.*, CASE sss. STATUS
			WHEN '2' THEN
			-- 不合格
			(
			SELECT
			GROUP_CONCAT(ii.`name` SEPARATOR ';')
			FROM
			p_check_input ci,
			p_input_item ii
			WHERE
			ci.check_id = sss.CheckID
			AND ci.is_ok = '2' -- 不合格
			AND ci.input_id = ii.id
			)
			END AS ErrorMsg
			FROM
			(
			SELECT
			ss.*, ttemp.TaskID
			FROM
			(
			SELECT
			a.check_time AS CheckTime,
			<!--  b.`name` AS RealName,
             d.id AS GroupID, -->
			a.user_id AS UserID,
			a.point_id AS PointID,
			a.id AS CheckID,
			<!--  c.id AS CompanyID, -->
			a.is_ok AS STATUS,
			<!-- 	                            IFNULL(case when b.mobile = '' or b.mobile is null then null else b.mobile END,b.telephone) as LoginName,
             -->	                           <!--  d.department_name AS `Name`,
	                            p.`name` AS pointName, -->
			a.org_code AS orgCode
			FROM
			p_check a,
			p_point p
			<!--        s_user b
               LEFT JOIN s_department d ON b.department_id = d.id
               LEFT JOIN s_company c ON c.id = b.company_id -->
			WHERE
			<!--   a.user_id = b.id
          AND -->
			p.id = a.point_id
			) ss
			LEFT JOIN (
			SELECT
			td.check_id,
			GROUP_CONCAT(td.task_id) AS TaskID
			FROM
			p_task_detail td,
			(
			SELECT
			a.check_time AS CheckTime,
			<!--  b.`name` AS RealName,
             d.id AS GroupID, -->
			a.user_id AS UserID,
			a.point_id AS PointID,
			a.id AS CheckID,
			<!--  c.id AS CompanyID, -->
			a.is_ok AS STATUS,
			<!-- IFNULL(case when b.mobile = '' or b.mobile is null then null else b.mobile END,b.telephone) as LoginName,
            d.department_name AS `Name`,-->
			p.`name` AS pointName,
			a.org_code AS orgCode
			FROM
			p_check a,
			p_point p
			<!-- ,
            s_user b
        LEFT JOIN s_department d ON b.department_id = d.id
        LEFT JOIN s_company c ON c.id = b.company_id -->
			WHERE
			<!--    a.user_id = b.id
           AND -->
			p.id = a.point_id
			) ss_
			WHERE
			td.check_id = ss_.CheckID
			GROUP BY
			td.check_id
			) ttemp ON ttemp.check_id = ss.CheckID
			) sss
		</createView>
	</changeSet>
	<changeSet author="suhuiguang" id="1610421278000-7" runAlways="true">
		<comment>p_point_classify_delete 触发器创建</comment>
		<sql>
			DROP TRIGGER IF EXISTS `trigger_p_point_classify_delete`;
		</sql>
	</changeSet>
	<changeSet author="suhuiguang" id="1610421278000-8" runAlways="true">
		<comment>p_point_classify_delete 触发器创建</comment>
		<sql endDelimiter="#">
			CREATE TRIGGER `trigger_p_point_classify_delete` AFTER DELETE ON `p_point_classify` FOR EACH ROW
			BEGIN
			update p_route_point_item rpi set rpi.point_classify_id = null where rpi.point_classify_id = old.id;
			update p_point_inputitem pi set pi.classify_ids = substring(replace(concat(',',pi.classify_ids,','),concat(',',old.id,','),','),2,LENGTH(replace(concat(',',pi.classify_ids,','),concat(',',old.id,','),''))-1)  where FIND_IN_SET(old.id, pi.classify_ids);
			END#
		</sql>
	</changeSet>
	<changeSet author="tianbo" id="1610421278000-10" runAlways="true">
		<comment>trigger_route_point_insert_update_point_route_info 触发器创建</comment>
		<sql>
			DROP TRIGGER
			IF EXISTS `trigger_route_point_insert_update_point_route_info`;
		</sql>
	</changeSet>
	<changeSet author="tianbo" id="1610421278000-11" runAlways="true">
		<comment>trigger_route_point_insert_update_point_route_info 触发器创建</comment>
		<sql endDelimiter="#">
			CREATE TRIGGER `trigger_route_point_insert_update_point_route_info` AFTER INSERT ON `p_route_point` FOR EACH ROW
			BEGIN

			DECLARE pointId BIGINT (20);


			DECLARE routeId VARCHAR (1000);


			DECLARE routeName VARCHAR (1000);


			SET pointId = NEW.point_id;

			SELECT
			GROUP_CONCAT(id),
			GROUP_CONCAT(NAME) INTO routeId,
			routeName
			FROM
			(
			SELECT
			pr.id,
			pr. NAME
			FROM
			p_route_point prp
			LEFT JOIN p_route pr ON pr.id = prp.route_id
			WHERE
			prp.point_id = pointId
			GROUP BY
			pr.id
			) t;

			UPDATE p_point
			SET route_id = routeId,
			route_name = routeName
			WHERE
			id = pointId;


			END#
		</sql>
	</changeSet>
	<changeSet author="tianbo" id="1610421278000-12" runAlways="true">
		<comment>trigger_route_point_update_update_point_route_info 触发器创建</comment>
		<sql >
			DROP TRIGGER
			IF EXISTS `trigger_route_point_update_update_point_route_info`;
		</sql>
	</changeSet>
	<changeSet author="tianbo" id="1610421278000-13" runAlways="true">
		<comment>trigger_route_point_update_update_point_route_info 触发器创建</comment>
		<sql endDelimiter="#">
			CREATE TRIGGER `trigger_route_point_update_update_point_route_info` AFTER UPDATE ON `p_route_point` FOR EACH ROW
			BEGIN

			DECLARE pointId BIGINT (20);


			DECLARE routeId VARCHAR (1000);


			DECLARE routeName VARCHAR (1000);


			SET pointId = OLD.point_id;

			SELECT
			GROUP_CONCAT(id),
			GROUP_CONCAT(NAME) INTO routeId,
			routeName
			FROM
			(
			SELECT
			pr.id,
			pr. NAME
			FROM
			p_route_point prp
			LEFT JOIN p_route pr ON pr.id = prp.route_id
			WHERE
			prp.point_id = pointId
			GROUP BY
			pr.id
			) t;

			UPDATE p_point
			SET route_id = routeId,
			route_name = routeName
			WHERE
			id = pointId;


			END#
		</sql>
	</changeSet>
	<changeSet author="tianbo" id="1610421278000-14" runAlways="true">
		<comment>trigger_route_point_update_update_point_route_info 触发器创建</comment>
		<sql>
			DROP TRIGGER
			IF EXISTS `trigger_route_point_delete_update_point_route_info`;
		</sql>
	</changeSet>
	<changeSet author="tianbo" id="1610421278000-15" runAlways="true">
		<comment>trigger_route_point_update_update_point_route_info 触发器创建</comment>
		<sql endDelimiter="#">
			CREATE TRIGGER `trigger_route_point_delete_update_point_route_info` AFTER DELETE ON `p_route_point` FOR EACH ROW
			BEGIN

			DECLARE pointId BIGINT (20);


			DECLARE routeId VARCHAR (1000);


			DECLARE routeName VARCHAR (1000);


			SET pointId = OLD.point_id;

			SELECT
			GROUP_CONCAT(id),
			GROUP_CONCAT(NAME) INTO routeId,
			routeName
			FROM
			(
			SELECT
			pr.id,
			pr. NAME
			FROM
			p_route_point prp
			LEFT JOIN p_route pr ON pr.id = prp.route_id
			WHERE
			prp.point_id = pointId
			GROUP BY
			pr.id
			) t;

			UPDATE p_point
			SET route_id = routeId,
			route_name = routeName
			WHERE
			id = pointId;


			END#
		</sql>
	</changeSet>

	<changeSet author="gaodongdong" id="1610421278000-20" runAlways="true">
		<createProcedure procedureName="updatePlanTask" >
			DROP PROCEDURE IF EXISTS `updatePlanTask`;
		</createProcedure>
	</changeSet>
	<changeSet author="gaodongdong" id="1610421278000-21" runAlways="true">
		<createProcedure procedureName="updatePlanTask" >
			CREATE PROCEDURE `updatePlanTask`(IN `planTaskId` int,IN `pointId` int,IN `planTaskDetailId` int,IN `executorId` int)
			BEGIN
			declare num int ;
			declare orgCode VARCHAR(50) ;
			declare currentTaskNum int ;
			update p_plan_task_detail set is_finish = 1 where id=planTaskDetailId;
			select finish_num, org_code from p_plan_task where id = planTaskId into num, orgCode;
			select count(1) into currentTaskNum from p_plan_task_detail where task_no = planTaskId and is_finish in(0,2);
			UPDATE p_plan_task_detail SET executor_id = executorId,executor_date = current_timestamp where id = planTaskDetailId;
			if currentTaskNum > 0 THEN
			update p_plan_task set finish_num = (num + 1) where id = planTaskId;
			ELSE
			update p_plan_task set finish_num = (num + 1), finish_status = 2 where id = planTaskId;
			end if;
			if executorId > 0 then
			call planTaskStatistics(executorId, null, orgCode);
			end if;
			END
		</createProcedure>
	</changeSet>
	<changeSet author="gaodongdong" id="1610421278000-22" runAlways="true">
		<createProcedure procedureName="planTaskStatistics" >
			DROP PROCEDURE IF EXISTS `planTaskStatistics`;
		</createProcedure>
	</changeSet>

	<changeSet author="gaodongdong" id="1610421278000-23" runAlways="true">
		<createProcedure procedureName="planTaskStatistics" >
			CREATE DEFINER=`root`@`%` PROCEDURE `planTaskStatistics`(IN `userId` VARCHAR(50) ,IN `currentDate` VARCHAR(40),IN `orgCode` VARCHAR(50))
			BEGIN
			declare planTaskNum BIGINT ;
			declare planTaskFinishNum BIGINT ;
			declare missedNum BIGINT ;
			declare personNum BIGINT ;
			declare outsideNum BIGINT ;
			if currentDate is null THEN
			set currentDate = DATE_FORMAT(SYSDATE(), '%Y-%m-%d');
			end if;
			if  userId !='' then
			SELECT sum(a.finish_num), sum(a.point_num), sum(a.miss),org_code FROM (
			SELECT finish_num, point_num, org_code, CASE finish_status WHEN 3 THEN (point_num - finish_num) ELSE 0 END miss
			FROM p_plan_task WHERE find_in_set(userId,user_id)>0 AND DATE_FORMAT(end_time, '%Y-%m-%d') <![CDATA[>=]]>currentDate AND DATE_FORMAT(begin_time, '%Y-%m-%d')<![CDATA[<=]]>  currentDate AND LOCATE(orgCode, org_code)> 0  ) a INTO planTaskFinishNum, planTaskNum, missedNum, orgCode;
			if planTaskNum is null then
			set planTaskNum = 0;
			end if;
			if planTaskFinishNum is null then
			set planTaskFinishNum = 0;
			end if;
			if outsideNum is null then
			set outsideNum = 0;
			end if;
			if planTaskNum > 0 then

			SELECT count(1) into personNum FROM p_plan_exec_statistics where check_time = currentDate AND user_id = userId AND org_code = orgCode;

			if personNum > 0 then
			SELECT count(1) into outsideNum FROM p_check where DATE_FORMAT(check_time, '%Y-%m-%d') = currentDate AND find_in_set(userId,user_id)>0 and org_code = (select substring(orgCode,1,(SELECT INSTR(orgCode,'-')-1))) and plan_task_id = 0;
			update p_plan_exec_statistics SET plan_task_num = planTaskNum, exec_plan_task_num = planTaskFinishNum, missed_check_num=missedNum, outside_plan_num=outsideNum  where check_time = currentDate AND user_id = userId AND org_code = orgCode;
			ELSE
			SELECT count(1) into outsideNum FROM p_check where DATE_FORMAT(check_time, '%Y-%m-%d') = currentDate AND find_in_set(userId,user_id)>0 and org_code = (select substring(orgCode,1,(SELECT INSTR(orgCode,'-')-1))) and (plan_task_id = 0 or plan_task_id = null);
			INSERT INTO p_plan_exec_statistics(check_time,org_code,user_id,plan_task_num,exec_plan_task_num,missed_check_num,outside_plan_num)
			VALUES(currentDate, orgCode, userId, planTaskNum, planTaskFinishNum, missedNum, outsideNum);
			end if;
			end if;
			end if;
			END
		</createProcedure>
	</changeSet>
	<changeSet author="gaojianqiang" id="1630567666-1" runAlways="true">
		<comment>创建函数func_split_TotalLength</comment>
		<sql  endDelimiter="#">
			DROP function IF EXISTS `func_split_TotalLength`#
			CREATE FUNCTION `func_split_TotalLength`(f_string varchar(1000),f_delimiter varchar(5)) RETURNS int(11)
			BEGIN
				return 1+(length(f_string) - length(replace(f_string,f_delimiter,'')));
			END
			#
		</sql>
	</changeSet>
	<changeSet author="gaojianqiang" id="1630567666-2" runAlways="true">
		<comment>创建函数func_split</comment>
		<sql endDelimiter="#">
			DROP function IF EXISTS `func_split`#
			CREATE DEFINER=`root`@`%` FUNCTION `func_split`
			(f_string varchar(1000),f_delimiter varchar(5),f_order int) RETURNS varchar(255) CHARSET utf8
			BEGIN
			declare result varchar(255) default '';
			set result = reverse(substring_index(reverse(substring_index(f_string,f_delimiter,f_order)),f_delimiter,1));
			return result;
			END#
		</sql>
	</changeSet>
	<changeSet author="gaojianqiang" id="1630567666-3" runAlways="true">
		<createProcedure procedureName="splitString" >
			DROP PROCEDURE IF EXISTS `splitString`;
		</createProcedure>
	</changeSet>
	<changeSet author="gaojianqiang" id="1630567666-4" runAlways="true">
		<createProcedure procedureName="splitString">
			CREATE PROCEDURE `splitString` (IN f_string varchar(1000),IN f_delimiter varchar(5))
			BEGIN
			declare cnt int default 0;
			declare i int default 0;
			set cnt = func_split_TotalLength(f_string,f_delimiter);
			DROP TABLE IF EXISTS `tmp_split`;
			create table `tmp_split` (`status` varchar(128) not null) DEFAULT CHARSET=utf8;
			while cnt > i
					  do
					  set i = i + 1;
			insert into tmp_split(`status`) values (func_split(f_string,f_delimiter,i));
			end while;
			END
		</createProcedure>
	</changeSet>
	<changeSet author="gaodongdong" id="1610421278000-24" runAlways="true">
		<createProcedure procedureName="everyDayInitPlanTaskStatistics" >
			DROP PROCEDURE IF EXISTS `everyDayInitPlanTaskStatistics`;
		</createProcedure>
	</changeSet>
	<changeSet author="gaodongdong" id="1610421278000-26" runAlways="true">
		<createProcedure procedureName="everyDayInitPlanTaskStatistics" >
			CREATE PROCEDURE `everyDayInitPlanTaskStatistics`()
			BEGIN
			declare userId bigint ;
			declare orgCode VARCHAR(50) ;
			declare currentDate VARCHAR(40) ;
			declare planTaskNum BIGINT ;
			declare planTaskFinishNum BIGINT ;
			declare personNum BIGINT ;
			DECLARE done BOOLEAN DEFAULT 0;
			DECLARE p_p_conf CURSOR FOR select DISTINCT `status` AS id from tmp_split;
			DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
			set currentDate = SYSDATE();
			open p_p_conf;
			REPEAT
			FETCH p_p_conf INTO userId;
			if done != 1 THEN
			BEGIN
			DECLARE orgDone BOOLEAN DEFAULT 0;
			DECLARE org_plan_task CURSOR FOR SELECT sum(finish_num), sum(point_num),org_code FROM p_plan_task WHERE  find_in_set(userId,user_id)>0 AND end_time >= STR_TO_DATE(SYSDATE(),'%Y-%m-%d') GROUP BY org_code;
			DECLARE CONTINUE HANDLER FOR NOT FOUND SET orgDone=1;

			open org_plan_task;
			REPEAT
			FETCH org_plan_task INTO planTaskFinishNum, planTaskNum, orgCode;
			if done != 1 THEN
			if planTaskNum is null then
			set planTaskNum = 0;
			end if;
			if planTaskFinishNum is null then
			set planTaskFinishNum = 0;
			end if;
			SELECT count(1) into personNum FROM p_plan_exec_statistics where check_time = DATE_FORMAT(currentDate, '%Y-%m-%d') AND user_id = userId AND org_code = orgCode;
			if orgCode is not null then
			if personNum > 0 THEN
			update p_plan_exec_statistics SET plan_task_num = planTaskNum, exec_plan_task_num = planTaskFinishNum, missed_check_num=0, outside_plan_num=0  where check_time = DATE_FORMAT(currentDate, '%Y-%m-%d') AND user_id = userId AND org_code = orgCode;
			ELSE
			INSERT INTO p_plan_exec_statistics(check_time,org_code,user_id,plan_task_num,exec_plan_task_num,missed_check_num,outside_plan_num)
			VALUES(DATE_FORMAT(currentDate, '%Y-%m-%d'), orgCode, userId, planTaskNum, planTaskFinishNum, 0, 0);
			end if;
			end if;
			end if;
			UNTIL orgDone END REPEAT;
			CLOSE org_plan_task;
			END;
			end if;

			UNTIL done END REPEAT;
			CLOSE p_p_conf;
			END
		</createProcedure>
	</changeSet>
	<changeSet author="gaojianqiang" id="1630567666-5" runAlways="true">
		<comment>创建事件planTaskStatisticsaEvent</comment>
		<sql endDelimiter="#">
			DROP EVENT IF EXISTS `planTaskStatisticsaEvent`#
			CREATE EVENT `planTaskStatisticsaEvent`
			ON SCHEDULE
			EVERY '1' DAY STARTS '2021-04-14 00:02:00'
			ON COMPLETION PRESERVE
			DO CALL everyDayInitPlanTaskStatistics()
			#
		</sql>
	</changeSet>
	<changeSet author="gaojianqiang" id="1630567666-6" runAlways="true">
		<comment>创建事件splitStringEvent</comment>
		<sql endDelimiter="#">
			DROP EVENT IF EXISTS `splitStringEvent`#
			CREATE EVENT `splitStringEvent`
			ON SCHEDULE
			EVERY '1' DAY STARTS '2021-09-02 00:00:00'
			DO CALL splitString((SELECT group_concat(distinct nullif(p.user_id, '')) AS id FROM p_plan_task p),",")
			#
		</sql>
	</changeSet>
	<changeSet author="gaodongdong" id="15640410855000-5" runAlways="true">
		<sql>
			CREATE EVENT IF NOT EXISTS `planTaskStatisticsaEvent` ON SCHEDULE EVERY 1 DAY STARTS date_add(
			date_add(curdate(), INTERVAL 24 HOUR),
			INTERVAL 0 HOUR ) ON COMPLETION PRESERVE DO CALL everyDayInitPlanTaskStatistics();
		</sql>
	</changeSet>
	<changeSet author="gaodongdong" id="1610421278000-16" runAlways="true">
		<comment>addPointConfig 触发器创建</comment>
		<sql>
			DROP TRIGGER IF EXISTS `addPointConfig`;
		</sql>
	</changeSet>

	<changeSet author="gaodongdong" id="1610421278000-17" runAlways="true">
		<comment>addPointConfig 触发器创建</comment>
		<sql endDelimiter="#">
			CREATE TRIGGER `addPointConfig` AFTER INSERT ON `p_point` FOR EACH ROW
			BEGIN
			INSERT INTO p_point_config(`name`, point_id, point_code, infomation_address, `status`, update_date)
			VALUES(CONCAT(new.name,'漏检'), new.id, CONCAT('N',LPAD(new.id, 10, 0)), 0, 0, SYSDATE());
			INSERT INTO p_point_config(`name`, point_id, point_code, infomation_address, `status`, update_date)
			VALUES(CONCAT(new.name,'合格'), new.id, CONCAT('S',LPAD(new.id, 10, 0)) ,0, 0, SYSDATE());
			END#
		</sql>
	</changeSet>
	<changeSet author="gaodongdong" id="1610421278000-18" runAlways="true">
		<comment>deletePointConfig 触发器创建</comment>
		<sql >
			DROP TRIGGER IF EXISTS `deletePointConfig`;
		</sql>
	</changeSet>
	<changeSet author="gaodongdong" id="1610421278000-19" runAlways="true">
		<comment>deletePointConfig 触发器创建</comment>
		<sql endDelimiter="#">
			CREATE TRIGGER `deletePointConfig` AFTER DELETE ON `p_point` FOR EACH ROW
			BEGIN
			update p_point_config set is_delete = 1 where point_id = old.id;
			END#
		</sql>
	</changeSet>


</databaseChangeLog>