DefaultController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. namespace addons\Agent\api\modules\v1\controllers;
  3. use addons\Agent\common\enums\AgentEnum;
  4. use addons\Agent\common\models\Agent;
  5. use addons\Agent\common\models\AgentApply;
  6. use addons\Agent\common\models\AgentLevel;
  7. use common\enums\GlobalFuncEnum;
  8. use common\enums\OrderStatusEnum;
  9. use common\enums\StatusEnum;
  10. use common\globals\GlobalInvoke;
  11. use common\models\common\CapitalLog;
  12. use common\models\common\GlobalVarMap;
  13. use common\models\order\Order;
  14. use common\models\order\OrderDetail;
  15. use common\models\user\User;
  16. use common\models\user\UserPartitionRelationship;
  17. use api\controllers\OnAuthController;
  18. use Yii;
  19. /**
  20. * 默认控制器
  21. *
  22. * Class DefaultController
  23. * @package addons\Agent\api\modules\v1\controllers
  24. */
  25. class DefaultController extends OnAuthController
  26. {
  27. public $modelClass = '';
  28. public $from_type = ['Agent'];
  29. /**
  30. * 不用进行登录验证的方法
  31. *
  32. * 例如: ['index', 'update', 'create', 'view', 'delete']
  33. * 默认全部需要验证
  34. *
  35. * @var array
  36. */
  37. protected $authOptional = ['index'];
  38. /**
  39. * 首页
  40. *
  41. * @return string
  42. */
  43. public function actionIndex()
  44. {
  45. return 'Hello world';
  46. }
  47. /**
  48. * @desc:渠道详情
  49. * @Author: hua
  50. * @Date: 2021/11/4
  51. * @Time: 16:47
  52. * @Copyright: copyright (c) 2021 广东七件事集团
  53. * @return mixed|void
  54. */
  55. public function actionInfo()
  56. {
  57. $data['user_id'] = $this->user_id;
  58. $data['nickname'] = empty($this->user->nickname) ? '' : $this->user->nickname;
  59. $data['avatar_url'] = empty($this->user->avatar_url) ? '' : $this->user->avatar_url;
  60. $agent = Agent::getOne([['user_id' => $this->user_id],['status'=>[StatusEnum::DISABLED,StatusEnum::ENABLED]]], true, ['agentLevel']);
  61. $data['is_agent'] = 0;
  62. $data['level_name'] = '';
  63. $data['frozen_price'] = 0;
  64. $data['total_price'] = 0;
  65. $data['total_income'] = 0;
  66. $data['yesterday_price'] = 0;
  67. $agent_apply=AgentApply::getOne([['user_id' => $this->user_id],['status'=>[StatusEnum::DISABLED,StatusEnum::ENABLED],'check_status'=>StatusEnum::ENABLED]], true);
  68. if(!empty($agent_apply)) $data['is_agent'] = 1;
  69. if ($agent) {
  70. $data['is_agent'] = 1;
  71. $data['level_name'] = $agent['agentLevel']['name'] ?? '默认等级';
  72. $wallet_type = 'commission';
  73. $yesterday = date('Y-m-d', strtotime('-1 day'));
  74. $start_time = "{$yesterday} 00:00:00";
  75. $end_time = "{$yesterday} 23:59:59";
  76. CapitalLog::$capitalType = "{$wallet_type}_frozen";
  77. $where = ['user_id' => $this->user_id, 'from_type' => AgentEnum::ADDONS_NAME, 'status' => CapitalLog::STATUS_WAIT_SEND];
  78. $query = CapitalLog::find();
  79. $frozen_price = $query->where($where)->sum('change_num');
  80. $yesterday_frozen_price = $query->where($where)
  81. ->andWhere(['<=', 'created_at', $end_time])
  82. ->andWhere(['>=', 'created_at', $start_time])
  83. ->sum('change_num');
  84. if (empty($yesterday_frozen_price)) $yesterday_frozen_price = 0;
  85. CapitalLog::$capitalType = "{$wallet_type}";
  86. $where = ['user_id' => $this->user_id, 'from_type' => AgentEnum::ADDONS_NAME];
  87. $query = CapitalLog::find();
  88. $total_price = $query->where($where)->sum('change_num');
  89. $yesterday_total_price = $query->where($where)
  90. ->andWhere(['<=', 'created_at', $end_time])
  91. ->andWhere(['>=', 'created_at', $start_time])
  92. ->sum('change_num');
  93. if (empty($yesterday_total_price)) $yesterday_total_price = 0;
  94. $data['total_price'] = empty($total_price) ? 0 : $total_price;
  95. $data['frozen_price'] = empty($frozen_price) ? 0 : $frozen_price;
  96. $data['total_income'] = bcadd($data['total_price'],$data['frozen_price'],2);
  97. $data['yesterday_price'] = bcadd($yesterday_total_price,$yesterday_frozen_price,2);
  98. }
  99. $level_list = [];
  100. // $children_id_arr = UserRelationship::getChildIdArr($this->user_id);
  101. $children_id_arr = UserPartitionRelationship::getChildIdArr($this->user_id);
  102. if (!empty($children_id_arr)) {
  103. $params = [];
  104. $params['where'] = [['user_id' => $children_id_arr],['status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]];
  105. $params['group'] = 'level';
  106. $params['with'] = ['agentLevel'];
  107. $params['select'] = 'count("user_id") as counts,level';
  108. $list = Agent::lists($params);
  109. foreach ($list as $val) {
  110. $level_list[] = [
  111. 'name' => $val['agentLevel']['name'] ?? '',
  112. 'level' => $val['agentLevel']['level'] ?? '',
  113. 'total' => $val['total'] ?? 0,
  114. ];
  115. }
  116. }
  117. $data['level_list'] = $level_list;
  118. //more_setting
  119. $more_setting = \Yii::$app->services->setting->addons->get($this->mall_id, AgentEnum::ADDONS_NAME, 'more');
  120. $data['is_enable_team_buy_detail'] = $more_setting['is_enable_team_buy_detail'] ?? 0;
  121. return $this->success('操作成功', $data);
  122. }
  123. /**
  124. * @desc:奖励列表
  125. * @Author: hua
  126. * @Date: 2021/11/4
  127. * @Time: 17:00
  128. * @Copyright: copyright (c) 2021 广东七件事集团
  129. * @return mixed|void
  130. */
  131. public function actionLogList()
  132. {
  133. $params = \Yii::$app->request->get();
  134. // $where[] = ['mall_id' => $this->mall_id];
  135. // $where[] = ['user_id' => $this->user_id];
  136. // $where[] = ['from_type' => AgentEnum::ADDONS_NAME];
  137. // $params['order'] = 'id desc';
  138. // $params['where'] = $where;
  139. // $params['select'] = 'id,change_num,source_table,source_table_id,created_at,from_type,capital_type,order_no,contributor_money,contributor_id,contributor_name';
  140. // $wallet_type = 'commission';
  141. // CapitalLog::$capitalType = "{$wallet_type}";
  142. // $list = CapitalLog::listPage($params);
  143. // if (!empty($list)) {
  144. // $contributor_ids = array_column($list['list'],'contributor_id');
  145. // $user_list = User::lists(['where'=>[['id'=>$contributor_ids]],'select'=>'id,nickname,avatar_url','index'=>'id']);
  146. // foreach ($list['list'] as &$item) {
  147. // $item['user_id'] = $item['contributor_id'];
  148. // $item['goods_price'] = $item['contributor_money'];
  149. // $item['goods_name'] = $item['contributor_name'];
  150. // $item['nickname'] = '';
  151. // $item['avatar_url'] = '';
  152. // if(isset($user_list[$item['contributor_id']])){
  153. // $item['nickname'] = $user_list[$item['contributor_id']]['nickname'];
  154. // $item['avatar_url'] = $user_list[$item['contributor_id']]['avatar_url'];
  155. // }
  156. // }
  157. // }
  158. //
  159. // $wallet_type = 'commission';
  160. // $params['mall_id'] = $this->mall_id;
  161. // $params['user_id'] = $this->user_id;
  162. $wallet_type = 'commission';
  163. if(!empty($params['list_type'])&&$params['list_type'] ==1){//未结算
  164. $wallet_type = 'commission_frozen';
  165. $params['status'] = StatusEnum::DISABLED;
  166. }
  167. $params['mall_id'] = $this->mall_id;
  168. $params['user_id'] = $this->user_id;
  169. $list = CapitalLog::getCapitalLogList($params, $wallet_type, AgentEnum::ADDONS_NAME);
  170. if (!empty(CapitalLog::getStaticError())) return $this->error(CapitalLog::getStaticError());
  171. if (!empty($list['list'])) {
  172. $commission_map = GlobalVarMap::getEnumMapByType('FromTypeMap');
  173. $capital_type_map = GlobalVarMap::getEnumMapByType('CapitalTypeMap');
  174. foreach ($list['list'] as &$item) {
  175. $item['source_text'] = ($commission_map[$item['from_type']] ?? '').($capital_type_map[$item['capital_type']]
  176. ?'-'.$capital_type_map[$item['capital_type']]:'');
  177. }
  178. }
  179. return $this->success('操作成功', $list);
  180. }
  181. /**
  182. * @desc:等级列表
  183. * @Author: hua
  184. * @Date: 2021/11/5
  185. * @Time: 16:20
  186. * @Copyright: copyright (c) 2021 广东七件事集团
  187. * @return mixed|void
  188. */
  189. public function actionLevelList(){
  190. $params = [];
  191. $params['where'] = [['mall_id'=>$this->mall_id],['status'=>StatusEnum::ENABLED]];
  192. $params['select'] = 'id,name,explain';
  193. $params['order'] = 'level asc';
  194. $list = AgentLevel::lists($params);
  195. return $this->success('操作成功', $list);
  196. }
  197. /**
  198. * Notes:渠道分红申请
  199. * User: LD
  200. * Date: 2024/4/11
  201. * Time: 14:15
  202. * @return mixed|void
  203. */
  204. public function actionApply(){
  205. $mall_id = $this->mall_id;
  206. if (\Yii::$app->request->isPost) {
  207. $params = \Yii::$app->request->post();
  208. $res = Yii::$app->services->validate->validate($params, [
  209. [['name', 'mobile'], 'required'],
  210. [['remark'], 'safe'],
  211. ['mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
  212. ]);
  213. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  214. $params['mall_id'] = $mall_id;
  215. $params['user_id'] = $this->user_id;
  216. $Agent = Agent::getOne([['user_id' => $this->user_id], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]], ['mall_id' => $this->mall_id]], true);
  217. if (!empty($Agent)) return $this->error('您已是渠道分红团队');
  218. $agent_apply = AgentApply::getOne([['user_id' => $this->user_id], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]], ['mall_id' => $this->mall_id]], true, [], 'id desc');
  219. if (!empty($agent_apply) && empty($agent_apply['check_status'])) return $this->error('您已经提交申请,请等待客服审核');
  220. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, AgentEnum::ADDONS_NAME, 'more');
  221. if (empty($setting['is_check'])) $params['check_status'] = 1; //不需要审核
  222. $res = AgentApply::setData($params);
  223. if ($res === false) return $this->error(AgentApply::getStaticError());
  224. return $this->success('操作成功');
  225. }
  226. if (\Yii::$app->request->isGet) {
  227. $params['mall_id'] = $mall_id;
  228. $params['user_id'] = $this->user_id;
  229. $agent_apply = AgentApply::getOne([['user_id' => $this->user_id],
  230. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  231. ['mall_id' => $this->mall_id]], true, [], 'id desc','id,user_id,check_status,check_remark');
  232. if(empty($agent_apply)){$agent_apply['check_msg']='您不是渠道分红';$agent_apply['check_status']=StatusEnum::DELETE;}
  233. if(!empty($agent_apply)){
  234. if($agent_apply['check_status'] ==0) $agent_apply['check_msg']='您已提交申请,请等待审核';
  235. if($agent_apply['check_status'] ==2) $agent_apply['check_msg']=$agent_apply['check_remark']??'';
  236. }
  237. //获取分红配置
  238. $agent_apply['setting'] = \Yii::$app->services->setting->addons->get($this->mall_id, AgentEnum::ADDONS_NAME, 'more');
  239. return $this->success('操作成功',$agent_apply);
  240. }
  241. }
  242. /**
  243. * @desc:团队购买明细
  244. * @return mixed|null
  245. */
  246. public function actionTeamBuyList()
  247. {
  248. $more_setting = \Yii::$app->services->setting->addons->get($this->mall_id, AgentEnum::ADDONS_NAME, 'more');
  249. $is_enable_team_buy_detail = $more_setting['is_enable_team_buy_detail'] ?? 0;
  250. $order_list = [];
  251. $order_status = OrderStatusEnum::ACCOMPLISH;
  252. if($is_enable_team_buy_detail){
  253. $compute_type = $more_setting['compute_type'] ?? 1;
  254. $child_ids = UserPartitionRelationship::getChildIdArr($this->user_id);
  255. if(!empty($child_ids)){
  256. $where = [
  257. ['mall_id'=>$this->mall_id],
  258. ['user_id'=>$child_ids],
  259. ['status'=>StatusEnum::ENABLED],
  260. ['pay_status' => StatusEnum::ENABLED]
  261. ];
  262. if ($compute_type == 1) {
  263. $order_status = [OrderStatusEnum::PAY,OrderStatusEnum::SHIPMENTS,OrderStatusEnum::SING,OrderStatusEnum::ACCOMPLISH];
  264. }
  265. $where[] = ['order_status' => $order_status];
  266. $select = 'id,order_no,user_id,pay_time';
  267. $params['order'] = 'pay_time desc';
  268. $params['select'] = $select;
  269. $params['where'] = $where;
  270. $params['with'] = ['user' => function($query){
  271. $query->select('id,nickname,mobile,avatar_url');
  272. }];
  273. $params['page'] = \Yii::$app->request->get('page') ?? 1;
  274. $params['limit'] = \Yii::$app->request->get('limit') ?? 10;
  275. $order_list = Order::listPage($params);
  276. $order_ids = array_column($order_list['list'],'id');
  277. $order_goods_num = OrderDetail::find()->where(['order_id'=>$order_ids,'mall_id'=>$this->mall_id])->groupBy('order_id')->select('order_id,sum(num) as goods_num')->indexBy('order_id')->asArray()->all();
  278. foreach($order_list['list'] as $key =>$item){
  279. $item['goods_num'] = $order_goods_num[$item['id']]['goods_num'] ?? 0;
  280. $item['pay_time'] = date('Y-m-d H:i:s',$item['pay_time']);
  281. $order_list['list'][$key] = $item;
  282. }
  283. //昨日订单商品总数:
  284. $yesterday_order_goods_num = OrderDetail::find()->where(['mall_id'=>$this->mall_id,'order_status'=> $order_status,'user_id' => $child_ids])->andWhere(['between','updated_at',strtotime(date('Y-m-d',strtotime("-1 day"))),strtotime(date('Y-m-d',strtotime("-1 day"))) + 86400])->sum('num') ?? 0;
  285. //累计订单总数:200
  286. $total_order_goods_num = OrderDetail::find()->where(['mall_id'=>$this->mall_id,'order_status'=> $order_status,'user_id' => $child_ids])->sum('num') ?? 0;
  287. }
  288. $order_list['yesterday_order_num'] = $yesterday_order_goods_num ?? 0;
  289. $order_list['total_order_num'] = $total_order_goods_num ?? 0;
  290. }
  291. return $this->success('操作成功',$order_list);
  292. }
  293. }