BookSubscribe.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace Modules\ContentManage\Services\CpManage;
  3. use Illuminate\Support\Facades\DB;
  4. use Modules\Common\Support\Trace\CustomizeLogger;
  5. use Modules\Common\Support\Trace\TraceContext;
  6. class BookSubscribe
  7. {
  8. /**
  9. * 从追书云拉取的订阅信息
  10. * <pre>
  11. * [
  12. * 'zhiyu_book_id' => 1, // 植宇书籍id
  13. * 'cp_source' => xxx, // 植宇cp
  14. * 'yestoday_available_amount' => xxx, // 订阅金额
  15. * 'yestoday_total_amount' => xxx , // 订阅结算统计书币
  16. * 'yestoday_last_amount' => xxx, // 应结算金额
  17. * ]
  18. * </pre>
  19. * @var array
  20. */
  21. public $subscribeInfo;
  22. // cp 信息
  23. private $cp = null;
  24. // 书籍信息
  25. private $book = null;
  26. // 日志对象
  27. private $logger;
  28. public function __construct(array $subscribeInfo)
  29. {
  30. $this->subscribeInfo = $subscribeInfo;
  31. $this->logger = CustomizeLogger::getSubscribeLogger();
  32. }
  33. public function getBook() {
  34. if(!$this->book) {
  35. $this->book = DB::table('books')
  36. ->where(['id' => $this->subscribeInfo['zhiyu_book_id']])
  37. ->select('id', 'settlement_type', 'cp_id')->first();
  38. }
  39. return $this->book;
  40. }
  41. public function getCp() {
  42. if(!$this->cp) {
  43. $this->cp = DB::table('cps')
  44. ->where(['cp_id' => $this->getBook()->cp_id])
  45. ->select('cp_id as id', 'share_per', 'cp_name')
  46. ->first();
  47. }
  48. return $this->cp;
  49. }
  50. /**
  51. * 是否需要将拉取的cp书籍订阅数据入库
  52. * @return bool
  53. */
  54. private function needSave() {
  55. $book = $this->getBook();
  56. if(!$book) {
  57. $this->logger->error('书籍信息不存在', [
  58. 'traceInfo' => app(TraceContext::class)->getTraceInfo(),
  59. 'zhiyu_book_id' => $this->subscribeInfo['zhiyu_book_id'],
  60. ]);
  61. return false;
  62. }
  63. $cp = $this->getCp();
  64. if(!$cp) {
  65. $this->logger->error('cp信息不存在', [
  66. 'traceInfo' => app(TraceContext::class)->getTraceInfo(),
  67. 'cp_id' => $book->cp_id,
  68. ]);
  69. return false;
  70. }
  71. if('buyout' == $book->settlement_type) {
  72. $this->logger->info('买断书籍不入cp订阅数据', [
  73. 'traceInfo' => app(TraceContext::class)->getTraceInfo(),
  74. 'bid' => $book->id,
  75. ]);
  76. return false;
  77. }
  78. return true;
  79. }
  80. /**
  81. * 获取书籍订阅数据
  82. * 如果 settlement_type 是 zhuishuyun 则采用 这个接口的计算数据
  83. * 如果使用 share 分成的模式;则 应结算金额 = 订阅金额 * 分成比例(cp表的); 保留两位小数,其他小数位舍去,例如 计算的结果是 16.129 那么保存 16.12
  84. * 买断模式: 数据中心不记录 书籍相关订阅数据;
  85. * @return array|null
  86. * <pre>
  87. * [
  88. * 'yesterday_available_amount' => xxx , // zy_cp_subscribe_statistic_data.yesterday_available_amount
  89. * 'yesterday_final_amount' => xxx , // zy_cp_subscribe_statistic_data.yesterday_final_amount
  90. * 'yesterday_total_coins' => xxx , // zy_cp_subscribe_statistic_data.yesterday_total_coins
  91. * 'cp_name' => xxx, // cp唯一名称
  92. * 'zy_bid' => xx, // zy_books.id
  93. * 'calculate_date' => xxx', // 结算日期,结算的数据是前一个日期的 zy_cp_subscribe_statistic_data.calculate_date
  94. * 'month' => xxx, // 月份
  95. * ]
  96. * </pre>
  97. */
  98. public function dealSubscribeInfo() {
  99. if(!$this->needSave()) {
  100. return null;
  101. }
  102. $book = $this->getBook();
  103. $cp = $this->getCp();
  104. $data['yesterday_available_amount'] = intval(($this->subscribeInfo['yestoday_available_amount'] ?? 0) * 100) / 100;
  105. $data['yesterday_total_coins'] = intval(($this->subscribeInfo['yestoday_total_amount'] ?? 0) * 100) / 100;
  106. $data['yesterday_final_amount'] = intval(($this->subscribeInfo['yestoday_last_amount'] ?? 0) * 100) / 100;
  107. $data['cp_name'] = $cp->cp_name;
  108. $data['zy_bid'] = $book->id;
  109. $data['calculate_date'] = $this->subscribeInfo['calculate_date'];
  110. $data['month'] = $this->subscribeInfo['month'];
  111. $data['book_settlement_type'] = $book->settlement_type;
  112. if('share' == $book->settlement_type) {
  113. $data['yesterday_final_amount'] = intval(round(
  114. $data['yesterday_available_amount'] * (intval($cp->share_per)),2)) / 100;
  115. }
  116. $this->logger->debug('cp订阅数据入库信息', [
  117. 'traceInfo' => app(TraceContext::class)->getTraceInfo(),
  118. 'beforeSubscribeInfo' => $this->subscribeInfo,
  119. 'afterSubscribeInfo' => $data
  120. ]);
  121. return $data;
  122. }
  123. }