CpSubscribeStatisticDataController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace Modules\ContentManage\Http\Controllers;
  3. use Catch\Base\CatchController;
  4. use Illuminate\Foundation\Validation\ValidatesRequests;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. class CpSubscribeStatisticDataController extends CatchController
  8. {
  9. use ValidatesRequests;
  10. use \Modules\User\Http\Controllers\UserTrait;
  11. /**
  12. * cp结算数据中心列表
  13. * @param Request $request
  14. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  15. * @throws \Illuminate\Validation\ValidationException
  16. */
  17. public function list(Request $request) {
  18. $this->validate($request, [
  19. 'book_name' => 'nullable|string|max:256',
  20. 'bid' => 'nullable|integer|min:1',
  21. 'cp_name' => 'nullable|string|min:1',
  22. 'start_date' => 'nullable|date_format:Y-m-d',
  23. 'end_date' => 'nullable|date_format:Y-m-d',
  24. 'page' => 'required|integer|min:1',
  25. 'limit' => 'integer|min:10|max:300',
  26. 'is_export' => 'nullable|integer|in:0,1', // 是否导出全部, 1-是,0-否
  27. 'final_amount_gt0' => 'nullable|integer|in:0,1', // 应结算金额是否要大于0,
  28. 'book_settlement_type' => 'nullable|in:share,bottomline', // 书籍合作模式
  29. ]);
  30. $bookName = $request->input('book_name');
  31. $bid = $request->input('bid');
  32. $cpName = $request->input('cp_name');
  33. $startDate = $request->input('start_date');
  34. $endDate = $request->input('end_date');
  35. $perPage = $request->input('limit', 15);
  36. $isExport = $request->input('is_export', 0);
  37. $finalAmountGt0 = $request->input('final_amount_gt0', 0);
  38. $bookSettlementType = $request->input('book_settlement_type');
  39. $settlementTypes = [
  40. 'buyout' => '买断', 'bottomline' => '保底', 'share' => '分成', 'zhuishuyun' => '追书云计算'
  41. ];
  42. $cpName = $this->getUserCpName() ?? $cpName;
  43. $sql = DB::table('cp_subscribe_statistic_data as statistic')
  44. ->leftJoin('books as book', 'statistic.bid' , 'book.id')
  45. ->when($bookName, function ($query, $bookName) {
  46. return $query->where('book.name', 'like', '%'.$bookName. '%');
  47. })->when($bid , function ($query, $bid) {
  48. return $query->where(['statistic.bid' => $bid]);
  49. })->when($cpName, function ($query, $cpName) {
  50. return $query->where(['statistic.cp_name' => $cpName]);
  51. })->when($startDate, function ($query, $startDate) {
  52. $realStartDate = date_add(date_create($startDate), date_interval_create_from_date_string('1 day'))
  53. ->format('Y-m-d');
  54. return $query->where('statistic.calculate_date', '>=', $realStartDate);
  55. })->when($endDate, function ($query, $endDate) {
  56. $realEndDate = date_add(date_create($endDate), date_interval_create_from_date_string('1 day'))
  57. ->format('Y-m-d');
  58. return $query->where('statistic.calculate_date', '<=', $realEndDate);
  59. })->when($finalAmountGt0, function ($query) {
  60. return $query->where('statistic.yesterday_final_amount', '>', 0);
  61. })->when($bookSettlementType, function ($query, $bookSettlementType){
  62. return $query->where('statistic.book_settlement_type', $bookSettlementType);
  63. })
  64. ->select('statistic.bid', 'book.name as book_name', 'statistic.cp_name',
  65. 'statistic.yesterday_total_coins', 'statistic.yesterday_available_amount', 'statistic.yesterday_final_amount',
  66. 'statistic.calculate_date', 'statistic.book_settlement_type')
  67. ->orderBy('statistic.calculate_date', 'desc')
  68. ->orderBy('statistic.bid', 'desc');
  69. if($isExport) {
  70. return $sql->get()->map(function ($item) use($settlementTypes) {
  71. $item->date = date_sub(date_create($item->calculate_date), date_interval_create_from_date_string('1 day'))
  72. ->format('Y-m-d');
  73. $item->book_settlement_type_str = $settlementTypes[$item->book_settlement_type] ?? '';
  74. return $item;
  75. });
  76. } else {
  77. return $sql->paginate($perPage)->through(function ($item) use ($settlementTypes) {
  78. $item->date = date_sub(date_create($item->calculate_date), date_interval_create_from_date_string('1 day'))
  79. ->format('Y-m-d');
  80. $item->book_settlement_type_str = $settlementTypes[$item->book_settlement_type] ?? '';
  81. return $item;
  82. });
  83. }
  84. }
  85. public function listStatistic(Request $request) {
  86. $this->validate($request, [
  87. 'book_name' => 'nullable|string|max:256',
  88. 'bid' => 'nullable|integer|min:1',
  89. 'cp_name' => 'nullable|string|min:1',
  90. 'start_date' => 'nullable|date_format:Y-m-d',
  91. 'end_date' => 'nullable|date_format:Y-m-d',
  92. 'book_settlement_type' => 'nullable|in:share,bottomline'
  93. ]);
  94. $bookName = $request->input('book_name');
  95. $bid = $request->input('bid');
  96. $cpName = $request->input('cp_name');
  97. $startDate = $request->input('start_date');
  98. $endDate = $request->input('end_date');
  99. $bookSettlementType = $request->input('book_settlement_type');
  100. $cpName = $this->getUserCpName() ?? $cpName;
  101. $res = DB::table('cp_subscribe_statistic_data as statistic')
  102. ->leftJoin('books as book', 'statistic.bid' , 'book.id')
  103. ->when($bookName, function ($query, $bookName) {
  104. return $query->where('book.name', 'like', '%'.$bookName. '%');
  105. })->when($bid , function ($query, $bid) {
  106. return $query->where(['statistic.bid' => $bid]);
  107. })->when($cpName, function ($query, $cpName) {
  108. return $query->where(['statistic.cp_name' => $cpName]);
  109. })->when($startDate, function ($query, $startDate) {
  110. $realStartDate = date_add(date_create($startDate), date_interval_create_from_date_string('1 day'))
  111. ->format('Y-m-d');
  112. return $query->where('statistic.calculate_date', '>=', $realStartDate);
  113. })->when($endDate, function ($query, $endDate) {
  114. $realEndDate = date_add(date_create($endDate), date_interval_create_from_date_string('1 day'))
  115. ->format('Y-m-d');
  116. return $query->where('statistic.calculate_date', '<=', $realEndDate);
  117. })->when($bookSettlementType, function ($query, $bookSettlementType){
  118. return $query->where('statistic.book_settlement_type', $bookSettlementType);
  119. })
  120. ->select(
  121. DB::raw('sum(yesterday_total_coins) as yesterday_total_coins'),
  122. DB::raw('sum(yesterday_available_amount) as yesterday_available_amount'),
  123. DB::raw('sum(yesterday_final_amount) as yesterday_final_amount')
  124. )->first();
  125. return $res;
  126. }
  127. /**
  128. * cp结算列表
  129. * @param Request $request
  130. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  131. * @throws \Illuminate\Validation\ValidationException
  132. */
  133. public function monthList(Request $request) {
  134. $this->validate($request, [
  135. 'cp_name' => 'nullable|string',
  136. 'start_month' => 'nullable|date_format:Y-m',
  137. 'end_month' => 'nullable|date_format:Y-m',
  138. 'page' => 'required|integer|min:1',
  139. 'limit' => 'integer|min:10',
  140. 'is_export' => 'integer|in:0,1'
  141. ]);
  142. $cpName = $request->input('cp_name');
  143. $startMonth = $request->input('start_month');
  144. $endMonth = $request->input('end_month');
  145. $isExport = $request->input('is_export', 0);
  146. $finalStateMap = ['notCheck' => '未结算', 'done' => '已结算'];
  147. $cpName = $this->getUserCpName() ?? $cpName;
  148. $sql = DB::table('cp_subscribe_month_statistic_data as data')
  149. ->leftJoin('cps as cp' , 'cp.cp_name', 'data.cp_name')
  150. ->when($cpName, function ($query, $cpName){
  151. return $query->where(['data.cp_name' => $cpName]);
  152. })->when($startMonth, function ($query, $startMonth){
  153. return $query->where('data.month', '>=', $startMonth);
  154. })->when($endMonth, function ($query, $endMonth){
  155. return $query->where('data.month', '<=', $endMonth);
  156. })
  157. ->select('data.id','data.month', 'cp.cp_id', 'cp.cp_name', 'cp.cp_company',
  158. 'data.book_num', 'data.final_amount', 'data.final_state', 'data.final_time')
  159. ->orderBy('data.month', 'desc')
  160. ->orderBy('cp.cp_id', 'desc');
  161. if($isExport) {
  162. return $sql->get()->map(function ($item) use ($finalStateMap){
  163. $item->final_state_str = $finalStateMap[$item->final_state] ?? '';
  164. return $item;
  165. });
  166. }else {
  167. return $sql->paginate($request->integer('limit', 10));
  168. }
  169. }
  170. public function saveFinalState(Request $request){
  171. $this->validate($request, [
  172. 'id' => 'required|integer|min:1',
  173. 'final_state' => 'required|string|in:done'
  174. ]);
  175. $now = date('Y-m-d H:i:s');
  176. DB::table('cp_subscribe_month_statistic_data')
  177. ->where(['id' => $request->input('id'), 'final_state' => 'notCheck'])
  178. ->update([
  179. 'final_state' => $request->input('final_state'),
  180. 'final_time' => $now,
  181. 'updated_at' => $now,
  182. ]);
  183. return 'ok';
  184. }
  185. /**
  186. * 导出某个cp某个月的各个书籍的应结算金额
  187. * @param Request $request
  188. * @return \Illuminate\Support\Collection
  189. * @throws \Illuminate\Validation\ValidationException
  190. */
  191. public function listCpMonthFinalAmount(Request $request) {
  192. $this->validate($request, [
  193. 'cp_name' => 'required|string',
  194. 'month' => 'required|date_format:Y-m'
  195. ]);
  196. $cpName = $request->input('cp_name');
  197. $cpName = $this->getUserCpName() ?? $cpName;
  198. return DB::table('cp_book_month_final_amounts as mfa')
  199. ->leftJoin('books', 'books.id', 'mfa.bid')
  200. ->where([
  201. 'mfa.is_enabled' => 1,
  202. 'mfa.cp_name' => $cpName,
  203. 'mfa.month' => $request->input('month')
  204. ])
  205. ->select('mfa.id', 'mfa.bid', 'books.name as book_name', 'mfa.final_amount', 'mfa.cp_name','mfa.month')
  206. ->get();
  207. }
  208. }