CompanyController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2018/3/23
  6. * Time: 上午9:09
  7. */
  8. namespace App\Http\Controllers\Manage\Channel;
  9. use App\Http\Controllers\Manage\BaseController;
  10. use App\Http\Controllers\Manage\Channel\Transformers\ChannelTransformer;
  11. use App\Http\Controllers\Manage\Channel\Transformers\CompanyTransformer;
  12. use App\Modules\Book\Services\BookService;
  13. use App\Modules\Channel\Services\ChannelUserService;
  14. use App\Modules\Channel\Services\CompanyService;
  15. use App\Modules\Channel\Services\CompanySpecialBookService;
  16. use Illuminate\Http\Request;
  17. class CompanyController extends BaseController
  18. {
  19. /**
  20. * @apiDefine channel 渠道
  21. */
  22. /**
  23. * @apiVersion 1.0.0
  24. * @apiDescription 添加公司
  25. * @api {POST} channel/company/add 添加公司
  26. * @apiGroup channel
  27. * @apiName channel/company/add
  28. * @apiParam {String} name 公司名称
  29. * @apiSuccessExample {json} Success-Response:
  30. *
  31. * {
  32. * "code": 0,
  33. * "msg": "",
  34. * "data":{}
  35. * }
  36. */
  37. public function addCompany(Request $request) {
  38. $name = $request->has('name') ? $request->input('name') : '';
  39. if(empty($name)) {
  40. return response()->error("PARAM_EMPTY");
  41. }
  42. $company = CompanyService::findByName($name);
  43. if($company) {
  44. return response()->error("COMPANY_EXIST");
  45. }
  46. $data = ['name' => $name, 'is_important' => 0];
  47. $company = CompanyService::addCompany($data);
  48. return response()->item(new CompanyTransformer(), $company);
  49. }
  50. /**
  51. * @apiVersion 1.0.0
  52. * @apiDescription 删除公司
  53. * @api {POST} channel/company/remove 删除公司
  54. * @apiGroup channel
  55. * @apiName channel/company/remove
  56. * @apiParam {Number} id 公司ID
  57. * @apiSuccessExample {json} Success-Response:
  58. *
  59. * {
  60. * "code": 0,
  61. * "msg": "",
  62. * "data":{}
  63. * }
  64. */
  65. public function rmCompany(Request $request) {
  66. $id = $request->has('id') ? $request->input('id') : '';
  67. if(empty($id)) {
  68. return response()->error("PARAM_EMPTY");
  69. }
  70. $company = CompanyService::getCompany($id);
  71. if(!$company) {
  72. return response()->error("PARAM_ERROR");
  73. }
  74. CompanySpecialBookService::rmCompany($id);
  75. $company->delete();
  76. return response()->success();
  77. }
  78. /**
  79. * @apiVersion 1.0.0
  80. * @apiDescription 更新公司
  81. * @api {POST} channel/company/update 更新公司
  82. * @apiGroup channel
  83. * @apiName channel/company/update
  84. * @apiParam {Number} id 公司ID
  85. * @apiParam {String} [name] 公司名称
  86. * @apiParam {Number} [is_important] 是否重要 0:不是重点,默认 1:重点商户
  87. * @apiParam {Number} [distribution_manages_id] 商务ID
  88. * @apiSuccessExample {json} Success-Response:
  89. *
  90. * {
  91. * "code": 0,
  92. * "msg": "",
  93. * "data":{}
  94. * }
  95. */
  96. public function updateCompany(Request $request) {
  97. $id = $request->has('id') ? $request->input('id') : '';
  98. if(empty($id)) {
  99. return response()->error("PARAM_EMPTY");
  100. }
  101. $is_important = $request->input('is_important','');
  102. if($request->has('is_important') && !in_array($is_important, [0,1])) {
  103. return response()->error("PARAM_ERROR");
  104. }
  105. if(!CompanyService::getCompany($id)) {
  106. return response()->error("PARAM_ERROR");
  107. }
  108. $name = $request->has('name') ? $request->input('name') : '';
  109. $data = [];
  110. if($name){
  111. $data['name'] =$name;
  112. }
  113. if($request->has('is_important') && in_array($is_important,[0,1])) {
  114. $data['is_important'] = $is_important;
  115. }
  116. //$data = ['name' => $name, 'is_important' => $is_important];
  117. if($this->getLoginUserRole() == "business_leader" || $this->getLoginUserRole() == "admin" || $this->getLoginUserRole()=="business") {
  118. //只有商务主管能修改 公司对应商务,
  119. $distribution_manages_id = $request->has('distribution_manages_id') ? $request->input('distribution_manages_id') : '';
  120. if(is_numeric($distribution_manages_id) && $distribution_manages_id > 0) {
  121. //更新公司对应用户的所属商务
  122. $data['distribution_manages_id']= $distribution_manages_id;
  123. ChannelUserService::updateChannelUserManager($distribution_manages_id, $id);
  124. }
  125. }
  126. $fans_type = $request->input('fans_gender','');
  127. if($fans_type){
  128. if(!in_array($fans_type,[1,2,3])){
  129. return response()->error('PARAM_ERROR');
  130. }
  131. $data['fans_gender'] = $fans_type;
  132. }
  133. if($data){
  134. //\Log::info('delete_company_books:rmCompany:$id'.$id);
  135. $company_old = CompanyService::getCompany($id);
  136. $company = CompanyService::updateCompany($id, $data);
  137. if($request->has('is_important') && $is_important == 0) {
  138. //设置为普通商户之后,对应书籍删除
  139. \Log::info('delete_company_books:rmCompany:$id'.$id.'is_important:'.$is_important);
  140. CompanySpecialBookService::rmCompany($id);
  141. }
  142. if($request->has('is_important') && $is_important == 1 && $company_old->is_important==0) {
  143. \Log::info('delete_company_books:rmCompany:$id'.$id.'is_important:'.$is_important);
  144. CompanySpecialBookService::rmCompany($id);
  145. $bids = CompanySpecialBookService::getBidByFirst();
  146. if($bids) {
  147. foreach ($bids as $i) {
  148. CompanySpecialBookService::addBook($i, $id);
  149. }
  150. }
  151. }
  152. }
  153. $company = CompanyService::getCompany($id);
  154. return response()->item(new CompanyTransformer(), $company);
  155. }
  156. /**
  157. * @apiVersion 1.0.0
  158. * @apiDescription 公司列表分页
  159. * @api {GET} channel/company/list 公司列表分页
  160. * @apiGroup channel
  161. * @apiName channel/company/list
  162. * @apiParam {String} [search_name] 搜索名称
  163. * @apiParam {Number} [is_important] 是否重要
  164. * @apiSuccess {Number} id 公司ID
  165. * @apiSuccess {String} name 公司名称
  166. * @apiSuccess {Number} is_important 是否重要
  167. * @apiSuccess {String} create_time 创建时间
  168. * @apiSuccessExample {json} Success-Response:
  169. *
  170. * {
  171. * "code": 0,
  172. * "msg": "",
  173. * "data": {
  174. * "list": [
  175. * {
  176. * "id": 2,
  177. * "name": "杭州掌维科技有限公司",
  178. * "is_important": 0,
  179. * "create_time": "2018-03-23 09:32:29"
  180. * },
  181. * ],
  182. * "meta": {
  183. * "total": 2,
  184. * "per_page": 15,
  185. * "current_page": 1,
  186. * "last_page": 1,
  187. * "next_page_url": "",
  188. * "prev_page_url": ""
  189. * }
  190. * }
  191. * }
  192. */
  193. public function getList(Request $request) {
  194. $search_name = $request->has('search_name') ? $request->input('search_name') : '';
  195. $is_important = $request->input('is_important');
  196. $searchData = [
  197. 'search_name' => $search_name,
  198. 'is_important' => $is_important,
  199. ];
  200. $manager_info= json_decode(json_encode(unserialize($request->session()->get('manage_user'))),true);
  201. $manager_id = '';
  202. if($manager_info['role']=='business'){
  203. $manager_id = $manager_info['id'];
  204. }
  205. $data = CompanyService::getList($searchData,false,$manager_id);
  206. return response()->pagination(new CompanyTransformer(), $data);
  207. }
  208. /**
  209. * @apiVersion 1.0.0
  210. * @apiDescription 公司列表
  211. * @api {GET} channel/company/allList 公司列表
  212. * @apiGroup channel
  213. * @apiName channel/company/allList
  214. * @apiParam {String} [search_name] 搜索名称
  215. * @apiParam {Number} [is_important] 是否重要
  216. * @apiSuccess {Number} id 公司ID
  217. * @apiSuccess {String} name 公司名称
  218. * @apiSuccess {Number} is_important 是否重要
  219. * @apiSuccess {String} create_time 创建时间
  220. * @apiSuccessExample {json} Success-Response:
  221. *
  222. * {
  223. * "code": 0,
  224. * "msg": "",
  225. * "data":[
  226. * {
  227. * "id": 2,
  228. * "name": "杭州掌维科技有限公司",
  229. * "is_important": 0,
  230. * "create_time": "2018-03-23 09:32:29"
  231. * }
  232. * ]
  233. * }
  234. */
  235. public function getAllList(Request $request) {
  236. $search_name = $request->has('search_name') ? $request->input('search_name') : '';
  237. $is_important = $request->input('is_important');
  238. $searchData = [
  239. 'search_name' => $search_name,
  240. 'is_important' => $is_important,
  241. ];
  242. $data = CompanyService::getList($searchData, true);
  243. return response()->collection(new CompanyTransformer(), $data);
  244. }
  245. /**
  246. * @apiVersion 1.0.0
  247. * @apiDescription 公司列表图书ID
  248. * @api {GET} channel/company/listByBookId 公司列表图书ID
  249. * @apiGroup channel
  250. * @apiName channel/company/listByBookId
  251. * @apiParam {Number} bid 书籍ID
  252. * @apiSuccess {Number} id 公司ID
  253. * @apiSuccess {String} name 公司名称
  254. * @apiSuccess {Number} is_important 是否重要
  255. * @apiSuccess {String} create_time 创建时间
  256. * @apiSuccessExample {json} Success-Response:
  257. *
  258. * {
  259. * "code": 0,
  260. * "msg": "",
  261. * "data":[
  262. * {
  263. * "id": 2,
  264. * "name": "杭州掌维科技有限公司",
  265. * "is_important": 0,
  266. * "create_time": "2018-03-23 09:32:29"
  267. * }
  268. * ]
  269. * }
  270. */
  271. public function getListByBookId(Request $request) {
  272. $bid = $request->has('bid') ? $request->input('bid') : '';
  273. if(empty($bid) || !is_numeric($bid)) {
  274. return response()->error("PARAM_ERROR");
  275. }
  276. $data = CompanySpecialBookService::findCompanyToBid($bid);
  277. return response()->collection(new CompanyTransformer(), $data);
  278. }
  279. /**
  280. * @apiVersion 1.0.0
  281. * @apiDescription 获取公司渠道列表
  282. * @api {GET} channel/company/getChannelList 获取公司渠道列表
  283. * @apiGroup channel
  284. * @apiName channel/company/getChannelList
  285. * @apiParam {Number} company_id 公司id
  286. * @apiParam {Number} channel_user_id 账户id
  287. *
  288. * @apiSuccess {Number} id 渠道ID.
  289. * @apiSuccess {String} name 渠道名称.
  290. * @apiSuccess {String} phone 手机号码.
  291. * @apiSuccess {String} pay_merchant 支付商户.
  292. * @apiSuccess {String} nickname 昵称.
  293. * @apiSuccess {String} latest_login_time 最后登陆时间.
  294. * @apiSuccess {String} latest_login_ip 最后登陆IP.
  295. * @apiSuccess {String} remark 备注.
  296. * @apiSuccess {String} register_ip 注册IP.
  297. * @apiSuccess {String} person_in_charge_name 负责人.
  298. * @apiSuccess {String} create_time 注册时间
  299. * @apiSuccess {Number} distribution_manages_id 管理员ID
  300. * @apiSuccess {String} distribution_manages_account 管理员
  301. * @apiSuccess {String} distribution_manages_number 管理员
  302. * @apiSuccess {String} distribution_manages_nickname 管理员昵称
  303. * @apiSuccess {String} price_rate 章节价格
  304. * @apiSuccessExample {json} Success-Response:
  305. *
  306. * {
  307. * "code": 0,
  308. * "msg": "",
  309. * "data": [
  310. * {
  311. * "id": 1,
  312. * "phone": "",
  313. * "name": "121",
  314. * "pay_merchant_id": 1,
  315. * "nickname": "是说",
  316. * "latest_login_time": "",
  317. * "latest_login_ip": "",
  318. * "is_enabled": 1,
  319. * "register_ip": "",
  320. * "remark": "",
  321. * "person_in_charge_name": "波哥帅",
  322. * "create_time": "2017-11-20 18:34:17",
  323. * "distribution_manages_id": 0,
  324. * "distribution_manages_account": null,
  325. * "distribution_manages_number": null,
  326. * "distribution_manages_nickname": null
  327. * }
  328. * ]
  329. * }
  330. */
  331. public function getChannelList(Request $request) {
  332. $company_id = $request->has('company_id') ? $request->input('company_id') : '';
  333. $channel_user_id = $request->has('channel_user_id') ? $request->input('channel_user_id') : '';
  334. if($company_id && !is_numeric($company_id) && !is_numeric($channel_user_id)) {
  335. return response()->error("PARAM_ERROR");
  336. }
  337. if($company_id == 0) {
  338. $channelUser = ChannelUserService::getById($channel_user_id);
  339. if($channelUser) {
  340. $company_id = $channelUser->company_id;
  341. }
  342. }
  343. $channelUserIds = [];
  344. if(is_numeric($company_id) && $company_id > 0) {
  345. $channelUserIds = ChannelUserService::getChannelUserIdListByCompany($company_id);
  346. } else {
  347. $channelUserIds[] = $channel_user_id;
  348. }
  349. // $channelUserIds = ChannelUserService::getChannelUserIdListByCompany($company_id);
  350. $channels = ChannelUserService::getChannelListByChannelUserIds($channelUserIds);
  351. foreach ($channels as &$v){
  352. $price_rate = BookService::getChapterPrice($v->id);
  353. $default_rate = (float)env('DEFAULT_CHAPTER_PRICE',0.015)*100;
  354. $v->price_rate = $price_rate?$price_rate:$default_rate;
  355. }
  356. return response()->collection(new ChannelTransformer(), $channels);
  357. }
  358. /**
  359. * @apiVersion 1.0.0
  360. * @apiDescription 添加书籍
  361. * @api {POST} channel/company/addBook 添加书籍
  362. * @apiGroup channel
  363. * @apiName channel/company/addBook
  364. * @apiParam {Number} bid 书籍ID
  365. * @apiParam {Number} company_ids 公司ids [1,2,3,4]
  366. * @apiSuccessExample {json} Success-Response:
  367. *
  368. * {
  369. * "code": 0,
  370. * "msg": "",
  371. * "data":{}
  372. * }
  373. */
  374. public function addCompanyBook(Request $request) {
  375. $bid = $request->has('bid') ? $request->input('bid') : '';
  376. $company_ids = $request->has('company_ids') ? $request->input('company_ids') : '';
  377. if(!is_numeric($bid)) {
  378. return response()->error("PARAM_ERROR");
  379. }
  380. // if(count($ids) == 0) {
  381. // return response()->error("PARAM_ERROR");
  382. // }
  383. \Log::info('delete_special_book_log:addCompanyBook:$bid:'.$bid);
  384. CompanySpecialBookService::rmAllBook($bid);
  385. if(!empty($company_ids)) {
  386. $ids = explode(',', $company_ids);
  387. foreach ($ids as $id) {
  388. $data = CompanySpecialBookService::addBook($bid, $id);
  389. }
  390. }
  391. return response()->success();
  392. }
  393. /**
  394. * @apiVersion 1.0.0
  395. * @apiDescription 删除书籍
  396. * @api {POST} channel/company/rmBook 删除书籍
  397. * @apiGroup channel
  398. * @apiName channel/company/rmBook
  399. * @apiParam {Number} bid 书籍ID
  400. * @apiParam {Number} company_ids 公司id[1,2,3,4]
  401. * @apiSuccessExample {json} Success-Response:
  402. *
  403. * {
  404. * "code": 0,
  405. * "msg": "",
  406. * "data":{}
  407. * }
  408. */
  409. public function rmCompanyBook(Request $request) {
  410. $bid = $request->has('bid') ? $request->input('bid') : '';
  411. $company_ids = $request->has('company_ids') ? $request->input('company_ids') : '';
  412. if(!is_numeric($bid) || empty($company_ids)) {
  413. return response()->error("PARAM_ERROR");
  414. }
  415. $ids = explode(',', $company_ids);
  416. if(count($ids) == 0) {
  417. return response()->error("PARAM_ERROR");
  418. }
  419. \Log::info('delete_special_book_log:rmCompanyBook:ids:'.json_encode($ids).'bid:'.$bid);
  420. foreach ($ids as $id) {
  421. $data = CompanySpecialBookService::rmBook($bid, $id);
  422. }
  423. return response()->success();
  424. }
  425. /**
  426. * 设置商务公司所在的城市
  427. * @param Request $request
  428. * @return mixed
  429. */
  430. function setChannelCityInfo(Request $request)
  431. {
  432. $city = $request->has('city') ? $request->input('city') : '';
  433. $companyId = $request->has('companyId') ? trim($request->input('companyId')) : '';
  434. if (empty($companyId) || empty($city)) {
  435. return response()->error('PARAM_ERROR');
  436. }
  437. $result = CompanyService::updateCompany($companyId, ['city' => $city]);
  438. if ($result) {
  439. return response()->success();
  440. }
  441. return response()->error("HANDLE_FAILED");
  442. }
  443. }