123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- <?php
- declare (strict_types=1);
- namespace app\common\model\za;
- use app\common\model\za\ZaActivityGood as ZaActivityGoodModel;
- use app\common\model\za\ZaActivityRelation as ZaActivityRelationModel;
- use app\common\model\Goods;
- use app\common\model\GoodsSku;
- use app\common\model\CommonHis;
- use app\common\service\store\User as StoreUserService;
- use app\common\model\BaseModel;
- use think\facade\Db;
- use app\common\model\UploadFile;
- use app\common\library\helper;
- /**
- * 活动中心 ▸ 买一赠一活动
- * Class Ad
- * @package app\common\model
- */
- class ZaActivity extends BaseModel {
-
- protected $name = 'za_activity';
- protected $append = ['status'];
- /**
- * 隐藏字段
- * @var array
- */
- protected $hidden = [
- 'goods'
- ];
- // public function getImageAttr(){
- // $a = UploadFile::field('file_id,file_type,storage,domain,file_path')->find($this->image_up_id)->ali_url??'';
- // $b = UploadFile::field('file_id,file_type,storage,domain,file_path')->find($this->image_down_id)->ali_url??'';
- // return [$a,$b];
- // }
- // public function imageshare(){
- // return $this->hasOne('app\common\model\UploadFile', 'file_id', 'image_share_id');
- // }
- // public function imageup(){
- // return $this->hasOne('app\common\model\UploadFile', 'file_id', 'image_up_id');
- // }
- // public function imagedown(){
- // return $this->hasOne('app\common\model\UploadFile', 'file_id', 'image_down_id');
- // }
- // public static function getCurrent(){
- // $now = Date("Y-m-d H:i:s",time());
- // // $filter['audit_status_zg'] = 10;
- // $filter['is_up'] = 1;
- // $one = ZaActivity::where('start_time','<',$now)->where('end_time','>',$now)->where($filter)->find();
- // if($one){
- // return $one;
- // }else{
- // $one = ZaActivity::find();
- // return $one;
- // }
- // return false;
- // }
- /**
- * 一对多关联商品表
- */
- public function goods(){
- return $this->hasMany('ZaActivityGood', 'za_activity_id')
- // ->order('is_end','asc')
- // ->order('start_time', 'asc')
- ;
- }
- public function goodsInfos(){
- return $this->hasMany('ZaActivityGood', 'za_activity_id')
- // ->order('is_end','asc')
- // ->order('start_time', 'asc')
- ;
- }
- // /**
- // * 一对多关联商品表
- // */
- // public function imagelist(){
- // return $this->hasMany('MsActivityImage', 'ms_activity_id')->order('id', 'desc');
- // }
- /**
- * 作废
- * @param array $data
- * @return bool
- */
- public function down(array $data): bool {
- $this->transaction(function () use ($data) {
- $id = (int)$this['id'];
- $this->save($data);
- });
- return true;
- }
-
- // //获取秒杀价
- // public function getMsPrice($goods_id,$goods_price_min){
- // $now = Date("Y-m-d H:i:s",time());
- // $filter[] = ['activity.start_time','<=',date('Y-m-d H:i:s')];
- // $filter[] = ['activity.end_time','>=',date('Y-m-d H:i:s')];
- // $filter[] = ['activity.is_up','=',1];
- // $filter[] = ['activity.audit_status','=',1];
- // $filter[] = ['ms_activity_goods.goods_id','=',$goods_id];
- // $filter[] = ['ms_activity_goods.end_time','>',$now];
- // $goods = $this->field('ms_activity_goods.*')->alias('activity')->leftjoin('ms_activity_goods','ms_activity_goods.ms_activity_id=activity.id')->where($filter)->find();
- // $type = 0;
- // if($goods){
- // $now = date("Y-m-d H:i:s",time());
- // if ($now < $goods->start_time) {
- // $d = Date("d",time());
- // // 2022-01-01 15:00:00;
- // if($d==substr($goods->start_time,8,2)){
- // $type = 1;
- // $text = substr($goods->start_time,11,5).'开始秒杀';
- // }else{
- // $type = 2;
- // $text = Date('Y-m-d H:i',strtotime($goods->start_time)).'开始秒杀';
- // }
- // }
- // if ($now > $goods->end_time) {
- // $type = 3;
- // $text = '秒杀结束';
- // }
- // if($now>$goods->start_time&&$now<$goods->end_time){
- // $type = 4;
- // $text = '限时秒杀';
- // }
- // $data['ms_price'] = $goods->ms_price; //秒杀价
- // $data['type'] = $type; //类型 1 当天开始秒杀,2 某天某时开始秒杀 4秒杀中
- // $data['text'] = $text; //文字
- // $data['dif_end_time'] = strtotime($goods->end_time) - time();
- // $data['limit_mount'] = $goods->limit_mount;
-
- // $data['stock_num'] = $goods->stock_num;
- // $data['ms_id'] = $goods->id;
- // $data['start_time'] = substr($goods->start_time,0,16);
- // $data['zj_price'] = helper::bcsub($goods_price_min - $goods->ms_price,0,2);
- // $data['ms_activity_id'] = $goods->ms_activity_id;
- // return $data;
- // }
- // return null ;
- // }
- /**
- * 获取列表
- * @param array $param
- * @return \think\Paginator
- * @throws \think\db\exception\DbException
- */
- public function getList($param = [])
- {
- // 检索查询砍价列表
- $filter = $this->getFilter($param);
- // 排序条件
- // $sort = $this->setQuerySort($param);
- $sort = ['activity.id'=>'desc'];
- // 查询列表数据
- $param['status'] = $param['status']??-1;
- //
- $list = $this->field('activity.*')
- ->alias('activity')
- ->leftjoin('za_activity_goods','za_activity_goods.za_activity_id=activity.id')
- ->where($filter)
- ->with(['goods'])
- ->group('activity.id')
- ->order($sort)
- ->paginate(15)
- ->each(function($item) {
- // 查询下单人数
- $orderCount = ZaActivityRelationModel::where('za_activity_id', $item['id'])
- ->where('is_pay', "=", "1")->count();
- $item['order_count'] = $orderCount;
- // 查询领取人数
- $getCount = ZaActivityRelationModel::where('za_activity_id', $item['id'])
- ->where('is_pay', "=", "1")->where('receive_state', "=", "1")->count();
- $item['get_count'] = $getCount;
- // 库存
- $stock_num = 0;
- foreach($item['goods'] as $k){
- $stock_num += $k['stock_num'];
- }
- $item['stock_num'] = $stock_num;
- });
-
-
- return $list ;
- }
- /**
- * 检索查询条件
- * @param array $param
- * @return array
- */
- private function getFilter($param = [])
- {
- // 默认查询条件
- $params = $this->setQueryDefaultValue($param, ['name'=>'','audit_status'=>-1,'status'=>-1]);
- // 检索查询条件
- $filter = [];
-
- if(isset($params['audit_status']) && $params['audit_status']>-1){
- $filter[] = ['audit_status', '=', $params['audit_status']];
- }
- if(isset($params['name']) && !empty($params['name'])){
- $filter[] = ['activity.name', 'like','%'.$params['name'].'%'];
- }
- if (!empty($params['betweenTime'])) {
- $times = between_time_format($params['betweenTime']);
- $filter[] = ['activity.start_time', '>=', Date('Y-m-d H:i:s',$times['start_time'])];
- $filter[] = ['activity.end_time', '<', Date('Y-m-d H:i:s',$times['end_time'])];
- }
- //活动状态
- if(isset($params['status'])&&$params['status']>-1){
- switch ($params['status']){
- case 1://进行中
- $filter[] = ['activity.start_time','<=',date('Y-m-d H:i:s')];
- $filter[] = ['activity.end_time','>=',date('Y-m-d H:i:s')];
- break;
- case 2://未开始
- $filter[] = ['activity.start_time','>',date('Y-m-d H:i:s')];
- break;
- case 3://已结束
- $filter[] = ['activity.end_time','<',date('Y-m-d H:i:s')];
- break;
- }
- }
- return $filter;
- }
- //活动状态 即将开始或已结束
- public function getStatusAttr() {
- $now = date("Y-m-d H:i:s",time());
- if($this->is_up==0){
- return 4;
- }
- if ($now < $this->start_time) {
- return 2;
- }
- if ($now > $this->end_time) {
- return 3;
- }
- if($now>$this->start_time&&$now<$this->end_time){
- return 1;
- }
- return 0;
- }
- /**
- * 审核
- * @param array $data
- * @return bool
- */
- public function audit(array $data): bool
- {
- $this->transaction(function () use ($data) {
- $user = StoreUserService::getLoginInfo();
- $real_name = $user['user']['real_name']??'';
- $id = (int)$this['id'];
- $this->save($data);
- $id = (int)$this['id'];
- if($data['audit_status']==1){
- (new CommonHis())->add(4, $id, $real_name,'同意并发布活动');
- }
- if($data['audit_status']==2){
- $remark = '不同意'." ".$data['audit_reason'];
- (new CommonHis())->add(4, $id, $real_name,$remark);
- }
-
- });
- return true;
- }
- /**
- * 详情
- * @param int $id
- * @return null|static
- */
- public static function detail(int $id,array $with=[])
- {
- return self::get($id,$with);
- }
- /**
- * 添加
- * @param $data
- * @return false|int
- */
- public function addOne($data)
- {
- $times = between_time_format($data['between_time']);
- $data['start_time'] = Date('Y-m-d H:i:s',$times['start_time']);
- $data['end_time'] = Date('Y-m-d H:i:s',$times['end_time']);
- isset($data['start_time']) && empty($data['start_time']) && $data['start_time'] = null;
- isset($data['end_time']) && empty($data['end_time']) && $data['end_time'] = null;
- $this->transaction(function () use ($data) {
- if(isset($data['goods'])){
- $this->save($data);
- $goods = new ZaActivityGoodModel;
- $flag = $goods->add($this->id,$data['goods']);
- if(!$flag){
- $this->error = $goods->error;
- Db::rollback();
- return false;
- }
- } else {
- $this->error = '请添加活动商品';
- return false;
- }
-
- $user = StoreUserService::getLoginInfo();
- $real_name = $user['user']['real_name']??'';
- $id = (int)$this['id'];
- (new CommonHis())->add(4, $id, $real_name,'提交审核');
- });
- return true;
-
- }
- /**
- * 编辑
- * @param $data
- * @return bool|int
- */
- public function edit($data)
- {
- $times = between_time_format($data['between_time']);
- $data['start_time'] = Date('Y-m-d H:i:s',$times['start_time']);
- $data['end_time'] = Date('Y-m-d H:i:s',$times['end_time']);
- isset($data['start_time']) && empty($data['start_time']) && $data['start_time'] = null;
- isset($data['end_time']) && empty($data['end_time']) && $data['end_time'] = null;
- $this->transaction(function () use ($data) {
- $this->save($data);
- if(isset($data['goods'])){
- $goods = new ZaActivityGoodModel;
- $flag = $goods->add($this->id,$data['goods']);
- if(!$flag){
- $this->error = $goods->error;
- Db::rollback();
- return false;
- }
- }
-
- $user = StoreUserService::getLoginInfo();
- $real_name = $user['user']['real_name']??'';
- $id = (int)$this['id'];
- (new CommonHis())->add(4, $id, $real_name,'再次编辑');
- });
- return true;
- }
- /**
- * 关联记录
- * @return \think\model\relation\BelongsTo
- */
- public function his()
- {
- return $this->hasMany('app\common\model\CommonHis','target_id','id')->where('target_type',4)->order('id', 'desc');
- }
- // /**
- // * 删除
- // * @param $id
- // * @return bool|int
- // */
- // public function remove()
- // {
- // return $this->delete();
- // }
- public function orderSt(int $id) {
- $data = [];
- // 查询订单数
- $orderCount = ZaActivityRelationModel::where('za_activity_id', $id)
- ->count();
- $data['st']['order_count'] = $orderCount;
- // 查询下单人数
- $payCount = ZaActivityRelationModel::where('za_activity_id', $id)
- ->where('is_pay', "=", "1")->count();
- $data['st']['pay_count'] = $payCount;
- // 查询领取人数
- $getCount = ZaActivityRelationModel::where('za_activity_id', $item['id'])
- ->where('is_pay', "=", "1")
- ->where('receive_state', "=", "1")->count();
- $data['st']['get_count'] = $getCount;
-
- $data['st']['total_price'] = '0.00';
- $list = ZaActivityRelationModel::where('za_activity_id', $id);
- $data['list'] = $list;
- return $data;
- }
- }
|