ProductOrder.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use Exception;
  5. class ProductOrder extends Model
  6. {
  7. // 表名
  8. protected $table = 'product_order';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'create_time';
  13. protected $updateTime = 'update_time';
  14. protected $deleteTime = false;
  15. // 追加属性
  16. protected $append = [
  17. 'create_time_text',
  18. 'update_time_text'
  19. ];
  20. //状态
  21. const Ordered = 0;
  22. const Paid = 1;
  23. const Transferred = 2;
  24. const Shipped = 3;
  25. const Cancelled = 4;
  26. const Closure = 5;
  27. const Freeze = 6; //冻结
  28. //类型
  29. const Popular = 0; //热销
  30. const Transfer = 1; //转让
  31. const Giveaway = 2; //赠送
  32. const Newbie = 3; //新人茶权空投
  33. const Super = 4; //新人福利空投
  34. const Airdrop = 5; //空投
  35. const Synthesi = 6; //合成
  36. // 订单状态 '已下单', 1=>'已付款', 2=>'已转让', 3=>'提货', 4=>'已取消', 5=>'完成'];
  37. public $order_status = [
  38. '-1' => '全部',
  39. self::Ordered => '已下单',
  40. self::Paid => '已付款',
  41. self::Transferred => '已转让',
  42. self::Shipped => '提货',
  43. self::Cancelled => '已取消',
  44. self::Closure => '关闭',
  45. self::Freeze => '质押',
  46. ];
  47. //抢购下单:未选择地区购买
  48. public static function setPopularNoAreaOrder($num, $orderId, $price, $productId, $uid, $typeId):bool
  49. {
  50. for ($i = 0; $i < $num; $i++) {
  51. $order_arr['price'] = $price;
  52. $order_arr['product_id']= $productId;
  53. $order_arr['area_id'] = 0;
  54. self::setCreateOrder($orderId, $order_arr, $typeId, $uid, 0, getOrderSN('R'.$i), 0, $price);
  55. }
  56. return true;
  57. }
  58. //抢购下单:选择地区购买
  59. public static function setPopularAreaOrder($areaArr, $orderId, $price, $productId, $uid, $typeId):bool
  60. {
  61. $result = true;
  62. foreach ($areaArr as $item) {
  63. // 生成订单
  64. $order_arr['price'] = $price;
  65. $order_arr['product_id']= $productId;
  66. $order_arr['area_id'] = $item;
  67. self::setCreateOrder($orderId, $order_arr, $typeId, $uid, 0, getOrderSN('R'), 0, $price);
  68. }
  69. //修改区域状态
  70. $count = ProductArea::whereIn('id', $areaArr)->setField('status', ProductLists::Stop);
  71. if($count != count($areaArr)) $result = false;
  72. return $result;
  73. }
  74. //合成订单
  75. public static function setSynthesisOrder($synthesis, $productId, $num, $uid):bool
  76. {
  77. $result = true;
  78. foreach ($productId as $key => $item) {
  79. //获取需求数量
  80. if(!isset($synthesis[$key.'_num'])) throw new Exception(__("参数有误,无可用产品"));;
  81. //判断持有数量足够
  82. $total_num = $synthesis[$key.'_num'] * $num;
  83. if($total_num > self::getUserOrderNum($uid, $item)) throw new Exception(__("材料不足"));;
  84. //关闭持有订单
  85. $result = self::where('user_id', $uid)->where('product_id', $item)->where('status', self::Paid)->limit($total_num)->setField('status', self::Closure);
  86. }
  87. return $result;
  88. }
  89. /**
  90. * @param int $orderId 订单id
  91. * @param array $orderInfo 订单详情
  92. * @param int $typeId 订单类型
  93. * @param int $userId 用户id
  94. * @param int $fromUser 来源id
  95. * @param float $fees 手续费
  96. * @param float $fees 抢购价
  97. */
  98. public static function setCreateOrder(int $orderId, $orderInfo, $typeId, $userId, $fromUser, string $orderNo, float $fees, float $price): object
  99. {
  100. return self::create([
  101. 'order_id' => $orderId,
  102. 'product_id' => $orderInfo['product_id'],
  103. 'type_id' => $typeId,
  104. 'status' => self::Paid,
  105. 'area_id' => $orderInfo['area_id'],
  106. 'order_no' => $orderNo,
  107. 'user_id' => $userId,
  108. 'from_user' => $fromUser,
  109. 'price' => $orderInfo['price'],
  110. 'popular_price'=> $price,
  111. 'fees' => $fees,
  112. 'num' => 1
  113. ]);
  114. }
  115. //获取持有产品数量
  116. public static function getByOrderProductNum($productId, $num, $uid)
  117. {
  118. if(empty($productId)) return (object)[];
  119. foreach ($productId as &$item) {
  120. $item['num'] = $num;
  121. $count = self::getUserOrderNum($uid, $item['id']);
  122. if(empty($count)) $count = 0;
  123. $item['hold_num'] = $count;
  124. }
  125. return $productId;
  126. }
  127. //获取持有产品数量
  128. private static function getUserOrderNum(int $uid, int $product_id): int
  129. {
  130. return self::where('user_id', $uid)->where('product_id', $product_id)->where('status', '=', self::Paid)->sum('num');
  131. }
  132. // 获取订单状态
  133. public static function getProductOrder($orderId, $status, string $field){
  134. return self::alias('a')->where('a.id', $orderId)
  135. ->join("product_list b", "a.product_id = b.id", "left")
  136. ->field('a.*,'.$field)
  137. ->where('a.status', $status)
  138. ->find();
  139. }
  140. // 获取新人福利领取订单记录
  141. public static function getUserWelfare($uid, $typeId){
  142. return self::where('user_id', $uid)
  143. ->where('type_id', $typeId)
  144. ->order('create_time desc')
  145. ->find();
  146. }
  147. public static function getStatusList()
  148. {
  149. return [self::Ordered => __('已下单'), self::Paid => __('已付款'), self::Transferred => __('已转让'),
  150. self::Shipped => __('提货'), self::Cancelled => __('已取消'), self::Closure => __('关闭'), self::Freeze => __('质押')];
  151. }
  152. //全部类型:
  153. public static function getStatusAll()
  154. {
  155. return [
  156. ['type_id'=>self::Popular, 'status'=> self::Paid, 'text' => __('已购买')],
  157. ['type_id'=>self::Transfer, 'status'=> self::Transferred,'text' => __('转让中')],
  158. ['type_id'=>self::Giveaway, 'status'=> self::Closure, 'text' => __('已赠送')],
  159. ['type_id'=>self::Transfer, 'status'=> self::Closure, 'text' => __('已转让')],
  160. ['type_id'=>self::Popular, 'status' => self::Shipped, 'text' => __('已提货')],
  161. ['type_id'=>self::Transfer, 'status'=> self::Cancelled,'text' => __('已取消')],
  162. ['type_id'=>self::Popular, 'status' => self::Freeze, 'text' => __('质押')],
  163. ];
  164. }
  165. //产品
  166. public function products()
  167. {
  168. return $this->hasOne('ProductLists', 'id', 'product_id', [], 'LEFT')->setEagerlyType(0);
  169. }
  170. //用户 user_id
  171. public function users()
  172. {
  173. return $this->hasOne('UserModel', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  174. }
  175. //区域
  176. public function areas()
  177. {
  178. return $this->hasOne('ProductArea', 'id', 'area_id', [], 'LEFT')->setEagerlyType(0);
  179. }
  180. //提货地址
  181. public function address()
  182. {
  183. return $this->hasOne('UserArea', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
  184. }
  185. public function getCreateTimeTextAttr($value, $data)
  186. {
  187. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  188. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  189. }
  190. public function getUpdateTimeTextAttr($value, $data)
  191. {
  192. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  193. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  194. }
  195. protected function setCreateTimeAttr($value)
  196. {
  197. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  198. }
  199. protected function setUpdateTimeAttr($value)
  200. {
  201. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  202. }
  203. }