ProductTransfer.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use Exception;
  5. class ProductTransfer extends Model
  6. {
  7. protected $name = "product_transfer";
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'create_time';
  12. protected $updateTime = 'update_time';
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'create_time_text',
  17. 'update_time_text'
  18. ];
  19. const Stop = 0;
  20. const Normal = 1;
  21. const Lock = 1; //锁定
  22. const NotLock = 0; //未锁定
  23. //状态
  24. public $status_list = [
  25. '-1' => '全部',
  26. self::Stop => '停用',
  27. self::Normal => '正常'
  28. ];
  29. //添加/更新转让订单
  30. public static function setTransferOrder($uid, $product_id, $area_id, $feeAmount, $orderNo, $params)
  31. {
  32. $rows = self::where('order_id', $params['order_id'])->find();
  33. if(empty($rows)){
  34. // 生成订单
  35. $order_data['user_id'] = $uid;
  36. $order_data['price'] = $params['price'];
  37. $order_data['product_id'] = $product_id;
  38. $order_data['fees'] = $feeAmount;
  39. $order_data['area_id'] = $area_id;
  40. $order_data['order_id'] = $params['order_id']; //订单ID
  41. $order_data['order_no'] = $orderNo;
  42. return self::create($order_data);
  43. }else{
  44. $rows->price = $params['price'];
  45. $rows->fees = $feeAmount;
  46. $rows->status = self::Normal;
  47. return $rows->save();
  48. }
  49. }
  50. //获取转让订单
  51. public static function getTransferMinPrice(int $product_id)
  52. {
  53. return self::where('product_id', $product_id)->where('status', self::Normal)->min('price');
  54. }
  55. //获取最小价格
  56. public static function getTransferMinPriceByProduct(int $product_id, float $price)
  57. {
  58. return self::where('product_id', $product_id)->where('status', self::Normal)->where('price', '>', $price)->min('price');
  59. }
  60. //产品
  61. public function products()
  62. {
  63. return $this->hasOne('ProductLists', 'id', 'product_id', [], 'LEFT')->setEagerlyType(0);
  64. }
  65. //用户 user_id
  66. public function users()
  67. {
  68. return $this->hasOne('UserModel', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  69. }
  70. public function getCreateTimeTextAttr($value, $data)
  71. {
  72. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  73. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  74. }
  75. public function getUpdateTimeTextAttr($value, $data)
  76. {
  77. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  78. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  79. }
  80. protected function setCreateTimeAttr($value)
  81. {
  82. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  83. }
  84. protected function setUpdateTimeAttr($value)
  85. {
  86. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  87. }
  88. }