version.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**
  2. * 版本管理相关js
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/28
  6. * @version
  7. */
  8. $(document).ready(function() {
  9. let isAdding = false;
  10. let model = '';
  11. let section = 'bill';
  12. let id = $("#version-id").val();
  13. // 计价规则页面初始化数据
  14. if ($("#section").length > 0) {
  15. initVersion();
  16. }
  17. // 计价类型选择
  18. $(".nav-tabs li > a").click(function() {
  19. section = $(this).attr("aria-controls");
  20. });
  21. // 新增版本
  22. $("#add-version").click(function() {
  23. try {
  24. let [name, standardBill, rationLib, standardBillString, rationLibString] = getAndValidData(model);
  25. let url = '/version/add';
  26. if (model === 'all') {
  27. // 新增版本操作
  28. $.ajax({
  29. url: url,
  30. type: 'post',
  31. data: {name: name},
  32. error: function() {
  33. isAdding = false;
  34. },
  35. beforeSend: function() {
  36. isAdding = true;
  37. },
  38. success: function(response) {
  39. isAdding = false;
  40. if (response.err === 0) {
  41. window.location.reload();
  42. } else {
  43. let msg = response.msg === undefined ? '未知错误' : response.msg;
  44. alert(msg);
  45. }
  46. }
  47. });
  48. } else {
  49. // 新增标准清单/定额库
  50. let addLib = {
  51. name: '',
  52. id: ''
  53. };
  54. switch (model) {
  55. case 'bill':
  56. addLib.name = standardBillString;
  57. addLib.id = standardBill;
  58. break;
  59. case 'ration':
  60. addLib.name = rationLibString;
  61. addLib.id = rationLib;
  62. break;
  63. }
  64. // 判断是否有重复的数据
  65. if ($("input:hidden[name='"+ model +"_lib'][data-id='"+ addLib.id +"']").length > 0) {
  66. alert('重复添加数据!');
  67. return false;
  68. }
  69. let removeHtml = '<a class="pull-right text-danger remove-lib" data-model="bill" ' +
  70. 'title="移除"><span class="glyphicon glyphicon-remove"></span></a>';
  71. let tmpHtml = '<p class="form-control-static">' + removeHtml + addLib.name +
  72. '<input type="hidden" data-id="'+ addLib.id +'" name=\'' + model + '_lib\' value=\'' + JSON.stringify(addLib) + '\'>' + '</p>';
  73. $("." + model + "-list").append(tmpHtml);
  74. $('#addversion').modal('hide');
  75. }
  76. } catch (error) {
  77. alert(error);
  78. }
  79. });
  80. // 新增计价规则
  81. $("#add-valuation").click(function() {
  82. try {
  83. if (id === '') {
  84. throw '页面数据有误';
  85. }
  86. let name = $("input[name='valuation_name']").val();
  87. if (name === '') {
  88. throw '请填写计价规则名称';
  89. }
  90. $.ajax({
  91. url: '/version/add-valuation',
  92. type: 'post',
  93. data: {name: name, id: id, section: section},
  94. error: function() {
  95. isAdding = false;
  96. },
  97. beforeSend: function() {
  98. isAdding = true;
  99. },
  100. success: function(response) {
  101. isAdding = false;
  102. if (response.err === 0) {
  103. window.location.reload();
  104. } else {
  105. let msg = response.msg === undefined ? '未知错误' : response.msg;
  106. alert(msg);
  107. }
  108. }
  109. });
  110. } catch (error) {
  111. alert(error);
  112. return false;
  113. }
  114. });
  115. // 选择省份后读取数据
  116. $("select[name='standard_bill_province'],select[name='ration_lib_province']").change(function() {
  117. let name = $(this).attr('name');
  118. let billListData = billList === undefined ? [] : JSON.parse(billList);
  119. let rationLibData = rationList === undefined ? [] : JSON.parse(rationList);
  120. if (billListData.length <= 0 || rationLibData.length <= 0) {
  121. return false;
  122. }
  123. let sourceData = name === 'standard_bill_province' ? billListData : rationLibData;
  124. let selectedId = $(this).val();
  125. if (sourceData[selectedId] === undefined) {
  126. return false;
  127. }
  128. let defaultString = name === 'standard_bill_province' ? '请选择标准清单' : '请选择定额库';
  129. let html = '<option value="">' + defaultString + '</option>';
  130. for(let tmp of sourceData[selectedId]) {
  131. let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
  132. html += tmpHtml;
  133. }
  134. // 渲染
  135. let targetSelector = name === 'standard_bill_province' ? $("select[name='standard_bill']") : $("select[name='ration_lib']");
  136. targetSelector.children('option').remove();
  137. targetSelector.html(html);
  138. });
  139. // 添加
  140. $(".add-version").click(function() {
  141. model = $(this).data('model');
  142. switch (model) {
  143. case 'all':
  144. $("#name-area").show();
  145. $("#bill-area").hide();
  146. $("#ration-area").hide();
  147. $("#add-version-title").text('添加新版本');
  148. break;
  149. case 'bill':
  150. $("#name-area").hide();
  151. $("#bill-area").show();
  152. $("#ration-area").hide();
  153. $("#add-version-title").text('添加标准清单');
  154. break;
  155. case 'ration':
  156. $("#name-area").hide();
  157. $("#bill-area").hide();
  158. $("#ration-area").show();
  159. $("#add-version-title").text('添加定额库');
  160. break;
  161. }
  162. $("#addversion").modal('show');
  163. });
  164. // 保存计价规则
  165. $("#save-valuation").click(function() {
  166. if (validValuation()) {
  167. $("form").submit();
  168. }
  169. });
  170. // 移除操作
  171. $(".bill-list, .ration-list").on("click", ".remove-lib", function() {
  172. $(this).parent().remove();
  173. });
  174. // 计价规则启用/禁止
  175. $(".enable").click(function() {
  176. let goingChangeStatus = switchChange($(this));
  177. let id = $(this).data('id');
  178. if (id === undefined || id === '' || isAdding) {
  179. return false;
  180. }
  181. $.ajax({
  182. url: '/version/valuation/' + section + '/enable',
  183. type: 'post',
  184. dataType: "json",
  185. data: {id: id, enable: goingChangeStatus},
  186. error: function() {
  187. isAdding = false;
  188. switchChange($(this));
  189. },
  190. beforeSend: function() {
  191. isAdding = true;
  192. },
  193. success: function(response) {
  194. isAdding = false;
  195. if (response.err !== 0) {
  196. switchChange($(this));
  197. alert('更改失败');
  198. }
  199. }
  200. });
  201. });
  202. });
  203. /**
  204. * 初始化
  205. *
  206. * @return {void|boolean}
  207. */
  208. function initVersion() {
  209. if (province === undefined) {
  210. alert('初始化失败!');
  211. return false;
  212. }
  213. province = JSON.parse(province);
  214. if (province.length <= 0) {
  215. alert('省份数据加载失败!');
  216. return false;
  217. }
  218. let billProvinceElement = $("select[name='standard_bill_province']");
  219. let rationProvinceElement = $("select[name='ration_lib_province']");
  220. let html = '';
  221. for (let tmp of province) {
  222. let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
  223. html += tmpHtml;
  224. }
  225. billProvinceElement.children('option').first().after(html);
  226. rationProvinceElement.children('option').first().after(html);
  227. }
  228. /**
  229. * 校验数据
  230. *
  231. * @param {String} model
  232. * @return {Array}
  233. */
  234. function getAndValidData(model) {
  235. let name = $("input[name='version_name']").val();
  236. let standardBill = $("select[name='standard_bill']").children("option:selected").val();
  237. let rationLib = $("select[name='ration_lib']").children("option:selected").val();
  238. if (name === '' && model === 'all') {
  239. throw '版本名字不能为空';
  240. }
  241. if ( model === 'bill' && (standardBill === '' || standardBill === undefined)) {
  242. throw '请选择标准清单库';
  243. }
  244. if (model === 'ration' && (rationLib === '' || rationLib === undefined)) {
  245. throw '请选择定额库';
  246. }
  247. let standardBillString = $("select[name='standard_bill']").children("option:selected").text();
  248. let rationLibString = $("select[name='ration_lib']").children("option:selected").text();
  249. return [name, standardBill, rationLib, standardBillString, rationLibString];
  250. }
  251. /**
  252. * 验证计价规则数据
  253. *
  254. * @return {boolean}
  255. */
  256. function validValuation() {
  257. let result = false;
  258. try {
  259. let valuationName = $("input[name='name']").val();
  260. if (valuationName === '') {
  261. throw '请填写计价规则名称';
  262. }
  263. let engineering = $("select[name='engineering']").val();
  264. if (engineering === '' || engineering <= 0) {
  265. throw '请选择工程专业';
  266. }
  267. if ($("input:hidden[name='bill_lib']").length <= 0) {
  268. throw '请添加标准清单';
  269. }
  270. if ($("input:hidden[name='ration_lib']").length <= 0) {
  271. throw '请添加定额库';
  272. }
  273. result = true;
  274. } catch (error) {
  275. alert(error);
  276. result = false;
  277. }
  278. return result;
  279. }
  280. /**
  281. * 切换switch效果
  282. *
  283. * @param {Object} element
  284. * @return {boolean}
  285. */
  286. function switchChange(element) {
  287. // 第一个元素判断当前的状态
  288. let firstButton = element.children("button").first();
  289. let secondButton = element.children("button").eq(1);
  290. let currentStatus = firstButton.is(":disabled");
  291. if (currentStatus) {
  292. // 当前为true切换到false
  293. firstButton.removeClass('btn-success').removeClass('disabled').addClass('btn-default').removeAttr("disabled");
  294. firstButton.text('开启');
  295. secondButton.removeClass("btn-default").addClass("btn-danger").addClass("disabled").attr("disabled", "disabled");
  296. secondButton.text('已禁用');
  297. } else {
  298. // 当前false切换到true
  299. firstButton.removeClass("btn-default").addClass("btn-success").addClass("disabled").attr("disabled", "disabled");
  300. firstButton.text('已开启');
  301. secondButton.removeClass('btn-danger').removeClass('disabled').addClass('btn-default').removeAttr("disabled");
  302. secondButton.text('禁用');
  303. }
  304. return !currentStatus;
  305. }