addMusic.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. import { defineComponent, h, onMounted, reactive, ref } from 'vue'
  2. import SaveForm from '@components/save-form'
  3. import {
  4. DataTableColumns,
  5. DataTableRowKey,
  6. NButton,
  7. NCascader,
  8. NDataTable,
  9. NFormItem,
  10. NIcon,
  11. NImage,
  12. NInput,
  13. NInputNumber,
  14. NSelect,
  15. NSpace,
  16. NStep,
  17. NSteps,
  18. useDialog,
  19. useMessage
  20. } from 'naive-ui'
  21. import Pagination from '@components/pagination'
  22. import { getMapValueByKey, getSelectDataFromObj } from '@/utils/objectUtil'
  23. import { musicSheetSourceType, musicSheetType } from '@/utils/constant'
  24. import {musicSheetApplicationExtendCategoryList, musicSheetApplicationExtendSaveBatch, musicSheetPage} from '@views/music-library/api'
  25. import deepClone from '@/utils/deep.clone'
  26. import { getOwnerName } from '@views/music-library/musicUtil'
  27. import TheTooltip from '@/components/TheTooltip'
  28. export default defineComponent({
  29. name: 'kt-addMusic',
  30. props: {
  31. appId: {
  32. type: String,
  33. required: true
  34. },
  35. subjectList: {
  36. type: Array,
  37. default: () => []
  38. },
  39. musicSheetCategories: {
  40. type: Array,
  41. default: () => []
  42. }
  43. },
  44. emits: ['close', 'getList'],
  45. setup(props, { slots, attrs, emit }) {
  46. const dialogs = useDialog()
  47. const message = useMessage()
  48. const btnLoading = ref(false)
  49. const state = reactive({
  50. loading: false,
  51. pagination: {
  52. page: 1,
  53. rows: 5,
  54. pageTotal: 0
  55. },
  56. stepPagination: {
  57. page: 1,
  58. rows: 5,
  59. pageTotal: 0
  60. },
  61. searchForm: {
  62. keyword: null,
  63. musicSheetType: null,
  64. subjectId: null,
  65. sourceType: null
  66. },
  67. subjectList: [] as any,
  68. showAdd: false,
  69. currentStep: 1,
  70. dataList: [],
  71. selectRowData: [] as any, // 选择的数据列表
  72. musicSheetCategories: [] as any,
  73. startSortNum: null as any, // 排序起始值
  74. projectMusicCategoryId: null as any // 曲目分类ID
  75. })
  76. onMounted(async () => {
  77. state.loading = true
  78. state.subjectList = props.subjectList
  79. // state.musicSheetCategories = props.musicSheetCategories
  80. //加载曲目分类列表
  81. try {
  82. const {data} = await musicSheetApplicationExtendCategoryList({
  83. applicationIds: props.appId
  84. })
  85. if (data && data.length > 0) {
  86. state.musicSheetCategories = data[0].musicSheetCategories
  87. }
  88. } catch {
  89. }
  90. await getList()
  91. })
  92. const getList = async () => {
  93. try {
  94. state.loading = true
  95. const { data } = await musicSheetPage({
  96. ...state.pagination,
  97. ...state.searchForm,
  98. addAppId: props.appId
  99. })
  100. state.pagination.pageTotal = Number(data.total)
  101. state.dataList = data.rows || []
  102. } catch {}
  103. state.loading = false
  104. }
  105. const saveForm = ref()
  106. const onSearch = () => {
  107. saveForm.value?.submit()
  108. }
  109. const onBtnReset = () => {
  110. saveForm.value?.reset()
  111. }
  112. const onSubmit = () => {
  113. state.pagination.page = 1
  114. getList()
  115. }
  116. const onSave = async () => {
  117. if (state.selectRowData.length == 0) {
  118. message.error('未选择曲目')
  119. return
  120. }
  121. const params = [] as any[]
  122. for (let i = 0; i < state.selectRowData.length; i++) {
  123. const item = state.selectRowData[i]
  124. if (!item.projectMusicCategoryId) {
  125. message.error('乐谱教材不能为空')
  126. return
  127. }
  128. if (!item.sortNo) {
  129. item.sortNo = 0
  130. }
  131. params.push({
  132. ...item,
  133. musicSheetId: item.id,
  134. musicSheetCategoryId: item.projectMusicCategoryId,
  135. applicationId: props.appId,
  136. id: null
  137. })
  138. }
  139. btnLoading.value = true
  140. try {
  141. const res = (await musicSheetApplicationExtendSaveBatch(params)) as any
  142. if (res && res.code == '200') {
  143. message.success(`添加成功`)
  144. emit('getList')
  145. emit('close')
  146. }
  147. } catch (err) {}
  148. btnLoading.value = false
  149. }
  150. const columnsField = [
  151. {
  152. type: 'selection'
  153. },
  154. {
  155. title: '曲目编号',
  156. key: 'id'
  157. },
  158. {
  159. title: '封面图',
  160. key: 'titleImg',
  161. render(row: any) {
  162. return <NImage width={40} height={40} src={row.musicCover} />
  163. }
  164. },
  165. {
  166. title: '声部',
  167. key: 'subjectNames'
  168. },
  169. {
  170. title: '曲目名称',
  171. key: 'name'
  172. },
  173. {
  174. title: '音乐人',
  175. key: 'composer'
  176. },
  177. {
  178. title: '曲目类型',
  179. key: 'musicSheetType',
  180. render: (row: any) => {
  181. return (
  182. <div>
  183. {getMapValueByKey(row.musicSheetType, new Map(Object.entries(musicSheetType)))}
  184. </div>
  185. )
  186. }
  187. },
  188. {
  189. title: '作者属性',
  190. key: 'sourceType',
  191. render(row: any) {
  192. return getMapValueByKey(row.sourceType, new Map(Object.entries(musicSheetSourceType)))
  193. }
  194. },
  195. {
  196. title: '所属人',
  197. key: 'userName',
  198. render: (row: any) => {
  199. return <TheTooltip content={getOwnerName(row.musicSheetExtend, row.sourceType)} />
  200. }
  201. }
  202. ]
  203. const columns = (): any => {
  204. return columnsField
  205. }
  206. const stepColumns = (): DataTableColumns => {
  207. const field = deepClone(columnsField)
  208. field.splice(0, 1)
  209. field.push({
  210. title(column: any) {
  211. return (
  212. <NSpace>
  213. 乐谱教材
  214. <NButton
  215. type="primary"
  216. size="small"
  217. text
  218. onClick={() => {
  219. dialogs.create({
  220. title: '请选择乐谱教材',
  221. showIcon: false,
  222. content: () => {
  223. return h(
  224. 'div',
  225. {
  226. class: 'flex flex-col justify-center items-center text-14px'
  227. },
  228. [
  229. // icon
  230. h(NCascader, {
  231. onUpdateValue(v) {
  232. state.projectMusicCategoryId = v
  233. },
  234. valueField: 'id',
  235. labelField: 'name',
  236. childrenField: 'children',
  237. placeholderField: '请选择乐谱教材',
  238. options: state.musicSheetCategories
  239. })
  240. ]
  241. )
  242. },
  243. positiveText: '确定',
  244. negativeText: '取消',
  245. onPositiveClick: () => {
  246. for (let i = 0; i < state.selectRowData.length; i++) {
  247. const item = state.selectRowData[i]
  248. item.projectMusicCategoryId = state.projectMusicCategoryId
  249. }
  250. }
  251. })
  252. }}
  253. >
  254. <NIcon size={15} style="padding-left: 5px;margin-top:4px">
  255. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
  256. <path d="M2 26h28v2H2z" fill="currentColor"></path>
  257. <path
  258. d="M25.4 9c.8-.8.8-2 0-2.8l-3.6-3.6c-.8-.8-2-.8-2.8 0l-15 15V24h6.4l15-15zm-5-5L24 7.6l-3 3L17.4 7l3-3zM6 22v-3.6l10-10l3.6 3.6l-10 10H6z"
  259. fill="currentColor"
  260. ></path>
  261. </svg>
  262. </NIcon>
  263. </NButton>
  264. </NSpace>
  265. )
  266. },
  267. key: 'projectMusicCategoryId',
  268. fixed: 'right',
  269. width: 200,
  270. render: (row: any) => {
  271. // })
  272. return (
  273. <NCascader
  274. valueField="id"
  275. labelField="name"
  276. children-field="children"
  277. placeholder="请选择曲目分类"
  278. value={row.projectMusicCategoryId}
  279. options={state.musicSheetCategories}
  280. onUpdateValue={(value: any) => {
  281. row.projectMusicCategoryId = value
  282. }}
  283. clearable
  284. />
  285. )
  286. }
  287. })
  288. field.push({
  289. title(column: any) {
  290. return (
  291. <NSpace>
  292. 排序
  293. <NButton
  294. type="primary"
  295. size="small"
  296. text
  297. onClick={() => {
  298. dialogs.create({
  299. title: '请输入排序起始值',
  300. showIcon: false,
  301. content: () => {
  302. return h(
  303. 'div',
  304. {
  305. class: 'flex flex-col justify-center items-center text-14px'
  306. },
  307. [
  308. // icon
  309. h(NInputNumber, {
  310. onUpdateValue(v) {
  311. state.startSortNum = v
  312. },
  313. min: 0,
  314. max: 9999
  315. })
  316. ]
  317. )
  318. },
  319. positiveText: '确定',
  320. negativeText: '取消',
  321. onPositiveClick: () => {
  322. if (state.startSortNum) {
  323. for (let i = 0; i < state.selectRowData.length; i++) {
  324. const item = state.selectRowData[i]
  325. item.sortNo = state.startSortNum + i
  326. }
  327. }
  328. }
  329. })
  330. }}
  331. >
  332. <NIcon size={15} style="padding-left: 5px;margin-top:4px">
  333. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
  334. <path d="M2 26h28v2H2z" fill="currentColor"></path>
  335. <path
  336. d="M25.4 9c.8-.8.8-2 0-2.8l-3.6-3.6c-.8-.8-2-.8-2.8 0l-15 15V24h6.4l15-15zm-5-5L24 7.6l-3 3L17.4 7l3-3zM6 22v-3.6l10-10l3.6 3.6l-10 10H6z"
  337. fill="currentColor"
  338. ></path>
  339. </svg>
  340. </NIcon>
  341. </NButton>
  342. </NSpace>
  343. )
  344. },
  345. key: 'sortNo',
  346. fixed: 'right',
  347. width: 150,
  348. render: (row: any) => {
  349. return h(NInputNumber, {
  350. value: row.sortNo,
  351. min: 0,
  352. max: 9999,
  353. defaultValue: 0,
  354. onUpdateValue(value: any) {
  355. row.sortNo = value
  356. }
  357. })
  358. }
  359. })
  360. field.push({
  361. title: '操作',
  362. key: 'operation',
  363. fixed: 'right',
  364. render(row: any) {
  365. return (
  366. <NSpace>
  367. <NButton
  368. type="primary"
  369. size="small"
  370. text
  371. //v-auth="musicSheet/update1602302618558099458"
  372. onClick={() => {
  373. dialogs.warning({
  374. title: '警告',
  375. content: `是否删除该数据?`,
  376. positiveText: '确定',
  377. negativeText: '取消',
  378. onPositiveClick: async () => {
  379. try {
  380. const index = state.selectRowData.findIndex((item: any) => {
  381. if (item.id == row.id) {
  382. return true
  383. }
  384. })
  385. if (index > -1) {
  386. state.selectRowData.splice(index, 1)
  387. }
  388. const index1 = checkedRowKeysRef.value.findIndex((item: any) => {
  389. if (item == row.id) {
  390. return true
  391. }
  392. })
  393. if (index1 > -1) {
  394. checkedRowKeysRef.value.splice(index, 1)
  395. }
  396. } catch {}
  397. }
  398. })
  399. }}
  400. >
  401. 移除
  402. </NButton>
  403. </NSpace>
  404. )
  405. }
  406. })
  407. return field
  408. }
  409. const checkedRowKeysRef = ref<DataTableRowKey[]>([])
  410. const handleCheck = (rowKeys: DataTableRowKey[]) => {
  411. checkedRowKeysRef.value = rowKeys
  412. // 添加行更新值
  413. state.dataList.forEach((next: any) => {
  414. if (checkedRowKeysRef.value.includes(next.id)) {
  415. const find = state.selectRowData.find((row: any) => {
  416. return row.id === next.id
  417. })
  418. if (!find) {
  419. state.selectRowData.push(next)
  420. }
  421. }
  422. })
  423. // 去掉行更新值
  424. state.selectRowData = state.selectRowData.filter((next: any) => {
  425. return checkedRowKeysRef.value.includes(next.id)
  426. })
  427. }
  428. return () => {
  429. return (
  430. <div class="system-menu-container">
  431. <NSpace vertical size="medium">
  432. <NSteps
  433. current={state.currentStep}
  434. // onUpdateCurrent={()=>{
  435. // state.currentStep = val
  436. // }}
  437. style={'margin-bottom: 10px;margin-top: 10px'}
  438. >
  439. <NStep title="选择曲目" description=""></NStep>
  440. <NStep title="设置曲目信息" description=""></NStep>
  441. </NSteps>
  442. </NSpace>
  443. {state.currentStep === 1 && (
  444. <div class="system-menu-container">
  445. <SaveForm
  446. ref={saveForm}
  447. model={state.searchForm}
  448. onSubmit={onSubmit}
  449. // saveKey="cooleshow-edu-addMusic"
  450. onSetModel={(val: any) => (state.searchForm = val)}
  451. >
  452. <NFormItem label="关键词" path="keyword">
  453. <NInput
  454. v-model:value={state.searchForm.keyword}
  455. placeholder="请输入曲目名称/编号"
  456. clearable
  457. />
  458. </NFormItem>
  459. <NFormItem label="曲目类型" path="musicSheetType">
  460. <NSelect
  461. placeholder="请选择曲目类型"
  462. v-model:value={state.searchForm.musicSheetType}
  463. options={getSelectDataFromObj(musicSheetType)}
  464. clearable
  465. />
  466. </NFormItem>
  467. <NFormItem label="声部" path="musicSubject">
  468. <NSelect
  469. placeholder="请选择声部"
  470. v-model:value={state.searchForm.subjectId}
  471. options={state.subjectList}
  472. clearable
  473. />
  474. </NFormItem>
  475. <NFormItem label="曲目来源" path="sourceType">
  476. <NSelect
  477. placeholder="请选择曲目来源"
  478. v-model:value={state.searchForm.sourceType}
  479. options={getSelectDataFromObj(musicSheetSourceType)}
  480. // onUpdateValue={async (value: any) => {
  481. // }}
  482. clearable
  483. />
  484. </NFormItem>
  485. <NFormItem>
  486. <NSpace>
  487. <NButton type="primary" onClick={onSearch}>
  488. 搜索
  489. </NButton>
  490. <NButton type="default" onClick={onBtnReset}>
  491. 重置
  492. </NButton>
  493. </NSpace>
  494. </NFormItem>
  495. </SaveForm>
  496. <p style={{ paddingBottom: '12px' }}>
  497. 你选择了<span style={'color:red;padding:0 8px'}>{state.selectRowData.length}</span>
  498. 条曲目
  499. </p>
  500. <NDataTable
  501. loading={state.loading}
  502. columns={columns()}
  503. data={state.dataList}
  504. rowKey={(row: any) => row.id}
  505. onUpdateCheckedRowKeys={handleCheck}
  506. ></NDataTable>
  507. <Pagination
  508. v-model:page={state.pagination.page}
  509. v-model:pageSize={state.pagination.rows}
  510. v-model:pageTotal={state.pagination.pageTotal}
  511. onList={getList}
  512. sync
  513. // saveKey="cooleshow-edu-addMusic"
  514. ></Pagination>
  515. </div>
  516. )}
  517. {state.currentStep === 2 && (
  518. <div class="system-menu-container" style={'margin-top: 15px;'}>
  519. <NDataTable
  520. loading={state.loading}
  521. columns={stepColumns()}
  522. data={state.selectRowData}
  523. rowKey={(row: any) => row.id}
  524. maxHeight={500}
  525. ></NDataTable>
  526. </div>
  527. )}
  528. <NSpace justify="end" style={'margin-top:10px'}>
  529. <NButton
  530. type="default"
  531. onClick={() => {
  532. if (state.currentStep > 1) {
  533. state.currentStep = state.currentStep - 1
  534. } else {
  535. emit('close')
  536. }
  537. }}
  538. >
  539. {state.currentStep === 1 ? '取消' : '上一步'}
  540. </NButton>
  541. <NButton
  542. type="primary"
  543. onClick={() => {
  544. if (state.currentStep < 2) {
  545. if (state.selectRowData.length == 0) {
  546. message.warning('请选择曲目')
  547. return
  548. }
  549. state.currentStep = state.currentStep + 1
  550. } else {
  551. onSave()
  552. }
  553. }}
  554. loading={btnLoading.value}
  555. disabled={btnLoading.value}
  556. >
  557. {state.currentStep === 2 ? '确定' : '下一步'}
  558. </NButton>
  559. </NSpace>
  560. </div>
  561. )
  562. }
  563. }
  564. })