| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- /*
- * @Author: YKH
- * @Date: 2021-12-08 10:26:27
- * @LastEditTime: 2022-09-16 23:31:48
- * @Description:
- * @FilePath: \cool-admin-3.x\cool-admin-midway-master\src\app\modules\shuyou\service\rent_computer.ts
- */
- import { Provide } from '@midwayjs/decorator';
- import { BaseService, CoolCommException } from 'midwayjs-cool-core';
- import { InjectEntityModel } from '@midwayjs/orm';
- import { Repository } from 'typeorm';
- import * as _ from 'lodash';
- import { ShuyouRentComputerEntity } from '../entity/rent_computer';
- import { ShuyouRentComputerUsageEntity } from '../entity/rent_computer_game_record';
- /**
- * 租机
- */
- @Provide()
- export class ShuyouRentComputerService extends BaseService {
- @InjectEntityModel(ShuyouRentComputerEntity)
- ShuyouRentComputerEntity: Repository<ShuyouRentComputerEntity>;
- @InjectEntityModel(ShuyouRentComputerUsageEntity)
- ShuyouRentComputerUsageEntity: Repository<ShuyouRentComputerUsageEntity>;
- /**
- * 重写新增接口
- */
- async add(param) {
- const existsTask = await this.ShuyouRentComputerEntity.findOne({
- pc_num: param.pc_num
- });
- if (!_.isEmpty(existsTask)) {
- throw new CoolCommException('已存在相同编号的租机');
- }
- param.createTime = new Date();
- param.updateTime = new Date();
- param.rent_price_day = parseFloat(param.rent_price) / parseFloat(param.rent_duration); // 折算天租金
- await this.updateTaskGame(param);
- return super.add(param);
- }
- /**
- * 更新租机-任务关系
- * @param pc_task
- */
- async updateTaskGame(pc_task) {
- await this.ShuyouRentComputerUsageEntity.delete({ pc_num: pc_task.pc_num });
- if (pc_task.taskIdList) {
- for (const task_id of pc_task.taskIdList) {
- // console.log(task_id)
- await this.ShuyouRentComputerUsageEntity.save({ pc_num: pc_task.pc_num, task_id });
- }
- }
- }
- //表编码格式不一致导致报错解决办法
- //SHOW CREATE TABLE shuyou_rent_computer_usage;
- //ALTER TABLE shuyou_rent_computer_usage DEFAULT CHARACTER SET utf8mb4 COLLATE=utf8mb4_german2_ci;
- // ALTER TABLE shuyou_rent_computer_usage CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci;
- /**
- * 根据ID获得信息
- * @param id
- */
- public async info(id) {
- const info = await this.ShuyouRentComputerEntity.findOne({ id });
- if (info) {
- const taskIds = await this.nativeQuery(
- `select
- a.task_id,
- b.task_name
- from
- shuyou_rent_computer_usage a
- LEFT JOIN shuyou_game_task b ON a.task_id = b.id
- where a.pc_num = ?`,
- [info.pc_num]
- );
- if (taskIds) {
- info.taskIdList = taskIds.map(e => {
- // item.id+' [ '+item.task_name+' ] '
- return (e.task_id);
- // return (e.task_id +' [ '+e.task_name+' ] ');
- });
- }
- }
- return info;
- }
- /**
- * 修改
- * @param param 数据
- */
- async update(param) {
- // console.log(param)
- const taskInfo = await this.ShuyouRentComputerEntity.findOne({ id: param.id });
- if (!taskInfo) {
- throw new CoolCommException('租机不存在');
- }
- await this.ShuyouRentComputerEntity.save(param);
- await this.updateTaskGame(param);
- }
- /**
- * 重写分页查询
- * @param query
- */
- async page(query) {
- const { pc_num } = query;
- const sql = `
- SELECT
- a.*,
- b.name as shop_name,c.name as rent_person_name,
- GROUP_CONCAT(d.task_id) AS taskIdList
- FROM
- shuyou_rent_computer a
- LEFT JOIN shuyou_rent_computer_shop b ON a.shop_id = b.id
- LEFT JOIN shuyou_game_director c ON a.rent_person_id = c.id
- LEFT JOIN shuyou_rent_computer_usage d ON a.pc_num = d.pc_num
- WHERE 1 = 1
- ${this.setSql(pc_num, 'and pc_num = ?', [pc_num])}
- GROUP BY a.id
- `;
- return this.sqlRenderPage(sql, query);
- }
- }
|