quantity_detail.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var quantity_detail = {
  5. createNew: function (project) {
  6. // 用户定义private方法
  7. var tools = {};
  8. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  9. var quantity_detail = function (proj) {
  10. this.gljTree = cacheTree.createNew(this);
  11. // this.project = proj;
  12. this.datas = [];
  13. var sourceType = ModuleNames.quantity_detail;
  14. this.getSourceType = function () {
  15. return sourceType;
  16. }
  17. proj.registerModule(ModuleNames.quantity_detail, this);
  18. this.temList=[];
  19. };
  20. // prototype用于定义public方法
  21. quantity_detail.prototype.loadData = function (datas) {
  22. this.datas = datas;
  23. };
  24. // 提交数据后返回数据处理
  25. quantity_detail.prototype.doAfterUpdate = function(err, data){
  26. if(!err){
  27. if(data.updateTpye=='ut_update'){
  28. this.refreshAfterUpdate(data);
  29. }else if(data.updateTpye=='ut_delete'){
  30. this.refreshAfterDelete(data);
  31. } else {
  32. this.refreshAfterSave(data);
  33. }
  34. }else {
  35. alert("输入的表达式有误,请重新输入");
  36. this.refreshSheetData();
  37. }
  38. };
  39. quantity_detail.prototype.refreshAfterSave=function(data){
  40. this.datas.push(data);
  41. gljOprObj.detailData.push(data);
  42. gljOprObj.detailData=_.sortBy(gljOprObj.detailData,'seq');
  43. this.refreshSheetData();
  44. };
  45. quantity_detail.prototype.refreshAfterUpdate=function(data){
  46. var detail_list = this.datas;
  47. var detail_index= _.findIndex(detail_list,(detail)=>{
  48. return detail.ID==data.query.ID;
  49. })
  50. _.forEach(data.doc, function(n, key) {
  51. detail_list[detail_index][key] = n;
  52. });
  53. var filter_object;
  54. if(detail_list[detail_index].hasOwnProperty('rationID')){
  55. filter_object={'rationID':detail_list[detail_index].rationID};
  56. }else {
  57. filter_object={'billID':detail_list[detail_index].billID};
  58. }
  59. var showList = _.filter(this.datas,filter_object);
  60. gljOprObj.detailData=showList;
  61. this.refreshSheetData();
  62. };
  63. quantity_detail.prototype.refreshAfterDelete=function(data){
  64. var glj_list = projectObj.project.ration_coe.datas;
  65. _.remove(glj_list,data.query);
  66. _.remove(gljOprObj.sheetData,data.query);
  67. this.refreshSheetData();
  68. };
  69. quantity_detail.prototype.refreshSheetData=function () {
  70. sheetCommonObj.showData(gljOprObj.detailSheet,gljOprObj.detailSetting,gljOprObj.detailData);
  71. };
  72. quantity_detail.prototype.getUpdateData=function(type,query,doc,callfunction){
  73. var updateData = [];
  74. var newobj = {
  75. 'updateType': type,
  76. 'query': query,
  77. }
  78. if(doc){
  79. newobj['doc']=doc;
  80. }
  81. if(callfunction){
  82. newobj['updateFunction']=callfunction;
  83. }
  84. updateData.push(newobj);
  85. return updateData;
  86. };
  87. quantity_detail.prototype.saveQuantityDetail=function (args,dataCode) {
  88. var doc={};
  89. var selected = projectObj.project.mainTree.selected;
  90. if(selected.sourceType==ModuleNames.ration){
  91. doc.rationID=selected.data.ID;
  92. }
  93. if(selected.sourceType==ModuleNames.bills){
  94. doc.billID=selected.data.ID;
  95. }
  96. doc.projectID = selected.data.projectID;
  97. doc[dataCode]=args.editingText;
  98. doc.seq=args.row;
  99. var updateData = this.getUpdateData('ut_create',null,doc);
  100. project.pushNow('saveQuantityDetail',[this.getSourceType()],updateData);
  101. };
  102. quantity_detail.prototype.updateQuantityDetail=function (args,dataCode,recode) {
  103. var doc ={};
  104. var query={
  105. ID:recode.ID,
  106. projectID:recode.projectID
  107. };
  108. doc[dataCode]=args.editingText;
  109. if (dataCode == 'regex') {
  110. if(recode.hasOwnProperty('rationID')){
  111. query.rationID=recode.rationID;
  112. }else {
  113. query.billID = recode.billID
  114. }
  115. query.index = args.row;
  116. this.updateQuantityRegex(query,doc,args)
  117. }else {
  118. this.normalUpdate(query,doc);
  119. }
  120. };
  121. quantity_detail.prototype.updateQuantityRegex=function(query,doc,args){
  122. var needupdate = false;
  123. if(args.editingText==null){
  124. needupdate =true;
  125. }else {
  126. args.editingText = _.trim(args.editingText,/\r\n/);
  127. if(this.regexChecking(args.editingText)&&this.referenceChecking(args.editingText,args.row,doc)){
  128. needupdate = true;
  129. }
  130. }
  131. if(needupdate){
  132. var updateData = this.getUpdateData('ut_update',query,doc,'updateQuantityRegex');
  133. project.pushNow('updateQuantityDetail',[this.getSourceType()],updateData);
  134. }
  135. };
  136. quantity_detail.prototype.isSummationUpdate=function (args,detailList,newval) {
  137. var query={
  138. ID:detailList[args.row].ID,
  139. projectID:detailList[args.row].projectID
  140. };
  141. var doc={
  142. isSummation:newval
  143. };
  144. this.normalUpdate(query,doc);
  145. };
  146. quantity_detail.prototype.normalUpdate=function(query,doc){
  147. var updateData = this.getUpdateData('ut_update',query,doc);
  148. project.pushNow('updateQuantityDetail',[this.getSourceType()],updateData);
  149. };
  150. quantity_detail.prototype.regexChecking=function(text){
  151. var regex=/^[0-9Cc\+\-\*\^/\(\)\.]*$/g;
  152. if(!regex.test(text)){
  153. alert("输入了非法字符,请重新输入!")
  154. return false;
  155. }else {
  156. return true;
  157. }
  158. };
  159. quantity_detail.prototype.referenceChecking=function (text,row,doc) {
  160. text = text.toUpperCase();
  161. //text= this.replaceSqr(text);
  162. var me = this;
  163. var refReg = /C\d+/g;
  164. var self ='C'+(row+1);
  165. var refList = text.match(refReg);
  166. var invalidate = _.includes(refList,self);
  167. var referenceIndexs = [];
  168. var indexOut = false;
  169. _.forEach(refList,function (item) {
  170. var ref_index = parseInt(item.substring(1));
  171. if(ref_index>me.datas.length){
  172. indexOut=true;
  173. return;
  174. }else {
  175. referenceIndexs.push(ref_index);
  176. }
  177. });
  178. if(indexOut){
  179. alert("引用有误,请重新输入!");
  180. return false;
  181. }
  182. referenceIndexs=_.uniq(referenceIndexs);
  183. doc.referenceIndexs = referenceIndexs;
  184. this.temList = referenceIndexs;
  185. invalidate=this.getAllReferenceList((row+1),referenceIndexs);
  186. if(invalidate){
  187. alert("计算式中产生了循环引用,请重新输入!");
  188. return false;
  189. }
  190. return true;
  191. };
  192. quantity_detail.prototype.getAllReferenceList=function(original,refList){
  193. var me =this;
  194. var invalidate=false;
  195. _.forEach(refList,function (item) {
  196. if(me.getReferenceList(item,original)){
  197. invalidate=true;
  198. }
  199. })
  200. return invalidate;
  201. };
  202. quantity_detail.prototype.getReferenceList=function(item,original) {
  203. var invalidate =false;
  204. var recode = this.datas[item - 1];
  205. if (recode.referenceIndexs.length > 0) {
  206. if(_.includes(recode.referenceIndexs,original)){
  207. invalidate = true;
  208. return invalidate;
  209. }
  210. this.temList = this.temList.concat(recode.referenceIndexs);
  211. _.forEach(recode.referenceIndex, function (item) {
  212. if(this.getReferenceList(item,original)){
  213. invalidate = true;
  214. }
  215. })
  216. }
  217. return invalidate;
  218. }
  219. quantity_detail.prototype.replaceSqr = function(text) {
  220. var squarRegex = /\([^\^]+\)\^\d+/g;
  221. var sqararr = text.match(squarRegex);
  222. var squarRegex2 = /C[0-9]+\^\d+|[0-9]+([.]{1}[0-9]+){0,1}\^\d+/g; //匹配没有括号的
  223. var sqararr2=text.match(squarRegex2);
  224. if(sqararr){
  225. text=converSqrByArr(sqararr,text);
  226. }
  227. if(sqararr2){
  228. text=converSqrByArr(sqararr2,text);
  229. }
  230. return text;
  231. };
  232. quantity_detail.prototype.converSqrByArr = function (sqararr,text) {
  233. var temp = text;
  234. sqararr.forEach(function (item) {
  235. var arr = item.split('\^');
  236. var y = parseInt(arr[1]);
  237. var x_arr = [];
  238. for (var i = 0; i < y; i++) {
  239. x_arr.push(arr[0]);
  240. }
  241. var temStr = x_arr.join('*');
  242. temp = temp.replace(item, temStr);
  243. });
  244. console.log(temp);
  245. return temp;
  246. };
  247. return new quantity_detail(project);
  248. }
  249. };