CpSubscribeStatisticDataController.php 10 KB

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