AwardIntegralPriceRepository.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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\dao\user\AwardIntegralPriceDayDao;
  14. use app\common\dao\user\AwardLakeDao;
  15. use app\common\dao\user\AwardLakeLogDao;
  16. use app\common\model\store\order\StoreOrder;
  17. use app\common\model\user\AwardIntegralPrice;
  18. use app\common\model\user\AwardIntegralPriceDay;
  19. use app\common\model\user\AwardLakeLog;
  20. use app\common\model\user\User;
  21. use app\common\model\user\UserExtract;
  22. use app\common\repositories\BaseRepository;
  23. class AwardIntegralPriceRepository extends BaseRepository
  24. {
  25. /**
  26. * @var AwardIntegralPriceDao
  27. */
  28. protected $dao;
  29. /**
  30. * UserSignRepository constructor.
  31. * @param AwardIntegralPriceDao $dao
  32. */
  33. public function __construct(AwardIntegralPriceDao $dao)
  34. {
  35. $this->dao = $dao;
  36. }
  37. public function awardIntegralPrice($day = '')
  38. {
  39. try {
  40. if (!$day) {
  41. $day = date('Y-m-d', strtotime('-1 day'));
  42. }
  43. if ($info = $this->dao->search(['day' => $day])->find()) {
  44. return $info->toArray();
  45. }
  46. // @file_put_contents('yesterday.txt', AwardIntegralPrice::getLastSql() . PHP_EOL, FILE_APPEND);
  47. $yesterday_price = $this->dao->search(['day' => date('Y-m-d', strtotime('-1 day', strtotime($day)))])->find();
  48. if ($yesterday_price) {
  49. $basePrice = $yesterday_price->price;
  50. } else {
  51. $basePrice = systemConfig('award_integral_price', 0.1);
  52. }
  53. $time_start = strtotime($day);
  54. $time_end = strtotime($day) + 86400;
  55. //todo 业绩
  56. $achievement = StoreOrder::where('paid', 1)
  57. ->whereBetween('create_time', [$day . ' 00:00:00', $day . ' 23:59:59'])
  58. ->where('is_del', 0)->where('is_system_del', 0)
  59. ->where('status', '>=', 0)
  60. ->sum('total_price');
  61. @file_put_contents('yesterday.txt', StoreOrder::getLastSql() . PHP_EOL, FILE_APPEND);
  62. $achievement = bcmul((string)$achievement, '0.15', 8); //从总业绩的5%上涨到10%,在上涨到15%
  63. /** @var UserExtractRepository $extractRepositories */
  64. $extractRepositories = app()->make(UserExtractRepository::class);
  65. $commission = $extractRepositories->search(['status' => 1])->whereBetween('check_time', [$time_start, $time_end])->sum('commission');
  66. @file_put_contents('yesterday.txt', UserExtract::getLastSql() . PHP_EOL, FILE_APPEND);
  67. $userService = app()->make(UserRepository::class);
  68. $num = $userService->search(['status' => 1])->sum('award_integral');
  69. $sum_achievement = bcadd((string)$commission, (string)$achievement, 8);
  70. $rise = $num > 0 ? bcdiv($sum_achievement, $num, 8) : 0;
  71. $add_price = 0;
  72. if ($rise > 0) {
  73. @file_put_contents('quanju4.txt', $rise . "-123\r\n", 8);
  74. // 增长值分成25份
  75. $day_25_rise = bcdiv($rise, 25, 8);
  76. } else {
  77. $day_25_rise = 0;
  78. }
  79. // @file_put_contents('quanju4.txt', $day_25_rise . "-456\r\n", 8);
  80. /** @var AwardIntegralPriceDayDao $AwardIntegralPriceDayDao */
  81. $AwardIntegralPriceDayDao = app()->make(AwardIntegralPriceDayDao::class);
  82. $AwardIntegralPriceDayDao->create(['day' => time(), 'all_add_price' => $rise, 'day_add_price' => $day_25_rise]); //保存今天增长的总价格和每日增长价格
  83. $day_25 = strtotime('-25 day');
  84. // @file_put_contents('quanju4.txt', $day_25 . "-25天前的时间\r\n", 8);
  85. // $add_price = $AwardIntegralPriceDayDao->search([])->where('day','>' ,$day_25)->sum('day_add_price');
  86. $add_price = AwardIntegralPriceDay::where('day', '>', $day_25)->sum('day_add_price');
  87. // @file_put_contents('quanju4.txt', sprintf("%.8f", $add_price) . "-789\r\n", 8);
  88. // @file_put_contents('quanju4.txt', AwardIntegralPriceDay::getLastSql() . "-91110212\r\n", 8);
  89. // @file_put_contents('quanju4.txt', $basePrice . "-之前的总价总价\r\n", 8);
  90. // @file_put_contents('quanju4.txt', $add_price . "-963\r\n", 8);
  91. // $price = bcadd(sprintf("%.8f", $add_price), (string)$basePrice, 8);
  92. $price = $add_price + $basePrice;
  93. // @file_put_contents('quanju4.txt', time() . "-正常获取走的到这里吗\r\n", 8);
  94. // @file_put_contents('quanju4.txt', $price . "-总价\r\n", 8);
  95. } catch (\Exception $e) {
  96. @file_put_contents('quanju4.txt', $e->getMessage() . "-积分价格报错内容\r\n", 8);
  97. @file_put_contents('quanju4.txt', $e->getFile() . "-文件\r\n", 8);
  98. @file_put_contents('quanju4.txt', $e->getLine() . "-位置\r\n", 8);
  99. }
  100. return compact('basePrice', 'price', 'rise', 'achievement', 'commission', 'num', 'add_price');
  101. }
  102. public static function userIntegralValue($price){
  103. $user_lst = User::where('status',1)->where('award_integral','>',0)->select(); //获取所有有效用户
  104. if (!empty($user_lst)){
  105. $user_lst = $user_lst->toArray();
  106. }
  107. $userBillRepository = app()->make(UserBillRepository::class);
  108. $userRepository = app()->make(UserRepository::class);
  109. try {
  110. foreach ($user_lst as $user){
  111. $user_price = bcmul($price, $user['award_integral'], 8); //用户积分总价值
  112. if ($user_price > $user['award_range']){
  113. $num = bcsub($user_price, $user['award_range'], 8); //超出额度的金额
  114. if ($num > 0.01){
  115. $num_integral = bcdiv($num, $price, 8); //金额除以价格得到超出额度的积分
  116. $userBillRepository->decBill($user['uid'], 'award_integral', 'extract_award', [
  117. 'link_id' => 0,
  118. 'status' => 1,
  119. 'title' => '提取分红积分',
  120. 'number' => $num_integral,
  121. 'mark' => '自动提取' . floatval($num_integral) . '分红积分',
  122. 'balance' => bcsub($user['award_integral'], $num_integral, 2)
  123. ]);
  124. $userRepository->decField($user['uid'], 'award_integral', $num);
  125. $userRepository->incBrokerage($user['uid'], $num);
  126. $userRepository->incField($user['uid'], 'brokerage_price', $num);
  127. }
  128. }
  129. }
  130. } catch (\Exception $e) {
  131. @file_put_contents('quanju4.txt', $e->getMessage() . "-计算超出额度的积分报错内容\r\n", 8);
  132. @file_put_contents('quanju4.txt', $e->getFile() . "-文件\r\n", 8);
  133. @file_put_contents('quanju4.txt', $e->getLine() . "-位置\r\n", 8);
  134. }
  135. return true;
  136. }
  137. public function setPrice($day, $price, $commission, $achievement, $num, $add_price)
  138. {
  139. $add_time = time();
  140. // @file_put_contents('quanju4.txt', $add_price . "-我add_price呢\r\n", 8);
  141. return $this->dao->create(compact('day', 'price', 'commission', 'achievement', 'num', 'add_time', 'add_price'));
  142. }
  143. /**
  144. * 列表搜索
  145. * @param int $merId
  146. * @param array $where
  147. * @param $page
  148. * @param $limit
  149. * @return array
  150. * @author Qinii
  151. */
  152. public function search(int $merId, array $where, $page, $limit)
  153. {
  154. $query = $this->dao->search($merId, $where)->order('add_time DESC');
  155. $count = $query->count($this->dao->getPk());
  156. $list = $query->page($page, $limit)->hidden(['update_time'])->select();
  157. return compact('count', 'list');
  158. }
  159. /**
  160. * 根据条件获取列表数据
  161. *
  162. * 每日分红价格列表
  163. * @param array $where 查询条件数组,用于指定数据库查询的条件。
  164. * @param int $page 当前页码,用于指定要返回的页码。
  165. * @param int $limit 每页的数据数量,用于指定每页返回的数据条数。
  166. * @return array 返回包含 'count' 和 'list' 两个元素的数组,'count' 为数据总数,'list' 为数据列表。
  167. */
  168. public function getList(array $where, $page, $limit)
  169. {
  170. // 根据条件查询数据,$where 为查询条件数组
  171. $query = $this->dao->search($where);
  172. // 统计满足条件的数据总数
  173. $count = $query->count();
  174. // 查询满足条件的数据列表,带有 'label' 关联数据,按 'label_rule_id' 倒序排列
  175. // 分页查询,返回当前页码的 $limit 条数据,并将结果转换为数组形式
  176. $list = $query->page($page, $limit)->order('day DESC')->select()->toArray();
  177. // 返回包含数据总数和数据列表的数组
  178. return compact('count', 'list');
  179. }
  180. /**
  181. * 根据条件获取列表数据
  182. *
  183. * 每日奖池列表
  184. * @param array $where 查询条件数组,用于指定数据库查询的条件。
  185. * @param int $page 当前页码,用于指定要返回的页码。
  186. * @param int $limit 每页的数据数量,用于指定每页返回的数据条数。
  187. * @return array 返回包含 'count' 和 'list' 两个元素的数组,'count' 为数据总数,'list' 为数据列表。
  188. */
  189. public function getLakeList(array $where, $page, $limit)
  190. {
  191. // 根据条件查询数据,$where 为查询条件数组
  192. $mark = app()->make(AwardLakeDao::class);
  193. $query = $mark->search($where);
  194. // 统计满足条件的数据总数
  195. $count = $query->count();
  196. // 查询满足条件的数据列表,带有 'label' 关联数据,按 'label_rule_id' 倒序排列
  197. // 分页查询,返回当前页码的 $limit 条数据,并将结果转换为数组形式
  198. $list = $query->page($page, $limit)->order('level asc')->select()->toArray();
  199. // 返回包含数据总数和数据列表的数组
  200. return compact('count', 'list');
  201. }
  202. /**
  203. * 根据条件获取列表数据
  204. *
  205. * 每日奖池列表
  206. * @param array $where 查询条件数组,用于指定数据库查询的条件。
  207. * @param int $page 当前页码,用于指定要返回的页码。
  208. * @param int $limit 每页的数据数量,用于指定每页返回的数据条数。
  209. * @return array 返回包含 'count' 和 'list' 两个元素的数组,'count' 为数据总数,'list' 为数据列表。
  210. */
  211. public function getLakeLogList(array $where, $page, $limit)
  212. {
  213. // 根据条件查询数据,$where 为查询条件数组
  214. $mark = app()->make(AwardLakeLogDao::class);
  215. $query = $mark->search($where);
  216. // 统计满足条件的数据总数
  217. $count = $query->count();
  218. // 查询满足条件的数据列表,带有 'label' 关联数据,按 'label_rule_id' 倒序排列
  219. // 分页查询,返回当前页码的 $limit 条数据,并将结果转换为数组形式
  220. $list = $query->page($page, $limit)->order('add_time DESC')->select()->toArray();
  221. // 返回包含数据总数和数据列表的数组
  222. return compact('count', 'list');
  223. }
  224. public function getPrice()
  225. {
  226. $mark = app()->make(AwardIntegralPriceDao::class);
  227. $price = $mark->search([])->order('add_time DESC')->value('price');
  228. return $price;
  229. }
  230. }