version.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. $("#release").click(function() {
  204. let id = $(this).data("id");
  205. let status = $(this).data("status");
  206. status = parseInt(status);
  207. if (isAdding || id === '' || isNaN(status)) {
  208. return false;
  209. }
  210. $.ajax({
  211. url: '/version/release',
  212. type: 'post',
  213. data: {id: id, status: status},
  214. dataType: "json",
  215. error: function() {
  216. isAdding = false;
  217. },
  218. beforeSend: function() {
  219. isAdding = true;
  220. },
  221. success: function(response) {
  222. isAdding = false;
  223. if (response.err === 0) {
  224. window.location.reload();
  225. } else {
  226. let msg = response.msg === undefined ? "未知错误" : response.msg;
  227. alert(msg);
  228. }
  229. }
  230. });
  231. });
  232. });
  233. /**
  234. * 初始化
  235. *
  236. * @return {void|boolean}
  237. */
  238. function initVersion() {
  239. if (province === undefined) {
  240. alert('初始化失败!');
  241. return false;
  242. }
  243. province = JSON.parse(province);
  244. if (province.length <= 0) {
  245. alert('省份数据加载失败!');
  246. return false;
  247. }
  248. let billProvinceElement = $("select[name='standard_bill_province']");
  249. let rationProvinceElement = $("select[name='ration_lib_province']");
  250. let html = '';
  251. for (let tmp of province) {
  252. let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
  253. html += tmpHtml;
  254. }
  255. billProvinceElement.children('option').first().after(html);
  256. rationProvinceElement.children('option').first().after(html);
  257. }
  258. /**
  259. * 校验数据
  260. *
  261. * @param {String} model
  262. * @return {Array}
  263. */
  264. function getAndValidData(model) {
  265. let name = $("input[name='version_name']").val();
  266. let standardBill = $("select[name='standard_bill']").children("option:selected").val();
  267. let rationLib = $("select[name='ration_lib']").children("option:selected").val();
  268. if (name === '' && model === 'all') {
  269. throw '版本名字不能为空';
  270. }
  271. if ( model === 'bill' && (standardBill === '' || standardBill === undefined)) {
  272. throw '请选择标准清单库';
  273. }
  274. if (model === 'ration' && (rationLib === '' || rationLib === undefined)) {
  275. throw '请选择定额库';
  276. }
  277. let standardBillString = $("select[name='standard_bill']").children("option:selected").text();
  278. let rationLibString = $("select[name='ration_lib']").children("option:selected").text();
  279. return [name, standardBill, rationLib, standardBillString, rationLibString];
  280. }
  281. /**
  282. * 验证计价规则数据
  283. *
  284. * @return {boolean}
  285. */
  286. function validValuation() {
  287. let result = false;
  288. try {
  289. let valuationName = $("input[name='name']").val();
  290. if (valuationName === '') {
  291. throw '请填写计价规则名称';
  292. }
  293. let engineering = $("select[name='engineering']").val();
  294. if (engineering === '' || engineering <= 0) {
  295. throw '请选择工程专业';
  296. }
  297. if ($("input:hidden[name='bill_lib']").length <= 0) {
  298. throw '请添加标准清单';
  299. }
  300. if ($("input:hidden[name='ration_lib']").length <= 0) {
  301. throw '请添加定额库';
  302. }
  303. result = true;
  304. } catch (error) {
  305. alert(error);
  306. result = false;
  307. }
  308. return result;
  309. }
  310. /**
  311. * 切换switch效果
  312. *
  313. * @param {Object} element
  314. * @return {boolean}
  315. */
  316. function switchChange(element) {
  317. // 第一个元素判断当前的状态
  318. let firstButton = element.children("button").first();
  319. let secondButton = element.children("button").eq(1);
  320. let currentStatus = firstButton.is(":disabled");
  321. if (currentStatus) {
  322. // 当前为true切换到false
  323. firstButton.removeClass('btn-success').removeClass('disabled').addClass('btn-default').removeAttr("disabled");
  324. firstButton.text('开启');
  325. secondButton.removeClass("btn-default").addClass("btn-danger").addClass("disabled").attr("disabled", "disabled");
  326. secondButton.text('已禁用');
  327. } else {
  328. // 当前false切换到true
  329. firstButton.removeClass("btn-default").addClass("btn-success").addClass("disabled").attr("disabled", "disabled");
  330. firstButton.text('已开启');
  331. secondButton.removeClass('btn-danger').removeClass('disabled').addClass('btn-default').removeAttr("disabled");
  332. secondButton.text('禁用');
  333. }
  334. return !currentStatus;
  335. }