| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- namespace app\api\logic;
- use app\api\controller\Product;
- use app\common\model\ProductArea;
- use app\common\controller\Api;
- use app\common\model\UserWelfare;
- use app\common\model\ProductPopular;
- use app\common\model\ProductOrder;
- use app\common\model\UserModel;
- use think\Loader;
- use think\Db;
- use think\Log;
- use Exception;
- /**
- * 空投福利
- */
- class WelfareLoginc
- {
-
- /** 添加新人福利
- * @param int $uid 用户id
- * @param array $productId 产品ID
- * @param int $num 数量
- * @param int $lan 语言
- */
- public static function setUserWelfareLos($uid, int $productId, $num, $tim, $lan, $typeId)
- {
- $result = ProductPopular::getPopularByTime($productId, $lan, $tim);
- if(!$result || $num > ($result->stock-$result->init_num - $result->num)) throw new Exception(__('库存不足'));
-
- //添加产品
- self::setUserProductOrder($num, $result->is_area, $result->id, $result->price, $productId, $uid, $typeId);
- return ['start_time'=>$tim, 'name'=>$result->name, 'num'=>$num, 'price'=>$result->price];
- }
- /*
- 这个条件是
- 向持有选定产品超过指定数量的用户空投指定数量/或比例/的指定产品,
- 比如
- 向持有A茶超过10套的用户,空投2套B茶
- 或
- 向持有A茶超过10套的用户,空投持有 数量X20%套B茶,数量取整
- */
- /** $mod 0指定数量 1比
- * 获取用户Rwa数量
- */
- public static function getUserRwaProductNum(int $productId, int $rwa_num, $mod, $num): int
- {
- //读取当前新增数据
- $list = ProductOrder::getUserOrderByProductId($productId, $rwa_num);
- if(empty($list)) return 0;
- $total = 0; //总数量
- if($mod == 1 && $num > 0){
- $num = bcdiv($num, 100, 2);
- foreach ($list as $item) {
- $total += bcmul($item->total_num, $num);
- }
- }else{
- $total = count($list) * $num;
- }
- return $total;
- }
- /**
- * 向达到一定持有量的用户空投产品
- * @param int $rwa_num //数量
- * @param $productId //产品ID
- * @param $rwaProductId //Rwa产品ID
- * @param $isArea
- * @param $orderId
- * @param $price
- * @param $mod
- * @param $num
- * @return int
- */
- public static function setUserExRwaNum(int $rwa_num, $productId, $rwaProductId, $isArea, $orderId, $price, $mod, $num): int
- {
- $total = 0; //总数量
- // 使用bcdiv函数进行高精度除法运算,$num除以100,保留两位小数
- $div = bcdiv($num, 100, 2);
- $list = ProductOrder::getUserOrderByProductId($rwaProductId, $rwa_num);
- $user = new UserModel();
- foreach($list as $order){
- //根据$mod的值决定是否需要对用户的rwa_num进行乘以$div的运算
- $num = ($mod == 1)? bcmul($order->total_num, $div): $num;
- // 尝试为用户设置产品订单,如果成功,则更新用户的rwa_num
- if(!empty(self::setUserProductOrder($num, $isArea, $orderId, $price, $productId, $order->user_id, ProductOrder::Airdrop))){
-
- // 调用静态方法更新用户的rwa_num,增加$num的值
- $user::updateForRwaNum($order->user_id, $user::getByParentId($order->user_id), $num, '+');
- }
- }
- return $total;
- }
- //合成订单
- public static function setSynthesisOrder($synthesis, $productId, $num, $uid):array
- {
- $result = array();
- $model = Loader::model('ProductOrder');
- foreach ($productId as $key => $item) {
- //获取需求数量
- if(!isset($synthesis[$key.'_num'])) throw new Exception(__("参数有误,无可用产品"));
- //判断持有数量足够
- $total_num = $synthesis[$key.'_num'] * $num;
-
- $order_arr = $model::getUserOrderByProductNum($uid, $item, $total_num);
- if($total_num != count($order_arr)) throw new Exception(__("材料不足"));
- //关闭持有订单
- $model::whereIn('id', $order_arr)->setField('status', $model::Closure);
- $result[$key][] = ['combine'=>$item, 'new_order'=>$order_arr];
- }
- return $result;
- }
- /**
- * 设置用户产品订单
- * 该方法根据是否需要区域信息来设置用户的订单信息如果没有指定区域,则调用setPopularNoAreaOrder方法,
- * 否则,首先查询产品关联的区域ID,然后调用setPopularAreaOrder方法设置订单信息
- * @param int $num 订单数量
- * @param bool $isArea 是否需要区域信息
- * @param mixed $orderId 订单ID
- * @param float $price 产品价格
- * @param int $productId 产品ID
- * @param int $uid 用户ID
- * @param int $typeId 订单类型ID
- * @return mixed 返回订单设置结果
- */
- public static function setUserProductOrder(int $num, $isArea, $orderId, $price, $productId, $uid, $typeId)
- {
- // 判断是否需要区域信息
- if(empty($isArea)){
- // 不需要区域信息,调用相应的方法设置订单
- $result = ProductOrder::setPopularNoAreaOrder($num, $orderId, $price, $productId, $uid, $typeId);
- }else{
- // 需要区域信息,首先查询符合条件的区域ID
- $areaArr = ProductArea::where('product_id', $productId)->where('status', ProductArea::Normal)->orderRaw('id desc')->limit($num)->column('id');
- // 使用查询到的区域ID调用相应的方法设置订单
- $result = ProductOrder::setPopularAreaOrder($areaArr, $orderId, $price, $productId, $uid, $typeId);
- }
- // 返回订单设置结果
- return $result;
- }
- }
|