rent_computer.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * @Author: YKH
  3. * @Date: 2021-12-08 10:26:27
  4. * @LastEditTime: 2022-09-19 17:55:06
  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. console.log('rent_price_day');
  38. console.log(param.rent_price_day);
  39. await this.updateTaskGame(param);
  40. return super.add(param);
  41. }
  42. /**
  43. * 更新租机-任务关系
  44. * @param pc_task
  45. */
  46. async updateTaskGame(pc_task) {
  47. await this.ShuyouRentComputerUsageEntity.delete({ pc_num: pc_task.pc_num });
  48. if (pc_task.taskIdList) {
  49. for (const task_id of pc_task.taskIdList) {
  50. // console.log(task_id)
  51. await this.ShuyouRentComputerUsageEntity.save({
  52. pc_num: pc_task.pc_num,
  53. task_id,
  54. });
  55. }
  56. }
  57. }
  58. //表编码格式不一致导致报错解决办法
  59. //SHOW CREATE TABLE shuyou_rent_computer_usage;
  60. //ALTER TABLE shuyou_rent_computer_usage DEFAULT CHARACTER SET utf8mb4 COLLATE=utf8mb4_german2_ci;
  61. // ALTER TABLE shuyou_rent_computer_usage CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci;
  62. /**
  63. * 根据ID获得信息
  64. * @param id
  65. */
  66. public async info(id) {
  67. const info = await this.ShuyouRentComputerEntity.findOne({ id });
  68. if (info) {
  69. const taskIds = await this.nativeQuery(
  70. `select
  71. task_id
  72. from
  73. shuyou_rent_computer_usage
  74. where pc_num = ?`,
  75. [info.pc_num]
  76. );
  77. if (taskIds) {
  78. info.taskIdList = taskIds.map(e => {
  79. return e.task_id;
  80. });
  81. }
  82. }
  83. return info;
  84. }
  85. /**
  86. * 修改
  87. * @param param 数据
  88. */
  89. async update(param) {
  90. // console.log(param)
  91. const taskInfo = await this.ShuyouRentComputerEntity.findOne({
  92. id: param.id,
  93. });
  94. if (!taskInfo) {
  95. throw new CoolCommException('租机不存在');
  96. }
  97. param.rent_price_day = parseFloat(param.rent_price) / parseFloat(param.rent_duration); // 折算天租金
  98. console.log('rent_price_day');
  99. console.log(param.rent_price_day);
  100. await this.ShuyouRentComputerEntity.save(param);
  101. await this.updateTaskGame(param);
  102. }
  103. /**
  104. * 重写分页查询
  105. * @param query
  106. */
  107. async page(query) {
  108. const { pc_num } = query;
  109. const sql = `
  110. SELECT
  111. a.*,
  112. b.name as shop_name,c.name as rent_person_name,
  113. GROUP_CONCAT(d.task_id) AS taskIdList,
  114. GROUP_CONCAT(DISTINCT f.name) AS directorNameList
  115. FROM
  116. shuyou_rent_computer a
  117. LEFT JOIN shuyou_rent_computer_shop b ON a.shop_id = b.id
  118. LEFT JOIN shuyou_game_director c ON a.rent_person_id = c.id
  119. LEFT JOIN shuyou_rent_computer_usage d ON a.pc_num = d.pc_num
  120. LEFT JOIN shuyou_game_task e ON e.id = d.task_id
  121. LEFT JOIN shuyou_game_director f ON f.id = e.directorId
  122. WHERE 1 = 1
  123. ${this.setSql(pc_num, 'and a.pc_num = ?', [pc_num])}
  124. GROUP BY a.id
  125. `;
  126. return this.sqlRenderPage(sql, query);
  127. }
  128. }