ExchangeOrder.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace app\admin\model\shop;
  3. use think\Model;
  4. use think\Db;
  5. use \app\common\model\User;
  6. use \app\admin\model\shop\Exchange;
  7. class ExchangeOrder extends Model
  8. {
  9. // 表名
  10. protected $name = 'shop_exchange_order';
  11. // 自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'int';
  13. // 定义时间戳字段名
  14. protected $createTime = 'createtime';
  15. protected $updateTime = 'updatetime';
  16. protected $deleteTime = false;
  17. // 追加属性
  18. protected $append = [
  19. 'type_text',
  20. 'status_text'
  21. ];
  22. public static function init()
  23. {
  24. self::afterWrite(function ($row) {
  25. $changeData = $row->getChangedData();
  26. //拒接回退积分
  27. if (isset($changeData['status']) && $changeData['status'] == 'rejected') {
  28. Db::transaction(function () use ($row) {
  29. User::score($row['score'], $row['user_id'], '积分兑换已拒绝,积分退还');
  30. //库存,销量回调
  31. Exchange::where('id', $row['exchange_id'])->setInc('stocks', $row['nums']);
  32. Exchange::where('id', $row['exchange_id'])->setDec('sales', $row['nums']);
  33. });
  34. }
  35. });
  36. }
  37. public function getTypeList()
  38. {
  39. return ['virtual' => __('Type virtual'), 'reality' => __('Type reality')];
  40. }
  41. public function getStatusList()
  42. {
  43. return ['created' => __('Created'), 'inprogress' => __('Inprogress'), 'rejected' => __('Rejected'), 'delivered' => __('Delivered'), 'completed' => __('Completed')];
  44. }
  45. public function getTypeTextAttr($value, $data)
  46. {
  47. $value = $value ?: ($data['type'] ?? '');
  48. $list = $this->getTypeList();
  49. return $list[$value] ?? '';
  50. }
  51. public function getStatusTextAttr($value, $data)
  52. {
  53. $value = $value ?: ($data['status'] ?? '');
  54. $list = $this->getStatusList();
  55. return $list[$value] ?? '';
  56. }
  57. public function Exchange()
  58. {
  59. return $this->belongsTo('Exchange', 'exchange_id', 'id', [], 'LIKE');
  60. }
  61. public function User()
  62. {
  63. return $this->belongsTo('\app\common\model\User', 'user_id', 'id', [], 'LEFT');
  64. }
  65. }