EventRegistration.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\ump;
  12. use app\admin\controller\AuthController;
  13. use app\admin\model\ump\EventPrice;
  14. use service\JsonService as Json;
  15. use think\Request;
  16. use think\Url;
  17. use app\admin\model\user\User;
  18. use app\admin\model\user\UserBill;
  19. use service\FormBuilder as Form;
  20. use app\admin\model\ump\EventRegistration as EventRegistrationModel;
  21. use app\admin\model\ump\EventSignUp as EventSignUpModel;
  22. use app\admin\model\ump\EventData as EventDataModel;
  23. use app\admin\model\ump\EventPrice as EventPriceModel;
  24. use app\admin\model\merchant\MerchantFlowingWater;
  25. use app\admin\model\merchant\Merchant;
  26. use service\AlipayTradeWapService;
  27. use behavior\wechat\PaymentBehavior;
  28. use service\HookService;
  29. /**活动控制器
  30. * Class EventRegistration
  31. * @package app\admin\controller\ump
  32. */
  33. class EventRegistration extends AuthController
  34. {
  35. public function index()
  36. {
  37. $mer_list = Merchant::getMerchantList();
  38. $this->assign(['mer_list' => $mer_list]);
  39. return $this->fetch();
  40. }
  41. /**
  42. * 活动列表
  43. */
  44. public function event_registration_list()
  45. {
  46. $where = parent::getMore([
  47. ['title', ''],
  48. ['status', 1],
  49. ['is_show', ''],
  50. ['mer_id', ''],
  51. ['page', 1],
  52. ['limit', 20],
  53. ], $this->request);
  54. return Json::successlayui(EventRegistrationModel::systemPage($where));
  55. }
  56. /**活动审核
  57. * @return mixed
  58. */
  59. public function examine()
  60. {
  61. $mer_list = Merchant::getMerchantList();
  62. $this->assign(['mer_list' => $mer_list]);
  63. return $this->fetch();
  64. }
  65. /**
  66. * 异步查找产品
  67. *
  68. * @return json
  69. */
  70. public function event_examine_ist()
  71. {
  72. $where = parent::getMore([
  73. ['title', ''],
  74. ['page', 1],
  75. ['limit', 20],
  76. ['mer_id', ''],
  77. ['status', ''],
  78. ]);
  79. return Json::successlayui(EventRegistrationModel::eventExamineList($where));
  80. }
  81. public function examineDetails($id)
  82. {
  83. if (!$id) return Json::fail('参数错误');
  84. $details = EventRegistrationModel::get($id);
  85. if (!$details) return Json::fail('活动不存在');
  86. $details->activity_rules = htmlspecialchars_decode($details->activity_rules);
  87. $details->content = htmlspecialchars_decode($details->content);
  88. $event = EventDataModel::eventDataList($id);
  89. $price = EventPriceModel::eventPriceList($id);
  90. $this->assign(['details' => json_encode($details), 'event' => json_encode($event), 'price' => json_encode($price)]);
  91. return $this->fetch('activity');
  92. }
  93. /**不通过
  94. * @param $id
  95. * @throws \think\exception\DbException
  96. */
  97. public function fail($id)
  98. {
  99. $fail_msg = parent::postMore([
  100. ['message', ''],
  101. ]);
  102. if (!EventRegistrationModel::be(['id' => $id, 'status' => 0])) return Json::fail('操作记录不存在或状态错误!');
  103. $special = EventRegistrationModel::get($id);
  104. if (!$special) return Json::fail('操作记录不存!');
  105. if ($special->status != 0) return Json::fail('您已审核,请勿重复操作');
  106. EventRegistrationModel::beginTrans();
  107. $res = EventRegistrationModel::changeFail($id, $special['mer_id'], $fail_msg['message']);
  108. if ($res) {
  109. EventRegistrationModel::commitTrans();
  110. return Json::successful('操作成功!');
  111. } else {
  112. EventRegistrationModel::rollbackTrans();
  113. return Json::fail('操作失败!');
  114. }
  115. }
  116. /**通过
  117. * @param $id
  118. * @throws \think\exception\DbException
  119. */
  120. public function succ($id)
  121. {
  122. if (!EventRegistrationModel::be(['id' => $id, 'status' => 0])) return Json::fail('操作记录不存在或状态错误!');
  123. $special = EventRegistrationModel::get($id);
  124. if (!$special) return Json::fail('操作记录不存!');
  125. if ($special->status != 0) return Json::fail('您已审核,请勿重复操作');
  126. EventRegistrationModel::beginTrans();
  127. $res = EventRegistrationModel::changeSuccess($id, $special['mer_id']);
  128. if ($res) {
  129. EventRegistrationModel::commitTrans();
  130. return Json::successful('操作成功!');
  131. } else {
  132. EventRegistrationModel::rollbackTrans();
  133. return Json::fail('操作失败!');
  134. }
  135. }
  136. /**编辑
  137. * @param string $is_show
  138. * @param string $id
  139. */
  140. public function set_show($is_show = '', $id = '')
  141. {
  142. if ($is_show == '' || $id == '') return Json::fail('缺少参数');
  143. $res = parent::getDataModification('event', $id, 'is_show', (int)$is_show);
  144. if ($res)
  145. return Json::successful($is_show == 1 ? '显示成功' : '隐藏成功');
  146. else
  147. return Json::fail($is_show == 1 ? '显示失败' : '隐藏失败');
  148. }
  149. /**获得添加
  150. * @return mixed|void
  151. * @throws \think\db\exception\DataNotFoundException
  152. * @throws \think\db\exception\ModelNotFoundException
  153. * @throws \think\exception\DbException
  154. */
  155. public function create($id = 0)
  156. {
  157. $news = [];
  158. $event = [];
  159. $price = [];
  160. if ($id) {
  161. $news = EventRegistrationModel::eventRegistrationOne($id);
  162. $event = EventDataModel::eventDataList($id);
  163. $price = EventPriceModel::eventPriceList($id);
  164. if (!count($price)) {
  165. $price[0] = [
  166. 'event_id' => $id,
  167. 'event_number' => 1,
  168. 'event_price' => $news['price'],
  169. 'event_mer_price' => $news['member_price'],
  170. 'sort' => 0
  171. ];
  172. }
  173. }
  174. $this->assign(['id' => $id, 'news' => json_encode($news), 'event' => json_encode($event), 'price' => json_encode($price)]);
  175. return $this->fetch();
  176. }
  177. /**
  178. * 删除活动
  179. * */
  180. public function delete($id)
  181. {
  182. $res = EventRegistrationModel::delArticleCategory($id);
  183. if (!$res)
  184. return Json::fail(EventRegistrationModel::getErrorInfo('删除失败,请稍候再试!'));
  185. else {
  186. EventDataModel::delEventData($id);
  187. EventPriceModel::delEventPrice($id);
  188. return Json::successful('删除成功!');
  189. }
  190. }
  191. /**
  192. * 添加和修改活动
  193. */
  194. public function add_new()
  195. {
  196. $data = parent::postMore([
  197. ['id', 0],
  198. 'title',
  199. 'image',
  200. 'qrcode_img',
  201. 'activity_rules',
  202. 'content',
  203. 'number',
  204. 'province',
  205. 'city',
  206. 'district',
  207. 'detail',
  208. 'signup_start_time',
  209. 'signup_end_time',
  210. 'start_time',
  211. 'end_time',
  212. ['sort', 0],
  213. ['event', ''],//资料
  214. ['event_price', ''],//价格
  215. ['restrictions', 0],
  216. ['is_fill', 1],
  217. ['is_show', 0],
  218. ['pay_type', 1],
  219. 'price',
  220. ['member_pay_type', 1],
  221. 'member_price'
  222. ]);
  223. $data['signup_start_time'] = strtotime($data['signup_start_time']);
  224. $data['signup_end_time'] = strtotime($data['signup_end_time']);
  225. $data['start_time'] = strtotime($data['start_time']);
  226. $data['end_time'] = strtotime($data['end_time']);
  227. if (bcsub($data['signup_end_time'], $data['signup_start_time'], 0) <= 0) return Json::fail('报名结束时间不能小于等于开始时间');
  228. if (bcsub($data['start_time'], $data['signup_end_time'], 0) <= 0) return Json::fail('活动开始时间不能小于等于报名结束时间');
  229. if (bcsub($data['end_time'], $data['start_time'], 0) <= 0) return Json::fail('活动结束时间不能小于等于开始时间');
  230. $data['content'] = htmlspecialchars($data['content']);
  231. $data['activity_rules'] = htmlspecialchars($data['activity_rules']);
  232. if (isset($data['event']) && $data['event'] != '') {
  233. $event = json_decode($data['event'], true);
  234. } else {
  235. $event = [];
  236. }
  237. if (isset($data['event_price']) && $data['event_price'] != '') {
  238. $price = json_decode($data['event_price'], true);
  239. } else {
  240. $price = [];
  241. }
  242. $number = $data['number'];
  243. if ($data['id']) {
  244. $id = $data['id'];
  245. unset($data['id'], $data['event'], $data['event_price']);
  246. EventRegistrationModel::beginTrans();
  247. $res1 = EventRegistrationModel::edit($data, $id, 'id');
  248. $res2 = EventDataModel::eventDataAdd($id, $event);
  249. $res3 = EventPriceModel::eventPriceAdd($id, $price, $number);
  250. $res = $res1 && $res2 && $res3;
  251. EventRegistrationModel::checkTrans($res);
  252. if ($res) {
  253. return Json::successful('修改活动成功!', $id);
  254. } else
  255. return Json::fail('修改活动失败,您并没有修改什么!', $id);
  256. } else {
  257. $data['add_time'] = time();
  258. $data['statu'] = 0;
  259. EventRegistrationModel::beginTrans();
  260. $id = EventRegistrationModel::insertGetId($data);
  261. $res2 = EventDataModel::eventDataAdd($id, $event);
  262. $res3 = EventPriceModel::eventPriceAdd($id, $price, $number);
  263. $res = $id && $res2 && $res3;
  264. EventRegistrationModel::checkTrans($res);
  265. if ($res)
  266. return Json::successful('添加活动成功!', $id);
  267. else
  268. return Json::successful('添加活动失败!', $id);
  269. }
  270. }
  271. /**
  272. * 查看报名人员
  273. */
  274. public function viewStaff($id)
  275. {
  276. if (!$id) return Json::fail('参数错误!');
  277. $activity = EventRegistrationModel::where('id', $id)->find();
  278. if (!$activity) return Json::fail('活动不存在!');
  279. $this->assign(['aid' => $id]);
  280. return $this->fetch('view_staff');
  281. }
  282. /**报名订单订单
  283. * @return mixed
  284. */
  285. public function order()
  286. {
  287. $this->assign([
  288. 'year' => getMonth('y'),
  289. 'orderCount' => EventSignUpModel::orderCount()
  290. ]);
  291. return $this->fetch();
  292. }
  293. /**
  294. * 查看活动报名订单列表
  295. */
  296. public function get_sign_up_list()
  297. {
  298. $where = parent::getMore([
  299. ['id', $this->request->param('id')],
  300. ['page', 1],
  301. ['limit', 20],
  302. ['status', ''],
  303. ['type', $this->request->param('type')],
  304. ['real_name', ''],
  305. ['data', ''],
  306. ['excel', 0],
  307. ]);
  308. return Json::successlayui(EventSignUpModel::getUserSignUpAll($where));
  309. }
  310. /**
  311. * 统计
  312. */
  313. public function getBadge()
  314. {
  315. $where = parent::postMore([
  316. ['id', $this->request->param('id')],
  317. ['status', ''],
  318. ['type', ''],
  319. ['data', ''],
  320. ['real_name', '']
  321. ]);
  322. return Json::successful(EventSignUpModel::getBadge($where));
  323. }
  324. /**用户活动核销
  325. * @param string $order_id
  326. * @param int $aid
  327. * @param string $code
  328. * @throws \think\db\exception\DataNotFoundException
  329. * @throws \think\db\exception\ModelNotFoundException
  330. * @throws \think\exception\DbException
  331. */
  332. public function scanCodeSignIn($id)
  333. {
  334. if (!$id) $this->failed('参数有误!');
  335. $order = EventSignUpModel::where('id', $id)->find();
  336. if (!$order) $this->failed('订单不存在!');
  337. if ($order['status']) $this->failed('订单已核销!');
  338. $res = EventSignUpModel::where(['id' => $id, 'paid' => 1, 'is_del' => 0])->update(['status' => 1]);
  339. if ($res) return Json::successful('核销成功');
  340. else return Json::fail('核销失败');
  341. }
  342. /**
  343. * 修改退款状态
  344. * @param $id
  345. * @return \think\response\Json|void
  346. */
  347. public function refund_y($id)
  348. {
  349. if (!$id) return $this->failed('数据不存在');
  350. $product = EventSignUpModel::get($id);
  351. if (!$product) return Json::fail('数据不存在!');
  352. if ($product['paid'] == 1) {
  353. $f = array();
  354. $f[] = Form::input('order_id', '退款单号', $product->getData('order_id'))->disabled(1);
  355. $f[] = Form::number('refund_price', '退款金额', $product->getData('pay_price'))->precision(2)->min(0.01);
  356. $form = Form::make_post_form('退款处理', $f, Url::build('updateRefundY', array('id' => $id)), 4);
  357. $this->assign(compact('form'));
  358. return $this->fetch('public/form-builder');
  359. } else return Json::fail('数据不存在!');
  360. }
  361. /**退款处理
  362. * @param Request $request
  363. * @param $id
  364. */
  365. public function updateRefundY(Request $request, $id)
  366. {
  367. $data = parent::postMore([
  368. 'refund_price',
  369. ], $request);
  370. if (!$id) return Json::fail('数据不存在');
  371. $product = EventSignUpModel::get($id);
  372. if (!$product) return Json::fail('数据不存在!');
  373. if ($product['pay_price'] == $product['refund_price']) return Json::fail('已退完支付金额!不能再退款了');
  374. if (!$data['refund_price']) return Json::fail('请输入退款金额');
  375. $refund_price = $data['refund_price'];
  376. $data['refund_price'] = bcadd($data['refund_price'], $product['refund_price'], 2);
  377. $bj = bccomp((float)$product['pay_price'], (float)$data['refund_price'], 2);
  378. if ($bj < 0) return Json::fail('退款金额大于支付金额,请修改退款金额');
  379. $data['refund_status'] = 2;
  380. $refund_data['pay_price'] = $product['pay_price'];
  381. $refund_data['refund_price'] = $refund_price;
  382. if ($product['pay_type'] == 'weixin') {
  383. try {
  384. HookService::listen('wechat_pay_order_refund', $product['order_id'], $refund_data, true, PaymentBehavior::class);
  385. } catch (\Exception $e) {
  386. return Json::fail($e->getMessage());
  387. }
  388. } else if ($product['pay_type'] == 'yue') {
  389. EventSignUpModel::beginTrans();
  390. $res = User::bcInc($product['uid'], 'now_money', $refund_price, 'uid');
  391. EventSignUpModel::checkTrans($res);
  392. if (!$res) return Json::fail('余额退款失败!');
  393. } else if ($product['pay_type'] == 'zhifubao') {
  394. $res = AlipayTradeWapService::init()->AliPayRefund($product['order_id'], $product['trade_no'], $refund_price, '活动订单退款', 'refund');
  395. if (empty($res) || $res != 10000) {
  396. return Json::fail('支付宝退款失败!');
  397. }
  398. }
  399. $data['refund_reason_time'] = time();
  400. $resEdit = EventSignUpModel::edit($data, $id);
  401. if ($resEdit) {
  402. $pay_type = $product['pay_type'] == 'yue' ? 'now_money' : $product['pay_type'];
  403. if ($product['pay_type'] == 'yue') {
  404. $balance = User::where(['uid' => $product['uid']])->value('now_money');
  405. } else {
  406. $balance = 0;
  407. }
  408. UserBill::income('活动订单退款', $product['uid'], $pay_type, 'pay_sign_up_refund', $refund_price, $product['id'], $balance, '活动订单退款' . floatval($refund_price) . '元');
  409. MerchantFlowingWater::orderRefund($id, $product['mer_id'], 4);
  410. return Json::successful('修改成功!');
  411. } else {
  412. return Json::successful('修改失败!');
  413. }
  414. }
  415. /**订单详情
  416. * @param string $oid
  417. * @return mixed|void
  418. * @throws \think\exception\DbException
  419. */
  420. public function order_info($oid = '')
  421. {
  422. if (!$oid || !($orderInfo = EventSignUpModel::get($oid)))
  423. return $this->failed('订单不存在!');
  424. $userInfo = User::getAllUserinfo($orderInfo['uid']);
  425. $this->assign(compact('orderInfo', 'userInfo'));
  426. return $this->fetch();
  427. }
  428. /**订单删除
  429. * @param int $id
  430. */
  431. public function order_delete($id = 0)
  432. {
  433. if (!$id) return Json::fail('参数错误!');
  434. $data['is_del'] = 1;
  435. EventSignUpModel::edit($data, $id);
  436. return Json::successful('删除成功!');
  437. }
  438. }