ActivityNewUserForm.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\constants\OptionSetting;
  9. use app\models\Coupon;
  10. use app\models\Cat;
  11. use app\models\Goods;
  12. use app\models\GoodsCat;
  13. use app\models\ActivityNewUser;
  14. use app\models\ActivityNewUserGoods;
  15. use app\models\Option;
  16. use app\models\Store;
  17. use yii\base\Model;
  18. class ActivityNewUserForm extends Model
  19. {
  20. public $store_id;
  21. public $id;
  22. public $ids;
  23. public $name;
  24. public $start_time;
  25. public $end_time;
  26. public $status;
  27. public $goods_ext;
  28. public $goods_ids;
  29. public $coupon_ids;
  30. public $goods_name;
  31. public $coupon_name;
  32. public $buy_limit;
  33. public $is_platform = 0;
  34. public $is_platform_audit = 0;
  35. public $store_name;
  36. public function rules()
  37. {
  38. return [
  39. [['status', 'id', 'is_platform', 'is_platform_audit'], 'integer'],
  40. [['start_time', 'end_time', 'ids', 'name', 'store_name'], 'string'],
  41. [['goods_name', 'coupon_name', 'store_id', 'goods_ids', 'goods_ext', 'coupon_ids', 'buy_limit'], 'safe'],
  42. ];
  43. }
  44. public function init() {
  45. parent::init();
  46. if(empty($this->store_id)){
  47. $this->store_id = get_store_id();
  48. }
  49. }
  50. public function search ()
  51. {
  52. try {
  53. $query = ActivityNewUser::find()->where(['is_delete' => 0, 'store_id' => get_store_id()]);
  54. if ((int)$this->status === 1) {//未开始
  55. $query->andWhere(['>' , 'start_time', time()]);
  56. }
  57. if ((int)$this->status === 2) {//进行中
  58. $query->andWhere(['AND', ['<' , 'start_time', time()], ['>' , 'end_time', time()]]);
  59. }
  60. if ((int)$this->status === 3) { //已结束
  61. $query->andWhere(['<' , 'end_time', time()]);
  62. }
  63. if (!empty($this->name)) { //名称
  64. $query->andWhere(['LIKE' , 'name', $this->name]);
  65. }
  66. if (!empty($this->start_time)) {
  67. $query->andWhere(['>' , 'end_time', strtotime($this->start_time)]);
  68. }
  69. if (!empty($this->end_time)) {
  70. $query->andWhere(['<' , 'start_time', strtotime($this->end_time)]);
  71. }
  72. $query->andWhere(['is_platform' => $this->is_platform]);
  73. $pagination = pagination_make($query);
  74. foreach ($pagination['list'] as &$item) {
  75. $item['publish'] = $item['status'];
  76. //获取活动状态
  77. if ($item['start_time'] > time()) {
  78. $item['status'] = 1;
  79. }
  80. if ($item['start_time'] < time() && $item['end_time'] > time()) {
  81. $item['status'] = 2;
  82. }
  83. if ($item['end_time'] < time()) {
  84. $item['status'] = 3;
  85. }
  86. //格式化时间
  87. $item['start_time'] = date("Y-m-d H:i:s", $item['start_time']);
  88. $item['end_time'] = date("Y-m-d H:i:s", $item['end_time']);
  89. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  90. $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
  91. }
  92. return [
  93. 'code' => 0,
  94. 'msg' => 'success',
  95. 'data' => [
  96. 'data' => $pagination['list'],
  97. 'pageNo' => $pagination['pageNo'],
  98. 'totalCount' => $pagination['totalCount'],
  99. ]
  100. ];
  101. } catch (\Exception $e) {
  102. return [
  103. 'code' => 1,
  104. 'msg' => $e->getMessage()
  105. ];
  106. }
  107. }
  108. public function getGoods()
  109. {
  110. try {
  111. $query = Goods::find()->where(['is_delete' => 0, 'status' => 1, 'product_type' => 0, 'store_id' => $this->store_id]);
  112. if ($this->goods_name) {
  113. $query->andWhere(['LIKE' , 'name', $this->goods_name]);
  114. }
  115. $query->select('id, name, cover_pic, goods_num, price, attr, use_attr');
  116. $pagination = pagination_make($query);
  117. foreach ($pagination['list'] as &$item) {
  118. $item['attr'] = json_decode($item['attr'], true);
  119. $item['cat'] = GoodsCat::find()->alias('gc')->where(['gc.goods_id' => $item['id'], 'gc.is_delete' => 0])
  120. ->leftJoin(['c' => Cat::tableName()], 'gc.cat_id = c.id')->select('name')->asArray()->all();
  121. }
  122. return [
  123. 'code' => 0,
  124. 'msg' => "获取成功",
  125. 'data' => [
  126. 'data' => $pagination['list'],
  127. 'pageNo' => $pagination['pageNo'],
  128. 'totalCount' => $pagination['totalCount'],
  129. ]
  130. ];
  131. } catch (\Exception $e) {
  132. return [
  133. 'code' => 1,
  134. 'msg' => $e->getMessage()
  135. ];
  136. }
  137. }
  138. public function getCoupons()
  139. {
  140. try {
  141. $query = Coupon::find()->where(['is_delete' => 0, 'store_id' => $this->store_id]);
  142. if ($this->coupon_name) {
  143. $query->andWhere(['like', 'name', $this->coupon_name]);
  144. }
  145. $list = $query->asArray()->all();
  146. foreach($list as &$item){
  147. $item['name'] = 'id:' . $item['id'] . ' ' . $item['name'];
  148. }
  149. return [
  150. 'code' => 0,
  151. 'msg' => "获取成功",
  152. 'data' => $list,
  153. 's' => $query->createCommand()->getRawSql(),
  154. ];
  155. } catch (\Exception $e) {
  156. return [
  157. 'code' => 1,
  158. 'msg' => $e->getMessage()
  159. ];
  160. }
  161. }
  162. public function getInfo()
  163. {
  164. try {
  165. $activity = ActivityNewUser::find()->where(['id' => $this->id])->one();
  166. if ($activity) {
  167. $activity_goods = Goods::find()->where(['in', 'id', explode(',', $activity->goods_ids)])->andWhere(['is_delete' => 0])->asArray()->all();
  168. $activity_coupons = Coupon::find()->where(['in', 'id', explode(',', $activity->coupon_ids)])->andWhere(['is_delete' => 0])->asArray()->all();
  169. $activity['start_time'] = date("Y-m-d H:i:s", $activity['start_time']);
  170. $activity['end_time'] = date("Y-m-d H:i:s", $activity['end_time']);
  171. $goods_ext = ActivityNewUserGoods::findAll(['activity_id' => $activity->id, 'is_delete' => 0]);
  172. }
  173. return [
  174. 'code' => 0,
  175. 'msg' => '获取成功',
  176. 'data' => [
  177. 'activity_goods' => $activity_goods ?? [],
  178. 'goods_ext' => $goods_ext ?? [],
  179. 'activity_coupons' => $activity_coupons ?? [],
  180. 'activity' => $activity ?: []
  181. ]
  182. ];
  183. } catch (\Exception $e) {
  184. return [
  185. 'code' => 1,
  186. 'msg' => $e->getMessage() . $e->getFile() . $e->getLine()
  187. ];
  188. }
  189. }
  190. public function save ()
  191. {
  192. $t = \Yii::$app->db->beginTransaction();
  193. try {
  194. if (!$this->name || !$this->start_time || !$this->end_time) {
  195. throw new \Exception("请将参数填充完整");
  196. }
  197. // $has = ActivityNewUser::find()->where([
  198. // 'and',
  199. // ['is_delete' => 0, 'store_id' => $this->store_id, 'status' => 1],
  200. // ['<', 'start_time', strtotime($this->end_time)],
  201. // ['>', 'end_time', strtotime($this->start_time)],
  202. // ['!=', 'id', intval($this->id)],
  203. // ['is_platform' => intval($this->is_platform)],
  204. // ])->one();
  205. // if($has){
  206. // throw new \Exception("时间内已有活动:" . $has['name']);
  207. // }
  208. $activity = ActivityNewUser::findOne($this->id);
  209. if (empty($activity)) {
  210. $activity = new ActivityNewUser();
  211. $activity->store_id = $this->store_id;
  212. }
  213. $activity->name = $this->name;
  214. $activity->start_time = strtotime($this->start_time);
  215. $activity->end_time = strtotime($this->end_time);
  216. $activity->goods_ids = $this->goods_ids;
  217. $activity->coupon_ids = $this->coupon_ids;
  218. $activity->buy_limit = $this->buy_limit;
  219. $activity->is_platform = (int)$this->is_platform;
  220. if (!$activity->save()) {
  221. throw new \Exception(json_encode($activity->errors));
  222. }
  223. if(!empty($this->goods_ext)){
  224. foreach($this->goods_ext as &$item){
  225. $item['activity_id'] = $activity->id;
  226. $item['store_id'] = $this->store_id;
  227. }
  228. $result = ActivityNewUserGoods::saveList($this->goods_ext, $activity->id, $is_platform_audit);
  229. if ($result['code'] !== 0) {
  230. throw new \Exception($result['msg']);
  231. }
  232. if ($is_platform_audit) {
  233. $activity->is_platform_audit = 0;
  234. $activity->save();
  235. }
  236. }
  237. $t->commit();
  238. return [
  239. 'code' => 0,
  240. 'msg' => '操作成功!'
  241. ];
  242. } catch (\Exception $e) {
  243. $t->rollBack();
  244. return [
  245. 'code' => 1,
  246. 'msg' => $e->getMessage()
  247. ];
  248. }
  249. }
  250. public function setStatus ()
  251. {
  252. try {
  253. if ($this->ids) {
  254. $ids = explode(',', $this->ids);
  255. if (in_array($this->status, [0, 1])) {
  256. ActivityNewUser::updateAll(['status' => $this->status], ['and', ['in', 'id', $ids], ['store_id' => get_store_id()]]);
  257. }
  258. if ((int)$this->status === 2) {
  259. ActivityNewUser::updateAll(['is_delete' => 1], ['and', ['in', 'id', $ids], ['store_id' => get_store_id()]]);
  260. }
  261. }
  262. return [
  263. 'code' => 0,
  264. 'msg' => '操作成功!'
  265. ];
  266. } catch (\Exception $e) {
  267. return [
  268. 'code' => 1,
  269. 'msg' => $e->getMessage()
  270. ];
  271. }
  272. }
  273. /**
  274. * 审核列表
  275. */
  276. public function auditList() {
  277. try {
  278. $query = ActivityNewUser::find()->alias('a')->where(['a.is_delete' => 0, 'a.is_platform' => 1, 's.is_delete' => 0]);
  279. $query->leftJoin(['s' => Store::tableName()], 's.id = a.store_id');
  280. if (intval($this->status) === 1) {//未开始
  281. $query->andWhere(['AND', ['>' , 'a.start_time', time()], ['a.status' => 1]]);
  282. }
  283. if (intval($this->status) === 2) {//进行中
  284. $query->andWhere(['AND', ['<' , 'a.start_time', time()], ['>' , 'a.end_time', time()], ['a.status' => 1]]);
  285. }
  286. if (intval($this->status) === 3) { //已结束
  287. $query->andWhere(['AND', ['<' , 'a.end_time', time()], ['a.status' => 1]]);
  288. }
  289. if (intval($this->status) === 4) { //已终止
  290. $query->andWhere(['a.status' => 0]);
  291. }
  292. if (!empty($this->name)) { //名称
  293. $query->andWhere(['LIKE' , 'name', $this->name]);
  294. }
  295. if (!empty($this->store_name)) { //名称
  296. $query->andWhere(['LIKE' , 's.name', $this->store_name]);
  297. }
  298. //只有平台运营的商城或者单店铺的无独立小程序才展示商盟的营销活动
  299. if (\Yii::$app->prod_is_dandianpu()) {
  300. $store_id = Option::find()->where(['group' => 'store', 'name' => 'self_mini'])->select('store_id')->column();
  301. $query->andWhere(['NOT IN', 's.id', $store_id]);
  302. } else {
  303. $query->andWhere(['s.business_model' => 2]);
  304. }
  305. if (!empty($this->start_time) && !empty($this->end_time)) { //时间筛选
  306. $query->andWhere(['OR',
  307. ['AND',
  308. ['<=' , 'a.start_time', strtotime($this->start_time)],
  309. ['>=' , 'a.end_time',strtotime($this->end_time)]
  310. ],
  311. ['AND',
  312. ['<=' , 'a.start_time', strtotime($this->start_time)],
  313. ['<=' , 'a.end_time',strtotime($this->end_time)],
  314. ['>=' , 'a.end_time',strtotime($this->start_time)]
  315. ],
  316. ['AND',
  317. ['>=' , 'a.start_time', strtotime($this->start_time)],
  318. ['<=' , 'a.end_time',strtotime($this->end_time)]
  319. ],
  320. ['AND',
  321. ['>=' , 'a.start_time', strtotime($this->start_time)],
  322. ['>=' , 'a.end_time',strtotime($this->end_time)],
  323. ['<=' , 'a.start_time',strtotime($this->end_time)]
  324. ],
  325. ]);
  326. }
  327. if ($this->is_platform_audit > -1 && $this->is_platform_audit !== null) {
  328. $query->andWhere(['a.is_platform_audit' => $this->is_platform_audit]);
  329. }
  330. $query->select('a.id, a.start_time, a.end_time, a.created_at, a.updated_at, a.name, a.is_platform_audit, s.name store_name, s.logo, s.id store_id, a.status');
  331. $pagination = pagination_make($query);
  332. foreach ($pagination['list'] as &$item) {
  333. $item['logo'] = $item['logo'] ?: Option::get(OptionSetting::STORE_LOGO, $item['store_id'], 'store', '')['value'];
  334. //获取活动状态
  335. if (intval($item['status']) === 1) {
  336. if ($item['start_time'] > time()) {
  337. $item['status'] = 1;
  338. }
  339. if ($item['start_time'] < time() && $item['end_time'] > time()) {
  340. $item['status'] = 2;
  341. }
  342. if ($item['end_time'] < time()) {
  343. $item['status'] = 3;
  344. }
  345. } else {
  346. $item['status'] = 4;
  347. }
  348. $item['is_platform_audit'] = (int)$item['is_platform_audit'];
  349. $item['publish'] = $item['status'];
  350. //格式化时间
  351. $item['start_time'] = date("Y-m-d H:i:s", $item['start_time']);
  352. $item['end_time'] = date("Y-m-d H:i:s", $item['end_time']);
  353. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  354. $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
  355. }
  356. return [
  357. 'code' => 0,
  358. 'msg' => 'success',
  359. 'data' => [
  360. 'data' => $pagination['list'],
  361. 'pageNo' => $pagination['pageNo'],
  362. 'totalCount' => $pagination['totalCount'],
  363. ]
  364. ];
  365. } catch (\Exception $e) {
  366. return [
  367. 'code' => 1,
  368. 'msg' => $e->getMessage()
  369. ];
  370. }
  371. }
  372. /**
  373. * 处理审核
  374. */
  375. public function auditHandle()
  376. {
  377. try {
  378. $id = $this->id;
  379. $status = $this->status;
  380. $cut_activity = ActivityNewUser::findOne(['id' => $id, 'is_platform' => 1, 'is_delete' => 0]);
  381. if (!$cut_activity) {
  382. throw new \Exception('活动不存在');
  383. }
  384. if (in_array($status, [1, 2])) {
  385. if (intval($cut_activity->is_platform_audit) !== 0) {
  386. throw new \Exception('活动已经审核');
  387. }
  388. $cut_activity->is_platform_audit = $status;
  389. }
  390. if (intval($status) === 3) {
  391. $cut_activity->is_delete = 1;
  392. }
  393. if (in_array($status, [4, 5])) {
  394. $cut_activity->status = intval($status - 4);
  395. }
  396. if (!$cut_activity->save()) {
  397. throw new \Exception(json_encode($cut_activity->errors, JSON_UNESCAPED_UNICODE));
  398. }
  399. return [
  400. 'code' => 0,
  401. 'msg' => '操作成功'
  402. ];
  403. } catch (\Exception $e) {
  404. return [
  405. 'code' => 1,
  406. 'msg' => $e->getMessage()
  407. ];
  408. }
  409. }
  410. }