CpSubscribeStatisticDataController.php 11 KB

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