import { Provide } from '@midwayjs/core';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { OperationWastesaleEntity } from '../entity/wastesale';

/**
 * 可回收物收益服务
 */
@Provide()
export class OperationWastesaleService extends BaseService {
  @InjectEntityModel(OperationWastesaleEntity)
  operationWastesaleEntity: Repository<OperationWastesaleEntity>;

  /**
   * 写入前计算总价
   */
  async modifyBefore(data: any, type: 'add' | 'update') {
    if (type === 'add' || type === 'update') {
      if (data.weight !== undefined && data.unitPrice !== undefined) {
        data.totalPrice = Number((Number(data.weight) * Number(data.unitPrice)).toFixed(2));
      }
    }
  }
}