building-floor.component.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. import { GenerateFloorComponent } from "src/app/share/generate-floor/generate-floor.component";
  8. import { NzMessageService } from "ng-zorro-antd";
  9. @Component({
  10. selector: "app-building-floor",
  11. templateUrl: "./building-floor.component.html",
  12. styleUrls: ["./building-floor.component.less"],
  13. })
  14. export class BuildingFloorComponent implements OnInit {
  15. tableHeight;
  16. validateForm: FormGroup;
  17. validateFloorForm: FormGroup;
  18. allFloorList: any[] = []; //当前选中的楼栋包含的所有楼层
  19. mapOfCheckedId: { [key: string]: boolean } = {};
  20. checkedBuilding: any = {}; //选中楼栋
  21. buildingList: Array<any> = []; //楼栋信息
  22. hosId: any; //当前选择的院区id
  23. promptContent: string; //操作提示框提示信息
  24. ifSuccess: boolean; //操作成功/失败
  25. promptInfo: string; //操作结果提示信息
  26. promptModalShow: boolean; //是否展示提示框
  27. btnLoading: boolean = false; //确认按钮loading状态
  28. coopBtns: any = {}; // 初始化增删改按钮
  29. buildingsLoading = false; //获取楼栋列表的loading
  30. floorsLoading = false; //获取楼层列表的loading
  31. positionY = 0; //记录Y轴滚动距离
  32. selectedFloor = []; //选中的楼层
  33. coopModal: boolean = false; //楼栋模态框是否展示
  34. add: boolean = true; //楼栋新增的标识,true:新增;false:编辑
  35. floorModal: boolean = false; //楼层模态框是否展示
  36. addFloor: boolean = true; //楼层,true:新增;false:编辑
  37. floorDataEdit; //正在编辑的楼层
  38. delModal: boolean = false; //删除楼栋,模态框是否显示
  39. delFloorModal: boolean = false; //删除楼层,模态框是否显示
  40. isDelSingle: boolean = false; //是否单个删除楼层
  41. generateModal: boolean = false; //生成楼层的模态框
  42. isAllDisplayDataChecked = false; //当前页是否全选
  43. constructor(
  44. private fb: FormBuilder,
  45. private route: ActivatedRoute,
  46. private mainService: MainService,
  47. private msg: NzMessageService,
  48. private tool: ToolService
  49. ) {}
  50. @ViewChild("osComponentRef1", {
  51. read: OverlayScrollbarsComponent,
  52. static: false,
  53. })
  54. osComponentRef1: OverlayScrollbarsComponent;
  55. @ViewChild(GenerateFloorComponent, { static: false })
  56. generate1: GenerateFloorComponent;
  57. ngOnInit() {
  58. this.coopBtns = this.tool.initCoopBtns(this.route);
  59. this.hosId = this.tool.getCurrentHospital().id;
  60. this.getBuildingList();
  61. this.tableHeight = document.body.clientHeight - 267;
  62. }
  63. // 楼栋列表
  64. getBuildingList() {
  65. let postData = {
  66. idx: 0,
  67. sum: 9999,
  68. building: { hosId: this.hosId },
  69. };
  70. this.buildingsLoading = true;
  71. this.mainService
  72. .getFetchDataList("simple/data", "building", postData)
  73. .subscribe((result) => {
  74. this.buildingsLoading = false;
  75. if (result.status == 200) {
  76. this.buildingList = result.list;
  77. if (Object.keys(this.checkedBuilding).length) {
  78. result.list.forEach((item) => {
  79. if (item.id == this.checkedBuilding.id) {
  80. this.checkBuilding(item);
  81. }
  82. });
  83. } else {
  84. this.checkBuilding(result.list[0]);
  85. }
  86. }
  87. });
  88. }
  89. // 获取选中楼栋下楼层列表
  90. getAllFloor() {
  91. // 初始化的时候搜索一次,然后前端过滤
  92. let postData = {
  93. idx: 0,
  94. sum: 9999,
  95. floor: { hosId: this.hosId, buildId: this.checkedBuilding.id },
  96. };
  97. this.floorsLoading = true;
  98. this.mainService
  99. .getFetchDataList("simple/data", "floor", postData)
  100. .subscribe((result) => {
  101. this.floorsLoading = false;
  102. if (result.status == 200) {
  103. this.allFloorList = result.list;
  104. this.refreshStatus();
  105. }
  106. });
  107. }
  108. // 选中楼栋
  109. checkBuilding(data) {
  110. this.positionY = this.osComponentRef1.osInstance().scroll().position.y; //内容滚动的距离
  111. this.checkedBuilding = data ? data : {};
  112. this.mapOfCheckedId = {};
  113. this.getAllFloor();
  114. }
  115. selectedUser(data) {
  116. this.mapOfCheckedId[data.id] = !this.mapOfCheckedId[data.id];
  117. this.refreshStatus();
  118. }
  119. // 选中列表中楼层
  120. refreshStatus(): void {
  121. let arr = [];
  122. if (this.allFloorList.length) {
  123. this.isAllDisplayDataChecked = this.allFloorList.every(
  124. (item) => this.mapOfCheckedId[item.id]
  125. );
  126. } else {
  127. this.isAllDisplayDataChecked = false;
  128. }
  129. for (var m in this.mapOfCheckedId) {
  130. if (this.mapOfCheckedId[m]) {
  131. arr.push({ id: m });
  132. }
  133. }
  134. this.selectedFloor = arr;
  135. }
  136. // 新增/编辑楼栋模态框
  137. showCoopModal(type) {
  138. if (type == "edit" && !this.checkedBuilding.id) {
  139. this.msg.create("warning", "请选择需要编辑的楼栋!");
  140. return;
  141. }
  142. this.coopModal = true;
  143. this.add = type == "add";
  144. if (type == "edit") {
  145. this.initForm();
  146. this.validateForm.controls.buildingName.setValue(
  147. this.checkedBuilding["buildingName"]
  148. );
  149. } else {
  150. this.initForm();
  151. }
  152. }
  153. // 新增/编辑楼层模态框
  154. showFloorModal(e, type, data?) {
  155. this.floorModal = true;
  156. this.addFloor = type == "add";
  157. if (type == "edit") {
  158. this.initFormFloor();
  159. this.floorDataEdit = data;
  160. this.validateFloorForm.controls.buildId.setValue(data.buildId);
  161. this.validateFloorForm.controls.floorName.setValue(data.floorName);
  162. } else {
  163. this.initFormFloor();
  164. }
  165. e.stopPropagation();
  166. }
  167. // 隐藏楼栋模态框
  168. hideCoopModal() {
  169. this.coopModal = false;
  170. }
  171. // 隐藏楼层模态框
  172. hideFloorModal() {
  173. this.floorModal = false;
  174. }
  175. // 初始化新增form表单
  176. initForm() {
  177. this.validateForm = this.fb.group({
  178. buildingName: [null, [Validators.required]],
  179. });
  180. }
  181. // 初始化新增form表单floor
  182. initFormFloor() {
  183. this.validateFloorForm = this.fb.group({
  184. buildId: [this.checkedBuilding.id, [Validators.required]],
  185. floorName: [null, [Validators.required]],
  186. });
  187. }
  188. // 新增/编辑楼栋提交
  189. submitForm(): void {
  190. for (const i in this.validateForm.controls) {
  191. this.validateForm.controls[i].markAsDirty();
  192. this.validateForm.controls[i].updateValueAndValidity();
  193. }
  194. if (this.validateForm.invalid) return;
  195. this.btnLoading = true;
  196. let postData;
  197. let arr = this.buildingList.filter(
  198. (item) => item.buildingName == this.validateForm.value.buildingName
  199. );
  200. //有重复名称
  201. if (arr.length > 0) {
  202. this.btnLoading = false;
  203. this.showPromptModal(
  204. "新增",
  205. false,
  206. `存在重复的楼栋名称【${this.validateForm.value.buildingName}】请修改后再保存!`
  207. );
  208. return;
  209. }
  210. if (this.add) {
  211. postData = {
  212. buildingName: this.validateForm.value.buildingName,
  213. deleted: false,
  214. hosId: this.hosId,
  215. };
  216. } else {
  217. postData = {
  218. buildingName: this.validateForm.value.buildingName,
  219. deleted: false,
  220. hosId: this.hosId,
  221. id: this.checkedBuilding.id,
  222. };
  223. }
  224. this.mainService
  225. .simplePost("addData", "building", postData)
  226. .subscribe((result) => {
  227. this.hideCoopModal();
  228. this.btnLoading = false;
  229. if (result["status"] == 200) {
  230. this.showPromptModal(this.add ? "新增" : "编辑", true, "");
  231. } else {
  232. this.showPromptModal(
  233. this.add ? "新增" : "编辑",
  234. false,
  235. result["msg"]
  236. );
  237. }
  238. });
  239. }
  240. // 新增/编辑楼层提交
  241. submitFormFloor(): void {
  242. for (const i in this.validateFloorForm.controls) {
  243. this.validateFloorForm.controls[i].markAsDirty();
  244. this.validateFloorForm.controls[i].updateValueAndValidity();
  245. }
  246. if (this.validateFloorForm.invalid) return;
  247. this.btnLoading = true;
  248. let postData;
  249. let arr = this.allFloorList.filter(
  250. (item) => item.floorName == this.validateFloorForm.value.floorName
  251. );
  252. //有重复名称
  253. if (arr.length > 0) {
  254. this.btnLoading = false;
  255. this.showPromptModal(
  256. "新增",
  257. false,
  258. `同一个楼栋存在重复的楼层名称【${this.validateFloorForm.value.floorName}】请修改后再保存!`
  259. );
  260. return;
  261. }
  262. if (this.addFloor) {
  263. postData = {
  264. buildId: this.validateFloorForm.value.buildId,
  265. floorName: this.validateFloorForm.value.floorName,
  266. deleted: false,
  267. hosId: this.hosId,
  268. };
  269. } else {
  270. postData = {
  271. buildId: this.validateFloorForm.value.buildId,
  272. floorName: this.validateFloorForm.value.floorName,
  273. deleted: false,
  274. hosId: this.hosId,
  275. id: this.floorDataEdit.id,
  276. };
  277. }
  278. this.addFloorHandler(postData, false);
  279. }
  280. //新增楼层
  281. addFloorHandler(postData, flag) {
  282. this.mainService
  283. .simplePost(flag ? "addListData" : "addData", "floor", postData)
  284. .subscribe((result) => {
  285. this.hideFloorModal();
  286. this.generate1 && (this.generate1.delModal = false);
  287. this.hideGenerateModal();
  288. this.btnLoading = false;
  289. if (result["status"] == 200) {
  290. this.showPromptModal(this.addFloor ? "新增" : "编辑", true, "");
  291. } else {
  292. this.showPromptModal(
  293. this.addFloor ? "新增" : "编辑",
  294. false,
  295. result["msg"]
  296. );
  297. }
  298. });
  299. }
  300. //删除楼栋
  301. showDelModal() {
  302. if (!this.checkedBuilding.id) {
  303. this.msg.create("warning", "请选择需要删除的楼栋!");
  304. return;
  305. }
  306. this.delModal = true;
  307. }
  308. hideDelModal() {
  309. this.delModal = false;
  310. }
  311. // 确认删除
  312. confirmDel() {
  313. this.btnLoading = true;
  314. let postData = [this.checkedBuilding["id"]];
  315. this.mainService.delBuildingList(postData).subscribe((result) => {
  316. this.hideDelModal();
  317. this.btnLoading = false;
  318. if (result["status"] == 200 && !result["data"][0].msg) {
  319. this.showPromptModal("删除", true, "");
  320. this.checkedBuilding = {};
  321. } else {
  322. this.showPromptModal("删除", false, result["data"][0].msg);
  323. }
  324. });
  325. }
  326. /**
  327. * 删除楼层
  328. * @param e 事件对象
  329. * @param data 有值就是单个删除,无值就是批量删除
  330. */
  331. showDelFloorModal(e, data?) {
  332. if (data) {
  333. this.isDelSingle = true;
  334. this.floorDataEdit = data;
  335. } else {
  336. this.isDelSingle = false;
  337. }
  338. this.delFloorModal = true;
  339. e.stopPropagation();
  340. }
  341. hideDelFloorModal() {
  342. this.delFloorModal = false;
  343. }
  344. // 确认删除
  345. confirmFloorDel() {
  346. this.btnLoading = true;
  347. let selectedFloor = this.selectedFloor.map((item) => item.id);
  348. let postData = this.isDelSingle ? [this.floorDataEdit.id] : selectedFloor;
  349. this.mainService.delFloorList(postData).subscribe((result) => {
  350. this.hideDelFloorModal();
  351. this.btnLoading = false;
  352. if (result["status"] == 200 && !result["data"][0].msg) {
  353. this.showPromptModal("删除", true, "");
  354. } else {
  355. this.showPromptModal("删除", false, result["data"][0].msg);
  356. }
  357. });
  358. }
  359. // 全选
  360. checkAll(value: boolean): void {
  361. console.log(this.allFloorList);
  362. this.allFloorList.forEach((item) => {
  363. this.mapOfCheckedId[item.id] = value;
  364. });
  365. this.refreshStatus();
  366. }
  367. // 展示信息提示框(con:提示信息,success:操作是否成功,promptInfo:操作结果提示信息)(con:提示信息,success:操作是否成功,promptInfo:操作结果提示信息)
  368. showPromptModal(con, success, promptInfo) {
  369. this.promptModalShow = false;
  370. this.promptContent = con;
  371. this.ifSuccess = success;
  372. this.promptInfo = promptInfo;
  373. this.osComponentRef1.osInstance().scroll({ x: 0, y: this.positionY });
  374. if (success) {
  375. setTimeout(() => {
  376. this.promptModalShow = true;
  377. this.getBuildingList();
  378. }, 100);
  379. } else {
  380. setTimeout(() => {
  381. this.promptModalShow = true;
  382. }, 100);
  383. }
  384. }
  385. // 生成楼层
  386. generate() {
  387. this.generateModal = true;
  388. }
  389. // 隐藏楼层模态框
  390. hideGenerateModal() {
  391. this.generateModal = false;
  392. }
  393. // 确认生成
  394. confirmGenerate(e) {
  395. console.log(e);
  396. let arr = []; //生成的楼层
  397. for (let i = e[1]; i <= e[2]; i++) {
  398. arr.push({
  399. buildId: e[0].id,
  400. floorName: i + "",
  401. deleted: false,
  402. hosId: this.hosId,
  403. });
  404. }
  405. // 去重
  406. this.allFloorList.forEach((item1) => {
  407. arr = arr.filter(
  408. (item2) =>
  409. !(
  410. item1.buildId == item2.buildId && item1.floorName == item2.floorName
  411. )
  412. );
  413. });
  414. this.btnLoading = true;
  415. this.addFloorHandler(arr, true);
  416. }
  417. }