AwardIntegralPriceRepository.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\user;
  12. use app\common\dao\user\AwardIntegralPriceDao;
  13. use app\common\model\store\order\StoreOrder;
  14. use app\common\model\user\AwardIntegralPrice;
  15. use app\common\model\user\UserExtract;
  16. use app\common\repositories\BaseRepository;
  17. class AwardIntegralPriceRepository extends BaseRepository
  18. {
  19. /**
  20. * @var AwardIntegralPriceDao
  21. */
  22. protected $dao;
  23. /**
  24. * UserSignRepository constructor.
  25. * @param AwardIntegralPriceDao $dao
  26. */
  27. public function __construct(AwardIntegralPriceDao $dao)
  28. {
  29. $this->dao = $dao;
  30. }
  31. public function awardIntegralPrice($day = '')
  32. {
  33. if (!$day) {
  34. $day = date('Y-m-d');
  35. }
  36. if ($info = $this->dao->search(['day' => $day])->find()) {
  37. return $info->toArray();
  38. }
  39. // @file_put_contents('yesterday.txt', AwardIntegralPrice::getLastSql() . PHP_EOL, FILE_APPEND);
  40. $yesterday_price = $this->dao->search(['day' => date('Y-m-d', strtotime('-1 day', strtotime($day)))])->find();
  41. if ($yesterday_price) {
  42. $basePrice = $yesterday_price->price;
  43. } else {
  44. $basePrice = systemConfig('award_integral_price', 0.1);
  45. }
  46. $time_start = strtotime($day);
  47. $time_end = strtotime($day) + 86400;
  48. //todo 业绩
  49. $achievement = StoreOrder::where('paid', 1)
  50. ->whereBetween('create_time', [$day . ' 00:00:00', $day . ' 23:59:59'])
  51. ->where('is_del', 0)->where('is_system_del', 0)
  52. ->where('status', '>=', 0)
  53. ->sum('total_price');
  54. @file_put_contents('yesterday.txt', StoreOrder::getLastSql() . PHP_EOL, FILE_APPEND);
  55. $achievement = bcmul((string)$achievement, '0.05', 2);
  56. /** @var UserExtractRepository $extractRepositories */
  57. $extractRepositories = app()->make(UserExtractRepository::class);
  58. $commission = $extractRepositories->search(['status' => 1])->whereBetween('check_time', [$time_start, $time_end])->sum('commission');
  59. @file_put_contents('yesterday.txt', UserExtract::getLastSql() . PHP_EOL, FILE_APPEND);
  60. $userService = app()->make(UserRepository::class);
  61. $num = $userService->search(['status' => 1])->sum('award_integral');
  62. $sum_achievement = bcadd((string)$commission, (string)$achievement, 2);
  63. $rise = $num > 0 ? bcdiv($sum_achievement, $num, 3) : 0;
  64. $price = bcadd($rise, $basePrice, 3);
  65. return compact('basePrice', 'price', 'rise', 'achievement', 'commission', 'num');
  66. }
  67. public function setPrice($day, $price, $commission, $achievement, $num)
  68. {
  69. $add_time = time();
  70. return $this->dao->create(compact('day', 'price', 'commission', 'achievement', 'num', 'add_time'));
  71. }
  72. }