ShareForm.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1;
  8. use app\models\Cash;
  9. use app\models\common\InviteCode;
  10. use app\models\Goods;
  11. use app\models\Option;
  12. use app\models\Order;
  13. use app\models\RcCommissionLog;
  14. use app\models\Share;
  15. use app\models\User;
  16. use app\modules\client\models\ApiModel;
  17. use yii\helpers\Json;
  18. class ShareForm extends ApiModel
  19. {
  20. public $share;
  21. public $store_id;
  22. public $user_id;
  23. public $name;
  24. public $mobile;
  25. public $agree;
  26. public $form_id;
  27. /**
  28. * 场景说明:NONE_CONDITION:无条件; APPLY:需要申请
  29. * @return array
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['name', 'mobile', 'agree'], 'required', 'on' => 'APPLY'],
  35. [['agree'], 'integer'],
  36. [['name', 'mobile', 'form_id'], 'trim'],
  37. [['mobile'], 'match', 'pattern' => "/\+?\d[\d -]{8,12}\d/", 'message' => '手机号错误', 'on' => 'APPLY']
  38. ];
  39. }
  40. public function attributeLabels()
  41. {
  42. return [
  43. 'name' => '真实姓名',
  44. 'mobile' => '手机号'
  45. ];
  46. }
  47. public function save()
  48. {
  49. if ($this->validate()) {
  50. $t = \Yii::$app->db->beginTransaction();
  51. if (($this->agree || $this->agree == 0) && $this->agree != 1) {
  52. return [
  53. 'code' => 1,
  54. 'msg' => '请先阅读并确认分销申请协议'
  55. ];
  56. }
  57. $this->share->attributes = $this->attributes;
  58. if ($this->share->isNewRecord) {
  59. $this->share->is_delete = 0;
  60. $this->share->created_at = time();
  61. $this->share->store_id = $this->store_id;
  62. } else {
  63. if ($this->share->status == 1) {
  64. return [
  65. 'code' => 1,
  66. 'msg '=> '用户已经是分销商'
  67. ];
  68. }
  69. }
  70. $this->share->user_id = get_user_id();
  71. $user = User::findOne(['id' => get_user_id(), 'store_id' => $this->store_id]);
  72. $setting = Option::get('share_basic_setting', $this->store_id);
  73. $share_setting = $setting ? Json::decode($setting['value']) : [];
  74. if ($share_setting['share_condition']['value'] != 2) {
  75. $user->is_distributor = 2;
  76. $this->share->status = 0;
  77. $user->time = 0;
  78. } else {
  79. $user->is_distributor = 1;
  80. $this->share->status = 1;
  81. $user->time = time();
  82. }
  83. if (!$user->save()) {
  84. $t->rollBack();
  85. return [
  86. 'code' => 1,
  87. 'msg' => '网络异常'
  88. ];
  89. }
  90. if ($this->share->save()) {
  91. $t->commit();
  92. return [
  93. 'code' => 0,
  94. 'msg' => '成功'
  95. ];
  96. } else {
  97. $t->rollBack();
  98. return [
  99. 'code' => 1,
  100. 'msg' => '网络异常',
  101. 'data' => $this->errors,
  102. ];
  103. }
  104. } else {
  105. return [
  106. 'code' => 1,
  107. 'msg' => $this->getErrorSummary(false)[0],
  108. ];
  109. }
  110. }
  111. /**
  112. * @return array
  113. * 获取佣金相关信息
  114. */
  115. public function getPrice($cash_type = -1)
  116. {
  117. $user = User::find()->where(['id' => $this->user_id])->one();
  118. $query = Cash::find()->where([
  119. 'store_id' => $this->store_id, 'user_id' => $this->user_id, 'is_delete' => 0
  120. ]);
  121. if($cash_type > -1){
  122. $query->andWhere(['cash_type' => $cash_type]);
  123. }
  124. $list = $query->asArray()->all();
  125. $new_list = [];
  126. $new_list['total_price'] = $user->total_price;//分销佣金
  127. $new_list['price'] = $user->price;//可提现
  128. $new_list['cash_price'] = 0;//已提现
  129. $new_list['wait_audit'] = 0;//待审核
  130. $new_list['un_pay'] = 0;//未审核
  131. $new_list['total_cash'] = 0;//提现明细
  132. foreach ($list as $index => $value) {
  133. $new_list['total_cash'] = sprintf('%.2f', ($new_list['total_cash'] + $value['price']));
  134. if ($value['status'] == 0) {
  135. $new_list['wait_audit'] = sprintf('%.2f', ($new_list['wait_audit'] + $value['price']));
  136. } elseif ($value['status'] == 1) {
  137. $new_list['un_pay'] = sprintf('%.2f', ($new_list['un_pay'] + $value['price']));
  138. } elseif ($value['status'] == 2 || $value['status'] == 4 || $value['status'] == 5) {
  139. $new_list['cash_price'] = sprintf('%.2f', ($new_list['cash_price'] + $value['price']));
  140. }
  141. }
  142. return $new_list;
  143. }
  144. /**
  145. * @return array|null|\yii\db\ActiveRecord
  146. *
  147. */
  148. public function getCash()
  149. {
  150. $list = User::find()->alias('u')
  151. ->where(['u.is_delete' => 0, 'u.store_id' => $this->store_id, 'u.id' => $this->user_id])
  152. ->leftJoin('{{%cash}} c', 'c.user_id=u.id and c.is_delete=0')
  153. ->select([
  154. 'u.total_price', 'u.price',
  155. 'sum(case when c.status = 2 then c.price else 0 end) cash_price',
  156. 'sum(case when c.status = 1 then c.price else 0 end) un_pay'
  157. ])->groupBy('c.user_id')->asArray()->one();
  158. return $list;
  159. }
  160. /**
  161. * 获取分销团队总人数
  162. * @return array
  163. */
  164. public function getTeamCount()
  165. {
  166. $setting = Option::get('share_basic_setting', $this->store_id);
  167. $share_setting = $setting ? Json::decode($setting['value']) : [];
  168. if (!$share_setting || $share_setting['level']['value'] == 0) {
  169. return [
  170. 'team_count' => 0,
  171. 'team' => []
  172. ];
  173. }
  174. $team = [];
  175. $first = User::find()->select(['id'])
  176. ->where(['store_id' => $this->store_id, 'parent_id' => $this->user_id, 'is_delete' => 0])->column();
  177. $count = count($first);
  178. $team['f_c'] = $first;
  179. if ($share_setting['level']['value'] >= 2) {
  180. $second = User::find()->select(['id'])
  181. ->where(['store_id' => $this->store_id, 'parent_id' => $first, 'is_delete' => 0])->column();
  182. $count += count($second);
  183. $team['s_c'] = $second;
  184. if ($share_setting['level']['value'] >= 3) {
  185. $third = User::find()->select(['id'])
  186. ->where(['store_id' => $this->store_id, 'parent_id' => $second, 'is_delete' => 0])->column();
  187. $count += count($third);
  188. $team['t_c'] = $third;
  189. }
  190. }
  191. return [
  192. 'team_count' => $count,
  193. 'team' => $team
  194. ];
  195. }
  196. /**
  197. * @return array
  198. */
  199. public function getOrder()
  200. {
  201. $arr = $this->getTeamCount();
  202. $team_arr = $arr['team'];
  203. $order_money = 0;
  204. $first_price = Order::find()->alias('o')->where([
  205. 'o.is_delete' => 0, 'o.store_id' => $this->store_id
  206. ])->andWhere([
  207. '<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL
  208. ])->andWhere([
  209. 'o.parent_id' => $this->user_id,
  210. ])->select(['sum(first_price)'])->scalar();
  211. if ($first_price) {
  212. $order_money += doubleval($first_price);
  213. }
  214. if (!empty($team_arr['s_c'])) {
  215. $second_price = Order::find()->alias('o')->where([
  216. 'o.is_delete' => 0, 'o.store_id' => $this->store_id
  217. ])->andWhere([
  218. '<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL
  219. ])->andWhere([
  220. 'or',
  221. ['and', ['in', 'o.user_id', $team_arr['s_c']], ['o.parent_id' => $team_arr['f_c'], 'o.parent_id_1' => 0]],
  222. ['o.parent_id_1' => $this->user_id],
  223. ])->select(['sum(second_price)'])->scalar();
  224. if ($second_price) {
  225. $order_money += doubleval($second_price);
  226. }
  227. }
  228. if (!empty($team_arr['t_c'])) {
  229. $third_price = Order::find()->alias('o')->where([
  230. 'o.is_delete' => 0, 'o.store_id' => $this->store_id
  231. ])->andWhere([
  232. '<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL
  233. ])->andWhere([
  234. 'or',
  235. ['and', ['in', 'o.user_id', $team_arr['t_c']], ['o.parent_id' => $team_arr['s_c'], 'o.parent_id_1' => 0]],
  236. ['o.parent_id_2' => $this->user_id],
  237. ])->select(['sum(third_price)'])->scalar();
  238. if ($third_price) {
  239. $order_money += doubleval($third_price);
  240. }
  241. }
  242. $arr['order_money'] = doubleval(sprintf('%.2f', $order_money));
  243. return $arr;
  244. }
  245. /**
  246. * 获取分销配置
  247. * @return array
  248. */
  249. public function getShareSetting() {
  250. $setting = Option::get('share_basic_setting', $this->store_id);
  251. $share_basic_setting = $setting ? Json::decode($setting['value']) : [];
  252. $money_setting = Option::get('share_money_setting', $this->store_id);
  253. $share_money_setting = $money_setting ? Json::decode($money_setting['value']) : [];
  254. $arr['first'] = $share_money_setting['level_one']['value'] ?? '';
  255. $arr['second'] = $share_money_setting['level_two']['value'] ?? '';
  256. $arr['third'] = $share_money_setting['level_three']['value'] ?? '';
  257. $arr['store_id'] = $this->store_id;
  258. $arr['level'] = $share_basic_setting['level']['value'] ?? '';
  259. $arr['condition'] = $share_basic_setting['condition']['value'] ?? '';
  260. $arr['share_condition'] = $share_basic_setting['share_condition']['value'] ?? 2;
  261. $arr['content'] = $share_basic_setting['content']['value'] ?? '';
  262. $arr['pay_type'] = $share_basic_setting['pay_type']['value'] ?? '';
  263. $arr['bank'] = $share_basic_setting['bank']['value'] ?? '';
  264. $arr['min_money'] = $share_basic_setting['min_money']['value'] ?? '';
  265. $arr['cash_max_day'] = $share_basic_setting['cash_max_day']['value'] ?? '';
  266. $arr['agree'] = $share_basic_setting['agree']['value'] ?? '';
  267. $arr['first_name'] = $share_money_setting['level_one']['text'] ?? '';
  268. $arr['second_name'] = $share_money_setting['level_two']['text'] ?? '';
  269. $arr['third_name'] = $share_money_setting['level_three']['text'] ?? '';
  270. $arr['pic_url_1'] = $share_basic_setting['apply_image']['value'] ?? '';
  271. $arr['pic_url_2'] = $share_basic_setting['no_checked_image']['value'] ?? '';
  272. $arr['price_type'] = $share_money_setting['price_type'] ?? '';
  273. $arr['remaining_sum'] = $share_basic_setting['remaining_sum']['value'] ?? '';
  274. $arr['share_goods_status'] = $share_basic_setting['share_goods_status']['value'] ?? '';
  275. $arr['share_goods_id'] = $share_basic_setting['share_goods_id']['value'] ?? '';
  276. $arr['is_rebate'] = $share_basic_setting['is_rebate']['value'] ?? '';
  277. $arr['parent_label'] = $share_basic_setting['parent_label']['value'] ?? '';
  278. $arr['recharge_count'] = RcCommissionLog::find()->where(['parent_id' => get_user_id(), 'store_id' => $this->store_id])->count() ?? 0;
  279. $arr['yinbao'] = \app\modules\admin\models\pospal\PospalForm::isopen($this->store_id);
  280. $arr['invite_type'] = $share_basic_setting['invite_type']['value'] ?? 1; //邀请方式 1手机号 2邀请码
  281. //邀请码
  282. $arr['invite_code'] = InviteCode::id2Code(get_user_id());
  283. $goods = null;
  284. // 无需审核,判断是否需要其他条件
  285. if ($arr['share_condition'] == 2) {
  286. $exit = Share::find()->andWhere(['user_id' => get_user_id(), 'store_id' => $this->store_id, 'is_delete' => 0,
  287. 'status' => 1])->exists();
  288. if (!$exit && ($share_basic_setting['auto_share_val']['value'] > 0 || $share_basic_setting['share_goods_status']['value'] !=0 )) {
  289. if ($share_basic_setting['auto_share_val']['value'] > 0) {
  290. $goods = Goods::find()->select('cover_pic, goods_num, id, is_negotiable, name, original_price, price, product_type')
  291. ->where(['store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1])
  292. ->andWhere(['>', 'price', $share_basic_setting['auto_share_val']['value']])->limit(5)->asArray()->one();
  293. }
  294. if ((int)$arr['share_goods_status'] === 1) {
  295. $goods = Goods::find()->select('cover_pic, goods_num, id, is_negotiable, name, original_price, price, product_type')
  296. ->where(['store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1, 'product_type' => 0])->limit(5)->asArray()->all();
  297. }
  298. if ((int)$arr['share_goods_status'] === 2) {
  299. $ids = \explode(',', $arr['share_goods_id']);
  300. $goods = Goods::find()->select('cover_pic, goods_num, id, is_negotiable, name, original_price, price, product_type')
  301. ->where(['id' => $ids])->asArray()->all();
  302. }
  303. }
  304. }
  305. $arr['share_goods'] = null;
  306. if ($goods) {
  307. $arr['share_goods'] = $goods;
  308. }
  309. return ['code' => 0, 'msg' => 'success', 'data' => $arr];
  310. }
  311. /**
  312. * 获取分销配置
  313. * @return array
  314. */
  315. public function getShareMsg() {
  316. $setting = Option::get('share_basic_setting', $this->store_id);
  317. $share_basic_setting = $setting ? Json::decode($setting['value']) : [];
  318. $money_setting = Option::get('share_money_setting', $this->store_id);
  319. $share_money_setting = $money_setting ? Json::decode($money_setting['value']) : [];
  320. $arr['first'] = $share_money_setting['level_one']['value'] ?? '';
  321. $arr['second'] = $share_money_setting['level_two']['value'] ?? '';
  322. $arr['third'] = $share_money_setting['level_three']['value'] ?? '';
  323. $arr['store_id'] = $this->store_id;
  324. $arr['level'] = $share_basic_setting['level']['value'] ?? '';
  325. $arr['condition'] = $share_basic_setting['condition']['value'] ?? '';
  326. $arr['share_condition'] = $share_basic_setting['share_condition']['value'] ?? '';
  327. // 无需审核,判断是否需要其他条件
  328. if ($arr['share_condition'] == 2) {
  329. $exit = Share::find()->andWhere(['user_id' => get_user_id(), 'store_id' => $this->store_id, 'is_delete' => 0,
  330. 'status' => 1])->exists();
  331. if (!$exit && ($share_basic_setting['auto_share_val']['value'] > 0 || $share_basic_setting['share_goods_status']['value'] !=0 )) {
  332. return [
  333. 'code' => 1,
  334. 'msg' => $share_basic_setting['auto_share_val']['value'] > 0 ? '条件不满足,请消费'.$share_basic_setting['auto_share_val']['value'].'元': '请购买指定商品'
  335. ];
  336. }
  337. }
  338. return ['code' => 0, 'msg' => 'success'];
  339. }
  340. }