data-dictionary.component.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. import { Component, OnInit, ViewChild } from "@angular/core";
  2. import { ActivatedRoute } from "@angular/router";
  3. import { FormBuilder, Validators, FormGroup } from "@angular/forms";
  4. import { MainService } from "../../services/main.service";
  5. import { OverlayScrollbarsComponent } from "overlayscrollbars-ngx";
  6. import { ToolService } from "../../services/tool.service";
  7. @Component({
  8. selector: "app-data-dictionary",
  9. templateUrl: "./data-dictionary.component.html",
  10. styleUrls: ["./data-dictionary.component.less"],
  11. })
  12. export class DataDictionaryComponent implements OnInit {
  13. tableHeight;
  14. validateForm: FormGroup;
  15. allDataList: any[] = []; //数据字典下的数据列表
  16. mapOfCheckedId: { [key: string]: boolean } = {};
  17. checkedDictionary: any = {}; //选中
  18. dictionaryList: Array<any> = []; //数据字典信息
  19. parentList: Array<any> = []; //父级数据字典信息
  20. hosId: any; //当前选择的院区id
  21. promptContent: string; //操作提示框提示信息
  22. ifSuccess: boolean; //操作成功/失败
  23. promptInfo: string; //操作结果提示信息
  24. promptModalShow: boolean; //是否展示提示框
  25. btnLoading: boolean = false; //确认按钮loading状态
  26. coopBtns: any = {}; // 初始化增删改按钮
  27. dictionaryLoading = false; //loading
  28. positionY = 0; //记录Y轴滚动距离
  29. selectedDictionary = []; //选中的
  30. add: boolean = true; //新增的标识,true:新增;false:编辑
  31. dictionaryModal: boolean = false; //模态框是否展示
  32. dataEdit; //正在编辑的
  33. delModal: boolean = false; //删除,模态框是否显示
  34. delDictionaryModal: boolean = false; //删除,模态框是否显示
  35. isDelSingle: boolean = false; //是否单个删除
  36. isAllDisplayDataChecked = false; //当前页是否全选
  37. constructor(
  38. private fb: FormBuilder,
  39. private route: ActivatedRoute,
  40. private mainService: MainService,
  41. private tool: ToolService
  42. ) {}
  43. @ViewChild("osComponentRef1", {
  44. read: OverlayScrollbarsComponent,
  45. static: true,
  46. })
  47. osComponentRef1: OverlayScrollbarsComponent;
  48. ngOnInit() {
  49. this.coopBtns = this.tool.initCoopBtns(this.route);
  50. this.hosId = this.tool.getCurrentHospital().id;
  51. this.getDictionaryList();
  52. this.tableHeight = document.body.clientHeight - 312;
  53. }
  54. // 数据字典列表
  55. getDictionaryList() {
  56. this.dictionaryList = [
  57. {
  58. id: -1,
  59. dictionaryName: '被服种类',
  60. key: 'clothes_type',
  61. type: 'dictionaryTree',
  62. },
  63. {
  64. id: -2,
  65. dictionaryName: '统计分类',
  66. key: 'statistics_date_type',
  67. type: 'dictionary',
  68. }
  69. ];
  70. if (Object.keys(this.checkedDictionary).length) {
  71. this.dictionaryList.forEach((item) => {
  72. if (item.id == this.checkedDictionary.id) {
  73. this.checkDictionary(item);
  74. }
  75. });
  76. } else {
  77. this.checkDictionary(this.dictionaryList[0]);
  78. }
  79. }
  80. searchDto:any = {
  81. parent: null,
  82. }
  83. // 获取父级分类
  84. getParentList() {
  85. let postData = {
  86. idx: 0,
  87. sum: 9999,
  88. dictionaryTree: {
  89. hosId: this.hosId,
  90. level: 1,
  91. key: 'clothes_type',
  92. deleted: 0,
  93. },
  94. };
  95. this.mainService
  96. .getFetchDataList("simple/data", "dictionaryTree", postData)
  97. .subscribe((result) => {
  98. if(result.status == 200){
  99. this.parentList = result.list || [];
  100. }
  101. });
  102. }
  103. // 获取选中数据字典列表
  104. getAll() {
  105. this.dictionaryLoading = true;
  106. let postData = {};
  107. if(this.checkedDictionary.type === 'dictionary'){
  108. postData = {
  109. idx: 0,
  110. sum: 9999,
  111. dictionary: {
  112. key: this.checkedDictionary.key,
  113. deleted: 0,
  114. },
  115. }
  116. }else if(this.checkedDictionary.type === 'dictionaryTree'){
  117. postData = {
  118. idx: 0,
  119. sum: 9999,
  120. dictionaryTree: {
  121. hosId: this.hosId,
  122. level: 2,
  123. parent: this.searchDto.parent || undefined,
  124. key: this.checkedDictionary.key,
  125. deleted: 0,
  126. },
  127. }
  128. }
  129. this.mainService
  130. .getFetchDataList("simple/data", this.checkedDictionary.type, postData)
  131. .subscribe((result) => {
  132. this.dictionaryLoading = false;
  133. if (result.status == 200) {
  134. this.allDataList = result.list;
  135. this.refreshStatus();
  136. }
  137. });
  138. }
  139. // 选中数据字典
  140. checkDictionary(data) {
  141. this.positionY = this.osComponentRef1.osInstance() ? this.osComponentRef1.osInstance().scroll().position.y : 0; //内容滚动的距离
  142. this.checkedDictionary = data ? data : {};
  143. this.mapOfCheckedId = {};
  144. this.getAll();
  145. this.checkedDictionary.type === 'dictionaryTree' && this.getParentList();
  146. }
  147. selectedUser(data) {
  148. this.mapOfCheckedId[data.id] = !this.mapOfCheckedId[data.id];
  149. this.refreshStatus();
  150. }
  151. // 选中列表中
  152. refreshStatus(): void {
  153. let arr = [];
  154. if (this.allDataList.length) {
  155. this.isAllDisplayDataChecked = this.allDataList.every(
  156. (item) => this.mapOfCheckedId[item.id]
  157. );
  158. } else {
  159. this.isAllDisplayDataChecked = false;
  160. }
  161. for (var m in this.mapOfCheckedId) {
  162. if (this.mapOfCheckedId[m]) {
  163. arr.push({ id: m });
  164. }
  165. }
  166. this.selectedDictionary = arr;
  167. }
  168. // 新增/编辑模态框
  169. showModal(e, type, data?) {
  170. e.stopPropagation();
  171. this.dictionaryModal = true;
  172. this.add = type == "add";
  173. if (type == "edit") {
  174. this.initForm();
  175. this.dataEdit = data;
  176. if(this.checkedDictionary.type === 'dictionaryTree'){
  177. this.validateForm.controls.key.setValue(data.key);
  178. this.validateForm.controls.parent.setValue(data.parent);
  179. this.validateForm.controls.name.setValue(data.name);
  180. this.validateForm.controls.value.setValue(data.value);
  181. this.validateForm.controls.orders.setValue(data.orders);
  182. this.validateForm.controls.extra3.setValue(data.extra3);
  183. this.validateForm.controls.desc.setValue(data.desc);
  184. } else if(this.checkedDictionary.type === 'dictionary'){
  185. this.validateForm.controls.key.setValue(data.key);
  186. this.validateForm.controls.name.setValue(data.name);
  187. this.validateForm.controls.value.setValue(data.value);
  188. this.validateForm.controls.orders.setValue(data.orders);
  189. this.validateForm.controls.desc.setValue(data.desc);
  190. }
  191. } else {
  192. this.initForm();
  193. }
  194. }
  195. // 隐藏模态框
  196. hideModal() {
  197. this.dictionaryModal = false;
  198. }
  199. // 初始化新增form表单dictionary
  200. initForm() {
  201. if(this.checkedDictionary.type === 'dictionaryTree'){
  202. this.validateForm = this.fb.group({
  203. key: [this.checkedDictionary.key, [Validators.required]],
  204. parent: [null, [Validators.required]],
  205. name: [null, [Validators.required]],
  206. value: [null, [Validators.required]],
  207. orders: [0, [Validators.required]],
  208. extra3: [0, [Validators.required]],
  209. desc: [null, [Validators.required]],
  210. });
  211. } else if(this.checkedDictionary.type === 'dictionary'){
  212. this.validateForm = this.fb.group({
  213. key: [this.checkedDictionary.key, [Validators.required]],
  214. name: [null, [Validators.required]],
  215. value: [null, [Validators.required]],
  216. orders: [0, [Validators.required]],
  217. desc: [null, [Validators.required]],
  218. });
  219. }
  220. }
  221. // 数据字典修改name,value自动变化,且value禁止修改
  222. changeValue(){
  223. if(this.checkedDictionary.type === 'dictionary'){
  224. this.validateForm.controls.value.setValue(this.validateForm.value.name);
  225. }
  226. }
  227. // 新增/编辑提交
  228. submitForm(): void {
  229. for (const i in this.validateForm.controls) {
  230. this.validateForm.controls[i].markAsDirty();
  231. this.validateForm.controls[i].updateValueAndValidity();
  232. }
  233. if (this.validateForm.invalid) return;
  234. this.btnLoading = true;
  235. let postData;
  236. if(this.checkedDictionary.type === 'dictionaryTree'){
  237. if (this.add) {
  238. postData = {
  239. hosId: this.hosId,
  240. key: this.validateForm.value.key,
  241. parent: this.validateForm.value.parent,
  242. name: this.validateForm.value.name,
  243. value: this.validateForm.value.value,
  244. orders: this.validateForm.value.orders,
  245. extra3: this.validateForm.value.extra3,
  246. desc: this.validateForm.value.desc,
  247. level: 2,
  248. system: 0,
  249. };
  250. } else {
  251. postData = {
  252. hosId: this.hosId,
  253. key: this.validateForm.value.key,
  254. parent: this.validateForm.value.parent,
  255. name: this.validateForm.value.name,
  256. value: this.validateForm.value.value,
  257. orders: this.validateForm.value.orders,
  258. extra3: this.validateForm.value.extra3,
  259. desc: this.validateForm.value.desc,
  260. id: this.dataEdit.id,
  261. level: 2,
  262. system: 0,
  263. };
  264. }
  265. }else if(this.checkedDictionary.type === 'dictionary'){
  266. if (this.add) {
  267. postData = {
  268. key: this.validateForm.value.key,
  269. name: this.validateForm.value.name,
  270. value: this.validateForm.value.value,
  271. orders: this.validateForm.value.orders,
  272. desc: this.validateForm.value.desc,
  273. system: 0,
  274. };
  275. } else {
  276. postData = {
  277. key: this.validateForm.value.key,
  278. name: this.validateForm.value.name,
  279. value: this.validateForm.value.value,
  280. orders: this.validateForm.value.orders,
  281. desc: this.validateForm.value.desc,
  282. system: 0,
  283. id: this.dataEdit.id,
  284. };
  285. }
  286. }
  287. this.addHandler(postData);
  288. }
  289. //新增/编辑
  290. addHandler(postData) {
  291. this.mainService
  292. .simplePost("addData", this.checkedDictionary.type, postData)
  293. .subscribe((result) => {
  294. this.hideModal();
  295. this.btnLoading = false;
  296. if (result["status"] == 200) {
  297. this.showPromptModal(this.add ? "新增" : "编辑", true, "");
  298. } else {
  299. this.showPromptModal(
  300. this.add ? "新增" : "编辑",
  301. false,
  302. result["msg"]
  303. );
  304. }
  305. });
  306. }
  307. /**
  308. * 删除
  309. * @param e 事件对象
  310. * @param data 有值就是单个删除,无值就是批量删除
  311. */
  312. showDelDictionaryModal(e, data?) {
  313. if (data) {
  314. this.isDelSingle = true;
  315. this.dataEdit = data;
  316. } else {
  317. this.isDelSingle = false;
  318. }
  319. this.delDictionaryModal = true;
  320. e.stopPropagation();
  321. }
  322. hideDelDictionaryModal() {
  323. this.delDictionaryModal = false;
  324. }
  325. // 确认删除
  326. confirmDictionaryDel() {
  327. this.btnLoading = true;
  328. let selectedDictionary = this.selectedDictionary.map((item) => item.id);
  329. let postData = this.isDelSingle ? [this.dataEdit.id] : selectedDictionary;
  330. this.mainService.simplePost("rmvData", this.checkedDictionary.type, postData).subscribe((result) => {
  331. this.hideDelDictionaryModal();
  332. this.btnLoading = false;
  333. if (result.status == 200) {
  334. this.showPromptModal("删除", true, "");
  335. } else {
  336. this.showPromptModal("删除", false, result.msg);
  337. }
  338. });
  339. }
  340. // 全选
  341. checkAll(value: boolean): void {
  342. console.log(this.allDataList);
  343. this.allDataList.forEach((item) => {
  344. this.mapOfCheckedId[item.id] = value;
  345. });
  346. this.refreshStatus();
  347. }
  348. // 展示信息提示框(con:提示信息,success:操作是否成功,promptInfo:操作结果提示信息)(con:提示信息,success:操作是否成功,promptInfo:操作结果提示信息)
  349. showPromptModal(con, success, promptInfo) {
  350. this.promptModalShow = false;
  351. this.promptContent = con;
  352. this.ifSuccess = success;
  353. this.promptInfo = promptInfo;
  354. this.osComponentRef1.osInstance().scroll({ x: 0, y: this.positionY });
  355. if (success) {
  356. setTimeout(() => {
  357. this.promptModalShow = true;
  358. this.getDictionaryList();
  359. }, 100);
  360. } else {
  361. setTimeout(() => {
  362. this.promptModalShow = true;
  363. }, 100);
  364. }
  365. }
  366. }