AwardIntegralPriceRepository.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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_integral);
  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. // 订单上涨10%就进行一次分红
  141. public static function orderIntegralUp($price){ //传入当天价格
  142. $order_lst = StoreOrder::where('paid',1)->where('is_del',0)->where('is_system_del',0)->where('status','=',0)->select(); //获取所有有效订单
  143. try {
  144. $one_price = AwardIntegralPrice::where('price','>',0)->order('id asc')->value('price'); //获取第一次的价格
  145. foreach ($order_lst as $item){
  146. $user = User::where('uid',$item['uid'])->where('status',1)->find();
  147. if ($user) {
  148. // 订单购买当天价格
  149. list($startOfDay, $endOfDay) = self::getStartAndEndOfDay($item['pay_time']);
  150. @file_put_contents('quanju4.txt', $startOfDay . "-购买开始时间\r\n", 8);
  151. @file_put_contents('quanju4.txt', $endOfDay . "-购买结束时间\r\n", 8);
  152. $order_price = AwardIntegralPrice::where('add_time','>',$startOfDay)->where('add_time','<',$endOfDay)->value('price'); //购买当时价格
  153. if ($order_price == 0){
  154. $order_price = $one_price;
  155. }
  156. @file_put_contents('quanju4.txt', $order_price . "-当时价格\r\n", 8);
  157. $points_up_num = bcadd($item['points_up_num'],1,0); //订单触发次数+1
  158. $up_radio = bcmul($points_up_num, 0.1, 2); //触发需要增长的比例
  159. $up_num= bcadd(1,$up_radio,2); //比例加+1 1+10%
  160. $up_price = bcmul($order_price,$up_num ,8); //购买时积分价格乘以比例得到增长的价格
  161. if($price>$up_price){ //如果当天价格超过触发增长价格
  162. // 订单触发分红,计算该订单的分红额度
  163. $range = bcmul($item['pay_price'],0.8,2);
  164. $down_range = bcmul($range,0.1,2); //要扣除的10%额度 这分钱要加到奖金里
  165. if ($item['product_type'] == 1) {
  166. // $mark = '节能油';
  167. // $range_type = 'energy_saving_oil';
  168. // $integral_type = 'spread_oil_integral';
  169. // 发放分红积分
  170. $number2 = systemConfig('award_integral_ordinary', 6);
  171. } elseif ($item['product_type'] == 0) {
  172. // $mark = '普通';
  173. // $range_type = 'ordinary';
  174. // $integral_type = 'ordinary_integral';
  175. $number2 = systemConfig('award_integral_ordinary', 6);
  176. } elseif ($item['product_type'] == 2) {
  177. // $mark = '礼包';
  178. // $range_type = 'award_gift_pack';
  179. // $integral_type = 'spread_pack_integral';
  180. $number2 = systemConfig('award_integral_gift_pack', 1);
  181. }
  182. $number2 = bcmul($number2, 0.01, 2);
  183. $order_award_range = bcmul($item['pay_price'], $number2, 2);
  184. @file_put_contents('quanju4.txt', $order_award_range . "-订单积分金额\r\n", 8);
  185. $award_integral = bcdiv($order_award_range, $order_price, 2); //订单除以积分价格得到分红积分
  186. $award_integral= bcmul($award_integral,0.1,2); //要扣除的10%积分
  187. $make = app()->make(UserBillRepository::class);
  188. $userRepository = app()->make(UserRepository::class);
  189. $make->decBill($item['uid'], 'award_range', 'extract_award', [
  190. 'link_id' => $item['order_id'],
  191. 'status' => 1,
  192. 'title' => '获取分红利润扣除分红额度',
  193. 'number' => $down_range,
  194. 'mark' => $user['nickname'] . '获取订单分红利润,减少分红额度',
  195. 'balance' => bcsub($user['award_range'], $down_range, 2)
  196. ]);
  197. $userRepository->decField($item['uid'], 'award_range', $down_range);
  198. $make->decBill($item['uid'], 'award_integral', 'extract_award', [
  199. 'link_id' => $item['order_id'],
  200. 'status' => 1,
  201. 'title' => '获取分红利润扣除分红积分',
  202. 'number' => $award_integral,
  203. 'mark' => $user['nickname'] . '获取订单分红利润,减少分红积分',
  204. 'balance' => bcsub($user['award_integral'], $award_integral, 2)
  205. ]);
  206. $userRepository->decField($item['uid'], 'award_integral', $award_integral);
  207. @file_put_contents('quanju4.txt', $down_range . "-获得佣金\r\n", 8);
  208. $make->incBill($item['uid'], 'brokerage', 'order_up', [
  209. 'link_id' => $item['order_id'],
  210. 'status' => 1,
  211. 'title' => '获取分红利润',
  212. 'number' => $down_range,
  213. 'mark' => $user['nickname'] . '获取订单分红利润',
  214. 'balance' => bcadd($user['brokerage_price'], $down_range, 2)
  215. ]);
  216. $userRepository->incField($item['uid'], 'brokerage_price', $down_range);
  217. // $userRepository->incBrokerage($item['uid'], $down_range);
  218. // $userRepository->incField($item['uid'], 'brokerage_price', $all_price);
  219. StoreOrder::where('order_id',$item['order_id'])->update(['points_up_num' => $points_up_num]); //更新订单触发次数
  220. }
  221. }
  222. // die();
  223. }
  224. } catch (\Exception $e) {
  225. @file_put_contents('quanju4.txt', $e->getMessage() . "-计算订单价格增长报错内容\r\n", 8);
  226. @file_put_contents('quanju4.txt', $e->getFile() . "-文件\r\n", 8);
  227. @file_put_contents('quanju4.txt', $e->getLine() . "-位置\r\n", 8);
  228. }
  229. return true;
  230. }
  231. public static function getStartAndEndOfDay($dateTimeString) {
  232. // 将日期时间字符串转换为日期时间对象
  233. $date = new \DateTime($dateTimeString);
  234. // 设置时间为当天的0点0分0秒
  235. $date->setTime(0, 0, 0);
  236. $startOfDay = $date->getTimestamp();
  237. // 设置时间为当天的23点59分59秒
  238. $date->setTime(23, 59, 59);
  239. $endOfDay = $date->getTimestamp();
  240. return [$startOfDay, $endOfDay];
  241. }
  242. public function setPrice($day, $price, $commission, $achievement, $num, $add_price)
  243. {
  244. $add_time = time();
  245. // @file_put_contents('quanju4.txt', $add_price . "-我add_price呢\r\n", 8);
  246. return $this->dao->create(compact('day', 'price', 'commission', 'achievement', 'num', 'add_time', 'add_price'));
  247. }
  248. /**
  249. * 列表搜索
  250. * @param int $merId
  251. * @param array $where
  252. * @param $page
  253. * @param $limit
  254. * @return array
  255. * @author Qinii
  256. */
  257. public function search(int $merId, array $where, $page, $limit)
  258. {
  259. $query = $this->dao->search($merId, $where)->order('add_time DESC');
  260. $count = $query->count($this->dao->getPk());
  261. $list = $query->page($page, $limit)->hidden(['update_time'])->select();
  262. return compact('count', 'list');
  263. }
  264. /**
  265. * 根据条件获取列表数据
  266. *
  267. * 每日分红价格列表
  268. * @param array $where 查询条件数组,用于指定数据库查询的条件。
  269. * @param int $page 当前页码,用于指定要返回的页码。
  270. * @param int $limit 每页的数据数量,用于指定每页返回的数据条数。
  271. * @return array 返回包含 'count' 和 'list' 两个元素的数组,'count' 为数据总数,'list' 为数据列表。
  272. */
  273. public function getList(array $where, $page, $limit)
  274. {
  275. // 根据条件查询数据,$where 为查询条件数组
  276. $query = $this->dao->search($where);
  277. // 统计满足条件的数据总数
  278. $count = $query->count();
  279. // 查询满足条件的数据列表,带有 'label' 关联数据,按 'label_rule_id' 倒序排列
  280. // 分页查询,返回当前页码的 $limit 条数据,并将结果转换为数组形式
  281. $list = $query->page($page, $limit)->order('day DESC')->select()->toArray();
  282. // 返回包含数据总数和数据列表的数组
  283. return compact('count', 'list');
  284. }
  285. /**
  286. * 根据条件获取列表数据
  287. *
  288. * 每日奖池列表
  289. * @param array $where 查询条件数组,用于指定数据库查询的条件。
  290. * @param int $page 当前页码,用于指定要返回的页码。
  291. * @param int $limit 每页的数据数量,用于指定每页返回的数据条数。
  292. * @return array 返回包含 'count' 和 'list' 两个元素的数组,'count' 为数据总数,'list' 为数据列表。
  293. */
  294. public function getLakeList(array $where, $page, $limit)
  295. {
  296. // 根据条件查询数据,$where 为查询条件数组
  297. $mark = app()->make(AwardLakeDao::class);
  298. $query = $mark->search($where);
  299. // 统计满足条件的数据总数
  300. $count = $query->count();
  301. // 查询满足条件的数据列表,带有 'label' 关联数据,按 'label_rule_id' 倒序排列
  302. // 分页查询,返回当前页码的 $limit 条数据,并将结果转换为数组形式
  303. $list = $query->page($page, $limit)->order('level asc')->select()->toArray();
  304. // 返回包含数据总数和数据列表的数组
  305. return compact('count', 'list');
  306. }
  307. /**
  308. * 根据条件获取列表数据
  309. *
  310. * 每日奖池列表
  311. * @param array $where 查询条件数组,用于指定数据库查询的条件。
  312. * @param int $page 当前页码,用于指定要返回的页码。
  313. * @param int $limit 每页的数据数量,用于指定每页返回的数据条数。
  314. * @return array 返回包含 'count' 和 'list' 两个元素的数组,'count' 为数据总数,'list' 为数据列表。
  315. */
  316. public function getLakeLogList(array $where, $page, $limit)
  317. {
  318. // 根据条件查询数据,$where 为查询条件数组
  319. $mark = app()->make(AwardLakeLogDao::class);
  320. $query = $mark->search($where);
  321. // 统计满足条件的数据总数
  322. $count = $query->count();
  323. // 查询满足条件的数据列表,带有 'label' 关联数据,按 'label_rule_id' 倒序排列
  324. // 分页查询,返回当前页码的 $limit 条数据,并将结果转换为数组形式
  325. $list = $query->page($page, $limit)->order('add_time DESC')->select()->toArray();
  326. // 返回包含数据总数和数据列表的数组
  327. return compact('count', 'list');
  328. }
  329. public function getPrice()
  330. {
  331. $mark = app()->make(AwardIntegralPriceDao::class);
  332. $price = $mark->search([])->order('add_time DESC')->value('price');
  333. return $price;
  334. }
  335. }