dk-multi-owner.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*******************************************************************************
  2. * Copyright(c) 2022 dongke All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:
  5. * 2.功能描述:组件-面板
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 于继渤 2024-03-20 1.00 新建
  9. *******************************************************************************/
  10. const util = require('@/utils/util.js')
  11. Component({
  12. /**
  13. * 组件的属性列表
  14. */
  15. properties: {
  16. // 数据源
  17. list: {
  18. type: Array,
  19. value: []
  20. },
  21. // 内容
  22. content: {
  23. type: Array,
  24. value: []
  25. },
  26. // 回显名称
  27. nameKey: {
  28. type: String
  29. },
  30. idKey: {
  31. type: String
  32. },
  33. //类型key
  34. typeKey: {
  35. type: String
  36. },
  37. },
  38. options: {
  39. },
  40. /**
  41. * 组件的初始数据
  42. */
  43. data: {
  44. },
  45. /**
  46. * 组件的方法列表
  47. */
  48. methods: {
  49. /**
  50. * @desc : checkBox选中/取消事件
  51. * @date : 2024/3/20 15:49
  52. * @author : 于继渤
  53. */
  54. onChangeFlag(event) {
  55. let allIsMain = false
  56. let index = event.currentTarget.dataset.index
  57. var _this = this
  58. var list = _this.data.list
  59. var content = _this.data.content
  60. let colSwitchList = content.filter(res => {
  61. return res.type == 'switch'
  62. })
  63. let commissionList = content.filter(res => {
  64. return res.type == 'commission'
  65. })
  66. let switchCode = null
  67. let commissionCode = null
  68. if (colSwitchList.length > 0) {
  69. switchCode = colSwitchList[0].code
  70. }
  71. if (commissionList.length > 0) {
  72. commissionCode = commissionList[0].code
  73. }
  74. // 设置点击的项目checkbox的值
  75. for (let i = 0; i < list.length; i++) {
  76. if (i == index) {
  77. list[i].checked = !list[i].checked
  78. break;
  79. }
  80. }
  81. if (list[index].checked) {
  82. var isMain = false;
  83. for (let i = 0; i < list.length; i++) {
  84. if (list[i][switchCode]) {
  85. isMain = true;
  86. break;
  87. }
  88. }
  89. list[index][switchCode] = !isMain;
  90. } else {
  91. if (switchCode) {
  92. list[index][switchCode] = false;
  93. }
  94. if (commissionCode) {
  95. list[index][commissionCode] = parseFloat(100).toFixed(2)
  96. }
  97. }
  98. //实现一个关闭出现一换个主业务员
  99. for (let i = 0; i < list.length; i++) {
  100. if (list[i][switchCode]) {
  101. allIsMain = true
  102. }
  103. }
  104. if (!allIsMain) {
  105. for (let i = 0; i < list.length; i++) {
  106. if (list[i].checked) {
  107. list[i][switchCode] = true
  108. break
  109. }
  110. }
  111. }
  112. this.calculationScale(list, commissionCode, switchCode)
  113. // 选择条数
  114. _this.setData({
  115. list: list
  116. })
  117. },
  118. /**
  119. * @desc : Switch切换事件
  120. * @date : 2024/3/20 15:49
  121. * @author : 于继渤
  122. */
  123. onChangeSwitch(e) {
  124. var _this = this
  125. var list = _this.data.list
  126. var index = e.target.dataset.index
  127. //如果没有选中就不能触发这个主从的勾选
  128. if (!list[index].checked) {
  129. return;
  130. }
  131. //获取需要需改的 key
  132. var content = _this.data.content
  133. let colSwitchList = content.filter(res => {
  134. return res.type == 'switch'
  135. })
  136. let switchCode = null
  137. if (colSwitchList.length > 0) {
  138. switchCode = colSwitchList[0].code
  139. }
  140. // 设置点击的项目checkbox的值
  141. for (let i = 0; i < list.length; i++) {
  142. if (i == index && list[i].checked) {
  143. list[i][switchCode] = !list[i][switchCode]
  144. continue;
  145. }
  146. list[i][switchCode] = false;
  147. }
  148. // 选择条数
  149. _this.setData({
  150. list: list,
  151. })
  152. },
  153. /**
  154. * @desc : 输入提成比例的
  155. * @date : 2024/3/20 15:49
  156. * @author : 于继渤
  157. */
  158. inputChange(e) {
  159. let list = this.data.list;
  160. let index = e.currentTarget.dataset.index;
  161. var content = this.data.content
  162. let commissionList = content.filter(res => {
  163. return res.type == 'commission'
  164. })
  165. let commissionCode = null
  166. if (commissionList.length > 0) {
  167. commissionCode = commissionList[0].code
  168. }
  169. if (commissionCode) {
  170. list[index][commissionCode] = e.detail.value
  171. }
  172. this.setData({
  173. list: list
  174. })
  175. },
  176. /**
  177. * @desc : 输入提成比例 离开焦点事件
  178. * @date : 2024/3/20 15:49
  179. * @author : 于继渤
  180. */
  181. inputBlurChange(e) {
  182. let value = null
  183. let index = e.currentTarget.dataset.index
  184. let id = e.currentTarget.dataset.id
  185. let list = this.data.list
  186. let valueBlur = e.detail.value
  187. if (Number(valueBlur) == 0 || Number(valueBlur)) {
  188. value = Number(valueBlur)
  189. if (Number(valueBlur) > 100) {
  190. value = Number(100)
  191. }
  192. if (Number(valueBlur) < 0) {
  193. value = Number(0)
  194. }
  195. }
  196. var content = this.data.content
  197. let commissionList = content.filter(res => {
  198. return res.type == 'commission'
  199. })
  200. let commissionCode = null
  201. let idKey = null
  202. if (commissionList.length > 0) {
  203. commissionCode = commissionList[0].code
  204. idKey = commissionList[0].idKey
  205. }
  206. if (commissionCode) {
  207. list[index][commissionCode] = value.toFixed(2)
  208. }
  209. this.setData({
  210. list,
  211. inputBlurChange: true
  212. })
  213. let number = Number(100 - value.toFixed(2))
  214. let showListTemp = []
  215. list.forEach(it => {
  216. if (id != it[idKey] && it.checked) {
  217. showListTemp.push(it)
  218. }
  219. })
  220. let allocationRatio = (number / showListTemp.length).toFixed(2)
  221. let allocationRatioTotal = 0
  222. //数据取余2为0
  223. if (number % showListTemp.length == 0) {
  224. //循环将计算好的百分比放入数据中
  225. for (let i in showListTemp) {
  226. showListTemp[i][commissionCode] = Number(allocationRatio).toFixed(2)
  227. }
  228. list.forEach(item1 => {
  229. showListTemp.forEach(item2 => {
  230. if (item1[idKey] == item2[idKey]) {
  231. item1[commissionCode] = Number(item2[commissionCode]).toFixed(2)
  232. }
  233. })
  234. })
  235. this.setData({
  236. list
  237. })
  238. } else {
  239. let showListTemp2 = [...showListTemp]
  240. let endList = showListTemp2.splice(-1)
  241. for (let i = 0; i < showListTemp2.length; i++) {
  242. allocationRatioTotal = allocationRatioTotal + Number(allocationRatio)
  243. showListTemp2[i][commissionCode] = Number(allocationRatio).toFixed(2)
  244. }
  245. if (endList[0]) {
  246. endList[0][commissionCode] = (number - allocationRatioTotal).toFixed(2)
  247. }
  248. list.forEach(item1 => {
  249. showListTemp2.forEach(item2 => {
  250. if (item1[idKey] == item2[idKey]) {
  251. item1[commissionCode] = item2[commissionCode]
  252. }
  253. })
  254. })
  255. this.setData({
  256. list
  257. })
  258. }
  259. },
  260. /**
  261. * @desc : 选择计算事件
  262. * @date : 2024/3/20 15:49
  263. * @author : 于继渤
  264. */
  265. calculationScale(list, commissionCode) {
  266. list = list.filter((item) => {
  267. return item.checked == true
  268. })
  269. let allocationRatio = (100 / list.length).toFixed(2)
  270. let allocationRatioTotal = 0
  271. //数据取余2为0
  272. if (100 % list.length == 0) {
  273. //循环将计算好的百分比放入数据中
  274. for (let i in list) {
  275. list[i][commissionCode] = Number(allocationRatio).toFixed(2)
  276. }
  277. } else { //数据取余2不为0
  278. //获取最后选中checkbox数组中最后一个元素
  279. let endList = list.splice(-1)
  280. for (let i = 0; i < list.length; i++) {
  281. allocationRatioTotal = allocationRatioTotal + Number(allocationRatio)
  282. list[i][commissionCode] = Number(allocationRatio).toFixed(2)
  283. }
  284. if (endList[0]) {
  285. endList[0][commissionCode] = (100 - allocationRatioTotal).toFixed(2)
  286. }
  287. }
  288. },
  289. //设置控件信息
  290. setDataFromPage(data) {
  291. this.setData({
  292. list:data
  293. })
  294. },
  295. /**
  296. * 确定
  297. * @param {*} e
  298. */
  299. btnOK(e) {
  300. var content = this.data.content
  301. let colSwitchList = content.filter(res => {
  302. return res.type == 'switch'
  303. })
  304. let commissionList = content.filter(res => {
  305. return res.type == 'commission'
  306. })
  307. let switchCode = null
  308. let commissionCode = null
  309. if (colSwitchList.length > 0) {
  310. switchCode = colSwitchList[0].code
  311. }
  312. if (commissionList.length > 0) {
  313. commissionCode = commissionList[0].code
  314. }
  315. var indexswitch = 0;
  316. // 判断是否有选择的checkbox
  317. var allcheck = false;
  318. var allswitch = false;
  319. var list = this.data.list;
  320. for (let i = 0; i < list.length; i++) {
  321. if (list[i].checked) {
  322. allcheck = true;
  323. break;
  324. }
  325. }
  326. // 校验
  327. if (!allcheck) {
  328. util.showToast('请至少选择一条数据');
  329. return;
  330. }
  331. if (switchCode) {
  332. for (let i = 0; i < list.length; i++) {
  333. if (list[i][switchCode]) {
  334. allswitch = true;
  335. indexswitch = i;
  336. break;
  337. }
  338. }
  339. // 校验
  340. if (!allswitch) {
  341. util.showToast('请选择' + colSwitchList[0].title);
  342. return;
  343. }
  344. }
  345. if (commissionCode) {
  346. let commission = Number(0)
  347. for (let i = 0; i < list.length; i++) {
  348. if (list[i].checked) {
  349. commission += Number(list[i][commissionCode])
  350. }
  351. }
  352. if (commission !== 100) {
  353. util.showToast('提成比例不足 100');
  354. return;
  355. }
  356. let sum = 0
  357. list.forEach(res => {
  358. if (res.checked) {
  359. sum += parseFloat(res[commissionCode])
  360. }
  361. })
  362. if (sum > 100 || sum < 100) {
  363. util.showToast('提成比率不正确');
  364. return;
  365. }
  366. }
  367. let nameKey = this.data.nameKey
  368. let idKey = this.data.idKey
  369. let typeKey = this.data.typeKey
  370. let names = []
  371. let ids = []
  372. let tableDataList = []
  373. let main = {}
  374. list.forEach(item => {
  375. if (item.checked) {
  376. item.allocationRatio = commissionCode ? item[commissionCode] : null
  377. item.ownerFlag = switchCode ? item[switchCode] : null
  378. tableDataList.push(item)
  379. if (nameKey) {
  380. names.push(item[nameKey])
  381. }
  382. if (idKey) {
  383. ids.push(item[idKey])
  384. }
  385. if (item[switchCode]) {
  386. main = item
  387. }
  388. }
  389. })
  390. // 给父页面传递数据
  391. let obj = {
  392. main: main,
  393. list: tableDataList
  394. }
  395. obj['item'] = {
  396. text: names,
  397. value: ids
  398. }
  399. this.triggerEvent("toMulti", obj)
  400. },
  401. }
  402. })