rent_computer.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * @Author: YKH
  3. * @Date: 2021-12-08 10:26:27
  4. * @LastEditTime: 2022-09-16 23:31:48
  5. * @Description:
  6. * @FilePath: \cool-admin-3.x\cool-admin-midway-master\src\app\modules\shuyou\service\rent_computer.ts
  7. */
  8. import { Provide } from '@midwayjs/decorator';
  9. import { BaseService, CoolCommException } from 'midwayjs-cool-core';
  10. import { InjectEntityModel } from '@midwayjs/orm';
  11. import { Repository } from 'typeorm';
  12. import * as _ from 'lodash';
  13. import { ShuyouRentComputerEntity } from '../entity/rent_computer';
  14. import { ShuyouRentComputerUsageEntity } from '../entity/rent_computer_game_record';
  15. /**
  16. * 租机
  17. */
  18. @Provide()
  19. export class ShuyouRentComputerService extends BaseService {
  20. @InjectEntityModel(ShuyouRentComputerEntity)
  21. ShuyouRentComputerEntity: Repository<ShuyouRentComputerEntity>;
  22. @InjectEntityModel(ShuyouRentComputerUsageEntity)
  23. ShuyouRentComputerUsageEntity: Repository<ShuyouRentComputerUsageEntity>;
  24. /**
  25. * 重写新增接口
  26. */
  27. async add(param) {
  28. const existsTask = await this.ShuyouRentComputerEntity.findOne({
  29. pc_num: param.pc_num
  30. });
  31. if (!_.isEmpty(existsTask)) {
  32. throw new CoolCommException('已存在相同编号的租机');
  33. }
  34. param.createTime = new Date();
  35. param.updateTime = new Date();
  36. param.rent_price_day = parseFloat(param.rent_price) / parseFloat(param.rent_duration); // 折算天租金
  37. await this.updateTaskGame(param);
  38. return super.add(param);
  39. }
  40. /**
  41. * 更新租机-任务关系
  42. * @param pc_task
  43. */
  44. async updateTaskGame(pc_task) {
  45. await this.ShuyouRentComputerUsageEntity.delete({ pc_num: pc_task.pc_num });
  46. if (pc_task.taskIdList) {
  47. for (const task_id of pc_task.taskIdList) {
  48. // console.log(task_id)
  49. await this.ShuyouRentComputerUsageEntity.save({ pc_num: pc_task.pc_num, task_id });
  50. }
  51. }
  52. }
  53. //表编码格式不一致导致报错解决办法
  54. //SHOW CREATE TABLE shuyou_rent_computer_usage;
  55. //ALTER TABLE shuyou_rent_computer_usage DEFAULT CHARACTER SET utf8mb4 COLLATE=utf8mb4_german2_ci;
  56. // ALTER TABLE shuyou_rent_computer_usage CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci;
  57. /**
  58. * 根据ID获得信息
  59. * @param id
  60. */
  61. public async info(id) {
  62. const info = await this.ShuyouRentComputerEntity.findOne({ id });
  63. if (info) {
  64. const taskIds = await this.nativeQuery(
  65. `select
  66. a.task_id,
  67. b.task_name
  68. from
  69. shuyou_rent_computer_usage a
  70. LEFT JOIN shuyou_game_task b ON a.task_id = b.id
  71. where a.pc_num = ?`,
  72. [info.pc_num]
  73. );
  74. if (taskIds) {
  75. info.taskIdList = taskIds.map(e => {
  76. // item.id+' [ '+item.task_name+' ] '
  77. return (e.task_id);
  78. // return (e.task_id +' [ '+e.task_name+' ] ');
  79. });
  80. }
  81. }
  82. return info;
  83. }
  84. /**
  85. * 修改
  86. * @param param 数据
  87. */
  88. async update(param) {
  89. // console.log(param)
  90. const taskInfo = await this.ShuyouRentComputerEntity.findOne({ id: param.id });
  91. if (!taskInfo) {
  92. throw new CoolCommException('租机不存在');
  93. }
  94. await this.ShuyouRentComputerEntity.save(param);
  95. await this.updateTaskGame(param);
  96. }
  97. /**
  98. * 重写分页查询
  99. * @param query
  100. */
  101. async page(query) {
  102. const { pc_num } = query;
  103. const sql = `
  104. SELECT
  105. a.*,
  106. b.name as shop_name,c.name as rent_person_name,
  107. GROUP_CONCAT(d.task_id) AS taskIdList
  108. FROM
  109. shuyou_rent_computer a
  110. LEFT JOIN shuyou_rent_computer_shop b ON a.shop_id = b.id
  111. LEFT JOIN shuyou_game_director c ON a.rent_person_id = c.id
  112. LEFT JOIN shuyou_rent_computer_usage d ON a.pc_num = d.pc_num
  113. WHERE 1 = 1
  114. ${this.setSql(pc_num, 'and pc_num = ?', [pc_num])}
  115. GROUP BY a.id
  116. `;
  117. return this.sqlRenderPage(sql, query);
  118. }
  119. }