CpSubscribeStatisticDataController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. $sql = DB::table('cp_subscribe_statistic_data')
  42. ->where('id', -1);
  43. if($isExport) {
  44. return $sql->get();
  45. } else {
  46. return $sql->paginate($perPage);
  47. }
  48. $cpName = $this->getUserCpName() ?? $cpName;
  49. $sql = DB::table('cp_subscribe_statistic_data as statistic')
  50. ->leftJoin('books as book', 'statistic.bid' , 'book.id')
  51. ->when($bookName, function ($query, $bookName) {
  52. return $query->where('book.name', 'like', '%'.$bookName. '%');
  53. })->when($bid , function ($query, $bid) {
  54. return $query->where(['statistic.bid' => $bid]);
  55. })->when($cpName, function ($query, $cpName) {
  56. return $query->where(['statistic.cp_name' => $cpName]);
  57. })->when($startDate, function ($query, $startDate) {
  58. $realStartDate = date_add(date_create($startDate), date_interval_create_from_date_string('1 day'))
  59. ->format('Y-m-d');
  60. return $query->where('statistic.calculate_date', '>=', $realStartDate);
  61. })->when($endDate, function ($query, $endDate) {
  62. $realEndDate = date_add(date_create($endDate), date_interval_create_from_date_string('1 day'))
  63. ->format('Y-m-d');
  64. return $query->where('statistic.calculate_date', '<=', $realEndDate);
  65. })->when($finalAmountGt0, function ($query) {
  66. return $query->where('statistic.yesterday_final_amount', '>', 0);
  67. })->when($bookSettlementType, function ($query, $bookSettlementType){
  68. return $query->where('statistic.book_settlement_type', $bookSettlementType);
  69. })
  70. ->select('statistic.bid', 'book.name as book_name', 'statistic.cp_name',
  71. 'statistic.yesterday_total_coins', 'statistic.yesterday_available_amount', 'statistic.yesterday_final_amount',
  72. 'statistic.calculate_date', 'statistic.book_settlement_type')
  73. ->orderBy('statistic.calculate_date', 'desc')
  74. ->orderBy('statistic.bid', 'desc');
  75. if($isExport) {
  76. return $sql->get()->map(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. } else {
  83. return $sql->paginate($perPage)->through(function ($item) use ($settlementTypes) {
  84. $item->date = date_sub(date_create($item->calculate_date), date_interval_create_from_date_string('1 day'))
  85. ->format('Y-m-d');
  86. $item->book_settlement_type_str = $settlementTypes[$item->book_settlement_type] ?? '';
  87. return $item;
  88. });
  89. }
  90. }
  91. public function listStatistic(Request $request) {
  92. $this->validate($request, [
  93. 'book_name' => 'nullable|string|max:256',
  94. 'bid' => 'nullable|integer|min:1',
  95. 'cp_name' => 'nullable|string|min:1',
  96. 'start_date' => 'nullable|date_format:Y-m-d',
  97. 'end_date' => 'nullable|date_format:Y-m-d',
  98. 'book_settlement_type' => 'nullable|in:share,bottomline'
  99. ]);
  100. $bookName = $request->input('book_name');
  101. $bid = $request->input('bid');
  102. $cpName = $request->input('cp_name');
  103. $startDate = $request->input('start_date');
  104. $endDate = $request->input('end_date');
  105. $bookSettlementType = $request->input('book_settlement_type');
  106. return [
  107. 'yesterday_total_coins' => 0,
  108. 'yesterday_available_amount' => 0,
  109. 'yesterday_final_amount' => 0
  110. ];
  111. $cpName = $this->getUserCpName() ?? $cpName;
  112. $res = DB::table('cp_subscribe_statistic_data as statistic')
  113. ->leftJoin('books as book', 'statistic.bid' , 'book.id')
  114. ->when($bookName, function ($query, $bookName) {
  115. return $query->where('book.name', 'like', '%'.$bookName. '%');
  116. })->when($bid , function ($query, $bid) {
  117. return $query->where(['statistic.bid' => $bid]);
  118. })->when($cpName, function ($query, $cpName) {
  119. return $query->where(['statistic.cp_name' => $cpName]);
  120. })->when($startDate, function ($query, $startDate) {
  121. $realStartDate = date_add(date_create($startDate), date_interval_create_from_date_string('1 day'))
  122. ->format('Y-m-d');
  123. return $query->where('statistic.calculate_date', '>=', $realStartDate);
  124. })->when($endDate, function ($query, $endDate) {
  125. $realEndDate = date_add(date_create($endDate), date_interval_create_from_date_string('1 day'))
  126. ->format('Y-m-d');
  127. return $query->where('statistic.calculate_date', '<=', $realEndDate);
  128. })->when($bookSettlementType, function ($query, $bookSettlementType){
  129. return $query->where('statistic.book_settlement_type', $bookSettlementType);
  130. })
  131. ->select(
  132. DB::raw('sum(yesterday_total_coins) as yesterday_total_coins'),
  133. DB::raw('sum(yesterday_available_amount) as yesterday_available_amount'),
  134. DB::raw('sum(yesterday_final_amount) as yesterday_final_amount')
  135. )->first();
  136. return $res;
  137. }
  138. /**
  139. * cp结算列表
  140. * @param Request $request
  141. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  142. * @throws \Illuminate\Validation\ValidationException
  143. */
  144. public function monthList(Request $request) {
  145. $this->validate($request, [
  146. 'cp_name' => 'nullable|string',
  147. 'start_month' => 'nullable|date_format:Y-m',
  148. 'end_month' => 'nullable|date_format:Y-m',
  149. 'page' => 'required|integer|min:1',
  150. 'limit' => 'integer|min:10',
  151. 'is_export' => 'integer|in:0,1'
  152. ]);
  153. $cpName = $request->input('cp_name');
  154. $startMonth = $request->input('start_month');
  155. $endMonth = $request->input('end_month');
  156. $isExport = $request->input('is_export', 0);
  157. $finalStateMap = ['notCheck' => '未结算', 'done' => '已结算'];
  158. $cpName = $this->getUserCpName() ?? $cpName;
  159. $sql = DB::table('cp_subscribe_month_statistic_data as data')
  160. ->leftJoin('cps as cp' , 'cp.cp_name', 'data.cp_name')
  161. ->when($cpName, function ($query, $cpName){
  162. return $query->where(['data.cp_name' => $cpName]);
  163. })->when($startMonth, function ($query, $startMonth){
  164. return $query->where('data.month', '>=', $startMonth);
  165. })->when($endMonth, function ($query, $endMonth){
  166. return $query->where('data.month', '<=', $endMonth);
  167. })
  168. ->select('data.id','data.month', 'cp.cp_id', 'cp.cp_name', 'cp.cp_company',
  169. 'data.book_num', 'data.final_amount', 'data.final_state', 'data.final_time')
  170. ->orderBy('data.month', 'desc')
  171. ->orderBy('cp.cp_id', 'desc');
  172. if($isExport) {
  173. return $sql->get()->map(function ($item) use ($finalStateMap){
  174. $item->final_state_str = $finalStateMap[$item->final_state] ?? '';
  175. return $item;
  176. });
  177. }else {
  178. return $sql->paginate($request->integer('limit', 10));
  179. }
  180. }
  181. public function saveFinalState(Request $request){
  182. $this->validate($request, [
  183. 'id' => 'required|integer|min:1',
  184. 'final_state' => 'required|string|in:done'
  185. ]);
  186. $now = date('Y-m-d H:i:s');
  187. DB::table('cp_subscribe_month_statistic_data')
  188. ->where(['id' => $request->input('id'), 'final_state' => 'notCheck'])
  189. ->update([
  190. 'final_state' => $request->input('final_state'),
  191. 'final_time' => $now,
  192. 'updated_at' => $now,
  193. ]);
  194. return 'ok';
  195. }
  196. /**
  197. * 导出某个cp某个月的各个书籍的应结算金额
  198. * @param Request $request
  199. * @return \Illuminate\Support\Collection
  200. * @throws \Illuminate\Validation\ValidationException
  201. */
  202. public function listCpMonthFinalAmount(Request $request) {
  203. $this->validate($request, [
  204. 'cp_name' => 'required|string',
  205. 'month' => 'required|date_format:Y-m'
  206. ]);
  207. $cpName = $request->input('cp_name');
  208. $cpName = $this->getUserCpName() ?? $cpName;
  209. return DB::table('cp_book_month_final_amounts as mfa')
  210. ->leftJoin('books', 'books.id', 'mfa.bid')
  211. ->where([
  212. 'mfa.is_enabled' => 1,
  213. 'mfa.cp_name' => $cpName,
  214. 'mfa.month' => $request->input('month')
  215. ])
  216. ->select('mfa.id', 'mfa.bid', 'books.name as book_name', 'mfa.final_amount', 'mfa.cp_name','mfa.month')
  217. ->get();
  218. }
  219. }