Platform.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ChangeLog;
  4. use think\App;
  5. use think\Exception;
  6. use think\facade\Db;
  7. use think\facade\Validate;
  8. //平台
  9. class Platform extends Base
  10. {
  11. public function create()
  12. {
  13. $param = $this->request->only([
  14. 'platform_name',
  15. 'platform_type',
  16. 'use_type',
  17. 'is_select_pay_rate' => 0,
  18. 'desc' => '0',
  19. 'status' => 0,
  20. 'pay_list' => []
  21. ], 'post', 'trim');
  22. $val = Validate::rule([
  23. 'platform_name|平台名称' => 'require|max:255',
  24. 'platform_type|对接平台' => 'require|number|in:0,1,2',
  25. 'use_type|对接类型' => 'require|number|in:0,1,2',
  26. 'is_select_pay_rate|是否开启支付渠道' => 'require|number|in:0,1',
  27. 'desc|权重' => 'float',
  28. 'status|状态' => 'number|in:0,1',
  29. 'pay_list|渠道配置' => 'requireIf:is_select_pay_rate,1|array|max:100'
  30. ]);
  31. if ($val->check($param) == false) return json_show(1004, $val->getError());
  32. $action_id = $this->uid;//isset($user["data"]['id']) ? $user["data"]['id'] : "";
  33. $action_name = $this->uname;//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  34. Db::startTrans();
  35. try {
  36. $tmp = Db::name('platform')
  37. ->field('id')
  38. ->where(['is_del' => 0, 'platform_name' => $param['platform_name']])
  39. ->findOrEmpty();
  40. if (!empty($tmp)) throw new Exception('该平台名称已存在');
  41. $date = date('Y-m-d H:i:s');
  42. $platform_code = makeNo("PT");
  43. $data = [
  44. "platform_code" => $platform_code,
  45. "platform_name" => $param['platform_name'],
  46. "platform_type" => $param['platform_type'],
  47. "use_type" => $param['use_type'],
  48. "desc" => $param['desc'],
  49. "createrid" => $action_id,
  50. "creater" => $action_name,
  51. 'is_select_pay_rate' => $param['is_select_pay_rate'],
  52. // 'pay_title' => $param['pay_title'],
  53. "status" => $param['status'],
  54. "is_del" => 0,
  55. "addtime" => $date,
  56. "updatetime" => $date
  57. ];
  58. $platform_id = Db::name("platform")->insertGetId($data);
  59. if (!$platform_id) throw new Exception();
  60. //新增平台分类
  61. $this->addPlat($platform_id);
  62. if ($param['is_select_pay_rate'] == 1) {
  63. $weight=0;
  64. foreach ($param['pay_list'] as $pay) {
  65. $insert_da[] = [
  66. 'platform_id' => $platform_id,
  67. 'channel_id' => $pay['channel_id'],
  68. 'apply_id' => $this->uid,
  69. 'apply_name' => $this->uname,
  70. 'weight' => $weight++,
  71. 'is_del' => 0,
  72. "addtime" => $date,
  73. "updatetime" => $date
  74. ];
  75. }
  76. if ($insert_da) Db::name('platform_channel')->insertAll($insert_da);
  77. }
  78. Db::commit();
  79. return json_show(0, '新建成功');
  80. } catch (Exception $exception) {
  81. Db::rollback();
  82. return json_show(1004, '新建失败,' . $exception->getMessage());
  83. }
  84. }
  85. public function list()
  86. {
  87. $param = $this->request->only([
  88. 'page' => 1,
  89. 'size' => 10,
  90. 'platform_name' => '',
  91. 'platform_type' => '',
  92. 'status' => '',
  93. 'start' => '',
  94. 'end' => '',
  95. 'is_show' => 1,
  96. 'creater' => ''
  97. ], 'post', 'trim');
  98. $where = [["p.is_del", "=", 0]];
  99. if ($param['platform_name'] != "") $where[] = ['p.platform_name', "like", '%' . $param['platform_name'] . '%'];
  100. if ($param['platform_type'] !== "") $where[] = ['p.platform_type', "=", $param['platform_type']];
  101. if ($param['status'] !== "") $where[] = ['p.status', "=", $param['status']];
  102. if ($param['start'] !== "") $where[] = ['p.addtime', ">=", date('Y-m-d H:i:s', strtotime($param['start']))];
  103. if ($param['end'] !== "") $where[] = ['p.addtime', "<", date('Y-m-d H:i:s', strtotime($param['end']) + 24 * 3600)];
  104. if ($param['is_show'] == 1) {
  105. $role = $this->checkRole();
  106. if (!empty($role['platform'])) $where[] = ["p.id", "in", $role['platform']];
  107. }
  108. if ($param['creater'] != '') $where[] = ['p.creater', 'like', '%' . $param['creater'] . '%'];
  109. $count = Db::name('platform')
  110. ->alias('p')
  111. ->where($where)
  112. ->count('p.id');
  113. $list = Db::name('platform')
  114. ->alias('p')
  115. ->where($where)
  116. ->page($param['page'], $param['size'])
  117. ->order(['p.desc' => 'desc', 'p.id' => 'desc'])
  118. ->select()
  119. ->toArray();
  120. $createrid = array_column($list, 'createrid');
  121. $company = get_company_name_by_uid($createrid);
  122. foreach ($list as &$value) {
  123. $value['company_name'] = $company[$value['createrid']] ?? '';
  124. }
  125. return app_show(0, "获取成功", ['list' => $list, 'count' => $count]);
  126. }
  127. public function edit()
  128. {
  129. $param = $this->request->only([
  130. 'id',
  131. 'platform_name',
  132. 'platform_type',
  133. 'use_type',
  134. 'is_select_pay_rate' => 0,
  135. 'desc' => 0,
  136. 'status' => 0,
  137. 'pay_list' => []
  138. ], 'post', 'trim');
  139. $val = Validate::rule([
  140. 'id' => 'require|number|gt:0',
  141. 'platform_name|平台名称' => 'require|max:255',
  142. 'platform_type|对接平台' => 'require|number|in:0,1,2',
  143. 'use_type|对接类型' => 'require|number|in:0,1,2',
  144. 'is_select_pay_rate|是否开启支付渠道' => 'require|number|in:0,1',
  145. 'desc|排序权重' => 'float',
  146. 'status|状态' => 'number|in:0,1',
  147. 'pay_list|渠道配置' => 'requireIf:is_select_pay_rate,1|array|max:100'
  148. ]);
  149. if ($val->check($param) == false) return json_show(1004, $val->getError());
  150. Db::startTrans();
  151. try {
  152. $info = Db::name('platform')
  153. ->where(['is_del' => 0, 'id' => $param['id']])
  154. ->findOrEmpty();
  155. if (empty($info)) throw new Exception('未找到数据');
  156. $tmp = Db::name('platform')
  157. ->field('id')
  158. ->where(['is_del' => 0, 'platform_name' => $param['platform_name']])
  159. ->where('id', '<>', $param['id'])
  160. ->findOrEmpty();
  161. if (!empty($tmp)) throw new Exception('平台名称已存在');
  162. $date = date('Y-m-d H:i:s');
  163. $data = [
  164. "platform_name" => $param['platform_name'],
  165. "platform_type" => $param['platform_type'],
  166. "use_type" => $param['use_type'],
  167. "desc" => $param['desc'],
  168. 'is_select_pay_rate' => $param['is_select_pay_rate'],
  169. "status" => $param['status'],
  170. "updatetime" => $date
  171. ];
  172. Db::name("platform")->where(['is_del' => 0, 'id' => $param['id']])->update($data);
  173. $temp = array_diff($data, $info);
  174. $json = json_encode($temp, JSON_UNESCAPED_UNICODE);
  175. $jsp = json_encode($info, JSON_UNESCAPED_UNICODE);
  176. ChangeLog::logAdd(8, $info['platform_code'], $jsp, $json, ['id' => $this->uid, 'nickname' => $this->uname]);
  177. if ($param['is_select_pay_rate'] == 1) {
  178. $weight = 0;
  179. foreach ($param['pay_list'] as $pay) {
  180. $insert_da = [
  181. "id"=>$pay["id"]??null,
  182. 'platform_id' => $info['id'],
  183. 'channel_id' => $pay['channel_id'],
  184. 'apply_id' => $this->uid,
  185. 'apply_name' => $this->uname,
  186. 'weight' => $weight++,
  187. 'is_del' => $pay['is_del']??0,
  188. "addtime" => $date,
  189. "updatetime" => $date
  190. ];
  191. $up =Db::name("platform_channel")->save($insert_da);
  192. if($up==false) throw new \Exception("平台渠道信息更新失败");
  193. }
  194. }
  195. Db::commit();
  196. return json_show(0, '更新成功');
  197. } catch (Exception $exception) {
  198. Db::rollback();
  199. return json_show(1004, '更新失败,' . $exception->getMessage());
  200. }
  201. }
  202. public function info()
  203. {
  204. $id = $this->request->filter('trim')->post('id/d', 0);
  205. $info = Db::name('platform')
  206. ->where(['id' => $id, 'is_del' => 0])
  207. ->find();
  208. if (empty($info)) return error_show(1004, '未找到数据');
  209. else {
  210. $info['pay_list'] = Db::name('platform_channel')->alias("a")
  211. ->leftJoin("channel b","a.channel_id=b.id")
  212. ->field('a.id,b.companyNo,b.companyName,b.channel_name,a.channel_id,a.is_del')
  213. ->where(['a.platform_id' => $info['id'], 'a.is_del' => 0,"b.is_del"=>0])
  214. ->order(['weight' => 'desc'])
  215. ->select()
  216. ->toArray();
  217. return app_show(0, "获取成功", $info);
  218. }
  219. }
  220. public function del()
  221. {
  222. $id = $this->request->filter('trim')->post('id/d', 0);
  223. Db::startTrans();
  224. try {
  225. $rs = Db::name("platform")
  226. ->where(['is_del' => 0, 'id' => $id])
  227. ->update([
  228. 'is_del' => 1,
  229. 'updatetime' => date("Y-m-d H:i:s"),
  230. ]);
  231. if ($rs == 0) throw new Exception('该平台不存在');
  232. Db::commit();
  233. return json_show(0, '删除成功');
  234. } catch (Exception $exception) {
  235. Db::rollback();
  236. return json_show(1004, '删除失败,' . $exception->getMessage());
  237. }
  238. }
  239. public function status()
  240. {
  241. $param = $this->request->only(['id', 'status'], 'post', 'trim');
  242. $val = Validate::rule([
  243. 'id' => 'require|number|gt:0',
  244. 'status|状态' => 'require|number|in:0,1',
  245. ]);
  246. if ($val->check($param) == false) return json_show(1004, $val->getError());
  247. Db::startTrans();
  248. try {
  249. $rs = Db::name("platform")
  250. ->where(['is_del' => 0, 'id' => $param['id']])
  251. ->where('status', '<>', $param['status'])
  252. ->update([
  253. 'status' => $param['status'],
  254. 'updatetime' => date("Y-m-d H:i:s"),
  255. ]);
  256. if ($rs == 0) throw new Exception('该平台不存在或重复操作');
  257. Db::commit();
  258. return json_show(0, '操作成功');
  259. } catch (Exception $exception) {
  260. Db::rollback();
  261. return json_show(1004, '操作失败,' . $exception->getMessage());
  262. }
  263. }
  264. /** 平台添加分类 **/
  265. private function addPlat($plat_id)
  266. {
  267. $tmp = Db::name('cat_plat')
  268. ->where(["platform_id" => $plat_id, "is_del" => 0])
  269. ->column('id', 'cat_id');
  270. $cat = Db::name("cat")
  271. ->where(["status" => 1, "is_del" => 0])
  272. ->cursor();
  273. $insert_data = [];
  274. $date = date("Y-m-d H:i:s");
  275. foreach ($cat as $value) {
  276. $temp = [
  277. 'cat_id' => $value['id'],
  278. 'platform_id' => $plat_id,
  279. 'fund_code' => $value['fund_code'],
  280. 'status' => $value['status'],
  281. 'apply_id' => $value['createrid'],
  282. 'apply_name' => $value['creater'],
  283. 'addtime' => $date,
  284. 'updatetime' => $date,
  285. ];
  286. if (isset($tmp[$value['id']])) {
  287. unset($temp['cat_id']);
  288. unset($temp['addtime']);
  289. Db::name("cat_plat")
  290. ->where(["id" => $tmp[$value['id']], "is_del" => 0])
  291. ->update($temp);
  292. } else $insert_data[] = $temp;
  293. }
  294. if ($insert_data) Db::name("cat_plat")->insertAll($insert_data);
  295. }
  296. //获取当前账号所能看到平台
  297. public function getPlatformListByUid()
  298. {
  299. $param = $this->request->only(['page' => 1, 'size' => 10, 'platform_code' => '', 'platform_name' => '', 'platform_type' => '', 'use_type' => ''], 'post', 'trim');
  300. $where = [['is_del', '=', 0]];
  301. if ($param['platform_code'] !== '') $where[] = ['platform_code', 'like', '%' . $param['platform_code'] . '%'];
  302. if ($param['platform_name'] !== '') $where[] = ['platform_name', 'like', '%' . $param['platform_name'] . '%'];
  303. if ($param['platform_type'] !== '') $where[] = ['platform_type', '=', $param['platform_type']];
  304. if ($param['use_type'] !== '') $where[] = ['use_type', '=', $param['use_type']];
  305. if ($this->level == 2) {
  306. $ids = Db::name('user_platform')
  307. ->where(['uid' => $this->uid, 'is_del' => 0])
  308. ->value('platform');
  309. $where[] = ['id', 'in', $ids];
  310. }
  311. $count = Db::name('platform')
  312. ->where($where)
  313. ->count('id');
  314. $list = Db::name('platform')
  315. ->field('id,platform_code,platform_name,platform_type,use_type,status')
  316. ->where($where)
  317. ->order(['desc' => 'desc', 'id' => 'desc'])
  318. ->page($param['page'], $param['size'])
  319. ->select()
  320. ->toArray();
  321. return json_show(0, '获取成功', ['count' => $count, 'list' => $list]);
  322. }
  323. public function platformSourceCreate(){
  324. $param=$this->request->param(["platform_id","source"],"post","trim");
  325. $valid=Validate::rule(["platform_id|平台id"=>"require|number|gt:0","source|渠道名称"=>"require|max:255"]);
  326. if($valid->check($param)==false)$this->error($valid->getError());
  327. $data=["platform_id"=>$param["platform_id"],"source"=>$param["source"],"apply_id"=>$this->uid,
  328. "apply_name"=>$this->uname];
  329. Db::name("platform_source")->save($data);
  330. $this->success("创建成功");
  331. }
  332. public function platformSourceDelete(){
  333. $param=$this->request->param(['id'],'post','trim');
  334. $info =Db::name("platform_source")->findOrEmpty($param['id']);
  335. if(empty($info))$this->error("未找到数据");
  336. $info["is_del"]=1;
  337. $info["updatetime"]=date("Y-m-d H:i:s");
  338. Db::name('platform_source')->save($info);
  339. $this->success('删除成功');
  340. }
  341. }