AwardIntegralPriceRepository.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. @file_put_contents('quanju4.txt', $user_price . "-用户积分总价值\r\n", 8);
  113. if ($user_price > $user['award_range']){
  114. $num = bcsub($user_price, $user['award_range'], 8); //超出额度的金额
  115. @file_put_contents('quanju4.txt', $num . "-超出额度的金额\r\n", 8);
  116. if ($num > 0.01){
  117. $num_integral = bcdiv($num, $price, 8); //金额除以价格得到超出额度的积分
  118. @file_put_contents('quanju4.txt', $num_integral . "-金额除以价格得到超出额度的积分\r\n", 8);
  119. $userBillRepository->decBill($user['uid'], 'award_integral', 'extract_award', [
  120. 'link_id' => 0,
  121. 'status' => 1,
  122. 'title' => '提取分红积分',
  123. 'number' => $num_integral,
  124. 'mark' => '自动提取' . floatval($num_integral) . '分红积分',
  125. 'balance' => bcsub($user['award_integral'], $num_integral, 2)
  126. ]);
  127. $userRepository->decField($user['uid'], 'award_integral', $num);
  128. $userRepository->incBrokerage($user['uid'], $num);
  129. $userRepository->incField($user['uid'], 'brokerage_price', $num);
  130. }
  131. }
  132. }
  133. } catch (\Exception $e) {
  134. @file_put_contents('quanju4.txt', $e->getMessage() . "-计算超出额度的积分报错内容\r\n", 8);
  135. @file_put_contents('quanju4.txt', $e->getFile() . "-文件\r\n", 8);
  136. @file_put_contents('quanju4.txt', $e->getLine() . "-位置\r\n", 8);
  137. }
  138. return true;
  139. }
  140. public function setPrice($day, $price, $commission, $achievement, $num, $add_price)
  141. {
  142. $add_time = time();
  143. // @file_put_contents('quanju4.txt', $add_price . "-我add_price呢\r\n", 8);
  144. return $this->dao->create(compact('day', 'price', 'commission', 'achievement', 'num', 'add_time', 'add_price'));
  145. }
  146. /**
  147. * 列表搜索
  148. * @param int $merId
  149. * @param array $where
  150. * @param $page
  151. * @param $limit
  152. * @return array
  153. * @author Qinii
  154. */
  155. public function search(int $merId, array $where, $page, $limit)
  156. {
  157. $query = $this->dao->search($merId, $where)->order('add_time DESC');
  158. $count = $query->count($this->dao->getPk());
  159. $list = $query->page($page, $limit)->hidden(['update_time'])->select();
  160. return compact('count', 'list');
  161. }
  162. /**
  163. * 根据条件获取列表数据
  164. *
  165. * 每日分红价格列表
  166. * @param array $where 查询条件数组,用于指定数据库查询的条件。
  167. * @param int $page 当前页码,用于指定要返回的页码。
  168. * @param int $limit 每页的数据数量,用于指定每页返回的数据条数。
  169. * @return array 返回包含 'count' 和 'list' 两个元素的数组,'count' 为数据总数,'list' 为数据列表。
  170. */
  171. public function getList(array $where, $page, $limit)
  172. {
  173. // 根据条件查询数据,$where 为查询条件数组
  174. $query = $this->dao->search($where);
  175. // 统计满足条件的数据总数
  176. $count = $query->count();
  177. // 查询满足条件的数据列表,带有 'label' 关联数据,按 'label_rule_id' 倒序排列
  178. // 分页查询,返回当前页码的 $limit 条数据,并将结果转换为数组形式
  179. $list = $query->page($page, $limit)->order('day DESC')->select()->toArray();
  180. // 返回包含数据总数和数据列表的数组
  181. return compact('count', 'list');
  182. }
  183. /**
  184. * 根据条件获取列表数据
  185. *
  186. * 每日奖池列表
  187. * @param array $where 查询条件数组,用于指定数据库查询的条件。
  188. * @param int $page 当前页码,用于指定要返回的页码。
  189. * @param int $limit 每页的数据数量,用于指定每页返回的数据条数。
  190. * @return array 返回包含 'count' 和 'list' 两个元素的数组,'count' 为数据总数,'list' 为数据列表。
  191. */
  192. public function getLakeList(array $where, $page, $limit)
  193. {
  194. // 根据条件查询数据,$where 为查询条件数组
  195. $mark = app()->make(AwardLakeDao::class);
  196. $query = $mark->search($where);
  197. // 统计满足条件的数据总数
  198. $count = $query->count();
  199. // 查询满足条件的数据列表,带有 'label' 关联数据,按 'label_rule_id' 倒序排列
  200. // 分页查询,返回当前页码的 $limit 条数据,并将结果转换为数组形式
  201. $list = $query->page($page, $limit)->order('level asc')->select()->toArray();
  202. // 返回包含数据总数和数据列表的数组
  203. return compact('count', 'list');
  204. }
  205. /**
  206. * 根据条件获取列表数据
  207. *
  208. * 每日奖池列表
  209. * @param array $where 查询条件数组,用于指定数据库查询的条件。
  210. * @param int $page 当前页码,用于指定要返回的页码。
  211. * @param int $limit 每页的数据数量,用于指定每页返回的数据条数。
  212. * @return array 返回包含 'count' 和 'list' 两个元素的数组,'count' 为数据总数,'list' 为数据列表。
  213. */
  214. public function getLakeLogList(array $where, $page, $limit)
  215. {
  216. // 根据条件查询数据,$where 为查询条件数组
  217. $mark = app()->make(AwardLakeLogDao::class);
  218. $query = $mark->search($where);
  219. // 统计满足条件的数据总数
  220. $count = $query->count();
  221. // 查询满足条件的数据列表,带有 'label' 关联数据,按 'label_rule_id' 倒序排列
  222. // 分页查询,返回当前页码的 $limit 条数据,并将结果转换为数组形式
  223. $list = $query->page($page, $limit)->order('add_time DESC')->select()->toArray();
  224. // 返回包含数据总数和数据列表的数组
  225. return compact('count', 'list');
  226. }
  227. public function getPrice()
  228. {
  229. $mark = app()->make(AwardIntegralPriceDao::class);
  230. $price = $mark->search([])->order('add_time DESC')->value('price');
  231. return $price;
  232. }
  233. }