DefaultController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. namespace addons\ScoreMall\backend\controllers;
  3. use addons\ScoreMall\common\models\ScoreMallExchangeRecord;
  4. use addons\ScoreMall\common\models\ScoreMallGoods;
  5. use addons\ScoreMall\common\models\ScoreMallGoodsAttr;
  6. use addons\ScoreMall\common\enums\ScoreMallEnum;
  7. use common\models\goods\Goods;
  8. use common\models\goods\GoodsAttr;
  9. use common\models\goods\GoodsMarketing;
  10. use common\models\user\User;
  11. use common\enums\StatusEnum;
  12. use common\enums\AddonsEnum;
  13. use common\enums\GoodsTypeEnum;
  14. use Yii;
  15. /**
  16. * 默认控制器
  17. *
  18. * Class DefaultController
  19. * @package addons\ScoreMall\backend\controllers
  20. */
  21. class DefaultController extends BaseController
  22. {
  23. public $enableCsrfValidation = false;
  24. /**
  25. * 首页
  26. *
  27. * @author hepi
  28. * @return mixed
  29. */
  30. public function actionIndex()
  31. {
  32. if (Yii::$app->request->isAjax) {
  33. if (Yii::$app->request->isGet) {
  34. $get = Yii::$app->request->get();
  35. $where[] = ['mall_id' => $this->mall_id];
  36. $where[] = ['<>','status',StatusEnum::DELETE];
  37. if (isset($get['goods_name']) && !empty($get['goods_name'])) {
  38. $goodsLists = Goods::goodsSearchFromAddons('goods_name', $get['goods_name'], $this->mall_id);
  39. if (empty($goodsLists)) {
  40. return $this->success('操作成功', ['list' => []]);
  41. } else {
  42. $goodsIds = array_column($goodsLists, 'id');
  43. $where[] = ['goods_id' => $goodsIds];
  44. }
  45. }
  46. $params = [
  47. 'where' => $where,
  48. 'limit' => 10,
  49. 'select' => ['goods_id', 'num', 'created_at', 'money', 'type', 'price', 'score'],
  50. 'order' => 'created_at desc'
  51. ];
  52. $list = ScoreMallGoods::listPage($params);
  53. if ($list['list']) {
  54. $url = Yii::$app->services->setting->mall->get($this->mall_id, 'basic.web_url');
  55. $url = $url ?? '';
  56. $list['domain'] = $url;
  57. if (!isset($goodsLists)) {
  58. $goodsLists = Goods::find()
  59. ->select(['id', 'goods_name', 'cover_pic', 'price'])
  60. ->where(['in', 'id', array_column($list['list'], 'goods_id')])
  61. ->asArray()->indexBy('id')->all();
  62. }
  63. $goodsLists = array_column($goodsLists, null, 'id');
  64. foreach ($list['list'] as &$val) {
  65. $val['money'] = $val['money'] ?? '0.00';
  66. $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
  67. $val['type_info'] = ScoreMallEnum::getTypeMap($val['type']);
  68. $val['goods_name'] = isset($goodsLists[$val['goods_id']]) ? $goodsLists[$val['goods_id']]['goods_name'] : '';
  69. $val['cover_pic'] = isset($goodsLists[$val['goods_id']]) ? $goodsLists[$val['goods_id']]['cover_pic'] : '';
  70. $val['price'] = isset($goodsLists[$val['goods_id']]) ? $goodsLists[$val['goods_id']]['price'] : '';
  71. }
  72. }
  73. return $this->success('操作成功', $list);
  74. }
  75. }
  76. return $this->render('index');
  77. }
  78. /**
  79. * 兑换记录
  80. *
  81. * @author hepi
  82. * @return mixed
  83. */
  84. public function actionExchangeRecord() {
  85. if (Yii::$app->request->isAjax) {
  86. if (Yii::$app->request->isGet) {
  87. $get = Yii::$app->request->get();
  88. $where[] = ['=', 'mall_id', $this->mall_id];
  89. if (!empty($get['date'])) {
  90. $date = date($get['date'] . '-01');
  91. }
  92. $start_time = strtotime($date);
  93. $end_time = strtotime($date . '+1 month -1 day 23:59:59');
  94. $where[] = ['>=', 'created_at', $start_time];
  95. $where[] = ['<=', 'created_at', $end_time];
  96. if (isset($get['user_id']) && !empty($get['user_id'])) {
  97. $where[] = ['=', 'user_id', $get['user_id']];
  98. }
  99. if (isset($get['goods_name']) && !empty($get['goods_name'])) {
  100. $goodsLists = Goods::goodsSearchFromAddons('goods_name', $get['goods_name'], $this->mall_id);
  101. if (empty($goodsLists)) {
  102. return $this->success('操作成功', ['list' => []]);
  103. } else {
  104. $goodsIds = array_column($goodsLists, 'id');
  105. $where[] = ['in', 'goods_id', $goodsIds];
  106. }
  107. }
  108. $params = [
  109. 'where' => $where,
  110. 'limit' => 10,
  111. 'order' => 'created_at desc',
  112. 'select' => ['user_id', 'goods_id', 'order_no', 'order_id', 'exchange_price', 'num', 'goods', 'created_at']
  113. ];
  114. $list = ScoreMallExchangeRecord::listPage($params);
  115. if ($list['list']) {
  116. $list['date'] = $date ?? '';
  117. $userList = User::find()
  118. ->select(['id', 'mobile', 'nickname', 'avatar_url'])
  119. ->where(['in', 'id', array_column($list['list'], 'user_id')])
  120. ->asArray()->indexBy('id')->all();
  121. $userList = array_column($userList, null, 'id');
  122. foreach ($list['list'] as &$val) {
  123. $goodsInfo = json_decode($val['goods'], true);
  124. $val['goods_name'] = $goodsInfo['goods_name'];
  125. $val['pic_url'] = $goodsInfo['cover_pic'];
  126. $val['price'] = $goodsInfo['price'];
  127. $val['avatar_url'] = isset($userList[$val['user_id']]) ? $userList[$val['user_id']]['avatar_url'] : '';
  128. $val['nickname'] = isset($userList[$val['user_id']]) ? $userList[$val['user_id']]['nickname'] : '';
  129. $val['mobile'] = isset($userList[$val['user_id']]) ? $userList[$val['user_id']]['mobile'] : '';
  130. $val['money'] = $val['money'] ?? '0.00';
  131. $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
  132. }
  133. }
  134. return $this->success('操作成功', $list);
  135. }
  136. }
  137. return $this->render('exchange-record');
  138. }
  139. /**
  140. * 分类
  141. *
  142. * @author hpei
  143. * @return mixed
  144. */
  145. public function actionClassification() {
  146. if (Yii::$app->request->isAjax) {
  147. $res = Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_SCORE_MALL, 'basic');
  148. $list = !empty($res) && isset($res['classification']) ? json_decode($res['classification'], true) : [];
  149. if (Yii::$app->request->isGet) {
  150. return $this->success('操作成功', $list);
  151. } else {
  152. $post = Yii::$app->request->post();
  153. $data = [];
  154. switch ($post['type']) {
  155. case 'add':
  156. $name = $post['name'] ?? '';
  157. if (empty($name)) throw new \Exception('分类名字不能为空');
  158. $id = array_column($list, 'id');
  159. $id = empty($id) ? 1 : max($id) +1 ;
  160. $data = ['id' => $id, 'name' => $name, 'status' => $post['status'] ?? 1, 'created_at' => date('Y-m-d H:i:s')];
  161. $list[] = $data;
  162. $insertData = ['basic' => ['classification' => json_encode($list, JSON_UNESCAPED_UNICODE)]];
  163. break;
  164. case 'updateStatus':
  165. $status = $post['status'] ?? 1;
  166. $status = $status == 1 ? 0: 1;
  167. $id = $post['id'];
  168. $list = array_column($list, null, 'id');
  169. if (!isset($list[$id])) throw new \Exception('该分类不存在');
  170. $list[$id]['status'] = $status;
  171. $list = array_values($list);
  172. $insertData = ['basic' => ['classification' => json_encode($list, JSON_UNESCAPED_UNICODE)]];
  173. break;
  174. case 'delete':
  175. $id = $post['id'];
  176. $list = array_column($list, null, 'id');
  177. if (!isset($list[$id])) throw new \Exception('该分类不存在');
  178. unset($list[$id]);
  179. $list = array_values($list);
  180. $insertData = ['basic' => ['classification' => json_encode($list, JSON_UNESCAPED_UNICODE)]];
  181. break;
  182. }
  183. Yii::$app->services->setting->addons->set($this->mall_id, AddonsEnum::ADDONS_NAME_SCORE_MALL, $insertData);
  184. return $this->success('操作成功', $data);
  185. }
  186. }
  187. return $this->render('classification');
  188. }
  189. /**
  190. * 新增&编辑积分商品
  191. *
  192. * @author hpei
  193. * @return mixed
  194. */
  195. public function actionEdit() {
  196. if (Yii::$app->request->isAjax) {
  197. if (Yii::$app->request->isGet) {
  198. $get = Yii::$app->request->get();
  199. $data = [];
  200. $goodsInfo = [];
  201. $attr = [];
  202. if (isset($get['goods_id']) && !empty($get['goods_id'])) {
  203. $where = [['goods_id' => $get['goods_id'], 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]];
  204. $data = ScoreMallGoods::getOne($where, true, [], false, ['goods_id', 'cate_id', 'price', 'type', 'use_coupon']);
  205. if (!$data) throw new \Exception('商品不存在');
  206. $where = [['id' => $get['goods_id'], 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]];
  207. $goodsInfo = Goods::getOne($where, true, [], false, ['id', 'price', 'cover_pic', 'goods_name']);
  208. }
  209. $res = Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_SCORE_MALL, 'basic');
  210. $cateList = !empty($res) && isset($res['classification']) ? json_decode($res['classification'], true) : [];
  211. $cateList = array_column($cateList, null, 'id');
  212. $list['data'] = $data;
  213. $list['cate_list'] = $cateList;
  214. $list['goods_info'] = $goodsInfo;
  215. return $this->success('操作成功', $list);
  216. } else {
  217. $post = Yii::$app->request->post();
  218. $res = Yii::$app->services->validate->validate($post, [
  219. [['cate_id', 'use_coupon', 'goods_id', 'type', 'attr'], 'required'],
  220. [['cate_id', 'use_coupon', 'goods_id'], 'integer'],
  221. [['money','score','price'], 'double'],// 双精度浮点型
  222. [['type'], 'integer', 'max' => 3, 'min' => 1],
  223. [['attr', 'action'], 'safe']
  224. ]);
  225. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  226. $goods_id = $post['goods_id'] ?? '';
  227. if (!$goods_id) return $this->error('商品不能为空');
  228. // 目前积分商城仅支持聚合供应链商品或本商城商品
  229. $good = Goods::find()->where(['id' => $post['goods_id']])->asArray()->one();
  230. $goods_sources = [
  231. AddonsEnum::ADDONS_NAME_JUHE_SHOP,
  232. AddonsEnum::ADDONS_NAME_QI_JUHE,
  233. AddonsEnum::ADDONS_NAME_QI_DING_DONG,
  234. AddonsEnum::ADDONS_NAME_QI_SHANG_TONG,
  235. 'StarChain'
  236. ];
  237. if ($good && !empty($good['goods_source']) && !in_array($good['goods_source'], $goods_sources)) return $this->error('目前积分商城仅支持聚合/怡亚通/企叮咚/企商通供应链商品或本商城商品');
  238. $attr = $post['attr'];
  239. $goodsAttrInstall = [];
  240. foreach ($attr as &$val) {
  241. if ($post['type'] == ScoreMallEnum::TYPE_SCORE) {
  242. if (!$val['scoreGoodsAttr']['score']) return $this->error('积分不能为空');
  243. }
  244. if ($post['type'] == ScoreMallEnum::TYPE_SCORE_MONEY) {
  245. if (!$val['scoreGoodsAttr']['score'] || !$val['scoreGoodsAttr']['money']) return $this->error('积分和金额不能为空');
  246. }
  247. if ($post['type'] == ScoreMallEnum::TYPE_LIMIT) {
  248. $deductionLimit = $val['scoreGoodsAttr']['deduction_limit'];
  249. if (($deductionLimit[0]['limit'] && !$deductionLimit[0]['score']) || ($deductionLimit[1]['limit'] && !$deductionLimit[1]['score'])) {
  250. return $this->error('积分不能为空');
  251. }
  252. }
  253. $goodsAttrInstall[] = [
  254. 'goods_id' => $goods_id,
  255. 'goods_attr_id' => $val['id'],
  256. 'price' => $val['price'],
  257. 'score' => $post['type'] != ScoreMallEnum::TYPE_LIMIT ? $val['scoreGoodsAttr']['score'] : 0,
  258. 'money' => $post['type'] != ScoreMallEnum::TYPE_LIMIT ? $val['scoreGoodsAttr']['money'] : 0,
  259. 'created_at' => time(),
  260. 'updated_at' => time(),
  261. 'deduction_limit' => $post['type'] == ScoreMallEnum::TYPE_LIMIT ? json_encode([
  262. ['limit' => $val['scoreGoodsAttr']['deduction_limit'][0]['limit'],'score'=>$val['scoreGoodsAttr']['deduction_limit'][0]['score']],
  263. ['limit' => $val['scoreGoodsAttr']['deduction_limit'][1]['limit'],'score'=>$val['scoreGoodsAttr']['deduction_limit'][1]['score']]]) : ''
  264. ];
  265. }
  266. try {
  267. $t = Yii::$app->db->beginTransaction();
  268. $post['mall_id'] = $this->mall_id;
  269. if ($post['action'] == 'add') $post['status'] = StatusEnum::ENABLED;
  270. $res1 = ScoreMallGoods::setData($post);
  271. $res2 = ScoreMallGoodsAttr::setData($goodsAttrInstall, $post['action']);
  272. if ($res1 && $res2) {
  273. $t->commit();
  274. } else {
  275. $t->rollBack();
  276. $msg = $res1 == false ? ScoreMallGoods::getStaticError() : ScoreMallGoodsAttr::getStaticError();
  277. return $this->error($msg);
  278. }
  279. return $this->success('操作成功');
  280. } catch (\Exception $e) {
  281. $t->rollBack();
  282. return $this->error($e->getMessage());
  283. }
  284. return $this->success('操作成功');
  285. }
  286. }
  287. return $this->render('edit', ['goods_id' => Yii::$app->request->get('goods_id', '')]);
  288. }
  289. /**
  290. * 获取商品属性列表
  291. *
  292. * @param string $goods_name
  293. * @author hpei
  294. * @return array
  295. */
  296. public function actionAttrList()
  297. {
  298. if (Yii::$app->request->isAjax) {
  299. $get = Yii::$app->request->get();
  300. $goods_id = $get['goods_id'] ?? 0;
  301. // $where[] = ['>', 'stock', 1];
  302. $where[] = ['=', 'goods_id', $goods_id];
  303. $with = [];
  304. if ($get['type'] == 'edit') {
  305. $with = ['scoreGoodsAttr' => function($query) {
  306. $query->where(['<>','status',StatusEnum::DELETE])->select(['goods_attr_id', 'score', 'money', 'deduction_limit']);
  307. }];
  308. }
  309. $list = GoodsAttr::getGoodsAttrs($where, $with, true, ['id', 'name', 'price']);
  310. if (empty($list)) return $this->error('该商品暂不可选择');
  311. return $this->success('操作成功', $list);
  312. }
  313. }
  314. public function actionDelete() {
  315. if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
  316. $goods_id = Yii::$app->request->post('goods_id', 0);
  317. $trans = Yii::$app->db->beginTransaction();
  318. try {
  319. $score_goods = ScoreMallGoods::setData(['goods_id' => $goods_id, 'status' => StatusEnum::DELETE]);
  320. if (!$score_goods) return $this->error(ScoreMallGoods::getStaticError());
  321. $res = ScoreMallGoodsAttr::updateAll(['status' => StatusEnum::DELETE],['goods_id' => $goods_id]);
  322. if (!$res) return $this->error(ScoreMallGoodsAttr::getStaticError());
  323. // 删除成功解除商品列表积分商品的状态
  324. GoodsMarketing::updateAll(['status' => StatusEnum::DELETE], ['mall_id' => $this->mall_id, 'marketing_id' => $score_goods['id'], 'marketing_type' => AddonsEnum::ADDONS_NAME_SCORE_MALL, 'goods_id' => $goods_id]);
  325. $trans->commit();
  326. } catch (\Exception $e) {
  327. $trans->rollBack();
  328. return $this->error($e->getMessage());
  329. }
  330. }
  331. return $this->success('操作成功');
  332. }
  333. /**
  334. * 获取积分商城可以选择的商品列表
  335. *
  336. * @return mixed
  337. */
  338. public function actionGoodsList()
  339. {
  340. $get = Yii::$app->request->get();
  341. $rules = [
  342. [['keyword'], 'string'],
  343. [['price'], 'number'],
  344. ];
  345. $res = Yii::$app->services->validate->validate($get, $rules);
  346. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  347. $page_data = Goods::getPaginationList(15, function($query) use($get) {
  348. $query->select([
  349. 'id', 'mall_id', 'cover_pic', 'mch_id', 'cost_price', 'original_price', 'price', 'sales_num',
  350. 'stock', 'goods_name', 'sort', 'is_on_sale', 'goods_source', 'created_at',
  351. ]);
  352. $query->where([
  353. 'mall_id' => $this->mall_id,
  354. 'mch_id' => $this->admin->mch_id,
  355. 'is_on_sale' => 1,
  356. 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED],
  357. 'goods_type' => [GoodsTypeEnum::GoodsTypePhysical, GoodsTypeEnum::GoodsTypeVirtual]
  358. ]);
  359. $query->andWhere(['!=', 'goods_name', '代客下单插件商品']);
  360. !empty($get['keyword']) && $query->andWhere([
  361. 'or',
  362. ['=', 'id', $get['keyword']],
  363. ['like', 'goods_name', $get['keyword']]
  364. ]);
  365. !empty($get['price']) && $query->andWhere(['=', 'price', $get['price']]);
  366. }, 'sort ASC,id DESC');
  367. return $page_data === false
  368. ? $this->error(Goods::getStaticError())
  369. : $this->success('操作成功', $page_data);
  370. }
  371. }