AwardIntegralPriceRepository.php 18 KB

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