MsActivity.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\common\model\ms;
  4. use app\common\model\ms\MsActivityGood as MsActivityGoodModel;
  5. use app\common\model\ms\MsActivityImage as MsActivityImageModel;
  6. use app\common\model\Goods;
  7. use app\common\model\GoodsSku;
  8. use app\common\model\CommonHis;
  9. use app\common\service\store\User as StoreUserService;
  10. use app\common\model\BaseModel;
  11. use think\facade\Db;
  12. use app\common\model\UploadFile;
  13. use app\common\library\helper;
  14. /**
  15. * 活动中心 ▸ 秒杀活动
  16. * Class Ad
  17. * @package app\common\model
  18. */
  19. class MsActivity extends BaseModel
  20. {
  21. protected $name = 'ms_activity';
  22. protected $append = ['status','image'];
  23. public function getImageAttr(){
  24. $a = UploadFile::field('file_id,file_type,storage,domain,file_path')->find($this->image_up_id)->ali_url??'';
  25. $b = UploadFile::field('file_id,file_type,storage,domain,file_path')->find($this->image_down_id)->ali_url??'';
  26. return [$a,$b];
  27. }
  28. public function imageshare(){
  29. return $this->hasOne('app\common\model\UploadFile', 'file_id', 'image_share_id');
  30. }
  31. public function imageup(){
  32. return $this->hasOne('app\common\model\UploadFile', 'file_id', 'image_up_id');
  33. }
  34. public function imagedown(){
  35. return $this->hasOne('app\common\model\UploadFile', 'file_id', 'image_down_id');
  36. }
  37. public static function getCurrent(){
  38. $now = Date("Y-m-d H:i:s",time());
  39. $filter['audit_status_zg'] = 10;
  40. $filter['is_up'] = 1;
  41. $one = MsActivity::where('start_time','<',$now)->where('end_time','>',$now)->where($filter)->find();
  42. if($one){
  43. return $one;
  44. }else{
  45. $one = MsActivity::find();
  46. return $one;
  47. }
  48. return false;
  49. }
  50. /**
  51. * 一对多关联商品表
  52. */
  53. public function goods(){
  54. return $this->hasMany('MsActivityGood', 'ms_activity_id')->order('is_end','asc')->order('start_time', 'asc');
  55. }
  56. /**
  57. * 一对多关联商品表
  58. */
  59. public function imagelist(){
  60. return $this->hasMany('MsActivityImage', 'ms_activity_id')->order('id', 'desc');
  61. }
  62. /**
  63. * 作废
  64. * @param array $data
  65. * @return bool
  66. */
  67. public function down(array $data): bool {
  68. $this->transaction(function () use ($data) {
  69. $id = (int)$this['id'];
  70. $this->save($data);
  71. });
  72. return true;
  73. }
  74. //获取秒杀价
  75. public function getMsPrice($goods_id,$goods_price_min){
  76. $now = Date("Y-m-d H:i:s",time());
  77. $filter[] = ['activity.start_time','<=',date('Y-m-d H:i:s')];
  78. $filter[] = ['activity.end_time','>=',date('Y-m-d H:i:s')];
  79. $filter[] = ['activity.is_up','=',1];
  80. $filter[] = ['activity.audit_status','=',1];
  81. $filter[] = ['ms_activity_goods.goods_id','=',$goods_id];
  82. $filter[] = ['ms_activity_goods.end_time','>',$now];
  83. $goods = $this->field('ms_activity_goods.*')->alias('activity')->leftjoin('ms_activity_goods','ms_activity_goods.ms_activity_id=activity.id')->where($filter)->find();
  84. $type = 0;
  85. if($goods){
  86. $now = date("Y-m-d H:i:s",time());
  87. if ($now < $goods->start_time) {
  88. $d = Date("d",time());
  89. // 2022-01-01 15:00:00;
  90. if($d==substr($goods->start_time,8,2)){
  91. $type = 1;
  92. $text = substr($goods->start_time,11,5).'开始秒杀';
  93. }else{
  94. $type = 2;
  95. $text = Date('Y-m-d H:i',strtotime($goods->start_time)).'开始秒杀';
  96. }
  97. }
  98. if ($now > $goods->end_time) {
  99. $type = 3;
  100. $text = '秒杀结束';
  101. }
  102. if($now>$goods->start_time&&$now<$goods->end_time){
  103. $type = 4;
  104. $text = '限时秒杀';
  105. }
  106. $data['ms_price'] = $goods->ms_price; //秒杀价
  107. $data['type'] = $type; //类型 1 当天开始秒杀,2 某天某时开始秒杀 4秒杀中
  108. $data['text'] = $text; //文字
  109. $data['dif_end_time'] = strtotime($goods->end_time) - time();
  110. $data['limit_mount'] = $goods->limit_mount;
  111. $data['stock_num'] = $goods->stock_num;
  112. $data['ms_id'] = $goods->id;
  113. $data['start_time'] = substr($goods->start_time,0,16);
  114. $data['zj_price'] = helper::bcsub($goods_price_min - $goods->ms_price,0,2);
  115. $data['ms_activity_id'] = $goods->ms_activity_id;
  116. return $data;
  117. }
  118. return null ;
  119. }
  120. /**
  121. * 获取列表
  122. * @param array $param
  123. * @return \think\Paginator
  124. * @throws \think\db\exception\DbException
  125. */
  126. public function getList($param = [])
  127. {
  128. // 检索查询砍价列表
  129. $filter = $this->getFilter($param);
  130. // 排序条件
  131. // $sort = $this->setQuerySort($param);
  132. $sort = ['activity.id'=>'desc'];
  133. // 查询列表数据
  134. $param['status'] = $param['status']??-1;
  135. //
  136. $list = $this->field('activity.*')->alias('activity')->leftjoin('ms_activity_goods','ms_activity_goods.ms_activity_id=activity.id')->where($filter)->with(['goods'])->group('activity.id')->order($sort)->paginate(15)->each(function($item) {
  137. $item['good_cnt'] = count($item['goods']);
  138. $sale_cnt = 0;
  139. foreach($item['goods'] as $k){
  140. $sale_cnt = $sale_cnt + $k['sale_num'];
  141. }
  142. $item['sale_cnt'] = $sale_cnt;
  143. });
  144. return $list ;
  145. }
  146. /**
  147. * 检索查询条件
  148. * @param array $param
  149. * @return array
  150. */
  151. private function getFilter($param = [])
  152. {
  153. // 默认查询条件
  154. $params = $this->setQueryDefaultValue($param, ['goods_name'=>'','audit_status'=>-1,'status'=>-1]);
  155. // 检索查询条件
  156. $filter = [];
  157. if(isset($params['audit_status']) && $params['audit_status']>-1){
  158. $filter[] = ['audit_status', '=', $params['audit_status']];
  159. }
  160. if(isset($params['goods_name']) && !empty($params['goods_name'])){
  161. $filter[] = ['ms_activity_goods.goods_name', 'like','%'.$params['goods_name'].'%'];
  162. }
  163. if (!empty($params['betweenTime'])) {
  164. $times = between_time_format($params['betweenTime']);
  165. $filter[] = ['activity.start_time', '>=', Date('Y-m-d H:i:s',$times['start_time'])];
  166. $filter[] = ['activity.end_time', '<', Date('Y-m-d H:i:s',$times['end_time'])];
  167. }
  168. //活动状态
  169. if(isset($params['status'])&&$params['status']>-1){
  170. switch ($params['status']){
  171. case 1://进行中
  172. $filter[] = ['activity.start_time','<=',date('Y-m-d H:i:s')];
  173. $filter[] = ['activity.end_time','>=',date('Y-m-d H:i:s')];
  174. // $filter[] = ['audit_status_zg','=',10];
  175. // $filter[] = ['is_up','=',1];
  176. // $filter[] = ['audit_status_zg','=',10];
  177. break;
  178. case 2://未开始
  179. $filter[] = ['activity.start_time','>',date('Y-m-d H:i:s')];
  180. // $filter[] = ['is_up','=',1];
  181. // $filter[] = ['audit_status_zg','=',10];
  182. break;
  183. case 3://已结束
  184. // $filter[] = ['end_time','<',date('Y-m-d H:i:s')];
  185. // $filter[] = ['is_up','in',[0,1]];
  186. $filter[] = ['activity.end_time','<',date('Y-m-d H:i:s')];
  187. break;
  188. }
  189. }
  190. return $filter;
  191. }
  192. //活动状态 即将开始或已结束
  193. public function getStatusAttr() {
  194. $now = date("Y-m-d H:i:s",time());
  195. if($this->is_up==0){
  196. return 4;
  197. }
  198. if ($now < $this->start_time) {
  199. return 2;
  200. }
  201. if ($now > $this->end_time) {
  202. return 3;
  203. }
  204. if($now>$this->start_time&&$now<$this->end_time){
  205. return 1;
  206. }
  207. return 0;
  208. }
  209. /**
  210. * 审核
  211. * @param array $data
  212. * @return bool
  213. */
  214. public function audit(array $data): bool
  215. {
  216. $this->transaction(function () use ($data) {
  217. $user = StoreUserService::getLoginInfo();
  218. $real_name = $user['user']['real_name']??'';
  219. $id = (int)$this['id'];
  220. $this->save($data);
  221. $id = (int)$this['id'];
  222. if($data['audit_status']==1){
  223. (new CommonHis())->add(3, $id, $real_name,'同意并发布活动');
  224. }
  225. if($data['audit_status']==2){
  226. $remark = '不同意'." ".$data['audit_reason'];
  227. (new CommonHis())->add(3, $id, $real_name,$remark);
  228. }
  229. });
  230. return true;
  231. }
  232. /**
  233. * 详情
  234. * @param int $id
  235. * @return null|static
  236. */
  237. public static function detail(int $id,array $with=[])
  238. {
  239. return self::get($id,$with);
  240. }
  241. /**
  242. * 添加
  243. * @param $data
  244. * @return false|int
  245. */
  246. public function addOne($data)
  247. {
  248. $times = between_time_format($data['between_time']);
  249. $data['start_time'] = Date('Y-m-d H:i:s',$times['start_time']);
  250. $data['end_time'] = Date('Y-m-d H:i:s',$times['end_time']);
  251. isset($data['start_time']) && empty($data['start_time']) && $data['start_time'] = null;
  252. isset($data['end_time']) && empty($data['end_time']) && $data['end_time'] = null;
  253. $this->transaction(function () use ($data) {
  254. $this->save($data);
  255. if(isset($data['goods'])){
  256. $goods = new MsActivityGoodModel;
  257. $flag = $goods->add($this->id,$data['goods']);
  258. if(!$flag){
  259. $this->error = $goods->error;
  260. Db::rollback();
  261. return false;
  262. }
  263. }
  264. if(isset($data['images'])){
  265. $images = new MsActivityImageModel;
  266. $flag = $images->add($this->id,$data['images']);
  267. if(!$flag){
  268. $this->error = $images->error;
  269. Db::rollback();
  270. return false;
  271. }
  272. }
  273. $user = StoreUserService::getLoginInfo();
  274. $real_name = $user['user']['real_name']??'';
  275. $id = (int)$this['id'];
  276. (new CommonHis())->add(3, $id, $real_name,'提交审核');
  277. });
  278. return true;
  279. }
  280. /**
  281. * 编辑
  282. * @param $data
  283. * @return bool|int
  284. */
  285. public function edit($data)
  286. {
  287. $times = between_time_format($data['between_time']);
  288. $data['start_time'] = Date('Y-m-d H:i:s',$times['start_time']);
  289. $data['end_time'] = Date('Y-m-d H:i:s',$times['end_time']);
  290. isset($data['start_time']) && empty($data['start_time']) && $data['start_time'] = null;
  291. isset($data['end_time']) && empty($data['end_time']) && $data['end_time'] = null;
  292. $this->transaction(function () use ($data) {
  293. $this->save($data);
  294. if(isset($data['goods'])){
  295. $goods = new MsActivityGoodModel;
  296. $flag = $goods->add($this->id,$data['goods']);
  297. if(!$flag){
  298. $this->error = $goods->error;
  299. Db::rollback();
  300. return false;
  301. }
  302. }
  303. if(isset($data['images'])){
  304. $images = new MsActivityImageModel;
  305. $flag = $images->add($this->id,$data['images']);
  306. if(!$flag){
  307. $this->error = $images->error;
  308. Db::rollback();
  309. return false;
  310. }
  311. }
  312. $user = StoreUserService::getLoginInfo();
  313. $real_name = $user['user']['real_name']??'';
  314. $id = (int)$this['id'];
  315. (new CommonHis())->add(3, $id, $real_name,'再次编辑');
  316. });
  317. return true;
  318. }
  319. /**
  320. * 关联记录
  321. * @return \think\model\relation\BelongsTo
  322. */
  323. public function his()
  324. {
  325. return $this->hasMany('app\common\model\CommonHis','target_id','id')->where('target_type',3)->order('id', 'desc');
  326. }
  327. // /**
  328. // * 删除
  329. // * @param $id
  330. // * @return bool|int
  331. // */
  332. // public function remove()
  333. // {
  334. // return $this->delete();
  335. // }
  336. }