DefaultController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. namespace addons\BusinessBonus\api\modules\v1\controllers;
  3. use addons\BusinessBonus\common\enums\BusinessBonusEnums;
  4. use addons\BusinessBonus\common\models\BusinessBonusLevel;
  5. use addons\BusinessBonus\common\models\BusinessBonusLevelInfo;
  6. use addons\BusinessBonus\common\models\BusinessBonusReward;
  7. use addons\BusinessBonus\common\models\BusinessBonusStore;
  8. use addons\BusinessBonus\common\models\BusinessBonusUpgradeApply;
  9. use addons\BusinessBonus\common\models\BusinessBonusUpgradeCondition;
  10. use addons\BusinessBonus\common\models\BusinessBonusUser;
  11. use addons\Mch\common\models\Mch;
  12. use addons\Store\common\models\Store;
  13. use common\enums\StatusEnum;
  14. use Yii;
  15. use api\controllers\OnAuthController;
  16. /**
  17. * 默认控制器
  18. *
  19. * Class DefaultController
  20. * @package addons\BusinessBonus\api\modules\v1\controllers
  21. */
  22. class DefaultController extends OnAuthController
  23. {
  24. public $modelClass = '';
  25. /**
  26. * 不用进行登录验证的方法
  27. *
  28. * 例如: ['index', 'update', 'create', 'view', 'delete']
  29. * 默认全部需要验证
  30. *
  31. * @var array
  32. */
  33. protected $authOptional = ['index'];
  34. /**
  35. * 首页
  36. *
  37. * @return string
  38. */
  39. public function actionIndex()
  40. {
  41. return 'Hello world';
  42. }
  43. /**
  44. * 获取招商员信息
  45. */
  46. public function actionUserInfo()
  47. {
  48. $request = Yii::$app->request;
  49. if ($request->isPost) {
  50. $user_info = BusinessBonusUser::getInfo($this->mall_id, $this->user);
  51. $config = Yii::$app->services->setting->addons->get($this->mall_id, BusinessBonusEnums::ADDONS_NAME, 'basic');
  52. // 结算周期
  53. $settle_circle = $config['settle_circle'] ?? BusinessBonusEnums::SETTLE_CIRCLE_DAY;
  54. // 今天
  55. $today_count = BusinessBonusReward::getCustomStatistic($this->mall_id, $this->user_id, strtotime('today'), strtotime('tomorrow') - 1);
  56. if ($settle_circle == BusinessBonusEnums::SETTLE_CIRCLE_DAY) { // 一天
  57. // 昨天、前天
  58. $current_count = BusinessBonusReward::getCustomStatistic($this->mall_id, $this->user_id, strtotime('yesterday'), strtotime('today') - 1);
  59. $pre_count = BusinessBonusReward::getCustomStatistic($this->mall_id, $this->user_id, strtotime('yesterday -1 day'), strtotime('yesterday') - 1);
  60. $bonus_count = [
  61. ['title' => '今天', 'value' => $today_count],
  62. ['title' => '昨天', 'value' => $current_count],
  63. ['title' => '前天', 'value' => $pre_count]
  64. ];
  65. } else if ($settle_circle == BusinessBonusEnums::SETTLE_CIRCLE_WEEK) { // 一周
  66. // 本周、上周
  67. $current_count = BusinessBonusReward::getCustomStatistic($this->mall_id, $this->user_id, strtotime('Monday this week'), time());
  68. $pre_count = BusinessBonusReward::getCustomStatistic($this->mall_id, $this->user_id, strtotime('Monday last week'), strtotime('Monday this week') - 1);
  69. $bonus_count = [
  70. ['title' => '今天', 'value' => $today_count],
  71. ['title' => '本周', 'value' => $current_count],
  72. ['title' => '上周', 'value' => $pre_count]
  73. ];
  74. } else { // 一个月
  75. // 本月、上月
  76. $current_count = BusinessBonusReward::getCustomStatistic($this->mall_id, $this->user_id, strtotime('midnight first day of this month'), time());
  77. $pre_count = BusinessBonusReward::getCustomStatistic($this->mall_id, $this->user_id, strtotime('midnight first day of last month'), strtotime('midnight first day of this month') - 1);
  78. $bonus_count = [
  79. ['title' => '今天', 'value' => $today_count],
  80. ['title' => '本月', 'value' => $current_count],
  81. ['title' => '上月', 'value' => $pre_count]
  82. ];
  83. }
  84. $user_info['bonus_count'] = $bonus_count;
  85. return $this->success('招商员信息', $user_info);
  86. }
  87. }
  88. /**
  89. * 获取申请页面的数据
  90. */
  91. public function actionApplyInfo()
  92. {
  93. $request = Yii::$app->request;
  94. if ($request->isPost) {
  95. // 初级招商员的福利介绍
  96. $business_bonus_level = BusinessBonusEnums::FIRST_BUSINESS_LEVEL;
  97. $benefit_intro = BusinessBonusLevelInfo::getBenefitIntro($this->mall_id, $business_bonus_level);
  98. $level_info = BusinessBonusLevel::getOne([
  99. ['=', 'mall_id', $this->mall_id],
  100. ['=', 'level', $business_bonus_level],
  101. ['>=', 'status', StatusEnum::DISABLED]
  102. ], true);
  103. $is_apply = BusinessBonusUpgradeApply::getOne([
  104. ['=', 'mall_id', $this->mall_id],
  105. ['=', 'user_id', $this->user_id],
  106. ['=', 'status', StatusEnum::ENABLED],
  107. ], true, '', 'id desc');
  108. $is_applying = 0;
  109. if ($is_apply && $is_apply['audit_status'] == BusinessBonusEnums::AUDIT_STATUS_CHECKING) {
  110. $is_applying = 1;
  111. }
  112. $info = [
  113. 'benefit_intro' => $benefit_intro,
  114. 'level_name' => $level_info['level_name'] ?? '',
  115. 'bonus_rate' => $level_info['bonus_rate'] ?? 0,
  116. 'user_id' => $this->user->id,
  117. 'nickname' => $this->user->nickname,
  118. 'mobile' => $this->user->mobile,
  119. 'is_applying' => $is_applying
  120. ];
  121. return $this->success('申请成为招商员', $info);
  122. }
  123. }
  124. /**
  125. * 获取等级说明
  126. */
  127. public function actionLevelDesc()
  128. {
  129. $request = Yii::$app->request;
  130. if ($request->isPost) {
  131. // 初级招商员的福利介绍
  132. $config = Yii::$app->services->setting->addons->get($this->mall_id, BusinessBonusEnums::ADDONS_NAME, 'basic');
  133. return $this->success('申请成为招商员', ['level_desc' => $config['level_desc'] ?? '']);
  134. }
  135. }
  136. /**
  137. * 申请成为招商员
  138. */
  139. public function actionApplyBusinessUser()
  140. {
  141. $request = Yii::$app->request;
  142. if ($request->isPost) {
  143. $res = BusinessBonusUpgradeApply::apply($this->mall_id, $this->user_id);
  144. if ($res == false) return $this->error(BusinessBonusUpgradeApply::getStaticError());
  145. return $this->success('提交成功', ['res_code' => $res]);
  146. }
  147. }
  148. /**
  149. * 购买指定商品成为招商员-等级列表
  150. */
  151. public function actionBusinessUserLevel()
  152. {
  153. $request = Yii::$app->request;
  154. if ($request->isPost) {
  155. $params = $request->post();
  156. $res = Yii::$app->services->validate->validate($params, [
  157. [['page'], 'integer'],
  158. ]);
  159. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  160. $where = [
  161. ['=', 'mall_id', $this->mall_id],
  162. ['=', 'status', StatusEnum::ENABLED],
  163. ];
  164. $order = 'level asc';
  165. $list = BusinessBonusLevel::listPage(compact('where', 'order'), false, true);
  166. $list['list'] = $list['list'] ?? [];
  167. $level_array = array_unique(array_filter(array_column($list['list'], 'level')));
  168. $info_list = BusinessBonusLevelInfo::find()
  169. ->select('business_bonus_level,benefit_intro')
  170. ->andWhere(['=', 'status', StatusEnum::ENABLED])
  171. ->andWhere(['in', 'business_bonus_level', $level_array])
  172. ->andWhere(['=', 'mall_id', $this->mall_id])
  173. ->asArray()
  174. ->all();
  175. $level_info_data = array_column($info_list, 'benefit_intro', 'business_bonus_level');
  176. $cond_list = BusinessBonusUpgradeCondition::find()
  177. ->select('business_bonus_level,goods_id,upgrade_type,is_audit')
  178. ->andWhere(['=', 'status', StatusEnum::ENABLED])
  179. ->andWhere(['in', 'business_bonus_level', $level_array])
  180. ->andWhere(['=', 'mall_id', $this->mall_id])
  181. ->asArray()
  182. ->all();
  183. $cond_data = array_column($cond_list, null, 'business_bonus_level');
  184. $business_user = BusinessBonusUser::getOne([
  185. ['=', 'mall_id', $this->mall_id],
  186. ['>=', 'status', StatusEnum::DISABLED],
  187. ['=', 'user_id', $this->user_id]
  188. ], true);
  189. foreach ($list['list'] as $key => $val) {
  190. $val['benefit_intro'] = $level_info_data[$val['level']] ?? '';
  191. $condition = $cond_data[$val['level']] ?? [];
  192. $val['goods_id'] = $condition['goods_id'] ?? 0;
  193. $val['goods_id'] = explode(',', $val['goods_id'])[0]; // 防止有多个,目前只有一个
  194. $val['upgrade_type'] = $condition['upgrade_type'] ?? 0;
  195. $val['current_level'] = $business_user['business_bonus_level'] ?? 0;
  196. $list['list'][$key] = $val;
  197. }
  198. return $this->success('招商员等级', $list);
  199. }
  200. }
  201. /**
  202. * 奖励记录
  203. */
  204. public function actionRewardList()
  205. {
  206. $request = Yii::$app->request;
  207. if ($request->isPost) {
  208. $params = $request->post();
  209. $res = Yii::$app->services->validate->validate($params, [
  210. [['page', 'bonus_status', 'store_id', 'store_type', 'add_order_goods'], 'integer'],
  211. [['time_value'], 'string']
  212. ]);
  213. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  214. $where = [
  215. ['=', 'mall_id', $this->mall_id],
  216. ['=', 'status', StatusEnum::ENABLED],
  217. ['=', 'user_id', $this->user_id],
  218. ];
  219. if ($params['bonus_status'] !== '' || $params['bonus_status'] !== null) {
  220. $where[] = ['=', 'bonus_status', $params['bonus_status']];
  221. }
  222. if ($params['store_id']) {
  223. $where[] = ['=', 'store_id', $params['store_id']];
  224. }
  225. if ($params['store_type'] == BusinessBonusEnums::STORE_TYPE_STORE) {
  226. $where[] = ['=', 'from_type', BusinessBonusEnums::FROM_TYPE_STORE];
  227. } else if ($params['store_type'] == BusinessBonusEnums::STORE_TYPE_MCH) {
  228. $where[] = ['=', 'from_type', BusinessBonusEnums::FROM_TYPE_MCH];
  229. }
  230. if ($params['time_value'] == 'today') { // 今天
  231. $where[] = ['>=', 'created_at', strtotime('today')];
  232. $where[] = ['<=', 'created_at', time()];
  233. } else if ($params['time_value'] == 'one_week') { // 最近1周
  234. $where[] = ['>=', 'created_at', strtotime('-1 week')];
  235. $where[] = ['<=', 'created_at', time()];
  236. } else if ($params['time_value'] == 'two_week') { // 最近2周
  237. $where[] = ['>=', 'created_at', strtotime('-2 weeks')];
  238. $where[] = ['<=', 'created_at', time()];
  239. } else if ($params['time_value'] == 'one_month') { // 最近1个月
  240. $where[] = ['>=', 'created_at', strtotime('-1 month')];
  241. $where[] = ['<=', 'created_at', time()];
  242. } else if ($params['time_value'] == 'three_month') { // 最近3个月
  243. $where[] = ['>=', 'created_at', strtotime('-3 months')];
  244. $where[] = ['<=', 'created_at', time()];
  245. } else if ($params['time_value'] == 'six_month') { // 最近6个月
  246. $where[] = ['>=', 'created_at', strtotime('-6 months')];
  247. $where[] = ['<=', 'created_at', time()];
  248. } else if ($params['time_value'] == 'one_year') { // 最近1年
  249. $where[] = ['>=', 'created_at', strtotime('-1 year')];
  250. $where[] = ['<=', 'created_at', time()];
  251. }
  252. $order = 'id desc';
  253. $select = 'id,order_no,settle_amount,bonus_rate,bonus_amount,from_type,store_id,order_id,created_at';
  254. $list = BusinessBonusReward::listPage(compact('where', 'order', 'select'), false, true);
  255. $list['list'] = $list['list'] ?? [];
  256. $list['list'] = BusinessBonusReward::dealRewardList($list['list'], $this->mall_id, $params['add_order_goods']);
  257. if ($params['page'] == 1) $list['bonus_amount_count'] = BusinessBonusReward::getOne($where, true, '', '', 'sum(bonus_amount) as total_amount')['total_amount'] ?? 0;
  258. return $this->success('分红记录', $list);
  259. }
  260. }
  261. /**
  262. * 获取可选门店
  263. * @return mixed|void
  264. */
  265. public function actionStoreList()
  266. {
  267. $request = Yii::$app->request;
  268. if ($request->isPost) {
  269. $params = $request->post();
  270. $res = Yii::$app->services->validate->validate($params, [
  271. [['store_type'], 'integer'],
  272. ]);
  273. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  274. $list = BusinessBonusStore::find()
  275. ->select('id,store_id,store_type')
  276. ->andWhere(['=', 'mall_id', $this->mall_id])
  277. ->andWhere(['=', 'business_user_id', $this->user_id])
  278. ->andWhere(['=', 'status', StatusEnum::ENABLED]);
  279. if ($params['store_type']) {
  280. $list = $list
  281. ->andWhere(['=', 'store_type', $params['store_type']]);
  282. }
  283. $list = $list
  284. ->orderBy('id desc')
  285. ->asArray()
  286. ->all();
  287. $store_ids = $mch_ids = [];
  288. foreach ($list as $item) {
  289. if ($item['store_type'] == BusinessBonusEnums::STORE_TYPE_STORE) {
  290. $store_ids[] = $item['store_id'];
  291. } else {
  292. $mch_ids[] = $item['store_id'];
  293. }
  294. }
  295. $store_array = $mch_array = [];
  296. if ($store_ids) {
  297. $store_list = Store::find()
  298. ->select('id,user_id,store_name')
  299. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  300. ->andWhere(['in', 'id', $store_ids])
  301. ->andWhere(['=', 'mall_id', $this->mall_id])
  302. ->asArray()
  303. ->all();
  304. $store_array = array_column($store_list, null, 'id');
  305. }
  306. if ($mch_ids) {
  307. $mch_list = Mch::find()
  308. ->select('id,user_id,store_name')
  309. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  310. ->andWhere(['in', 'id', $mch_ids])
  311. ->andWhere(['=', 'mall_id', $this->mall_id])
  312. ->asArray()
  313. ->all();
  314. $mch_array = array_column($mch_list, null, 'id');
  315. }
  316. foreach ($list as $index => $value) {
  317. $value['store_name'] = '';
  318. if ($value['store_type'] == BusinessBonusEnums::STORE_TYPE_STORE) {
  319. $value['store_name'] = $store_array[$value['store_id']]['store_name'] ?? '';
  320. } else {
  321. $value['store_name'] = $mch_array[$value['store_id']]['store_name'] ?? '';
  322. }
  323. unset($value['id']);
  324. $list[$index] = $value;
  325. }
  326. return $this->success('分红记录', $list);
  327. }
  328. }
  329. }