create procedure proc_copy_flow(IN oldflowno varchar(200), IN newflowno varchar(200), IN newflowname varchar(200))
BEGIN
      DECLARE result_code INTEGER DEFAULT 0; -- 定义返回结果并赋初值0
      DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET result_code=1; -- 在执行过程中出任何异常设置result_code为1
      DECLARE CONTINUE HANDLER FOR NOT FOUND SET result_code = 2; -- 如果表中没有下一条数据则置为2
 
      START TRANSACTION; -- 开始事务

insert into awe_menu_info (menuid,menuname,displayname,sortno,url,urlparam,target,style,isinuse,inputuserid,inputtime,updateuserid,updatetime,accesstype)
  select cast( a.menuid   as   SIGNED   INTEGER)+1,newflowname,newflowname,cast( a.sortno   as   SIGNED   INTEGER)+1 ,a.url,CONCAT('ComponentName=' , newflowname , substr(replace(a.urlparam,replace(oldflowno,'Flow','Apply'),replace(newflowno,'Flow','Apply')),instr(a.urlparam,'&'))),
  a.target,a.style,a.isinuse,a.inputuserid,a.inputtime,a.updateuserid,a.updatetime,a.accesstype
  from awe_menu_info a where urlparam like CONCAT('%', replace(oldflowno,'Flow','Apply') ,'%');

  insert into awe_role_menu select roleid, cast( menuid   as   SIGNED   INTEGER)+1 from awe_role_menu
  where menuid = (select menuid from awe_menu_info where urlparam like CONCAT('%', replace(oldflowno,'Flow','Apply') ,'%'));
 
  insert into flow_catalog (flowno,flowname,flowtype,flowdescribe,initphase,viewfile,isinuse,version,baseflowno,graphjsondata,flowstate)
  select newflowno,newflowname,replace(newflowno,'Flow','Apply'),newflowname,a.initphase,a.viewfile,a.isinuse,a.version,
  newflowno,a.graphjsondata,a.flowstate from flow_catalog a where flowno=oldflowno;

  insert into flow_model (flowno,phaseno,phasetype,phasename,prescript,initscript,choicedescribe,choicescript,actiondescribe,actionscript,
  postscript,attribute1,attribute2,attribute3,attribute4,attribute5,attribute6,attribute7,attribute8,attribute9,attribute10,aaenabled,
  aapointinitscript,aapointcomp,aapointcompurl,standardtime1,standardtime2,costlob,strips,checklist,decisionscript,riskscanrule,buttonset2,
  inputuser,inputtime,updateuser,updatetime,distributerule,id,type,xcoordinate,ycoordinate,width,height,version,swimlane,nodetype,
  flowphasecontext,opinionsrequired,isreadonly,flowpageconfig,flowprocessclass,flowpagecheck,backstepnexttype,backscript,deletescript,
  phasescript,loadproductcheck,nextsteps,flowtiptype,remindnoticttype,flowoverdate,overnoticetype,overnoticeuser)
  select newflowno,a.phaseno,a.phasetype,a.phasename,a.prescript,a.initscript,a.choicedescribe,a.choicescript,a.actiondescribe,
  a.actionscript,a.postscript,a.attribute1,a.attribute2,a.attribute3,a.attribute4,a.attribute5,a.attribute6,a.attribute7,a.attribute8,
  a.attribute9,a.attribute10,a.aaenabled,a.aapointinitscript,a.aapointcomp,a.aapointcompurl,a.standardtime1,a.standardtime2,a.costlob,
  a.strips,a.checklist,a.decisionscript,a.riskscanrule,a.buttonset2,a.inputuser,a.inputtime,a.updateuser,a.updatetime,a.distributerule,
  a.id,a.type,a.xcoordinate,a.ycoordinate,a.width,a.height,a.version,a.swimlane,a.nodetype,a.flowphasecontext,a.opinionsrequired,a.isreadonly,
  a.flowpageconfig,a.flowprocessclass,a.flowpagecheck,a.backstepnexttype,a.backscript,a.deletescript,a.phasescript,a.loadproductcheck,a.nextsteps,
  a.flowtiptype,a.remindnoticttype,a.flowoverdate,a.overnoticetype,a.overnoticeuser from flow_model a where flowno=oldflowno;

  insert into code_catalog (codeno,codetypeone,codename) select replace(newflowno,'Flow','ApplyMain'),codetypeone,replace(newflowno,'Flow','申请主树图') from code_catalog where codeno=replace(oldflowno,'Flow','ApplyMain');
  insert into code_library(codeno,itemno,itemname,isinuse,itemdescribe,itemattribute,attribute2,attribute5,attribute8)
  select codeno,replace(newflowno,'Flow','Apply'),newflowname,isinuse,replace(newflowno,'Flow','ApplyMain'),itemattribute,newflowno,attribute5,attribute8
  from code_library a WHERE a.attribute2=oldflowno AND a.codeno='ApplyType';

  insert into code_library (codeno,itemno,itemname,sortno,isinuse,itemdescribe,itemattribute,attribute1,attribute2,attribute4,attribute5,remark)
  select replace(newflowno,'Flow','ApplyMain'),a.itemno,a.itemname,a.sortno,a.isinuse,a.itemdescribe,a.itemattribute,a.attribute1,a.attribute2,a.attribute4,
  a.attribute5,a.remark from code_library a where codeno=replace(oldflowno,'Flow','ApplyMain');
  -- 复制审批页面数据字典
  insert into code_catalog (codeno,codetypeone,codename) select replace(newflowno,'Flow','ApproveMain'),codetypeone,replace(newflowno,'Flow','审批主树图') from code_catalog where codeno=replace(oldflowno,'Flow','ApproveMain');
  insert into code_library(codeno,itemno,itemname,isinuse,itemdescribe,itemattribute,attribute2,attribute5,attribute8)
  select codeno,replace(newflowno,'Flow','Approve'),replace(newflowname,'申请','审批'),isinuse,replace(newflowno,'Flow','ApproveMain'),itemattribute,newflowno,attribute5,attribute8
  from code_library a WHERE a.attribute2=oldflowno AND a.codeno='ApproveType';

  insert into code_library (codeno,itemno,itemname,sortno,isinuse,itemdescribe,itemattribute,attribute1,attribute2,attribute4,attribute5,remark)
  select replace(newflowno,'Flow','ApproveMain'),a.itemno,a.itemname,a.sortno,a.isinuse,a.itemdescribe,a.itemattribute,a.attribute1,a.attribute2,a.attribute4,
  a.attribute5,a.remark from code_library a where codeno=replace(oldflowno,'Flow','ApproveMain');

IF result_code = 1 THEN -- 可以根据不同的业务逻辑错误返回不同的result_code，这里只定义了1和0
         ROLLBACK; 
     ELSE 
         COMMIT; 
     END IF;
 select result_code;
 END;

