TeamGradesLevel.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. namespace app\models;
  3. use yii\behaviors\TimestampBehavior;
  4. /**
  5. * This is the model class for table "{{%team_grades_level}}".
  6. *
  7. * @property integer $id
  8. * @property integer $store_id
  9. * @property integer $level
  10. * @property string $level_name
  11. * @property integer $is_open_apply
  12. * @property string $level_reward_setting
  13. * @property integer $expires_type
  14. * @property integer $expires_in
  15. * @property string $level_conditions
  16. * @property string $desc
  17. * @property string $status
  18. * @property integer $is_delete
  19. * @property integer $created_at
  20. * @property integer $updated_at
  21. * @property float $tiered_bonus
  22. */
  23. class TeamGradesLevel extends \yii\db\ActiveRecord
  24. {
  25. //是否开启申请等级
  26. const IS_OPEN_APPLY_FALSE = 0;
  27. const IS_OPEN_APPLY_TRUE = 1;
  28. //状态
  29. const STATUS_FALSE = 0;
  30. const STATUS_TRUE = 1;
  31. const EXPIRES_TYPE_DAY = 0;
  32. const EXPIRES_TYPE_YEAR = 1;
  33. //等级有效期类型
  34. public static $expires_type_list = [
  35. self::EXPIRES_TYPE_DAY => '天',
  36. self::EXPIRES_TYPE_YEAR => '年'
  37. ];
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public static function tableName()
  42. {
  43. return '{{%team_grades_level}}';
  44. }
  45. /**
  46. * @inheritdoc
  47. */
  48. public function rules()
  49. {
  50. return [
  51. [['id', 'level', 'is_open_apply', 'expires_type', 'expires_in', 'is_delete', 'created_at', 'updated_at', 'store_id', 'status'], 'integer'],
  52. [['level_name', 'level_reward_setting', 'level_conditions', 'desc'], 'string'],
  53. [['tiered_bonus'], 'number']
  54. ];
  55. }
  56. public function behaviors()
  57. {
  58. return [
  59. [
  60. 'class' => TimestampBehavior::class
  61. ]
  62. ];
  63. }
  64. /**
  65. * @inheritdoc
  66. */
  67. public function attributeLabels()
  68. {
  69. return [
  70. 'id' => '',
  71. 'store_id' => '商城ID',
  72. 'level' => '团队等级',
  73. 'level_name' => '等级名称',
  74. 'is_open_apply' => '是否需要申请',
  75. 'level_reward_setting' => '业绩奖励设置',
  76. 'expires_type' => '等级有效期类型0天 1年',
  77. 'expires_in' => '等级有效期',
  78. 'level_conditions' => '升级条件',
  79. 'desc' => '特权说明',
  80. 'status' => '状态',
  81. 'is_delete' => '',
  82. 'created_at' => '',
  83. 'updated_at' => '',
  84. 'tiered_bonus' => '级差奖励'
  85. ];
  86. }
  87. public static function getLevelInfo($level, $store_id, $field = null) {
  88. $model = self::findOne(['level' => $level, 'store_id' => $store_id, 'is_delete' => 0]);
  89. if ($field) {
  90. return $model->$field;
  91. }
  92. return $model;
  93. }
  94. public static function getLevelList($store_id)
  95. {
  96. return self::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'status' => self::STATUS_TRUE])->select('level, level_name, is_open_apply')->orderBy('level ASC')->asArray()->all();
  97. }
  98. /**
  99. * 检测是否可以升级
  100. * @param $user_id
  101. * @param $store_id
  102. * @return bool
  103. */
  104. public static function getIsApply($next_level_info, $user_id, $store_id) {
  105. try {
  106. $is_apply = true;
  107. $team_grades_level_info_conditions = json_decode($next_level_info['level_conditions'], true);
  108. if (!empty($team_grades_level_info_conditions)) {
  109. foreach ($team_grades_level_info_conditions as $level_conditions_index => &$level_conditions_item) {
  110. $level_conditions_item['is_open'] = intval($level_conditions_item['is_open']);
  111. if (intval($level_conditions_item['is_open'])) {
  112. //获取团队人数
  113. $man_data = OldUserTreePath::find()->where([
  114. 'parent_id' => $user_id
  115. ])->andWhere([
  116. '!=',
  117. 'child_id',
  118. $user_id
  119. ])->asArray()->all();
  120. $child_id = array_column($man_data, 'child_id');
  121. //判断团队人数以及消费金额
  122. if ($level_conditions_index === 'man') {
  123. foreach ($level_conditions_item['value'] as $man_item) {
  124. // 团队人数是否达到条件
  125. if (count($child_id) < $man_item['count']) {
  126. //未达到条件 返回false
  127. $is_apply = false;
  128. continue;
  129. }
  130. // 团队人数消费金额是否达到条件
  131. $pay_price = Order::find()->where(
  132. [
  133. 'is_delete' => 0,
  134. 'trade_status' => Order::ORDER_FLOW_CONFIRM,
  135. 'user_id' => $child_id
  136. ])
  137. ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])->sum('pay_price');
  138. if ($pay_price < $man_item['money']) {
  139. $is_apply = false;
  140. continue;
  141. }
  142. }
  143. }
  144. //判断团队消费金额
  145. if ($level_conditions_index === 'order') {
  146. if (is_array($level_conditions_item['value'])) {
  147. foreach ($level_conditions_item['value'] as $order_item) {
  148. $order_money = Order::find()->where(
  149. [
  150. 'store_id' => $store_id,
  151. 'is_delete' => 0,
  152. 'trade_status' => Order::ORDER_FLOW_CONFIRM
  153. ])
  154. ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])
  155. ->andWhere(['in', 'user_id', $child_id])->sum('pay_price');
  156. if ($order_money < $order_item['count']) {
  157. $is_apply = false;
  158. continue;
  159. }
  160. }
  161. } else {
  162. $order_money = Order::find()->where(
  163. [
  164. 'store_id' => $store_id,
  165. 'is_delete' => 0,
  166. 'trade_status' => Order::ORDER_FLOW_CONFIRM
  167. ])
  168. ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])
  169. ->andWhere(['in', 'user_id', $child_id])->sum('pay_price');
  170. if ($order_money < $level_conditions_item['value']) {
  171. $is_apply = false;
  172. continue;
  173. }
  174. }
  175. }
  176. //判断充值金额
  177. if ($level_conditions_index === 'recharge') {
  178. $recharge_money = ReOrder::find()->where(['user_id' => $user_id, 'is_pay' => 1])->sum('pay_price');
  179. if ($recharge_money < $level_conditions_item['value']) {
  180. $is_apply = false;
  181. continue;
  182. }
  183. }
  184. //判断自身消费金额
  185. if ($level_conditions_index === 'self') {
  186. if (intval($level_conditions_item['value']['order_number'])) {
  187. // 订单单笔金额
  188. $order = Order::find()->where(
  189. [
  190. 'is_delete' => 0,
  191. 'trade_status' => Order::ORDER_FLOW_CONFIRM,
  192. 'user_id' => $user_id
  193. ])
  194. ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])
  195. ->andWhere(['>=' , 'pay_price', $level_conditions_item['value']['price']])->one();
  196. if (empty($order)) {
  197. $is_apply = false;
  198. continue;
  199. }
  200. } else {
  201. // 订单累计金额
  202. $order_money = Order::find()->where(
  203. [
  204. 'is_delete' => 0,
  205. 'trade_status' => Order::ORDER_FLOW_CONFIRM,
  206. 'user_id' => $user_id
  207. ])->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])->sum('pay_price');
  208. if ($order_money < $level_conditions_item['value']['price']) {
  209. $is_apply = false;
  210. continue;
  211. }
  212. }
  213. }
  214. //判断是否购买指定商品
  215. if ($level_conditions_index === 'goods') {
  216. $level_conditions_item['value_finished'] = empty($level_conditions_item['value']);
  217. if (!$level_conditions_item['value_finished']) {
  218. // 查找该用户是否买过此商品
  219. $goods_model = Order::find()->alias('o')
  220. ->leftJoin(['od' => OrderDetail::tableName()], 'od.order_id=o.id')
  221. ->where(
  222. [
  223. 'o.user_id' => $user_id,
  224. 'o.is_delete' => 0,
  225. 'o.trade_status' => Order::ORDER_FLOW_CONFIRM
  226. ])
  227. ->andWhere(['or', ['o.is_pay' => 1], ['o.pay_type' => 2]])
  228. ->andWhere(['od.goods_id' => $level_conditions_item['value']['id']])->one();
  229. if (empty($goods_model)) {
  230. $is_apply = false;
  231. }
  232. }
  233. }
  234. }
  235. }
  236. }
  237. return $is_apply;
  238. } catch (\Exception $e) {
  239. return false;
  240. }
  241. }
  242. //自动升级
  243. public static function auto_upgrade($user_id, $store_id)
  244. {
  245. $transaction = \Yii::$app->db->beginTransaction();
  246. try {
  247. $userTeamGrades = TeamGrades::getUserTeamGrades($user_id);
  248. //获取下一等级信息
  249. $next_level_info = TeamGradesLevel::find()->where([
  250. 'store_id' => $store_id,
  251. 'status' => TeamGradesLevel::STATUS_TRUE,
  252. 'is_delete' => 0
  253. ])->andWhere(['>', 'level', $userTeamGrades['team_grades_level']])->select('id, level, level_name, level_conditions, is_open_apply')
  254. ->orderBy('level ASC')->asArray()->one();
  255. if (empty($next_level_info)) {
  256. throw new \Exception('下一等级未查询到');
  257. }
  258. if (intval($next_level_info['is_open_apply'])) {
  259. throw new \Exception('当前等级未开启自动升级');
  260. }
  261. if (!self::getIsApply($next_level_info, $user_id, $store_id)) {
  262. throw new \Exception('未达到升级条件');
  263. }
  264. $teamGrades = TeamGrades::findOne($userTeamGrades['id']);
  265. if (!$teamGrades) {
  266. $user = User::findOne($user_id);
  267. if ($user) {
  268. $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
  269. $teamGrades = new TeamGrades();
  270. $teamGrades->user_id = $user_id;
  271. $teamGrades->store_id = $store_id;
  272. $teamGrades->name = $saas_user->name;
  273. $teamGrades->mobile = $saas_user->mobile;
  274. $teamGrades->team_grades_level = $next_level_info['level'];
  275. $teamGrades->province_id = 0;
  276. $teamGrades->city_id = 0;
  277. $teamGrades->district_id = 0;
  278. if (!$teamGrades->save()) {
  279. throw new \Exception(json_encode($teamGrades->errors, JSON_UNESCAPED_UNICODE));
  280. }
  281. }
  282. } else {
  283. $teamGrades->team_grades_level = $next_level_info['level'];
  284. if (!$teamGrades->save()) {
  285. throw new \Exception(json_encode($teamGrades, JSON_UNESCAPED_UNICODE));
  286. };
  287. }
  288. $transaction->commit();
  289. return true;
  290. } catch (\Exception $e) {
  291. debug_log(['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()],'team_grades_auto_upgrade_error.log');
  292. $transaction->rollback();
  293. return false;
  294. }
  295. }
  296. }