PledgeLogic.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace app\common\logic;
  3. use Exception;
  4. use think\Env;
  5. use think\Error;
  6. use think\Loader ;
  7. use app\common\model\UserPledge;
  8. use app\common\model\ProductPledges;
  9. class PledgeLogic
  10. {
  11. //获取产品信息 user_pledge
  12. public static function getByProductIdList(object $list, string $lan = 'zh')
  13. {
  14. $model = Loader::model('ProductLists');
  15. foreach ($list as &$item) {
  16. $item['product_list'] = $model->whereIn('id', $item->product_id)->column('id,thum,'.$lan.'_name as name');
  17. }
  18. return $list;
  19. }
  20. /*
  21. * 获取产品信息
  22. */
  23. public static function getHoldProductList($user_id, $product_id)
  24. {
  25. $model = Loader::model('ProductOrder');
  26. return $model::where('user_id', $user_id)->where('product_id', $product_id)->where('status', $model::Paid)->field('id,order_no')->select();
  27. }
  28. //添加质抵押订单
  29. public static function setPledgeOrder(object $pledge, array $order_no, int $user_id)
  30. {
  31. $model = Loader::model('ProductOrder');
  32. $order = $model::where('user_id', $user_id)->whereIn('order_no', $order_no)->select();
  33. if(empty($order) || count($order) != count($order_no)) throw new Exception('订单不存在');
  34. $pledge_num = 1;
  35. if($pledge->type_id == ProductPledges::Single){
  36. $pledge_num = count($order);
  37. }
  38. //添加订单
  39. Loader::model('UserPledge')::setPledgeData($user_id, $pledge->id, $pledge->product_id, $pledge->day_num, $pledge_num);
  40. //修改状态
  41. return $model->whereIn('order_no', $order_no)->setField('status', $model::Freeze);
  42. }
  43. //获取质抵押订单列表
  44. //(60*60*24)*(当前时间-最后一次收取时间)
  45. public static function getPledgeOrderList(int $user_id)
  46. {
  47. $model = Loader::model('UserPledge');
  48. $list = $model::where('user_id', $user_id)->where('status', $model::Ongoing)->select();
  49. if(empty($list)) throw new Exception('暂无质押订单');
  50. $day = 86400;
  51. $total = 0; //当前累计收益
  52. $growth= 0; //增长累计收益
  53. $time = time();
  54. foreach ($list as $item) {
  55. $reta = bcdiv($item->day_num, $day, 2); //天数
  56. $inter = ($item->last_time == 0) ? $time - $item->create_time: $time - $item->last_time; //最后收取时间
  57. $total += bcmul($reta, $inter, 2) * $item->num; //累计收益
  58. $growth += $reta* $item->num;
  59. }
  60. return ['total' => $total, 'growth' => $growth, 'list' => $list];
  61. }
  62. }