ZaActivity.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\store\controller;
  4. use app\common\model\za\ZaActivity as ZaActiivtyModel;
  5. use app\common\model\Goods;
  6. use app\common\model\za\ZaActivityRelation as ZaActivityRelationModel;
  7. class ZaActivity extends Controller {
  8. /**
  9. * 添加活动
  10. */
  11. public function add() {
  12. $model = new ZaActiivtyModel;
  13. $post = $this->postForm();
  14. $post['code'] = rand(10000000,99999999);
  15. if (isset($post['role']) && $post['role'] == 1) {
  16. $post['audit_time'] = Date("Y-m-d H:i:s", time());
  17. $post['audit_status'] = 1;
  18. } else {
  19. $post['audit_status'] = 0;
  20. }
  21. if ($model->addOne($post)&&$model->getError()=='') {
  22. return $this->renderSuccess('添加成功');
  23. }
  24. return $this->renderError($model->getError() ?: '添加失败');
  25. }
  26. /**
  27. * 列表
  28. * @return array
  29. * @throws \think\db\exception\DbException
  30. */
  31. public function list()
  32. {
  33. $model = new ZaActiivtyModel;
  34. $list = $model->getList($this->request->param());
  35. return $this->renderSuccess(compact('list'));
  36. }
  37. /**
  38. * 审核
  39. * @param int $coupon_id
  40. * @return array|bool
  41. */
  42. public function audit()
  43. {
  44. // 确认审核
  45. $postData = $this->postForm();
  46. $model = ZaActiivtyModel::where('id',$postData['id'])->find();
  47. if(empty($model)){
  48. return $this->renderError('找不到数据');
  49. }
  50. unset($postData['id']);
  51. $res ='操作成功';
  52. $audit_status = $postData['audit_status'];
  53. if($audit_status==1){
  54. $res = '已审核通过';
  55. }
  56. if($audit_status==2){
  57. $res = '已审核不通过';
  58. }
  59. $postData['audit_time'] = Date("Y-m-d H:i:s",time());
  60. $postData['admin_id'] = $this->store['user']['store_user_id'];
  61. if ($model->audit($postData)) {
  62. return $this->renderSuccess($postData,$res);
  63. }
  64. return $this->renderError($model->getError() ?: '操作失败');
  65. }
  66. /**
  67. * 详情
  68. */
  69. public function activityinfo(int $id){
  70. $data = ZaActiivtyModel::where('id',$id)->with(['goodsInfos','his'])->find();
  71. foreach($data['goodsInfos'] as &$r){
  72. $good = Goods::where('goods_id',$r['goods_id'])->find();
  73. $r['goods_status'] = $good->status??20;
  74. $r['stock_total'] = $good->stock_total??0;
  75. }
  76. if(empty($data)){
  77. return $this->renderError("找不到信息");
  78. }
  79. return $this->renderSuccess(compact("data"));
  80. }
  81. /**
  82. * 启用 禁用
  83. * @param int $coupon_id
  84. * @return array|bool
  85. */
  86. public function down(int $id){
  87. // 确认审核
  88. // $postData = $this->postForm();
  89. $model = ZaActiivtyModel::where('id',$id)->find();
  90. if(empty($model)){
  91. return $this->renderError('找不到数据');
  92. }
  93. if($model['is_up']==0){
  94. return $this->renderError('已经作废了,不要重复操作');
  95. }
  96. $postData['is_up'] = 0;
  97. $postData['end_time'] = Date("Y-m-d H:i:s",time()-1);
  98. if ($model->down($postData)) {
  99. return $this->renderSuccess($postData,'作废成功,活动已失效');
  100. }
  101. return $this->renderError($model->getError() ?: '操作失败');
  102. }
  103. public function edit(){
  104. // 新增记录
  105. $model = new ZaActiivtyModel;
  106. $post = $this->postForm();
  107. $post['audit_status'] = 0;
  108. $model = ZaActiivtyModel::where('id',$post['id'])->find();
  109. if ($model->edit($post)&&$model->getError()=='') {
  110. return $this->renderSuccess('修改成功');
  111. }
  112. return $this->renderError($model->getError() ?: '添加失败');
  113. }
  114. /**
  115. * 活动商品数据
  116. */
  117. public function activityGoods(int $id){
  118. $goods = [];
  119. $data = ZaActiivtyModel::where('id',$id)->with(['goodsInfos'])->find();
  120. foreach($data['goodsInfos'] as &$r){
  121. $good = Goods::field(['goods_id, goods_name', 'goods_no', 'goods_price_min'])
  122. ->where('goods_id',$r['goods_id'])->find();
  123. array_push($goods, $good);
  124. // $r['goods_status'] = $good->status??20;
  125. // $r['stock_total'] = $good->stock_total??0;
  126. }
  127. if(empty($data)){
  128. return $this->renderError("找不到信息");
  129. }
  130. return $this->renderSuccess(compact("goods"));
  131. }
  132. public function orderSt(int $id) {
  133. $data = [];
  134. // 查询订单数
  135. $orderCount = ZaActivityRelationModel::where('za_activity_id', $id)
  136. ->count();
  137. $data['order_count'] = $orderCount;
  138. // 查询下单人数
  139. $payCount = ZaActivityRelationModel::where('za_activity_id', $id)
  140. ->where('is_pay', "=", "1")->count();
  141. $data['pay_count'] = $payCount;
  142. // 查询领取人数
  143. $getCount = ZaActivityRelationModel::where('za_activity_id', $id)
  144. ->where('is_pay', "=", "1")
  145. ->where('receive_state', "=", "1")->count();
  146. $data['get_count'] = $getCount;
  147. $payTotal = ZaActivityRelationModel::alias('zrm')
  148. ->field('sum(g.goods_price) total_price')
  149. ->leftjoin('za_activity_goods g', 'g.za_activity_id=zrm.za_activity_id and g.id=zrm.za_goods_id')
  150. ->where('zrm.za_activity_id', $id)
  151. ->where('is_pay', "=", "1")
  152. ->find()
  153. ;
  154. $data['total_price'] = $payTotal['total_price']; // number_format($total_price, 2);
  155. return $this->renderSuccess(compact("data"));
  156. }
  157. public function orderList() {
  158. $param = $this->request->param();
  159. $list = ZaActivityRelationModel::alias('zrm')
  160. ->field(['zrm.id', 'user.nick_name', 'getu.nick_name as g_nick_name',
  161. 'zrm.order_id', 'zrm.child_order_id', 'zrm.receive_state',
  162. 'zrm.create_time', 'zrm.is_pay', 'zrm.order_price'
  163. ])
  164. ->leftjoin('za_activity_goods zzg','zzg.za_activity_id=zrm.za_activity_id and zzg.id=zrm.za_goods_id')
  165. ->leftjoin('order','order.order_id = zrm.order_id')
  166. ->leftjoin('user','user.user_id=order.user_id')
  167. ->leftjoin('user getu','getu.user_id=zrm.receive_user_id')
  168. ->where('zrm.za_activity_id', $param['id'])
  169. ->order('create_time desc')
  170. ->paginate(15)
  171. ;
  172. return $this->renderSuccess(compact("list"));
  173. }
  174. }
  175. ?>