ZaActivity.php 14 KB

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