| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- <?php
- namespace app\common\model;
- use think\Model;
- use Exception;
- class ProductOrder extends Model
- {
- // 表名
- protected $table = 'product_order';
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'create_time';
- protected $updateTime = 'update_time';
- protected $deleteTime = false;
- // 追加属性
- protected $append = [
- 'create_time_text',
- 'update_time_text'
- ];
- //状态
- const Paid = 1;
- const Transferred = 2;
- const Shipped = 3;
- const Cancelled = 4; //取消
- const Closure = 5; //关闭
- const Freeze = 6; //存储
- //类型
- const Popular = 0; //热销
- const Transfer = 1; //转让
- const Giveaway = 2; //赠送
- const Newbie = 3; //新人茶权空投
- const Super = 4; //新人福利空投
- const Airdrop = 5; //空投
- const Synthesi = 6; //合成
- const Buying = 7; //求购
- const RwaExchange = 8; //Rwa茶兑换
- const TeacExchange = 9; //Teac兑换
- // 订单状态 '已下单', 1=>'已付款', 2=>'已转让', 3=>'提货', 4=>'已取消', 5=>'完成'];
- public $order_status = [
- '-1' => '全部',
- self::Paid => '已付款',
- self::Transferred => '转让中',
- self::Shipped => '提货',
- self::Closure => '关闭',
- self::Freeze => '质押',
- ];
- //抢购下单:未选择地区购买
- public static function setPopularNoAreaOrder($num, $orderId, $price, $productId, $uid, $typeId): bool
- {
- $data = [];
- //抢购订单
- $time = time();
- $is_popular = ($typeId == self::Popular) ? self::Paid : 0;
- for ($i = 0; $i < $num; $i++) {
- $data[] = [
- 'order_id' => $orderId,
- 'product_id' => $productId,
- 'type_id' => $typeId,
- 'status' => self::Paid,
- 'area_id' => 0,
- 'price' => $price,
- 'popular_price' => $price,
- 'fees' => 0,
- 'num' => 1,
- 'user_id' => $uid,
- 'order_no' => getOrderSN('R' . $i),
- 'is_popular' => $is_popular,
- 'create_time' => $time,
- 'update_time' => $time
- ];
- }
- return self::insertAll($data);
- }
- //抢购下单:选择地区购买
- public static function setPopularAreaOrder($areaArr, $orderId, $price, $productId, $uid, $typeId): bool
- {
- $result = true;
- foreach ($areaArr as $item) {
- // 生成订单
- $order_arr['price'] = $price;
- $order_arr['product_id'] = $productId;
- $order_arr['area_id'] = $item;
- self::setCreateOrder($orderId, $order_arr, $typeId, $uid, 0, getOrderSN('R'), 0, $price);
- }
- //修改区域状态
- $count = ProductArea::whereIn('id', $areaArr)->setField('status', ProductLists::Stop);
- if ($count != count($areaArr)) $result = false;
- return $result;
- }
- /**
- * @param int $orderId 订单id
- * @param array $orderInfo 订单详情
- * @param int $typeId 订单类型
- * @param int $userId 用户id
- * @param int $fromUser 来源id
- * @param float $fees 手续费
- * @param float $price 抢购价
- */
- public static function setCreateOrder(int $orderId, $orderInfo, $typeId, $userId, $fromUser, string $orderNo, float $fees, float $price): object
- {
- $data = [
- 'order_id' => $orderId,
- 'product_id' => $orderInfo['product_id'],
- 'type_id' => $typeId,
- 'status' => self::Paid,
- 'area_id' => $orderInfo['area_id'],
- 'order_no' => $orderNo,
- 'user_id' => $userId,
- 'from_user' => $fromUser,
- 'price' => $orderInfo['price'],
- 'popular_price' => $price,
- 'fees' => $fees,
- 'num' => 1
- ];
- //抢购订单
- if ($typeId == self::Popular) $data['is_popular'] = self::Paid;
- return self::create($data);
- }
- //判断是否购买持有产品
- public function isBuyHoldProduct(array $productId, int $uid)
- {
- return self::where('user_id', $uid)->whereIn('product_id', $productId)->whereIn('status', [self::Paid, self::Transferred, self::Freeze])->count();
- }
- //获取持有持有产品数量
- public static function getHoldProductNum(string $productId)
- {
- return self::whereIn('product_id', $productId)->whereIn('status', [self::Paid, self::Transferred, self::Freeze])
- ->field('user_id,sum(num) as num')
- ->group('user_id')
- ->select();
- }
- //获取抢购触发产品次数
- public static function getTripPopularProduct(string $productId)
- {
- return self::where('product_id', $productId)->where('is_popular', self::Paid)->whereTime('create_time', 'yesterday')->sum('num');
- }
- //获取持有产品数量
- public static function getByOrderProductNum($productId, $num, $uid)
- {
- if (empty($productId)) return (object)[];
- foreach ($productId as &$item) {
- $item['num'] = $num;
- $count = self::getUserOrderNum($uid, $item['id']);
- //if(empty($count)) $count = 0;
- $item['hold_num'] = $count;
- }
- return $productId;
- }
- //空投产品向达到一定持有量的用户
- public static function getUserOrderByProductId(int $rwaProductId, int $rwaNum)
- {
- return ProductOrder::alias('a')
- ->join('product_list b', 'a.product_id = b.id', 'left')
- ->where('b.is_show', ProductLists::Normal)
- ->whereIn('a.status', [self::Paid, self::Transferred, self::Freeze])
- ->where('a.product_id', $rwaProductId)
- ->group('a.user_id')->having('total_num>=' . $rwaNum)
- ->field('a.id,a.user_id,count(a.num) as total_num')
- ->select();
- }
- //获取持有产品数量订单
- public static function getUserOrderByProductNum(int $uid, int $product_id, int $totalNum): array
- {
- return self::where('user_id', $uid)->where('product_id', $product_id)->where('status', self::Paid)->limit($totalNum)->column('id');
- }
- //获取持有产品数量
- private static function getUserOrderNum(int $uid, int $product_id): int
- {
- return self::alias('a')
- ->join('product_list b', 'a.product_id = b.id', 'left')
- ->where('b.is_show', ProductLists::Normal)
- ->where('a.user_id', $uid)
- ->where('a.product_id', $product_id)
- ->where('a.status', '=', self::Paid)
- ->sum('a.num');
- }
- // 获取订单状态
- public static function getProductOrder($orderId, $status, string $field)
- {
- return self::alias('a')->where('a.id', $orderId)
- ->join("product_list b", "a.product_id = b.id", "left")
- ->field('a.*,' . $field)
- ->where('a.status', $status)
- ->find();
- }
- // 获取新人福利领取订单记录
- public static function getUserWelfare($uid, $typeId)
- {
- return self::where('user_id', $uid)->where('type_id', $typeId)->order('create_time desc')->find();
- }
- public static function getStatusList()
- {
- return [self::Paid => __('已付款'), self::Transferred => __('已转让'), self::Shipped => __('提货'), self::Cancelled => __('已取消'), self::Closure => __('关闭'), self::Freeze => __('质押')];
- }
- //全部类型:
- public static function getStatusAll()
- {
- return [
- ['type_id' => self::Popular, 'status' => self::Paid, 'text' => __('已购买')],
- ['type_id' => self::Popular, 'status' => self::Closure, 'text' => __('已关闭')],
- ['type_id' => self::Transfer, 'status' => self::Transferred, 'text' => __('转让中')],
- ['type_id' => self::Giveaway, 'status' => self::Closure, 'text' => __('已赠送')],
- ['type_id' => self::Giveaway, 'status' => self::Paid, 'text' => __('受赠')],
- ['type_id' => self::Transfer, 'status' => self::Closure, 'text' => __('已转让')],
- ['type_id' => self::Popular, 'status' => self::Shipped, 'text' => __('已提货')],
- ['type_id' => self::Transfer, 'status' => self::Cancelled, 'text' => __('已取消')],
- ['type_id' => self::Popular, 'status' => self::Freeze, 'text' => __('质押')],
- ['type_id' => self::Transfer, 'status' => self::Paid, 'text' => __('购买寄售')],
- ['type_id' => self::Airdrop, 'status' => self::Paid, 'text' => __('空投')],
- ['type_id' => self::Airdrop, 'status' => self::Closure, 'text' => __('空投关闭')],
- ['type_id' => self::Synthesi, 'status' => self::Paid, 'text' => __('合成')],
- ['type_id' => self::Synthesi, 'status' => self::Closure, 'text' => __('合成关闭')],
- ['type_id' => self::Buying, 'status' => self::Paid, 'text' => __('求购')],
- ['type_id' => self::Buying, 'status' => self::Closure, 'text' => __('求购关闭')],
- ['type_id' => self::Newbie, 'status' => self::Paid, 'text' => __('新人茶权')],
- ['type_id' => self::Newbie, 'status' => self::Closure, 'text' => __('新人茶权关闭')],
- ['type_id' => self::Super, 'status' => self::Paid, 'text' => __('新人福利')],
- ['type_id' => self::Super, 'status' => self::Closure, 'text' => __('新人福利关闭')],
- ['type_id' => self::RwaExchange, 'status' => self::Paid, 'text' => __('Rwa兑换')],
- ['type_id' => self::RwaExchange, 'status' => self::Closure, 'text' => __('Rwa兑换关闭')],
- ['type_id' => self::TeacExchange, 'status' => self::Paid, 'text' => __('Teac兑换')],
- ['type_id' => self::TeacExchange, 'status' => self::Closure, 'text' => __('Teac兑换关闭')],
- ];
- }
- //产品
- public function products()
- {
- return $this->hasOne('ProductLists', 'id', 'product_id', [], 'LEFT')->setEagerlyType(0);
- }
- //用户 user_id
- public function users()
- {
- return $this->hasOne('UserModel', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
- }
- //区域
- public function areas()
- {
- return $this->hasOne('ProductArea', 'id', 'area_id', [], 'LEFT')->setEagerlyType(0);
- }
- //提货地址
- public function address()
- {
- return $this->hasOne('UserArea', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- public function getCreateTimeTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
- return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
- }
- public function getUpdateTimeTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
- return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
- }
- protected function setCreateTimeAttr($value)
- {
- return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
- }
- protected function setUpdateTimeAttr($value)
- {
- return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
- }
- //抢购下单:未选择地区购买
- public function setGiverwagoods($orderId, $price, $productId, $uid, $typeId)
- {
- //抢购订单
- $time = time();
- $is_popular = 0;
- $data = [
- 'order_id' => $orderId,
- 'product_id' => $productId,
- 'type_id' => $typeId,
- 'status' => 1,
- 'area_id' => 0,
- 'price' => $price,
- 'popular_price' => $price,
- 'fees' => 0,
- 'num' => 1,
- 'user_id' => $uid,
- 'order_no' => getOrderSN('R'),
- 'is_popular' => $is_popular,
- 'create_time' => $time,
- 'update_time' => $time
- ];
- return $data;
- // return self::insert($data);
- }
- }
|