SendOrderBatchHandleController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hp
  5. * Date: 2017/11/22
  6. * Time: 11:36
  7. */
  8. namespace App\Http\Controllers\Channel\SendOrder;
  9. use App\Http\Controllers\Channel\BaseController;
  10. use App\Modules\Book\Models\BookConfig;
  11. use App\Modules\Book\Services\BookConfigService;
  12. use App\Modules\Book\Services\BookSubscribleChapterService;
  13. use App\Modules\Book\Services\ChapterService;
  14. use App\Modules\SendOrder\Services\SendOrderService;
  15. use Hashids;
  16. use Illuminate\Http\Request;
  17. use PhpOffice\PhpSpreadsheet\IOFactory;
  18. use DB;
  19. class SendOrderBatchHandleController extends BaseController
  20. {
  21. /**
  22. * @apiDefine sendOrder 派单
  23. */
  24. public function downloadTemplate(Request $request) {
  25. $bids = $request->input('bids',[]);
  26. if(!$bids) {
  27. return response()->error('PARAM_EMPTY');
  28. }
  29. if(!is_array($bids)) {
  30. return response()->error('PARAM_ERROR');
  31. };
  32. foreach ($bids as $key=>$bid) {
  33. $bids[$key] = Hashids::decode($bid)[0];
  34. }
  35. $header = ['书本编号','派单渠道名称','成本', '书名','阅读章节原文','派单类型','推广类型'];
  36. $books = BookConfig::leftjoin('books','books.id','=','book_configs.bid')
  37. ->leftjoin('chapters','chapters.id','=','book_configs.recommend_cid')
  38. ->select(['books.id as bid','book_configs.book_name','chapters.sequence'])
  39. ->whereIn('book_configs.bid',$bids)
  40. ->get();
  41. $data = array();
  42. foreach ($books as $book) {
  43. $data[] = array(
  44. Hashids::encode($book->bid),
  45. '','',
  46. $book->book_name,
  47. $book->sequence,
  48. 1,
  49. 1
  50. );
  51. }
  52. saveExcelData($header,$data,'',[
  53. ['cell'=>'I2','contents'=>'1、推广类型一栏请填写1或者2 ,1代表微信,2代表站外'],
  54. ['cell'=>'I3','contents'=>'2、派单类型一栏请填写1或者2 ,1代表内部,2代表外部'],
  55. ['cell'=>'I4','contents'=>'3、阅读章节原文默认为推荐章节,原文链接为当前章节'],
  56. ['cell'=>'I5','contents'=>'4、同一书本编号可产生多条记录,上传后一条记录对应产生一条派单链接'],
  57. ]);
  58. }
  59. public function importSendOrders(Request $request) {
  60. if(!$request->hasFile('send_orders')) {
  61. return response()->error('PARAM_ERROR');
  62. }
  63. $file_obj = $request->file('send_orders');
  64. $spreadsheet = IOFactory::load($file_obj->path());
  65. $sheetData = $spreadsheet->getActiveSheet(0)->toArray(null, true, true, true);
  66. //\Log::info('sheetData:'.json_encode($sheetData));
  67. $sheetDataLength = count($sheetData);
  68. $distribution_channel_id = $this->getChannelId();
  69. DB::beginTransaction();
  70. try{
  71. for($i=2;$i<=$sheetDataLength;$i++) {
  72. $chapter_sequnce = $sheetData[$i]['E'];
  73. $book_id = $sheetData[$i]['A'];
  74. \Log::info('book_id'.$book_id);
  75. if($book_id){
  76. $bid = Hashids::decode($book_id)[0];
  77. if($bid==2423 && time() >= strtotime('2018-10-10 00:00:00') && time() <= strtotime('2018-10-15 23:59:59')) {
  78. DB::rollback();
  79. return response()->error('UPLOAD_FAILED');
  80. //return response()->error('PARAM_ERROR');
  81. }
  82. $chapter_info = ChapterService::getChapterInfoByBidAndSeq($bid,$chapter_sequnce);
  83. $chapter_id = $chapter_info->id;
  84. $book_info = BookConfig::getBookById($bid);
  85. $subscribe_chapter_seq =$this->getSubscribeChapterNum($book_info,$distribution_channel_id);
  86. $subscribe_chapter_info = ChapterService::getChapterInfoByBidAndSeq($bid,$subscribe_chapter_seq);
  87. $name = $sheetData[$i]['B'];
  88. $cost = $sheetData[$i]['C']?$sheetData[$i]['C']:0;
  89. $book_name = $sheetData[$i]['D'];
  90. $qr_code_id = '';
  91. $chapter_name = $chapter_info?$chapter_info->name:'';
  92. $channel_type = '';
  93. $promotion_type = $sheetData[$i]['F']==1?'INTERNAL':'EXTERNAL';
  94. $subscribe_chapter_id = $subscribe_chapter_info ? $subscribe_chapter_info->id : '';
  95. //$subscribe_chapter_seq = $book_info->force_subscribe_chapter_seq ? $book_info->force_subscribe_chapter_seq : '';
  96. $subscribe_chapter_name = $subscribe_chapter_info ? $subscribe_chapter_info->name : '';
  97. $promotion_point = $sheetData[$i]['G']==1?1:2;
  98. if (!in_array($promotion_point, [1, 2])) {
  99. $promotion_point = 1;
  100. }
  101. //if (!$name || !$channel_type || !$promotion_type || !$book_id || !$book_name || !$chapter_id || !$chapter_name || !$subscribe_chapter_id || !$subscribe_chapter_seq || !$subscribe_chapter_name) return response()->error('PARAM_EMPTY');
  102. if (empty($qr_code_id)) {
  103. $qr_code_id = 0;
  104. }
  105. $redirect_url = "/reader?bid={$book_id}&cid={$chapter_info->id}";
  106. $book_id = Hashids::decode($book_id)[0];
  107. if ($promotion_point == 2) {
  108. $domain = 'chunnuan555.com';
  109. } else {
  110. $domain = $this->getDomainByBid($book_id);
  111. }
  112. $charge_type = $this->getBookChargeTypeByBid($book_id);
  113. $sendOrder = SendOrderService::createFromDirectory(compact(
  114. 'name',
  115. 'channel_type',
  116. 'promotion_type',
  117. 'charge_type',
  118. 'cost',
  119. 'domain',
  120. 'book_id',
  121. 'qr_code_id',
  122. 'book_name',
  123. 'chapter_id',
  124. 'subscribe_chapter_id',
  125. 'subscribe_chapter_name',
  126. 'subscribe_chapter_seq',
  127. 'chapter_name',
  128. 'distribution_channel_id',
  129. 'redirect_url',
  130. 'promotion_point'
  131. ));
  132. // $promotion_url = '/yun/' . $sendOrder->id;
  133. if ($promotion_point == 2) {
  134. $promotion_url = 'https://bsite' . encodeDistributionChannelId($distribution_channel_id) . '.chunnuan555.com/yun/' . $sendOrder->id;
  135. } else {
  136. $promotion_url = 'https://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . $this->getDomainByBid($book_id) . '/yun/' . $sendOrder->id;
  137. }
  138. }
  139. }
  140. }catch (\Exception $e) {
  141. DB::rollback();
  142. \Log::error($e->getMessage());
  143. return response()->error('UPLOAD_FAILED',$e->getMessage());
  144. }
  145. DB::commit();
  146. return response()->success();
  147. }
  148. /**
  149. * 根据图书id获取域名
  150. * @param $bid
  151. */
  152. function getDomainByBid($bid = '')
  153. {
  154. $domain = 'chunnuan555.com';
  155. return $domain;
  156. }
  157. /**
  158. * 根据图书id获取图书的收费类型
  159. * @param string $bid
  160. */
  161. function getBookChargeTypeByBid($bid = '')
  162. {
  163. $charge_type = '';
  164. $bookConfig = BookConfigService::getBookById($bid);
  165. if ($bookConfig) {
  166. $charge_type = $bookConfig->charge_type;
  167. }
  168. return $charge_type;
  169. }
  170. protected function getSubscribeChapterNum($book,$distribution_channel_id)
  171. {
  172. //渠道设置的强关
  173. $subscribe = BookSubscribleChapterService::getSubcribleChapter($book->bid, $distribution_channel_id);
  174. if ($subscribe) {
  175. return (isset($subscribe->subscribe_chapter_id) && $subscribe->subscribe_chapter_id > 0) ? $subscribe->subscribe_chapter_id : $book->force_subscribe_chapter_seq;
  176. }
  177. //默认强关
  178. return $book->force_subscribe_chapter_seq;
  179. }
  180. }