123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- namespace Modules\ContentManage\Services\CpManage;
- use Illuminate\Support\Facades\DB;
- use Modules\Common\Support\Trace\CustomizeLogger;
- use Modules\Common\Support\Trace\TraceContext;
- class BookSubscribe
- {
- /**
- * 从追书云拉取的订阅信息
- * <pre>
- * [
- * 'zhiyu_book_id' => 1, // 植宇书籍id
- * 'cp_source' => xxx, // 植宇cp
- * 'yestoday_available_amount' => xxx, // 订阅金额
- * 'yestoday_total_amount' => xxx , // 订阅结算统计书币
- * 'yestoday_last_amount' => xxx, // 应结算金额
- * ]
- * </pre>
- * @var array
- */
- public $subscribeInfo;
- // cp 信息
- private $cp = null;
- // 书籍信息
- private $book = null;
- // 日志对象
- private $logger;
- public function __construct(array $subscribeInfo)
- {
- $this->subscribeInfo = $subscribeInfo;
- $this->logger = CustomizeLogger::getSubscribeLogger();
- }
- public function getBook() {
- if(!$this->book) {
- $this->book = DB::table('books')
- ->where(['id' => $this->subscribeInfo['zhiyu_book_id']])
- ->select('id', 'settlement_type', 'cp_id')->first();
- }
- return $this->book;
- }
- public function getCp() {
- if(!$this->cp) {
- $this->cp = DB::table('cps')
- ->where(['cp_id' => $this->getBook()->cp_id])
- ->select('cp_id as id', 'share_per', 'cp_name')
- ->first();
- }
- return $this->cp;
- }
- /**
- * 是否需要将拉取的cp书籍订阅数据入库
- * @return bool
- */
- private function needSave() {
- $book = $this->getBook();
- if(!$book) {
- $this->logger->error('书籍信息不存在', [
- 'traceInfo' => app(TraceContext::class)->getTraceInfo(),
- 'zhiyu_book_id' => $this->subscribeInfo['zhiyu_book_id'],
- ]);
- return false;
- }
- $cp = $this->getCp();
- if(!$cp) {
- $this->logger->error('cp信息不存在', [
- 'traceInfo' => app(TraceContext::class)->getTraceInfo(),
- 'cp_id' => $book->cp_id,
- ]);
- return false;
- }
- if('buyout' == $book->settlement_type) {
- $this->logger->info('买断书籍不入cp订阅数据', [
- 'traceInfo' => app(TraceContext::class)->getTraceInfo(),
- 'bid' => $book->id,
- ]);
- return false;
- }
- return true;
- }
- /**
- * 获取书籍订阅数据
- * 如果 settlement_type 是 zhuishuyun 则采用 这个接口的计算数据
- * 如果使用 share 分成的模式;则 应结算金额 = 订阅金额 * 分成比例(cp表的); 保留两位小数,其他小数位舍去,例如 计算的结果是 16.129 那么保存 16.12
- * 买断模式: 数据中心不记录 书籍相关订阅数据;
- * @return array|null
- * <pre>
- * [
- * 'yesterday_available_amount' => xxx , // zy_cp_subscribe_statistic_data.yesterday_available_amount
- * 'yesterday_final_amount' => xxx , // zy_cp_subscribe_statistic_data.yesterday_final_amount
- * 'yesterday_total_coins' => xxx , // zy_cp_subscribe_statistic_data.yesterday_total_coins
- * 'cp_name' => xxx, // cp唯一名称
- * 'zy_bid' => xx, // zy_books.id
- * 'calculate_date' => xxx', // 结算日期,结算的数据是前一个日期的 zy_cp_subscribe_statistic_data.calculate_date
- * 'month' => xxx, // 月份
- * ]
- * </pre>
- */
- public function dealSubscribeInfo() {
- if(!$this->needSave()) {
- return null;
- }
- $book = $this->getBook();
- $cp = $this->getCp();
- $data['yesterday_available_amount'] = intval(($this->subscribeInfo['yestoday_available_amount'] ?? 0) * 100) / 100;
- $data['yesterday_total_coins'] = intval(($this->subscribeInfo['yestoday_total_amount'] ?? 0) * 100) / 100;
- $data['yesterday_final_amount'] = intval(($this->subscribeInfo['yestoday_last_amount'] ?? 0) * 100) / 100;
- $data['cp_name'] = $cp->cp_name;
- $data['zy_bid'] = $book->id;
- $data['calculate_date'] = $this->subscribeInfo['calculate_date'];
- $data['month'] = $this->subscribeInfo['month'];
- $data['book_settlement_type'] = $book->settlement_type;
- if('share' == $book->settlement_type) {
- $data['yesterday_final_amount'] = intval(round(
- $data['yesterday_available_amount'] * (intval($cp->share_per)),2)) / 100;
- }
- $this->logger->debug('cp订阅数据入库信息', [
- 'traceInfo' => app(TraceContext::class)->getTraceInfo(),
- 'beforeSubscribeInfo' => $this->subscribeInfo,
- 'afterSubscribeInfo' => $data
- ]);
- return $data;
- }
- }
|