ProductOrder.php 9.1 KB

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