DefaultController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. namespace addons\SecondaryCard\backend\controllers;
  3. use addons\CloudStock\common\models\CloudStockDeductionDetail;
  4. use addons\CloudStock\common\models\CloudStockIncreaseDetail;
  5. use addons\SecondaryCard\common\enums\SecondaryCardEnum;
  6. use addons\SecondaryCard\common\models\SecondaryCard;
  7. use addons\SecondaryCard\common\service\SecondaryCardOrderService;
  8. use addons\SecondaryCard\common\service\SecondaryCardRedemptionCodeService;
  9. use addons\SecondaryCard\common\service\SecondaryCardService;
  10. use addons\SecondaryCard\common\service\SecondaryCardUseLogService;
  11. use common\enums\ApiStatusEnum;
  12. use common\enums\DownloadEnum;
  13. use common\enums\RabbitMqEnum;
  14. use common\helpers\csv\CsvExportHelper;
  15. use common\helpers\MiniQrCode;
  16. use Yii;
  17. /**
  18. * 默认控制器
  19. *
  20. * Class DefaultController
  21. * @package addons\SecondaryCard\backend\controllers
  22. */
  23. class DefaultController extends BaseController
  24. {
  25. public $enableCsrfValidation = false;
  26. /**
  27. * 次卡列表
  28. * @Author:hua
  29. * @Date:2024-02-28 11:04
  30. * @Copyright:copyright(c)2024 广东七件事集团
  31. * @return mixed|string|void
  32. */
  33. public function actionIndex()
  34. {
  35. if (\Yii::$app->request->isAjax) {
  36. if (\Yii::$app->request->isGet) {
  37. $params = \Yii::$app->request->get();
  38. $params['mall_id'] = $this->mall_id;
  39. $data = SecondaryCardService::getList($params);
  40. return $this->success('操作成功', $data);
  41. }
  42. }
  43. return $this->render('index', []);
  44. }
  45. /**
  46. * 次卡编辑
  47. * @Author:hua
  48. * @Date:2024-02-28 11:04
  49. * @Copyright:copyright(c)2024 广东七件事集团
  50. * @return mixed|string|void
  51. */
  52. public function actionEdit()
  53. {
  54. if (\Yii::$app->request->isAjax) {
  55. if (\Yii::$app->request->isPost) {
  56. $form = Yii::$app->request->post('form');
  57. $params = json_decode($form, true);
  58. $params['mall_id'] = $this->mall_id;
  59. $params['create_source_type'] = 0;
  60. $params['create_source_id'] = $this->admin['id'] ?? 0;
  61. if ($params['reservation_type'] == 1 && $params['reservation_model'] == 1) {
  62. if (empty($params['reservation_time'])) {
  63. return $this->error('请填写可预约时间');
  64. }
  65. }
  66. try {
  67. $params['rule'] = (string)$params['rule'];
  68. if ($params['reservation_model'] == SecondaryCardEnum::RESERVATION_MODEL_PEOPLE) {
  69. $params['reservation_service_type'] = 1;
  70. }
  71. $data = SecondaryCardService::setData($params);
  72. } catch (\Exception $e) {
  73. return $this->error($e->getMessage());
  74. }
  75. return $this->success('操作成功', $data);
  76. }
  77. if (\Yii::$app->request->isGet) {
  78. $params = \Yii::$app->request->get();
  79. $data = SecondaryCardService::getOne($params['id'], $this->mall_id);
  80. return $this->success('操作成功', $data);
  81. }
  82. }
  83. return $this->render('edit', [
  84. ]);
  85. }
  86. /**
  87. * 操作次卡下架
  88. * @Author:hua
  89. * @Date:2024-02-28 11:04
  90. * @Copyright:copyright(c)2024 广东七件事集团
  91. * @return mixed|void
  92. */
  93. public function actionChangeIsOnSales()
  94. {
  95. if (\Yii::$app->request->isAjax) {
  96. if (\Yii::$app->request->isPost) {
  97. try {
  98. $params = \Yii::$app->request->post();
  99. $res = Yii::$app->services->validate->validate($params, [
  100. [['id', 'is_on_sales'], 'required'],
  101. ]);
  102. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  103. $params['mall_id'] = $this->mall_id;
  104. $create_source_type = 0;
  105. $create_source_id = $this->admin['id'] ?? 0;
  106. SecondaryCardService::changeIsOnSales($params['id'], $this->mall_id, $params['is_on_sales'],$create_source_id,$create_source_type);
  107. return $this->success('操作成功');
  108. } catch (\Exception $e) {
  109. return $this->error($e->getMessage());
  110. }
  111. }
  112. }
  113. return $this->error();
  114. }
  115. public function actionUploadIsOnSales()
  116. {
  117. if (\Yii::$app->request->isAjax) {
  118. if (\Yii::$app->request->isPost) {
  119. try {
  120. $params = \Yii::$app->request->post();
  121. $res = Yii::$app->services->validate->validate($params, [
  122. [['id', 'is_on_sales'], 'required'],
  123. ]);
  124. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  125. $params['mall_id'] = $this->mall_id;
  126. SecondaryCardService::uploadIsOnSales($params['id'], $this->mall_id, $params['is_on_sales']);
  127. return $this->success('操作成功');
  128. } catch (\Exception $e) {
  129. return $this->error($e->getMessage());
  130. }
  131. }
  132. }
  133. return $this->error();
  134. }
  135. /**
  136. * 店铺列表
  137. * @Author:hua
  138. * @Date:2024-02-28 11:04
  139. * @Copyright:copyright(c)2024 广东七件事集团
  140. * @return mixed|void
  141. */
  142. public function actionStoreList()
  143. {
  144. if (\Yii::$app->request->isAjax) {
  145. try {
  146. $params = \Yii::$app->request->get();
  147. $params['mall_id'] = $this->mall_id;
  148. $list = SecondaryCardService::getStorePageList($params);
  149. return $this->success('操作成功', $list);
  150. } catch (\Exception $e) {
  151. return $this->error($e->getMessage());
  152. }
  153. }
  154. return $this->error();
  155. }
  156. /**
  157. * 开卡记录
  158. * @Author:hua
  159. * @Date:2024-02-29 11:41
  160. * @Copyright:copyright(c)2024 广东七件事集团
  161. * @return mixed|string|void
  162. */
  163. public function actionOrder()
  164. {
  165. if (\Yii::$app->request->isAjax) {
  166. if (\Yii::$app->request->isGet) {
  167. $params = \Yii::$app->request->get();
  168. $params['mall_id'] = $this->mall_id;
  169. $data = SecondaryCardOrderService::getList($params);
  170. return $this->success('操作成功', $data, ApiStatusEnum::CODE_SUCCESS, 0);
  171. }
  172. if (\Yii::$app->request->isPost) {
  173. $data = \Yii::$app->request->post();
  174. $filename = '开卡列表' . '-' . date('YmdHis') . '_' . rand(10000, 99999);
  175. $data['mall_id'] = $this->mall_id;
  176. $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_SECONDARY_CARD, SecondaryCardOrderService::class, 'exportHandle', $data);
  177. if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
  178. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  179. }
  180. }
  181. return $this->render('order', []);
  182. }
  183. /**
  184. *
  185. * @Author:hua
  186. * @Date:2024-03-01 10:09
  187. * @Copyright:copyright(c)2024 广东七件事集团
  188. * @return mixed|void
  189. */
  190. public function actionChangeOrderStatus()
  191. {
  192. if (\Yii::$app->request->isAjax) {
  193. if (\Yii::$app->request->isPost) {
  194. try {
  195. $params = \Yii::$app->request->post();
  196. $res = Yii::$app->services->validate->validate($params, [
  197. [['id', 'order_status'], 'required'],
  198. ]);
  199. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  200. $params['mall_id'] = $this->mall_id;
  201. SecondaryCardOrderService::changeOrderStatus($params['id'], $this->mall_id, $params['order_status']);
  202. return $this->success('操作成功');
  203. } catch (\Exception $e) {
  204. return $this->error($e->getMessage());
  205. }
  206. }
  207. }
  208. }
  209. /**
  210. * 兑换码管理
  211. * @Author:hua
  212. * @Date:2024-02-29 11:41
  213. * @Copyright:copyright(c)2024 广东七件事集团
  214. * @return mixed|string|void
  215. */
  216. public function actionRedemptionCode()
  217. {
  218. if (\Yii::$app->request->isAjax) {
  219. if (\Yii::$app->request->isGet) {
  220. $params = \Yii::$app->request->get();
  221. $params['mall_id'] = $this->mall_id;
  222. $data = SecondaryCardRedemptionCodeService::getList($params);
  223. return $this->success('操作成功', $data, ApiStatusEnum::CODE_SUCCESS, 0);
  224. }
  225. if (\Yii::$app->request->isPost) {
  226. $data = \Yii::$app->request->post();
  227. $filename = '兑换码列表' . '-' . date('YmdHis') . '_' . rand(10000, 99999);
  228. $data['mall_id'] = $this->mall_id;
  229. $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_SECONDARY_CARD, SecondaryCardRedemptionCodeService::class, 'exportHandle', $data);
  230. if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
  231. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  232. }
  233. }
  234. $id = \Yii::$app->request->get('id');
  235. $secondary_card = SecondaryCard::findOne(['id' => $id]);
  236. return $this->render('redemption-code', ['secondary_card_name' => $secondary_card['name']]);
  237. }
  238. /**
  239. * 新建兑换码
  240. * @Author:hua
  241. * @Date:2024-02-29 15:34
  242. * @Copyright:copyright(c)2024 广东七件事集团
  243. * @return mixed|void
  244. */
  245. public function actionRedemptionCodeAdd()
  246. {
  247. if (\Yii::$app->request->isAjax) {
  248. if (\Yii::$app->request->isPost) {
  249. $params = \Yii::$app->request->post();
  250. $res = Yii::$app->services->validate->validate($params, [
  251. [['secondary_card_id', 'num', 'remarks'], 'required'],
  252. ['num', function ($attribute) {
  253. if ($this->$attribute < 0) {
  254. $this->addError($attribute, '数量必须大于0');
  255. }
  256. }],
  257. [['code_pre'], 'safe'],
  258. ]);
  259. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  260. if(!empty($params['code_pre'])&&strlen($params['code_pre'])!=4){
  261. return $this->error('您当前兑换码无效,确认并输入正确兑换码');
  262. }
  263. $params['mall_id'] = $this->mall_id;
  264. $secondary_card = SecondaryCard::findOne(['id' => $params['secondary_card_id'], 'mall_id' => $this->mall_id]);
  265. if (empty($secondary_card)) {
  266. return $this->error('次卡不存在');
  267. }
  268. if ($params['num'] > $secondary_card['max_times']) {
  269. return $this->error('新建数量不能大于最大开卡数');
  270. }
  271. $params['use_type'] = $secondary_card['use_type'];
  272. $params['create_source_type'] = 0;
  273. $params['create_source_id'] = $this->admin['id'] ?? 0;
  274. Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $params, SecondaryCardRedemptionCodeService::class, 'add');
  275. return $this->success('操作成功');
  276. }
  277. }
  278. }
  279. /**
  280. * 兑换码删除
  281. * @Author:hua
  282. * @Date:2024-02-29 15:34
  283. * @Copyright:copyright(c)2024 广东七件事集团
  284. * @return mixed|void
  285. */
  286. public function actionRedemptionCodeDel()
  287. {
  288. if (\Yii::$app->request->isAjax) {
  289. if (\Yii::$app->request->isPost) {
  290. $params = \Yii::$app->request->post();
  291. $res = Yii::$app->services->validate->validate($params, [
  292. [['secondary_card_id', 'id'], 'required'],
  293. ]);
  294. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  295. $params['mall_id'] = $this->mall_id;
  296. $data = SecondaryCardRedemptionCodeService::del($params);
  297. return $this->success('操作成功', $data);
  298. }
  299. }
  300. }
  301. /**
  302. * 使用记录
  303. * @Author:hua
  304. * @Date:2024-03-07 16:40
  305. * @Copyright:copyright(c)2024 广东七件事集团
  306. * @return mixed|string|void
  307. */
  308. public function actionUseLog()
  309. {
  310. if (\Yii::$app->request->isAjax) {
  311. if (\Yii::$app->request->isGet) {
  312. $params = \Yii::$app->request->get();
  313. $params['mall_id'] = $this->mall_id;
  314. $data = SecondaryCardUseLogService::getList($params);
  315. return $this->success('操作成功', $data);
  316. }
  317. if (\Yii::$app->request->isPost) {
  318. $data = \Yii::$app->request->post();
  319. $filename = '使用记录' . '-' . date('YmdHis') . '_' . rand(10000, 99999);
  320. $data['mall_id'] = $this->mall_id;
  321. $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_SECONDARY_CARD, SecondaryCardUseLogService::class, 'exportHandle', $data);
  322. if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
  323. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  324. }
  325. }
  326. return $this->render('use-log', []);
  327. }
  328. /**
  329. * 退款
  330. * @Author:hua
  331. * @Date:2024-03-08 11:49
  332. * @Copyright:copyright(c)2024 广东七件事集团
  333. * @return mixed|void
  334. */
  335. public function actionOrderRefund()
  336. {
  337. if (\Yii::$app->request->isAjax) {
  338. if (\Yii::$app->request->isPost) {
  339. try {
  340. $params = \Yii::$app->request->post();
  341. $res = Yii::$app->services->validate->validate($params, [
  342. [['id', 'refund_money'], 'required'],
  343. ]);
  344. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  345. if ($params['refund_money'] <= 0) {
  346. return $this->error('退款金额必须大于0');
  347. }
  348. $params['mall_id'] = $this->mall_id;
  349. $params['user_id'] = 0;
  350. SecondaryCardOrderService::orderRefund($params);
  351. return $this->success('操作成功');
  352. } catch (\Exception $e) {
  353. return $this->error($e->getMessage());
  354. }
  355. }
  356. }
  357. }
  358. public function actionCanClerkStoreList()
  359. {
  360. if (\Yii::$app->request->isAjax) {
  361. if (\Yii::$app->request->isGet) {
  362. try {
  363. $params = \Yii::$app->request->get();
  364. $params['mall_id'] = $this->mall_id;
  365. $data = SecondaryCardService::canClerkStoreList($params);
  366. return $this->success('操作成功', $data);
  367. } catch (\Exception $e) {
  368. return $this->error($e->getMessage());
  369. }
  370. }
  371. }
  372. return $this->render('index', []);
  373. }
  374. public function actionShowMiniPromotionCode()
  375. {
  376. $request = \Yii::$app->request;
  377. if ($request->isAjax) {
  378. if ($request->isPost) {
  379. $params = \Yii::$app->request->post();
  380. // 检查提交的参数
  381. $res = \Yii::$app->services->validate->validate($params, [
  382. [['id'], 'required'],
  383. ]);
  384. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  385. $params['mall_id'] = $this->mall_id;
  386. $params['mall'] = $this->mall;
  387. $data = SecondaryCardService::showMiniPromotionCode($params);
  388. return $this->success('操作成功', $data);
  389. }
  390. }
  391. }
  392. }