CommissionController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <?php
  2. namespace addons\Happiness\api\modules\v1\controllers;
  3. use addons\Happiness\common\enums\HappinessEnum;
  4. use addons\Happiness\common\enums\HappinessRewardEnum;
  5. use addons\Happiness\common\models\HappinessAgent;
  6. use addons\Happiness\common\models\HappinessBusiness;
  7. use addons\Happiness\common\models\HappinessCashierOrderDetail;
  8. use addons\Happiness\common\models\HappinessCommissionLog;
  9. use addons\Happiness\common\models\HappinessStore;
  10. use common\enums\StatusEnum;
  11. use common\models\common\Region;
  12. use common\models\order\OrderDetail;
  13. use common\models\user\UserWallet;
  14. use Yii;
  15. use api\controllers\OnAuthController;
  16. class CommissionController extends OnAuthController
  17. {
  18. public $modelClass = '';
  19. public function actionLog()
  20. {
  21. if (Yii::$app->request->isGet) {
  22. $get = Yii::$app->request->get();
  23. // 检查提交的参数
  24. $res = Yii::$app->services->validate->validate($get, [
  25. [['page', 'limit', 'store_id','agent_id'], 'integer'],
  26. [['type', 'time', 'commission_status'], 'string'],
  27. ]);
  28. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  29. // 获取收益列表
  30. $where = [];
  31. $where[] = ['=', 'mall_id', $this->mall_id];
  32. $where[] = ['=', 'user_id', $this->user_id];
  33. $where[] = ['=', 'status', StatusEnum::ENABLED];
  34. if (!empty($get['store_id'])) {
  35. $where[] = ['=', 'store_id', $get['store_id']];
  36. }
  37. if (!empty($get['agent_id'])) {
  38. $where[] = ['=', 'agent_id', $get['agent_id']];
  39. }
  40. if (!empty($get['type'])) {
  41. switch ($get['type']) {
  42. case 'share':
  43. $where[] = ['=', 'commission_type', HappinessRewardEnum::COMMISSION_TYPE_SHARE];
  44. break;
  45. case 'lock':
  46. $where[] = ['=', 'commission_type', HappinessRewardEnum::COMMISSION_TYPE_LOCK];
  47. break;
  48. case 'agent':
  49. $where[] = ['=', 'commission_type', HappinessRewardEnum::COMMISSION_TYPE_AGENT];
  50. break;
  51. case 'command':
  52. $where[] = ['in', 'commission_type', [HappinessRewardEnum::COMMISSION_TYPE_COMMAND,
  53. HappinessRewardEnum::COMMISSION_TYPE_COMMAND_AGENT]];
  54. break;
  55. case 'command_agent':
  56. $where[] = ['=', 'commission_type', HappinessRewardEnum::COMMISSION_TYPE_COMMAND_AGENT];
  57. break;
  58. case 'command_store':
  59. $where[] = ['=', 'commission_type', HappinessRewardEnum::COMMISSION_TYPE_COMMAND];
  60. break;
  61. }
  62. }
  63. if (!empty($get['time'])) {
  64. $time = explode(',', $get['time']);
  65. $where[] = ['>=', 'created_at', strtotime($time[0])];
  66. if (count($time) == 2) {
  67. $where[] = ['<=', 'created_at', strtotime($time[1]) + 86400];
  68. }
  69. }
  70. if ($get['commission_status'] >= 0) {
  71. $where[] = ['=', 'commission_status', $get['commission_status']];
  72. }
  73. $order = 'created_at desc';
  74. $select = '*';
  75. $with = ['user' => function ($query) {
  76. $query->select('id,nickname,avatar_url');
  77. }, 'store' => function ($query) {
  78. $query->select('id,store_name');
  79. }];
  80. $params = array('where' => $where, 'with' => $with, 'order' => $order, 'select' => $select, 'limit' =>
  81. $get['limit']);;
  82. $list = HappinessCommissionLog::listPage($params, false, true);
  83. $order_ids_1=[];
  84. $order_ids_2=[];
  85. //筛选订单号带c开头的和不带c开头的
  86. foreach ($list['list'] as $v)
  87. {
  88. if(strpos($v['order_no'],'C')===0)
  89. {
  90. $order_ids_1[]=$v['order_id'];
  91. }
  92. else
  93. {
  94. $order_ids_2[]=$v['order_id'];
  95. }
  96. }
  97. $order_list_main=OrderDetail::find()->where(['order_id'=>$order_ids_2])->select('id,order_id,goods_name')
  98. ->asArray()->all();
  99. $order_list_cashier=HappinessCashierOrderDetail::find()->where(['cashier_order_id'=>$order_ids_1])
  100. ->select
  101. ('id,cashier_order_id,goods_name')
  102. ->asArray()->all();
  103. foreach ($list['list'] as &$v) {
  104. $goods_list=array_values(array_filter($order_list_main, function($value) use($v) {
  105. if($value['order_id']==$v['order_id'])
  106. {
  107. return true;
  108. }
  109. }));
  110. if(empty($goods_list))
  111. {
  112. $goods_list=array_values(array_filter($order_list_cashier, function($value) use($v) {
  113. if($value['cashier_order_id']==$v['order_id'])
  114. {
  115. return true;
  116. }
  117. }));
  118. }
  119. $v['created_at'] = date('Y-m-d H:i:s', $v['created_at']);
  120. $v['commission_status_text'] = HappinessRewardEnum::getMap()[$v['commission_status']];
  121. $v['store_name'] = $v['store']['store_name'];
  122. $v['goods_name'] = $goods_list[0]['goods_name'];
  123. unset($v['store']);
  124. }
  125. $data['list'] = $list;
  126. $this->success('获取成功', $data);
  127. }
  128. }
  129. //业务员查询小店列表
  130. public function actionGetStoreList()
  131. {
  132. if (Yii::$app->request->isGet) {
  133. $get = Yii::$app->request->get();
  134. $res = Yii::$app->services->validate->validate($get, [
  135. [['page', 'limit'], 'integer'],
  136. ]);
  137. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  138. $where = [];
  139. $where[] = ['=', 'mall_id', $this->mall_id];
  140. $where[] = ['=', 'business_id', $this->user_id];
  141. $where[] = ['=', 'status', StatusEnum::ENABLED];
  142. $order = 'created_at desc';
  143. $select = '*';
  144. $with = [];
  145. //总的收益
  146. $total = HappinessCommissionLog::find()->where([
  147. 'status' => StatusEnum::ENABLED,
  148. 'user_id' => $this->user_id,
  149. 'mall_id' => $this->mall_id, 'commission_status' => HappinessRewardEnum::SEND, 'commission_type' => HappinessRewardEnum::COMMISSION_TYPE_COMMAND])
  150. ->sum('money')??0;
  151. $params = array('where' => $where, 'with' => $with, 'order' => $order, 'select' => $select, 'limit' =>
  152. $get['limit']);;
  153. $list = HappinessStore::listPage($params, false, true);
  154. foreach ($list['list'] as &$v) {
  155. //该小店带来的收益
  156. $v['commission_money'] = HappinessCommissionLog::find()->where(['store_id' => $v['id'],
  157. 'status' => StatusEnum::ENABLED,
  158. 'user_id' => $this->user_id,
  159. 'mall_id' => $this->mall_id, 'commission_status' => HappinessRewardEnum::SEND, 'commission_type' => HappinessRewardEnum::COMMISSION_TYPE_COMMAND])
  160. ->sum('money')??0;
  161. }
  162. $list['total'] = $total;
  163. return $this->success('获取成功', $list);
  164. }
  165. }
  166. //查询代理列表
  167. public function actionGetAgentList()
  168. {
  169. if (Yii::$app->request->isGet) {
  170. $get = Yii::$app->request->get();
  171. $res = Yii::$app->services->validate->validate($get, [
  172. [['page', 'limit'], 'integer'],
  173. ['type', 'in', 'range' => ['city', 'district','store','province']],
  174. ['role', 'in', 'range' => ['agent', 'command']],
  175. ]);
  176. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  177. $where = [];
  178. if (!empty($get['type'])) {
  179. switch ($get['type']) {
  180. case 'city':
  181. $where[] = ['=', 'role', 2];
  182. break;
  183. case 'district':
  184. $where[] = ['=', 'role', 3];
  185. break;
  186. case 'province':
  187. $where[] = ['=', 'role', 1];
  188. break;
  189. }
  190. }
  191. $where[] = ['=', 'mall_id', $this->mall_id];
  192. $commission_type = HappinessRewardEnum::COMMISSION_TYPE_COMMAND_AGENT;
  193. if(!empty($get['role']) && $get['role']=='agent')
  194. {
  195. //代理查询
  196. $where[] = ['=', 'user_id', $this->user_id];
  197. $commission_type=HappinessRewardEnum::COMMISSION_TYPE_AGENT;
  198. }
  199. else
  200. {
  201. //业务员查询
  202. $where[] = ['=', 'parent_id', $this->user_id];
  203. }
  204. $where[] = ['=', 'status', StatusEnum::ENABLED];
  205. $order = 'created_at desc';
  206. $select = '*';
  207. $with = [];
  208. $params = array('where' => $where, 'with' => $with, 'order' => $order, 'select' => $select, 'limit' => $get['limit']);;
  209. $list = HappinessAgent::listPage($params, false, true);
  210. $total = HappinessCommissionLog::find()->where([
  211. 'status' => StatusEnum::ENABLED,
  212. 'user_id' => $this->user_id,
  213. 'mall_id' => $this->mall_id, 'commission_status' => HappinessRewardEnum::SEND, 'commission_type' => $commission_type])
  214. ->sum('money')??0;
  215. foreach ($list['list'] as &$v) {
  216. //收益
  217. $v['store_name'] = $v['region_name'];
  218. $v['commission_money'] = HappinessCommissionLog::find()->where(['agent_id' => $v['user_id'],
  219. 'status' => StatusEnum::ENABLED,
  220. 'user_id' => $this->user_id,
  221. 'mall_id' => $this->mall_id, 'commission_status' => HappinessRewardEnum::SEND,
  222. 'commission_type' => $commission_type])
  223. ->sum('money')??0;
  224. }
  225. $list['total'] = $total;
  226. return $this->success('获取成功', $list);
  227. }
  228. }
  229. //代理获取小店列表
  230. public function actionGetAgentStoreList()
  231. {
  232. if (Yii::$app->request->isGet) {
  233. $get = Yii::$app->request->get();
  234. $res = Yii::$app->services->validate->validate($get, [
  235. [['page', 'limit'], 'integer'],
  236. ]);
  237. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  238. $where = [];
  239. $where[] = ['=', 'mall_id', $this->mall_id];
  240. $where[] = ['=', 'status', StatusEnum::ENABLED];
  241. $where[] =['=','check_status',HappinessEnum::CHECK_ADOPT];
  242. $order = 'created_at desc';
  243. $select = '*';
  244. $with = [];
  245. //总的收益
  246. //我代理的所有区域
  247. $city_region=[];$district_region=[];$province_region=[];
  248. $userAgentList = HappinessAgent::find()->where(['user_id' => $this->user_id,
  249. 'status'=>StatusEnum::ENABLED,'mall_id'=>$this->mall_id])->select('id,role,city_id,district_id,province_id')
  250. ->asArray()->all();
  251. if(!empty($userAgentList))
  252. {
  253. foreach ($userAgentList as $userAgent) {
  254. //城市代理
  255. if ($userAgent['role'] == HappinessEnum::AGENT_CITY) {
  256. $city_region[] = $userAgent['city_id'];
  257. }
  258. //区域代理
  259. if ($userAgent['role'] == HappinessEnum::AGENT_DISTRICT) {
  260. $district_region[] = $userAgent['district_id'];
  261. }
  262. //省代理
  263. if ($userAgent['role'] == HappinessEnum::AGENT_PROVINCE) {
  264. $province_region[] = $userAgent['province_id'];
  265. }
  266. }
  267. }
  268. $where[] = ['or', ['in', 'city_id', $city_region], ['in', 'district_id', $district_region],['in', 'province_id', $province_region]];
  269. $total = HappinessCommissionLog::find()->where([
  270. 'status' => StatusEnum::ENABLED,
  271. 'user_id' => $this->user_id,
  272. 'mall_id' => $this->mall_id, 'commission_status' => HappinessRewardEnum::SEND, 'commission_type' =>
  273. HappinessRewardEnum::COMMISSION_TYPE_AGENT])
  274. ->sum('money')??0;
  275. $params = array('where' => $where, 'with' => $with, 'order' => $order, 'select' => $select, 'limit' =>
  276. $get['limit']);;
  277. $list = HappinessStore::listPage($params, false, true);
  278. foreach ($list['list'] as &$v) {
  279. //该小店带来的收益
  280. $v['commission_money'] = HappinessCommissionLog::find()->where(['store_id' => $v['id'],
  281. 'status' => StatusEnum::ENABLED,
  282. 'user_id' => $this->user_id,
  283. 'mall_id' => $this->mall_id, 'commission_status' => HappinessRewardEnum::SEND, 'commission_type' => HappinessRewardEnum::COMMISSION_TYPE_AGENT])
  284. ->sum('money')??0;
  285. }
  286. $list['total'] = $total;
  287. return $this->success('获取成功', $list);
  288. }
  289. }
  290. // 获取服务中心数据
  291. public function actionGetServiceData()
  292. {
  293. if (Yii::$app->request->isGet) {
  294. $get = Yii::$app->request->get();
  295. // 检查提交的参数
  296. $type = $get['type'];
  297. $res = Yii::$app->services->validate->validate($get, [
  298. [['type'], 'string'],
  299. ]);
  300. $userWallet = UserWallet::find()->where(['user_id' => $this->user_id,
  301. 'mall_id' => $this->mall_id])->asArray()->one();
  302. $data = [];
  303. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  304. $basic = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic');
  305. $agent_list = !empty($basic['agent_list'])? json_decode($basic['agent_list'],true):[];//身份收益设置
  306. $agent_list = array_column($agent_list, null, 'level');
  307. $business_list = !empty($basic['business_list'])? json_decode($basic['business_list'],true):[];//身份收益设置
  308. $business_list = array_column($business_list, null, 'level');
  309. $city_name='市代';
  310. $province_name='省代';
  311. $district_name='区代';
  312. foreach ($agent_list as $agent)
  313. {
  314. if($agent['role']==HappinessEnum::AGENT_PROVINCE)
  315. {
  316. $province_name=$agent['name'];
  317. }
  318. if($agent['role']==HappinessEnum::AGENT_CITY)
  319. {
  320. $city_name=$agent['name'];
  321. }
  322. if($agent['role']==HappinessEnum::AGENT_DISTRICT)
  323. {
  324. $district_name=$agent['name'];
  325. }
  326. }
  327. //代理(区域代理)
  328. if ($type == 'agent') {
  329. //判断是不是代理
  330. $userAgentList = HappinessAgent::find()->where(['user_id' => $this->user_id, 'status' => StatusEnum::ENABLED,
  331. 'mall_id' => $this->mall_id])->asArray()->all();
  332. if (!empty($userAgentList)) {
  333. $agent_name = '';
  334. $city_region = [];
  335. $district_region = [];
  336. $province_region=[];
  337. $city_num = 0;
  338. $district_num = 0;
  339. $province_num=0;
  340. foreach ($userAgentList as $userAgent) {
  341. //城市代理
  342. if ($userAgent['role'] == HappinessEnum::AGENT_CITY) {
  343. $city_num++;
  344. $city_region[] = $userAgent['city_id'];
  345. }
  346. //区域代理
  347. if ($userAgent['role'] == HappinessEnum::AGENT_DISTRICT) {
  348. $district_num++;
  349. $district_region[] = $userAgent['district_id'];
  350. }
  351. if ($userAgent['role'] == HappinessEnum::AGENT_PROVINCE) {
  352. $province_region[]=$userAgent['province_id'];
  353. $province_num++;
  354. }
  355. }
  356. //确认名称
  357. $agent_name_list=[];
  358. foreach ($userAgentList as $userAgent)
  359. {
  360. $agent_name_list[] = isset($agent_list[$userAgent['level']])?$agent_list[$userAgent['level']]['name']:'';
  361. }
  362. //去重复
  363. if(count($agent_name_list)>1)
  364. {
  365. $agent_name_list = array_unique($agent_name_list);
  366. $agent_name = implode('|',$agent_name_list);
  367. }
  368. $data = [
  369. 'role_name' => $agent_name,
  370. 'role_arr' => $agent_name_list,
  371. 'district_number' => $district_num,
  372. 'district_name' => $district_name,
  373. 'city_number' => $city_num,
  374. 'city_name' => $city_name,
  375. 'province_name' => $province_name,
  376. 'province_number' => $province_num,
  377. 'store_number' => HappinessStore::find()->where([
  378. 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id,
  379. 'check_status' => HappinessEnum::CHECK_ADOPT])->andWhere([
  380. 'or',
  381. ['in', 'city_id', $city_region],
  382. ['in', 'district_id', $district_region],
  383. ['in', 'province_id', $province_region]
  384. ])->count()
  385. ?? 0,
  386. ];
  387. } else {
  388. return $this->error('您还不是代理');
  389. }
  390. }
  391. //推广(业务员)
  392. if ($type == 'command') {
  393. //判断是不是业务员
  394. $userBusiness = HappinessBusiness::findOne(['user_id' => $this->user_id, 'status' => StatusEnum::ENABLED,
  395. 'mall_id' => $this->mall_id]);
  396. if (!empty($userBusiness)) {
  397. $data = [
  398. 'role_name' =>isset($business_list[$userBusiness['level']])?$business_list[$userBusiness['level']]['name']:'' ,
  399. 'district_number' => HappinessAgent::find()->where(['parent_id' => $this->user_id, 'role' => 3, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id])->count
  400. () ?? 0,
  401. 'store_number' => HappinessStore::find()->where(['business_id' => $this->user_id,
  402. 'check_status' => HappinessEnum::CHECK_ADOPT, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id])->count() ?? 0,
  403. 'city_number' => HappinessAgent::find()->where(['parent_id' => $this->user_id, 'role' => 2, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id])->count
  404. () ?? 0,
  405. 'province_number' => HappinessAgent::find()->where(['parent_id' => $this->user_id, 'role' => 1, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id])->count
  406. () ?? 0,
  407. 'city_name'=>$city_name,
  408. 'province_name'=>$province_name,
  409. 'district_name'=>$district_name,
  410. ];
  411. } else {
  412. return $this->error('您还不是推广员');
  413. }
  414. }
  415. $data['total_commission'] = empty($userWallet) ? [] : $userWallet['commission'];
  416. $this->success('获取成功', $data);
  417. }
  418. }
  419. }