UserCenter.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. namespace addons\OperationCenter\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\mall\MallAddons;
  5. use Yii;
  6. use yii\db\Expression;
  7. /**
  8. * This is the model class for table "{{%addons_operation_center_user_center}}".
  9. *
  10. * @property int $id
  11. * @property int $mall_id Mall Id
  12. * @property int $user_id User Id
  13. * @property int $level 等级
  14. * @property float $settlement_amount 结算金额
  15. * @property float $settlement_amount_total 总佣金
  16. * @property float $discard_amount 失效佣金
  17. * @property string|null $center_name 运营中心名称
  18. * @property string|null $logo logo
  19. * @property int|null $province_id 省份ID
  20. * @property string|null $province 省份
  21. * @property int|null $city_id 城市ID
  22. * @property string|null $city 城市
  23. * @property int|null $district_id 县区ID
  24. * @property string|null $district 县区
  25. * @property int|null $town_id 街镇ID
  26. * @property string|null $town 街镇
  27. * @property string|null $operation_address 详细地址
  28. * @property int $status
  29. * @property float|null $lon 经度
  30. * @property float|null $lat 纬度
  31. * @property int|null $deleted_at
  32. * @property int|null $updated_at
  33. * @property int|null $created_at
  34. */
  35. class UserCenter extends BaseActiveRecord
  36. {
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public static function tableName()
  41. {
  42. return '{{%addons_operation_center_user_center}}';
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function rules()
  48. {
  49. return [
  50. [['mall_id', 'user_id', 'level', 'province_id', 'city_id', 'district_id', 'town_id', 'status', 'deleted_at', 'updated_at', 'created_at'], 'integer'],
  51. [['level'], 'required'],
  52. [['settlement_amount', 'settlement_amount_total', 'discard_amount', 'lon', 'lat'], 'number'],
  53. [['center_name'], 'string', 'max' => 32],
  54. [['logo', 'province', 'city', 'district', 'town', 'operation_address'], 'string', 'max' => 255],
  55. ];
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function attributeLabels()
  61. {
  62. return [
  63. 'id' => 'ID',
  64. 'mall_id' => 'Mall ID',
  65. 'user_id' => 'User ID',
  66. 'level' => 'Level',
  67. 'settlement_amount' => 'Settlement Amount',
  68. 'settlement_amount_total' => 'Settlement Amount Total',
  69. 'discard_amount' => 'Discard Amount',
  70. 'center_name' => 'Center Name',
  71. 'logo' => 'Logo',
  72. 'province_id' => 'Province ID',
  73. 'province' => 'Province',
  74. 'city_id' => 'City ID',
  75. 'city' => 'City',
  76. 'district_id' => 'District ID',
  77. 'district' => 'District',
  78. 'town_id' => 'Town ID',
  79. 'town' => 'Town',
  80. 'operation_address' => 'Operation Address',
  81. 'status' => 'Status',
  82. 'lon' => 'Lon',
  83. 'lat' => 'Lat',
  84. 'deleted_at' => 'Deleted At',
  85. 'updated_at' => 'Updated At',
  86. 'created_at' => 'Created At',
  87. ];
  88. }
  89. /**
  90. * @return ActiveQueryInterface the relational query object.
  91. */
  92. public function getUser()
  93. {
  94. return $this->hasOne(MallUser::class, ['id' => 'user_id']);
  95. }
  96. /**
  97. * @return ActiveQueryInterface the relational query object.
  98. */
  99. public function getLevel()
  100. {
  101. return $this->hasOne(Level::class, ['level' => 'level', 'mall_id' => 'mall_id']);
  102. }
  103. /**
  104. * @return ActiveQueryInterface the relational query object.
  105. */
  106. public function getLevelInfo()
  107. {
  108. return $this->hasOne(Level::class, ['level' => 'level', 'mall_id' => 'mall_id']);
  109. }
  110. /**
  111. * @return ActiveQueryInterface the relational query object.
  112. */
  113. public function getApply()
  114. {
  115. return $this->hasOne(ApplyList::class, ['user_id' => 'user_id'])->orderBy('id desc');
  116. }
  117. /**
  118. * @return ActiveQueryInterface the relational query object.
  119. */
  120. public function getApplyInfo()
  121. {
  122. return $this->hasOne(ApplyList::class, ['user_id' => 'user_id'])
  123. ->andWhere(['status' => 2])
  124. ->orderBy('id desc');
  125. }
  126. /**
  127. * 获取推广中心
  128. *
  129. * @param integer $user_id
  130. * @return static|null
  131. */
  132. public static function getCenterByUserId(int $user_id)
  133. {
  134. return static::find()
  135. ->andWhere(['user_id' => $user_id])
  136. ->one();
  137. }
  138. /**
  139. * 获取推广中心
  140. *
  141. * @param integer $mall_id
  142. * @param integer $id
  143. * @return static|null
  144. */
  145. public static function getCenterById(int $mall_id, int $id)
  146. {
  147. return static::find()
  148. ->andWhere(['mall_id' => $mall_id])
  149. ->andWhere(['id' => $id])
  150. ->one();
  151. }
  152. /**
  153. * 查询距离
  154. *
  155. * @param float $lon
  156. * @param float $lat
  157. * @return Expression
  158. */
  159. public static function getDistanceSql(float $lon, float $lat)
  160. {
  161. $sql = <<<SQL
  162. ROUND(
  163. 6378.138 * 2 * ASIN(
  164. SQRT(
  165. POW(SIN(($lat * PI() / 180 - lat * PI() / 180 ) / 2), 2) +
  166. COS($lat * PI() / 180) * COS(lat * PI() / 180) *
  167. POW(SIN(($lon * PI() / 180 - lon * PI() / 180) / 2), 2)
  168. )
  169. ), 3
  170. ) AS distance
  171. SQL;
  172. return new Expression($sql);
  173. }
  174. /**
  175. * 获取地址
  176. *
  177. * @return string
  178. */
  179. public function getOperationAddress()
  180. {
  181. return $this->province
  182. . $this->city
  183. . $this->district
  184. . $this->town
  185. . $this->operation_address;
  186. }
  187. /**
  188. * 添加运营中心
  189. *
  190. * @param ApplyList $apply
  191. * @param Level $level
  192. * @return boolean
  193. */
  194. public static function addUserByApply(
  195. ApplyList $apply, Level $level, float $lon, float $lat
  196. )
  197. {
  198. $data['mall_id'] = $apply->mall_id;
  199. $data['user_id'] = $apply->user_id;
  200. $data['level'] = $level->level;
  201. $data['center_name'] = $apply->center_name;
  202. $data['logo'] = $apply->logo;
  203. $data['province_id'] = $apply->province_id;
  204. $data['province'] = $apply->province;
  205. $data['city_id'] = $apply->city_id;
  206. $data['city'] = $apply->city;
  207. $data['district_id'] = $apply->district_id;
  208. $data['district'] = $apply->district;
  209. $data['town_id'] = $apply->town_id;
  210. $data['town'] = $apply->town;
  211. $data['operation_address'] = $apply->operation_address;
  212. $data['status'] = $apply->status;
  213. $data['lon'] = $lon;
  214. $data['lat'] = $lat;
  215. $model = new static;
  216. $model->load($data, '');
  217. return $model->save();
  218. }
  219. /**
  220. * 添加总佣金
  221. *
  222. * @param integer $user_id
  223. * @param float $amount
  224. * @return float
  225. */
  226. public static function addSettlementAmountTotal(int $user_id, float $amount)
  227. {
  228. return static::updateAllCounters(
  229. ['settlement_amount_total' => $amount],
  230. ['user_id' => $user_id]
  231. );
  232. }
  233. /**
  234. * 添加总佣金
  235. *
  236. * @param integer $user_id
  237. * @param float $amount
  238. * @return float
  239. */
  240. public static function addSettlementAmount(int $user_id, float $amount)
  241. {
  242. return static::updateAllCounters(
  243. ['settlement_amount' => $amount],
  244. ['user_id' => $user_id]
  245. );
  246. }
  247. /**
  248. * 添加失效佣金
  249. *
  250. * @param integer $user_id
  251. * @param float $amount
  252. * @return float
  253. */
  254. public static function addDiscardAmount(int $user_id, float $amount)
  255. {
  256. return static::updateAllCounters(
  257. ['discard_amount' => $amount],
  258. ['user_id' => $user_id]
  259. );
  260. }
  261. /**
  262. * 设置等级
  263. *
  264. * @param Level $level
  265. * @return true|static
  266. */
  267. public function setLevel(Level $level)
  268. {
  269. $this->level = $level->level;
  270. // $this->upgrade_level_at = time();
  271. return $this->save();
  272. }
  273. /**
  274. * 当前是否有会员使用此等级
  275. *
  276. * @param Level $level
  277. * @return boolean
  278. */
  279. public static function hasLevel(Level $level)
  280. {
  281. return !empty(
  282. static::find()
  283. ->andWhere(['level' => $level->level])
  284. ->limit(1)
  285. ->select(['id'])
  286. ->one()
  287. );
  288. }
  289. /**
  290. * 推广中心数据显示
  291. * @param array $params
  292. * @return array
  293. */
  294. public static function getIncomeInfo(array $params)
  295. {
  296. $is_install = MallAddons::isInstall($params['mall_id'], $params['addons_name']);
  297. $data['total_commission'] = 0;
  298. $data['is_install'] = 0;
  299. $basic = \Yii::$app->services->setting->addons->get($params['mall_id'], $params['addons_name'], 'basic');
  300. $data['is_show_at_center'] = 1;
  301. if(isset($basic['show_center'])) $data['is_show_at_center'] = $basic['show_center'];
  302. if ($is_install) {
  303. $model = self::findOne([
  304. 'user_id' => $params['user_id'],
  305. 'status' => StatusEnum::ENABLED,
  306. ]);
  307. $data['total_commission'] = $model->settlement_amount_total ?? 0.00;// 累计总佣金
  308. $data['is_install'] = $is_install;
  309. }
  310. return $data;
  311. }
  312. }