ChannelSellPlatformController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hp
  5. * Date: 2017/12/1
  6. * Time: 17:18
  7. */
  8. namespace App\Http\Controllers\Manage\Channel;
  9. use App\Http\Controllers\Controller;
  10. use App\Http\Controllers\Manage\Channel\Transformers\ChannelBookSellTransformer;
  11. use App\Http\Controllers\Manage\Channel\Transformers\ChannelSellPlatformTransformer;
  12. use App\Modules\Book\Models\Book;
  13. use App\Modules\Channel\Models\ChannelBookSell;
  14. use App\Modules\Channel\Services\ChannelSellPlatformService;
  15. use DB;
  16. use GuzzleHttp\Client;
  17. use Illuminate\Http\Request;
  18. use PhpOffice\PhpSpreadsheet\IOFactory;
  19. /**
  20. * Class ChannelSellPlatformController
  21. * @package App\Http\Controllers\Manage\Channel
  22. */
  23. class ChannelSellPlatformController extends Controller
  24. {
  25. /**
  26. * 根据id 查找信息
  27. * @param Request $request
  28. * @return mixed
  29. */
  30. function getChannelSellPlatformById(Request $request)
  31. {
  32. $id = $request->has('id') ? $request->input('id') : '';
  33. if (empty($id)) {
  34. return response()->error('PARAM_EMPTY');
  35. }
  36. if (!is_numeric($id)) {
  37. return response()->error("PARAM_ERROR");
  38. }
  39. $data = ChannelSellPlatformService::getById($id);
  40. return response()->item(new ChannelSellPlatformTransformer(), $data);
  41. }
  42. /**
  43. * 根据code查找信息
  44. * @param Request $request
  45. * @return mixed
  46. */
  47. function getChannelSellPlatformByCode(Request $request)
  48. {
  49. $code = $request->has('code') ? $request->input('code') : '';
  50. if (empty($code)) {
  51. return response()->error('PARAM_EMPTY');
  52. }
  53. if (!preg_match("/^[a-zA-Z\s]+$/", $code)) {
  54. return response()->error('PARAM_ERROR');
  55. }
  56. $data = ChannelSellPlatformService::getByCode($code);
  57. return response()->item(new ChannelSellPlatformTransformer(), $data);
  58. }
  59. /**
  60. * 获取列表
  61. * @param Request $request
  62. * @return mixed
  63. */
  64. function getList(Request $request)
  65. {
  66. $data = ChannelSellPlatformService::getList();
  67. return response()->pagination(new ChannelSellPlatformTransformer(), $data);
  68. }
  69. /**
  70. * 添加或更新信息
  71. * @param Request $request
  72. * @return mixed
  73. */
  74. function createOrUpdateChannelSellPlatform(Request $request)
  75. {
  76. $name = $request->has('name') ? $request->input('name') : '';
  77. $code = $request->has('code') ? $request->input('code') : '';
  78. $desc = $request->has('desc') ? $request->input('desc') : '';
  79. $bill_type = $request->has('bill_type') ? $request->input('bill_type') : '';
  80. if (!$code) {
  81. return response()->error('PARAM_EMPTY');
  82. }
  83. if (!preg_match("/^[a-zA-Z\s]+$/", $code)) {
  84. return response()->error('PARAM_ERROR');
  85. }
  86. $params = [];
  87. if ($name) {
  88. $params['name'] = $name;
  89. }
  90. if ($desc) {
  91. $params['desc'] = $desc;
  92. }
  93. if ($bill_type) {
  94. $params['bill_type'] = $bill_type;
  95. }
  96. $params['code'] = $code;
  97. $data = ChannelSellPlatformService::getByCode($code);
  98. //存在就更新,不存在就添加
  99. if ($data) {
  100. $res = ChannelSellPlatformService::updateById($data->id, $params);
  101. } else {
  102. $res = ChannelSellPlatformService::addInfo($params);
  103. }
  104. if ($res) {
  105. return response()->success();
  106. } else {
  107. return response()->error("HANDLE_FAILED");
  108. }
  109. }
  110. public function importChannelBookSell(Request $request)
  111. {
  112. if (!$request->hasFile('channel_book_sell')) {
  113. return response()->error('PARAM_ERROR');
  114. }
  115. $file_obj = $request->file('channel_book_sell');
  116. $spreadsheet = IOFactory::load($file_obj->path());
  117. $sheetData = $spreadsheet->getActiveSheet(0)->toArray(null, true, true, true);
  118. \Log::info('sheetData:' . json_encode($sheetData));
  119. $sheetDataLength = count($sheetData);
  120. DB::beginTransaction();
  121. try {
  122. for ($i = 2; $i <= $sheetDataLength; $i++) {
  123. $plaform = $sheetData[$i]['A'];
  124. $zsy_bid = $sheetData[$i]['B'];
  125. $book_name = $sheetData[$i]['C'];
  126. $plaform_code = $sheetData[$i]['D'];
  127. $amount = $sheetData[$i]['E'];
  128. $sub_num = $sheetData[$i]['F'];
  129. $date = $sheetData[$i]['G'];
  130. $month = $sheetData[$i]['H'];
  131. if (!$plaform || !$zsy_bid || !$book_name || !$plaform_code
  132. || !$date || !$sub_num || !$month) {
  133. return response()->error('PARAM_EMPTY');
  134. }
  135. $channelSellPlatform = ChannelSellPlatformService::getByCode($plaform_code);
  136. if ($channelSellPlatform) {
  137. $is_checked = 0;
  138. $is_pushed = 0;
  139. $bill_type = $channelSellPlatform->bill_type;
  140. $data = compact(
  141. 'plaform', 'zsy_bid', 'book_name', 'plaform_code',
  142. 'amount', 'sub_num', 'date', 'month', 'is_checked', 'is_pushed', 'bill_type'
  143. );
  144. ChannelBookSell::addInfo($data);
  145. } else {
  146. DB::rollback();
  147. return ['code'=>304,'msg'=>'第'.$i.'行填写的平台code有误,数据已回滚,请更改后重新上传!'];
  148. }
  149. }
  150. } catch (\Exception $e) {
  151. DB::rollback();
  152. \Log::error($e->getMessage());
  153. return response()->error('UPLOAD_FAILED', $e->getMessage());
  154. }
  155. DB::commit();
  156. return response()->success();
  157. }
  158. /**
  159. * 获取列表
  160. * @param Request $request
  161. * @return mixed
  162. */
  163. function getChannelBookShellList(Request $request)
  164. {
  165. $data = ChannelBookSell::getList();
  166. return response()->pagination(new ChannelBookSellTransformer(), $data);
  167. }
  168. function updateStatus(Request $request)
  169. {
  170. $id = $request->has('id') ? $request->input('id') : '';
  171. $isPush = $request->has('isPush') ? $request->input('isPush') : '';
  172. $isChecked = $request->has('isChecked') ? $request->input('isChecked') : '';
  173. if (!$id) {
  174. return response()->error('PARAM_EMPTY');
  175. }
  176. if (!$isPush && !$isChecked) {
  177. return response()->error('PARAM_ERROR');
  178. }
  179. $params = [];
  180. if ($isPush) {
  181. $params['is_pushed'] = $isPush;
  182. }
  183. if ($isChecked) {
  184. $params['is_checked'] = $isChecked;
  185. }
  186. \Log::info('id is: ' . $id);
  187. $data = ChannelBookSell::getById($id);
  188. if ($isChecked) {
  189. //已审核通过
  190. if ($data && 1 == $data->is_checked) {
  191. return response()->error("ALREADY_CHECKED");
  192. } else {
  193. $res = ChannelBookSell::updateStatus($id, $params);
  194. if ($res) {
  195. return response()->success();
  196. } else {
  197. return response()->error("HANDLE_FAILED");
  198. }
  199. }
  200. } else {
  201. //如果没有审核通过进行推送
  202. if (!$data || (1 != $data->is_checked && 1 == $isPush)) {
  203. return response()->error("NOT_CHECKED");
  204. }
  205. //已经推送了
  206. if ($data && 1 == $isPush && 1 == $data->is_pushed) {
  207. return response()->error("ALREADY_PUSHED");
  208. }
  209. if ($data && 1 == $isPush && 1 == $data->is_checked) {
  210. $content = $this->uploadToYcsd($data);
  211. \Log::info('content is: ' . $content);
  212. if ($content) {
  213. $content = json_decode($content);
  214. if (isset($content->code)) {
  215. if ($content->code == 0) {
  216. \Log::info("推送到原创成功");
  217. ChannelBookSell::updateStatus($id, $params);
  218. return response()->success();
  219. } else {
  220. $data = ['code' => $content->code, 'msg' => $content->msg, 'data' => ''];
  221. return response()->json($data);
  222. }
  223. }
  224. }
  225. }
  226. return response()->error("HANDLE_FAILED");
  227. }
  228. }
  229. function uploadToYcsd($data)
  230. {
  231. $content = '';
  232. try {
  233. $bill_type = $data->bill_type;
  234. $book = Book::getBook($data->zsy_bid);
  235. $daytime = ($bill_type == 'month') ? ($data->month . '00') : $data->date;
  236. $params = [
  237. 'daytime' => $daytime,
  238. 'amount' => $data->amount,
  239. 'platform' => $data->plaform,
  240. 'lybid' => $book ? $book->ly_bid : '',
  241. 'platform_code' => $data->plaform_code,
  242. 'itype' => ($bill_type == 'month') ? 1 : 0,
  243. 'sign' => md5($daytime . 'ycsd2015')
  244. ];
  245. $path = 'http://www.ycsd.cn/ycsdApp/own/?action=getOutChannelPay';
  246. $client = new Client(['timeout' => 3.0,]);
  247. \Log::info($params);
  248. $response = $client->request('POST', $path, ['form_params' => $params]);
  249. $resultCode = $response->getStatusCode();
  250. \Log::info("code is: " . $resultCode);
  251. if ($resultCode == 200 || $resultCode == 302) {
  252. $content = $response->getBody()->getContents();
  253. }
  254. } catch (\Exception $e) {
  255. \Log::info($e->getMessage());
  256. }
  257. return $content;
  258. }
  259. }