SuperSalesForm.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. namespace app\modules\client\models\v1\super_sales;
  3. use app\models\Cash;
  4. use app\models\Goods;
  5. use app\models\Option;
  6. use app\models\Order;
  7. use app\models\OrderDetail;
  8. use app\models\Qrcode;
  9. use app\models\SaasUser;
  10. use app\models\Store;
  11. use app\models\SuperSales;
  12. use app\models\SuperSalesMoney;
  13. use app\models\SuperSalesSub;
  14. use app\models\SuperSalesUser;
  15. use app\models\User;
  16. use app\modules\client\models\v1\ShareQrcodeForm;
  17. use app\utils\ShareQrcode;
  18. class SuperSalesForm extends \yii\base\Model
  19. {
  20. public $user;
  21. public $saas_user;
  22. public $store_id;
  23. public $status;
  24. public $name;
  25. public $type;
  26. public $id;
  27. public function rules()
  28. {
  29. return [
  30. [['store_id', 'status', 'type', 'id'], 'integer'],
  31. [['name'], 'string']
  32. ];
  33. }
  34. //超级卖货中心
  35. public function index() {
  36. $store_id = $this->store_id;
  37. $user = $this->user;
  38. $saasUser = $this->saas_user;
  39. $superSalesUser = SuperSalesUser::findOne(['user_id' => $user->id]);
  40. $parent_user_info = User::findOne($superSalesUser->parent_user_id ?? $user->old_parent_id);
  41. $user_info = [
  42. 'name' => $user->nickname,
  43. 'avatar' => $user->avatar_url,
  44. 'parent_name' => $parent_user_info->nickname ?? '总店',
  45. ];
  46. //用户佣金 可提现佣金
  47. $wait_cash_amount = $user->price >= $superSalesUser->price ? $superSalesUser->price : $user->price;
  48. //用户佣金 未到账佣金
  49. $no_send_amount = SuperSalesMoney::find()->where(['user_id' => $user->id, 'status' => SuperSalesMoney::STATUS_NOT_SEND])
  50. ->select('money')->sum('money') ?: '0.00';
  51. //用户佣金 已提现佣金
  52. $cash_amount = Cash::find()->where(['user_id' => $user->id, 'cash_type' => Cash::IS_CASH_TYPE_SUPER_SALES, 'status' => [
  53. Cash::STATUS_GIVEN,
  54. Cash::STATUS_HAND,
  55. Cash::STATUS_RECHARGE
  56. ]])
  57. ->select('price')->sum('price') ?: '0.00';
  58. return [
  59. 'code' => 0,
  60. 'msg' => '',
  61. 'data' => [
  62. 'user_info' => $user_info,
  63. 'wait_cash_amount' => $wait_cash_amount,//可提现佣金
  64. 'no_send_amount' => $no_send_amount,//未到账佣金
  65. 'cash_amount' => $cash_amount,//已提现佣金
  66. 'total_amount' => bcadd($superSalesUser->group_award, $superSalesUser->direct_award, 2)//累计佣金
  67. ]
  68. ];
  69. }
  70. //分红奖
  71. public function dividendAwardList() {
  72. $status = $this->status;
  73. $user = $this->user;
  74. $query = SuperSalesMoney::find()->alias('sm')
  75. ->leftJoin(['ss' => SuperSales::tableName()], 'sm.sales_id = ss.id')
  76. ->where(['sm.user_id' => $user->id, 'sm.type' => SuperSalesMoney::TYPE_GROUP_AWARD]);
  77. if (isset($status) && in_array($status, [SuperSalesMoney::STATUS_NOT_SEND, SuperSalesMoney::STATUS_SEND])) {
  78. $query->andWhere(['sm.status' => $status]);
  79. }
  80. $query->select('sm.id, sm.money, sm.status, sm.send_time, ss.created_at, ss.finish_time, sm.sales_id')
  81. ->orderBy('sm.id DESC');
  82. $list = pagination_make($query);
  83. foreach ($list['list'] as &$item) {
  84. $item['id'] = $item['sales_id'];
  85. $item['status'] = intval($item['status']);
  86. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  87. $item['finish_time'] = $item['finish_time'] > 0 ? date('Y-m-d H:i:s', $item['finish_time']) : '';
  88. $item['send_time'] = $item['status'] ? date('Y-m-d H:i:s', $item['send_time']) : '暂未到账';
  89. }
  90. return [
  91. 'code' => 0,
  92. 'msg' => '',
  93. 'data' => $list
  94. ];
  95. }
  96. //直推奖 | 佣金明细(全部佣金)
  97. public function directAwardList() {
  98. $status = $this->status;
  99. $user = $this->user;
  100. $type = $this->type;
  101. $query = SuperSalesMoney::find()->alias('sm')
  102. ->leftJoin(['o' => Order::tableName()], 'sm.order_id = o.id')
  103. ->where(['sm.user_id' => $user->id]);
  104. if (!$type) {
  105. $query->andWhere(['sm.type' => SuperSalesMoney::TYPE_DIRECT_AWARD]);
  106. }
  107. if (isset($status) && in_array($status, [SuperSalesMoney::STATUS_NOT_SEND, SuperSalesMoney::STATUS_SEND])) {
  108. $query->andWhere(['sm.status' => $status]);
  109. }
  110. $query->select('sm.id, sm.money, sm.status, sm.send_time, sm.created_at money_created_at, sm.type, o.created_at, o.user_id, o.order_no')
  111. ->orderBy('sm.id DESC');
  112. $list = pagination_make($query);
  113. foreach ($list['list'] as &$item) {
  114. $item['status'] = intval($item['status']);
  115. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  116. $item['money_created_at'] = date('Y-m-d H:i:s', $item['money_created_at']);
  117. $item['send_time'] = $item['status'] ? date('Y-m-d H:i:s', $item['send_time']) : '暂未到账';
  118. $binding = User::findOne(['id' => $item['user_id'], 'is_delete' => 0])->binding;
  119. $user = SaasUser::find()->where(['mobile' => $binding])
  120. ->select('name')->asArray()->One();
  121. $item['order_user_name'] = $user['name'] ?: '';
  122. $item['award_name'] = SuperSalesMoney::$typeMap[$item['type']];
  123. }
  124. return [
  125. 'code' => 0,
  126. 'msg' => '',
  127. 'data' => $list
  128. ];
  129. }
  130. //团列表
  131. public function superSalesList() {
  132. $user = $this->user;
  133. $superSalesArray = SuperSalesSub::find()->where(['user_id' => $user->id])->orderBy('id DESC')
  134. ->select('sales_id')->column() ?: [];
  135. $query = SuperSales::find()->alias('ss')
  136. ->leftJoin(['u' => User::tableName()], 'ss.user_id = u.id')
  137. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding')
  138. ->where(['ss.id' => $superSalesArray])
  139. ->select('ss.id, ss.created_at, ss.status, su.name, su.avatar')->orderBy('ss.id DESC');
  140. $list = pagination_make($query);
  141. foreach ($list['list'] as &$item) {
  142. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  143. $item['num'] = SuperSalesSub::find()->where(['sales_id' => $item['id']])->count();
  144. $item['status'] = intval($item['status']);
  145. }
  146. return [
  147. 'code' => 0,
  148. 'msg' => '',
  149. 'data' => $list
  150. ];
  151. }
  152. //团信息
  153. public function superSalesInfo() {
  154. $superSalesId = $this->id;
  155. $superSalesSub = SuperSalesSub::find()->alias('sss')
  156. ->leftJoin(['o' => Order::tableName()], 'sss.order_id = o.id')
  157. ->leftJoin(['u' => User::tableName()], 'o.user_id = u.id')
  158. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding')
  159. ->where(['sss.sales_id' => $superSalesId])
  160. ->select('sss.id, sss.points, o.order_no, o.created_at, su.name, su.avatar, sss.order_detail_id')
  161. ->orderBy('sss.points DESC')
  162. ->asArray()->all();
  163. foreach ($superSalesSub as &$subItem) {
  164. $order_detail_id = explode(',', $subItem['order_detail_id']);
  165. $subItem['goods_list'] = OrderDetail::find()->where(['id' => $order_detail_id, 'is_delete' => 0])
  166. ->select('goods_id, total_price, goods_name, num, pic, attr')->asArray()->all();
  167. foreach ($subItem['goods_list'] as &$goods_item) {
  168. $goods_item['attr'] = json_decode($goods_item['attr'], true);
  169. }
  170. $subItem['created_at'] = date('Y-m-d H:i:s', $subItem['created_at']);
  171. $is_send = SuperSalesMoney::findOne(['sales_sub_id' => $subItem['id'], 'status' => SuperSalesMoney::STATUS_NOT_SEND]);
  172. if ($is_send) {
  173. $subItem['status'] = '待入账';
  174. $subItem['money'] = SuperSalesMoney::find()->where(['sales_sub_id' => $subItem['id'], 'status' => SuperSalesMoney::STATUS_NOT_SEND])
  175. ->sum('money') ?: '0.00';
  176. } else {
  177. $subItem['status'] = '已入账';
  178. $subItem['money'] = SuperSalesMoney::find()->where(['sales_sub_id' => $subItem['id']])
  179. ->sum('money') ?: '0.00';
  180. }
  181. }
  182. return [
  183. 'code' => 0,
  184. 'msg' => '',
  185. 'data' => [
  186. 'list' => $superSalesSub
  187. ]
  188. ];
  189. }
  190. public function superSalesGoods() {
  191. $store_id = $this->store_id;
  192. $super_sales_setting = Option::get('super_sales_setting', $store_id, 'super_sales')['value'];
  193. $super_sales_setting = json_decode($super_sales_setting ?? '', true);
  194. $goods_ids = explode(',', $super_sales_setting['goods_ids'] ?? '');
  195. $query = Goods::find()->where(['id' => $goods_ids, 'is_delete' => 0, 'status' => Goods::STATUS_NORMAL])
  196. ->select('id, name, price, cover_pic');
  197. $list = pagination_make($query);
  198. return [
  199. 'code' => 0,
  200. 'msg' => '',
  201. 'data' => $list
  202. ];
  203. }
  204. //我的推荐
  205. public function myRecommend() {
  206. $user = $this->user;
  207. $name = $this->name;
  208. $query = SuperSalesUser::find()->alias('ssu')
  209. ->leftJoin(['u' => User::tableName()], 'ssu.user_id = u.id')
  210. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding')
  211. ->where(['ssu.parent_user_id' => $user->id]);
  212. if ($name) {
  213. $query->andWhere(['like', 'su.name', $name]);
  214. }
  215. $query->select('ssu.id, su.name, su.avatar, ssu.created_at, ssu.user_id');
  216. $list = pagination_make($query);
  217. foreach ($list['list'] as &$item) {
  218. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  219. $item['num'] = SuperSales::find()->where(['user_id' => $item['user_id'], 'status' => SuperSales::STATUS_FINISH])
  220. ->count();
  221. }
  222. return [
  223. 'code' => 0,
  224. 'msg' => '',
  225. 'data' => $list
  226. ];
  227. }
  228. //我的二维码
  229. public function myQrcode() {
  230. $user = $this->user;
  231. $store_id = $this->store_id;
  232. $store = Store::findOne($store_id);
  233. $store_info = [
  234. 'name' => $store->name,
  235. 'logo' => $store->logo ?: (Option::get('logo', $store_id, 'store')['value'] ?: Option::get('web_log', $store_id, 'web')['value']),
  236. ];
  237. $url_path = '';
  238. try {
  239. $form = new ShareQrcodeForm();
  240. $form->store_id = $store_id;
  241. $form->type = Qrcode::TYPE_SUPER_SALES;
  242. $form->user_id = $user->id;
  243. $result = $form->search();
  244. if ($result['code']) {
  245. throw new \Exception($result['msg']);
  246. }
  247. $url_path = $result['data'];
  248. } catch (\Exception $e) {
  249. debug_log([
  250. 'code' => $e->getCode(),
  251. 'message' => $e->getMessage(),
  252. 'file' => $e->getFile(),
  253. 'line' => $e->getLine(),
  254. ], 'group_purchase_qrcode.log');
  255. }
  256. return [
  257. 'code' => 0,
  258. 'msg' => '',
  259. 'data' => [
  260. // 'store_info' => $store_info,
  261. 'qrcode_url' => $url_path,
  262. ]
  263. ];
  264. }
  265. // private function getQrcode($scene, $width = 240, $page = null)
  266. // {
  267. //
  268. // if (is_alipay_platform()) {
  269. // $res = ShareQrcode::getAlipayQrcode($page ?: 'pages/home/home', $scene);
  270. // if (empty($res['code']) || $res['code'] != 10000) {
  271. // return $res;
  272. // }
  273. // $res['url_path'] = $res['qr_code_url_circle_blue'];
  274. // return $res;
  275. // } else {
  276. // return ShareQrcode::wxQrcode($page, $scene, $width);
  277. // }
  278. //
  279. // }
  280. }