AwardIntegralPriceRepository.php 18 KB

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