measure_wlist.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/6
  7. * @version
  8. */
  9. // 向后端请求中间计量号
  10. function getNewMeasureCode() {
  11. postData('/measure/newCode', null, function (code) {
  12. if (code !== '') {
  13. $('#mj-code').val(code);
  14. }
  15. });
  16. }
  17. function getMeasureWorkHtml (data) {
  18. const html = [];
  19. html.push('<tr>');
  20. html.push('<td>', '<a href="/measure/work/' + data.mid + '">', data.code, '</a>', '</td>');
  21. html.push('<td>0.00</td>');
  22. html.push('<td>' + moment(data.in_time).format('YYYYMM') + '</td>');
  23. html.push('<td></td>');
  24. html.push('<td>未上报</td>');
  25. html.push('<td>', data.user.role + '-' + data.user.name, '</td>');
  26. html.push('<td><a href="javascript: void(0);" data-toggle="modal" class="btn btn-outline-primary btn-sm" name="edit">编辑</a></td>')
  27. html.push('</tr>');
  28. return html.join('');
  29. }
  30. class codeRuleSet {
  31. constructor (obj) {
  32. this.body = obj;
  33. // 切换规则组件类型
  34. $('select', obj).change(function () {
  35. const codeType = this.selectedIndex;
  36. if (codeType === ruleConst.ruleType.addNo) {
  37. $('#format', obj).show();
  38. $('#text', obj).show();
  39. $('#text>label', obj).text('起始编号');
  40. $('#text>input', obj).val('001');
  41. const s = '0000000000' + 1;
  42. $('#text>input', obj).val(s.substr(s.length - $('#format>input', obj).val()));
  43. } else if (codeType === ruleConst.ruleType.text) {
  44. $('#format', obj).hide();
  45. $('#text', obj).show();
  46. $('#text>label', obj).text('文本');
  47. $('#text>input', obj).val('').attr('placeholder', '请在这里输入需要的文本');
  48. } else {
  49. $('#format', obj).hide();
  50. $('#text', obj).hide();
  51. }
  52. });
  53. // 修改编号位数
  54. $('#format>input', obj).change(function () {
  55. const s = '0000000000' + parseInt($('#text>input', obj).val());
  56. $('#text>input', obj).val(s.substr(s.length - $(this).val()));
  57. });
  58. // 新增规则组件
  59. $('#addRule', obj).click(function () {
  60. const codeType = $('select', obj)[0].selectedIndex;
  61. const rule = {rule_type: codeType}, html = [];
  62. let preview;
  63. switch (codeType) {
  64. case ruleConst.ruleType.tenderName: {
  65. preview = $('#tenderName').text();
  66. break;
  67. }
  68. case ruleConst.ruleType.text: {
  69. rule.text = $('#text>input', obj).val();
  70. preview = rule.text;
  71. break;
  72. }
  73. case ruleConst.ruleType.inDate: {
  74. preview = moment().format('YYYYMM');
  75. break;
  76. }
  77. case ruleConst.ruleType.addNo: {
  78. rule.format = parseInt($('#format>input', obj).val());
  79. rule.start = parseInt($('#text>input', obj).val());
  80. const s = '0000000000';
  81. preview = s.substr(s.length - rule.format);
  82. break;
  83. }
  84. }
  85. // 更新规则
  86. codeRule.push(rule);
  87. // 更新规则显示
  88. html.push('<span class="badge badge-light" title="' + ruleConst.ruleString[codeType] + '" rule="' + JSON.stringify(rule) + '">');
  89. html.push('<span>' + preview + '</span>');
  90. html.push('<a href="javascript: void(0);" class="text-danger" title="移除"><i class="fa fa-remove"></i></a>');
  91. html.push('</span>');
  92. const part = $('#ruleParts', obj).append(html.join(''));
  93. // 更新规则预览
  94. $('#preview', obj).text($('#preview', obj).text() + preview);
  95. $('a', part).bind('click', function () {
  96. const index = $('a', obj).index(this);
  97. codeRule.splice(index, 1);
  98. $(this).parent().remove();
  99. const rules = $('span>span', obj), ruleText = [];
  100. for (const r of rules) {
  101. ruleText.push(r.innerText);
  102. }
  103. $('#preview', obj).text(ruleText.join(''));
  104. });
  105. });
  106. // 删除规则组件
  107. $('a', $('#ruleParts', obj)).bind('click', function () {
  108. const index = $('a', obj).index(this);
  109. codeRule.splice(index, 1);
  110. $(this).parent().remove();
  111. const rules = $('span>span', obj), ruleText = [];
  112. for (const r of rules) {
  113. console.log(r.innerText + ':1');
  114. ruleText.push(r.innerText);
  115. }
  116. $('#preview', obj).text('当前规则:' + ruleText.join(''));
  117. });
  118. }
  119. }
  120. $(document).ready(() => {
  121. autoFlashHeight();
  122. // 新增中间计量 modal显示
  123. $('#add-mj').on('show.bs.modal', function() {
  124. getNewMeasureCode();
  125. if ($('#wlist').children.length === 0) {
  126. $('#addCancel').hide();
  127. } else {
  128. $('#addCancel').show();
  129. }
  130. $('#mj-code').removeClass('is-invalid');
  131. });
  132. // 新增中间计量--刷新编号
  133. $('#autoCode').click(getNewMeasureCode);
  134. // 添加中间计量 确定
  135. $('#addOk').click(function () {
  136. const data = {
  137. code: $('#mj-code').val(),
  138. date: $('#mj-date').val(),
  139. };
  140. postData('/measure/add', data, function (rst) {
  141. $('#mj-code').removeClass('is-invalid');
  142. $('#mj-add').modal('hide');
  143. $('#wlist').append(getMeasureWorkHtml(rst));
  144. }, function () {
  145. $('#mj-code').addClass('is-invalid');
  146. $('#mj-Hint').show();
  147. });
  148. });
  149. // 首次进入设置
  150. if (!codeRule || codeRule.length === 0) {
  151. codeRule = [];
  152. const firstSet = new codeRuleSet($('div.modal-body', '#first'));
  153. // 确认规则上传服务器
  154. $('#setRule', '#first').bind('click', function () {
  155. const data = {
  156. rule: ruleType,
  157. data: JSON.stringify(codeRule),
  158. };
  159. postData('/tender/rule', data, function () {
  160. $('#first').modal('hide');
  161. $('#add-mj').modal('show');
  162. });
  163. });
  164. $('#first').modal('show');
  165. } else if ($('#wlist').children.length === 0) {
  166. $('#add-mj').modal('show');
  167. }
  168. // 中间计量设置页面
  169. const obj = $('#rule');
  170. console.log(obj);
  171. const ruleSet = new codeRuleSet(obj);
  172. // 编辑
  173. $('a[name=edit]').bind('click', function () {
  174. const tr = $(this).parent().parent();
  175. const mid = tr.attr('mid');
  176. const code = tr.attr('code');
  177. // 调整删除页面
  178. $('#delOk').attr('mid', mid);
  179. $('#code', $('#del')).text(code);
  180. // 调整编辑页面
  181. const edit = $('#edit');
  182. edit.attr('mid', mid);
  183. $('#codeEdit').val(code);
  184. $('[data-target=#del]').text('删除 ' + code);
  185. edit.modal('show');
  186. });
  187. // 编辑 确定
  188. $('#editOk').click(function () {
  189. const data = {
  190. mid: $('#edit').attr('mid'),
  191. code: $('#codeEdit').val()
  192. }
  193. postData('/measure/modify', data, function (m) {
  194. const tr = $('tr[mid= ' + m.mid + ']');
  195. const tds = tr.children();
  196. tds[0].text(m.code);
  197. tds[2].text(moment(m.in_time).format('YYYYMM'));
  198. });
  199. });
  200. // 删除 确定
  201. $('#delOk').click(function () {
  202. const data = {
  203. mid: $(this).attr('mid'),
  204. }
  205. postData('/measure/delete', data, function (result) {
  206. if (result) {
  207. $('tr[mid= ' + data.mid + ']').remove();
  208. }
  209. })
  210. })
  211. });