ActiveTime.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\store\model\sharp;
  10. use app\common\model\sharp\ActiveTime as ActiveTimeModel;
  11. use app\store\model\sharp\ActiveGoods as ActiveGoodsModel;
  12. /**
  13. * 整点秒杀-活动会场场次模型
  14. * Class ActiveTime
  15. * @package app\store\model\sharp
  16. */
  17. class ActiveTime extends ActiveTimeModel
  18. {
  19. /**
  20. * 获取活动会场场次列表
  21. * @param $activeId
  22. * @return \think\Paginator
  23. * @throws \think\exception\DbException
  24. */
  25. public function getList($activeId)
  26. {
  27. $list = $this->with(['active'])
  28. ->withCount(['goods'])
  29. ->where('active_id', '=', $activeId)
  30. ->order(['active_time' => 'asc'])
  31. ->paginate(15, false, [
  32. 'query' => \request()->request()
  33. ]);
  34. return $list;
  35. }
  36. /**
  37. * 修改商品状态
  38. * @param $state
  39. * @return false|int
  40. */
  41. public function setStatus($state)
  42. {
  43. return $this->allowField(true)->save(['status' => (int)$state]) !== false;
  44. }
  45. /**
  46. * 获取指定会场的所有场次时间
  47. * @param $activeId
  48. * @return array
  49. */
  50. public function getActiveTimeData($activeId)
  51. {
  52. return $this->where('active_id', '=', $activeId)->column('active_time');
  53. }
  54. /**
  55. * 根据活动场次ID获取商品列表 (格式化后用于编辑页)
  56. * @param $activeTimeId
  57. * @return array
  58. */
  59. public function getGoodsListByActiveTimeId($activeTimeId)
  60. {
  61. $data = [];
  62. foreach (ActiveGoodsModel::getGoodsListByActiveTimeId($activeTimeId) as $item) {
  63. $data[] = [
  64. 'goods_id' => $item['sharp_goods_id'],
  65. 'goods_name' => $item['goods']['goods_name'],
  66. 'goods_image' => $item['goods']['goods_image'],
  67. ];
  68. }
  69. return $data;
  70. }
  71. /**
  72. * 新增记录
  73. * @param $activeId
  74. * @param $data
  75. * @return bool|mixed
  76. */
  77. public function add($activeId, $data)
  78. {
  79. // 表单验证
  80. if (!$this->onValidate($data)) return false;
  81. // 事务处理
  82. return $this->transaction(function () use ($activeId, $data) {
  83. // 新增活动场次
  84. $this->onBatchAdd(
  85. $activeId,
  86. $data['active_times'],
  87. $data['sharp_goods'],
  88. $data['status']
  89. );
  90. return true;
  91. });
  92. }
  93. /**
  94. * 更新记录
  95. * @param $data
  96. * @return bool|mixed
  97. */
  98. public function edit($data)
  99. {
  100. // 验证是否选择商品
  101. if (!$this->onValidateSharpGoods($data)) {
  102. return false;
  103. }
  104. // 事务处理
  105. return $this->transaction(function () use ($data) {
  106. // 更新活动场次
  107. $this->allowField(true)->save($data);
  108. // 更新场次的商品关联记录
  109. $this->onUpdateActiveGoodsRec($data['sharp_goods']);
  110. return true;
  111. });
  112. }
  113. /**
  114. * 更新当前场次的商品关联记录
  115. * @param $sharpGoodsIds
  116. * @return array|false
  117. * @throws \think\Exception
  118. * @throws \think\exception\PDOException
  119. */
  120. private function onUpdateActiveGoodsRec($sharpGoodsIds)
  121. {
  122. $saveData = [];
  123. foreach ($sharpGoodsIds as $goodsId) {
  124. $saveData[] = [
  125. 'active_id' => $this['active_id'],
  126. 'active_time_id' => $this['active_time_id'],
  127. 'sharp_goods_id' => $goodsId,
  128. 'wxapp_id' => static::$wxapp_id,
  129. ];
  130. }
  131. $this->goods()->delete();
  132. return (new ActiveGoodsModel)->isUpdate(false)->saveAll($saveData);
  133. }
  134. /**
  135. * 表单验证
  136. * @param $data
  137. * @return bool
  138. */
  139. private function onValidate($data)
  140. {
  141. // 验证是否选择活动场次
  142. if (!isset($data['active_times']) || empty($data['active_times'])) {
  143. $this->error = '您还没有选择活动场次';
  144. return false;
  145. }
  146. // 验证是否选择商品
  147. if (!$this->onValidateSharpGoods($data)) {
  148. return false;
  149. }
  150. return true;
  151. }
  152. /**
  153. * 验证是否选择商品
  154. * @param $data
  155. * @return bool
  156. */
  157. private function onValidateSharpGoods($data)
  158. {
  159. // 验证是否选择商品
  160. if (!isset($data['sharp_goods']) || empty($data['sharp_goods'])) {
  161. $this->error = '您还没有选择秒杀商品';
  162. return false;
  163. }
  164. return true;
  165. }
  166. /**
  167. * 批量新增活动场次
  168. * @param $activeId
  169. * @param array $times
  170. * @param array $sharpGoodsIds
  171. * @param int $status
  172. * @return bool
  173. * @throws \Exception
  174. */
  175. public function onBatchAdd($activeId, $times, $sharpGoodsIds, $status = 1)
  176. {
  177. $saveData = [];
  178. foreach ($times as $time) {
  179. $saveData[] = [
  180. 'active_id' => $activeId,
  181. 'active_time' => (int)$time,
  182. 'status' => (int)$status,
  183. 'wxapp_id' => static::$wxapp_id,
  184. ];
  185. }
  186. // 批量更新
  187. $activeTimes = $this->isUpdate(false)->saveAll($saveData);
  188. // 新增活动场次与商品关联关系记录
  189. if (!empty($sharpGoodsIds)) {
  190. $this->onBatchAddActiveGoodsRec($activeTimes, $sharpGoodsIds);
  191. }
  192. return true;
  193. }
  194. /**
  195. * 新增活动场次与商品关联记录
  196. * @param $activeTimes
  197. * @param $sharpGoodsIds
  198. * @return array|false
  199. * @throws \Exception
  200. */
  201. private function onBatchAddActiveGoodsRec($activeTimes, $sharpGoodsIds)
  202. {
  203. $saveData = [];
  204. foreach ($activeTimes as $item) {
  205. foreach ($sharpGoodsIds as $goodsId) {
  206. $saveData[] = [
  207. 'active_id' => $item['active_id'],
  208. 'active_time_id' => $item['active_time_id'],
  209. 'sharp_goods_id' => $goodsId,
  210. 'wxapp_id' => static::$wxapp_id,
  211. ];
  212. }
  213. }
  214. return (new ActiveGoodsModel)->isUpdate(false)->saveAll($saveData);
  215. }
  216. /**
  217. * 根据活动ID删除全部场次和商品关系
  218. * @param $activeId
  219. * @return bool
  220. */
  221. public function onDeleteByActiveId($activeId)
  222. {
  223. $this->where('active_id', '=', $activeId)->delete();
  224. (new ActiveGoodsModel)->where('active_id', '=', $activeId)->delete();
  225. return true;
  226. }
  227. /**
  228. * 删除当前场次
  229. * @return bool
  230. * @throws \think\Exception
  231. * @throws \think\exception\PDOException
  232. */
  233. public function onDelete()
  234. {
  235. $this->delete();
  236. $this->goods()->delete();
  237. return true;
  238. }
  239. }