CloudStockTwoAgent.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. namespace addons\CloudStockTwo\common\models;
  3. use addons\CloudStockTwo\common\sms\RewardMessage;
  4. use addons\CloudStockTwo\common\sms\SmsConfig;
  5. use common\enums\StatusEnum;
  6. use common\enums\UserWalletEnum;
  7. use common\helpers\sms\Sms;
  8. use common\models\base\BaseActiveRecord;
  9. use common\models\common\Addons;
  10. use common\models\mall\MallAddons;
  11. use common\models\user\User;
  12. use common\models\user\UserPartitionRelationship;
  13. use Overtrue\EasySms\EasySms;
  14. use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
  15. use services\common\UserWalletService;
  16. use yii\db\Exception;
  17. /**
  18. * This is the model class for table "qimall_addons_cloud_stock_agent".
  19. *
  20. * @property int $id
  21. * @property int $mall_id 商城id
  22. * @property int $user_id 用户id
  23. * @property string $remarks 备注
  24. * @property int $total_childs 所有下级数量
  25. * @property int $total_order 订单数量
  26. * @property int $upgrade_at 等级升级时间
  27. * @property float $total_price 累计收益
  28. * @property float $price 已结算收益
  29. * @property float $frozen_price 未结算收益
  30. * @property int $upgrade_status 升级类型;1条件升级;2升级礼包;3平台添加;4上级添加
  31. * @property int $level 代理商等级权重
  32. * @property int|null $before_level 升级前等级;null为无
  33. * @property int $status 状态[-1:删除;0:禁用;1启用]
  34. * @property int $created_at
  35. * @property int $updated_at
  36. * @property int $fill_num 补货数量
  37. */
  38. class CloudStockTwoAgent extends BaseActiveRecord
  39. {
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public static function tableName()
  44. {
  45. return '{{%addons_cloud_stock_two_agent}}';
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function rules()
  51. {
  52. return [
  53. [['mall_id', 'user_id', 'total_childs', 'total_order', 'upgrade_status', 'upgrade_at', 'level', 'before_level', 'status', 'created_at', 'updated_at','fill_num'], 'integer'],
  54. [['total_price', 'price', 'frozen_price'], 'number'],
  55. [['remarks'], 'string', 'max' => 2048],
  56. ];
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function attributeLabels()
  62. {
  63. return [
  64. 'id' => 'ID',
  65. 'mall_id' => '商城id',
  66. 'user_id' => '用户id',
  67. 'remarks' => '备注',
  68. 'total_childs' => '所有下级数量',
  69. 'total_order' => '订单数量',
  70. 'upgrade_at' => '等级升级时间',
  71. 'total_price' => '累计收益',
  72. 'price' => '已结算收益',
  73. 'frozen_price' => '未结算收益',
  74. 'upgrade_status' => '升级类型;1条件升级;2升级礼包;3平台添加;4上级添加',
  75. 'level' => '代理商等级权重',
  76. 'before_level' => '升级前等级;null为无',
  77. 'status' => '状态[-1:删除;0:禁用;1启用]',
  78. 'created_at' => 'Created At',
  79. 'updated_at' => 'Updated At',
  80. 'fill_num' => '补货数量',
  81. ];
  82. }
  83. public function getUser()
  84. {
  85. return $this->hasOne(User::class, ['id' => 'user_id'])->select(['id', 'nickname', 'avatar_url', 'mobile', 'parent_id']);
  86. }
  87. public function getStockLevel()
  88. {
  89. return $this->hasOne(CloudStockTwoLevel::class, ['level' => 'level', 'mall_id' => 'mall_id'])->where(['status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  90. }
  91. public static function setData(array $params)
  92. {
  93. try {
  94. if (!empty($params['id'])) {
  95. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  96. if (empty($model)) throw new \Exception('服务不存在或已删除');
  97. $params['before_level'] = $model->level;
  98. if(isset($params['level'])){
  99. if ($params['before_level'] < $params['level']) $params['upgrade_at'] = time();
  100. }
  101. } else {
  102. $model = new self();
  103. $model->loadDefaultValues();
  104. }
  105. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  106. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  107. if(isset($params['level'])&&$params['level']>$model['before_level']){
  108. $level = CloudStockTwoLevel::findOne(['mall_id' => $params['mall_id'], 'level' => $params['level'],'status'=>[StatusEnum::ENABLED]]);
  109. if (!empty($level)) {
  110. $give_quota = json_decode($level['give_quota'], true);
  111. if (!empty($give_quota)) {
  112. foreach($give_quota as $item){
  113. if(!empty($item['is_use'])&&!empty($item['num'])){
  114. $param = [];
  115. $param['mall_id'] = $model['mall_id'];
  116. $param['user_id'] = $model['user_id'];
  117. $param['num'] = $item['num'];
  118. $param['level'] = $item['level'];
  119. $res = CloudStockTwoAgentQuota::setData($param);
  120. if ($res==false) throw new Exception(CloudStockTwoAgentQuota::getStaticError());
  121. }
  122. }
  123. }
  124. }
  125. }
  126. if(isset($params['status'])&&$params['status']==StatusEnum::DELETE){
  127. CloudStockTwoAgentQuota::updateAll(['status'=>StatusEnum::DELETE],['user_id'=>$model['user_id']]);
  128. }
  129. return $model;
  130. } catch (\Exception $e) {
  131. self::$static_error = $e->getMessage();
  132. return false;
  133. }
  134. }
  135. /**
  136. * @name:
  137. * @msg: 获取供应商
  138. * @param {*} $agent_user_id 当前代理商 user_id
  139. * @param {*} $level 当前代理商等级
  140. * @return object
  141. */
  142. public static function getUserParentAgent($agent_user_id, $level)
  143. {
  144. $parent_id_arr = UserPartitionRelationship::getParentIdArr($agent_user_id);
  145. $parent_id_arr = array_reverse($parent_id_arr);
  146. //补货只能找等级比自己大的代理补货
  147. $parent_agent_list = CloudStockTwoAgent::lists(['where' => [['user_id' => $parent_id_arr], ['>', 'level', $level],['status'=>[StatusEnum::DISABLED, StatusEnum::ENABLED]]], 'index' => 'user_id'], false);
  148. $parent_agent = null;
  149. foreach ($parent_id_arr as $user_id) {
  150. if (isset($parent_agent_list[$user_id])) {
  151. $parent_agent = $parent_agent_list[$user_id];
  152. break;
  153. }
  154. }
  155. return $parent_agent;
  156. }
  157. public static function getUserParentAgentList($agent_user_id,$is_array = false)
  158. {
  159. $parent_id_arr = UserPartitionRelationship::getParentIdArr($agent_user_id);
  160. $parent_id_arr = array_reverse($parent_id_arr);
  161. $parent_agent_list = CloudStockTwoAgent::lists(['where' => [['user_id' => $parent_id_arr]], 'index' => 'user_id'], $is_array);
  162. $list = [];
  163. foreach ($parent_id_arr as $user_id) {
  164. if (isset($parent_agent_list[$user_id])) {
  165. $list[] = $parent_agent_list[$user_id];
  166. }
  167. }
  168. return $list;
  169. }
  170. //投递到资产队列
  171. public static function sendUserWalletQueue($insert_wallet)
  172. {
  173. try {
  174. if ($insert_wallet) {
  175. $res = UserWalletService::batchHandleByQueue($insert_wallet);
  176. if ($res == false) throw new \Exception(UserWalletService::getStaticError());
  177. $user_ids = array_column($insert_wallet,'user_id');
  178. $user_arr = User::lists(['where'=>[['id'=>$user_ids]],'select'=>'id,mobile','index'=>'id']);
  179. foreach ($insert_wallet as $k => $user) {
  180. $agent = CloudStockTwoAgent::findOne(['user_id' => $user['user_id'],'status'=>StatusEnum::ENABLED]);
  181. if (!empty($user['is_frozen'])) {
  182. $agent->frozen_price += $user['money'];
  183. } else {
  184. $agent->price += $user['money'];
  185. }
  186. $agent->total_price += $user['money'];
  187. $res = $agent->save();
  188. if (empty($res)) throw new \Exception($agent->getErrorMessage());
  189. //奖励短信
  190. $mobile = $user_arr[$user['user_id']]['mobile']??0;
  191. if(!empty($mobile)&&!$user['is_frozen']){
  192. if (in_array($user['capital_type'], [UserWalletEnum::FILL, UserWalletEnum::OVER, UserWalletEnum::EQUAL,UserWalletEnum::TEAM])) {
  193. $obj = new CloudStockTwoAgent();
  194. $obj->sendMessage($user['mall_id'],$mobile,$user['money']);
  195. }
  196. //货款短信
  197. if (in_array($user['capital_type'], [UserWalletEnum::PAYMENT_FOR_GOODS_FILL, UserWalletEnum::PAYMENT_FOR_GOODS_BUY])) {
  198. $obj = new CloudStockTwoAgent();
  199. $obj->sendMessage($user['mall_id'],$mobile,$user['money']);
  200. }
  201. }
  202. }
  203. }
  204. } catch (\Exception $e) {
  205. // echo 'sendUserWalletQueue:' . $e->getMessage();
  206. \Yii::error('sendUserWalletQueue:' . $e->getMessage());
  207. // return false;
  208. }
  209. }
  210. /**
  211. * @name:
  212. * @msg: 设置下级代理
  213. * @param {int} $mall_id
  214. * @param {array} $data user_id:当前代理会员主表 id;agent_id:当前代理 id;child_id:下级会员 id;set_level:给下级设定的等级
  215. * @return bool
  216. */
  217. public static function setWholesaler(int $mall_id, array $data): bool
  218. {
  219. $transaction = \Yii::$app->db->beginTransaction();
  220. $params_agent = [
  221. 'mall_id' => $mall_id,
  222. 'user_id' => $data['child_id'],
  223. 'level' => $data['set_level'],
  224. 'upgrade_status' => $data['upgrade_status'],
  225. ];
  226. unset($data['upgrade_status']);
  227. $res_agent = self::setData($params_agent);
  228. if (false === $res_agent) {
  229. $transaction->rollBack();
  230. return false;
  231. }
  232. $data['mall_id'] = $mall_id;
  233. $data['child_agent_id'] = $res_agent->id;
  234. $res_log = CloudStockTwoSetAgentLog::setData($mall_id, $data);
  235. if (false === $res_log) {
  236. $transaction->rollBack();
  237. self::$static_error = CloudStockTwoSetAgentLog::getStaticError();
  238. return false;
  239. }
  240. $params = [];
  241. $params['mall_id'] = $mall_id;
  242. $params['user_id'] = $data['user_id'];
  243. $params['num'] = -1;
  244. $params['level'] =$data['set_level'];
  245. CloudStockTwoAgentQuota::setData($params);
  246. $transaction->commit();
  247. return true;
  248. }
  249. //发送短信
  250. public function sendMessage($mall_id,$mobile,$money){
  251. try{
  252. if(!empty($mobile)){
  253. $SmsConfig = new Sms($mall_id);
  254. $easy_sms = $SmsConfig->easySms;
  255. $message = new RewardMessage($mall_id,bcmul($money,1,2));
  256. $easy_sms->send($mobile, $message);
  257. }
  258. }catch (NoGatewayAvailableException $e){
  259. $res = $e->getExceptions();
  260. if(!empty($res)){
  261. foreach ($res as $exception){
  262. if(is_string($exception->getMessage())){
  263. \Yii::error('云库存收益通知短信发送失败:' .$exception->getMessage());
  264. }
  265. }
  266. }
  267. }
  268. }
  269. //货款短信
  270. public function sendGoodsMoneyMessage($user_arr,$item){
  271. $mobile = $user_arr[$item['user_id']]['mobile']??0;
  272. if(!empty($mobile)){
  273. $this->sendMessage($item['mall_id'],$mobile, $item['change_num']);
  274. }
  275. }
  276. public static function getAddons(array $params){
  277. $addons_name = $params['addons_name'];
  278. $is_install = MallAddons::isInstall($params['mall_id'], $addons_name);
  279. if($is_install){
  280. $detail = Addons::getOne([['name' =>$addons_name], ['is_online' => 1]], true,);
  281. return [
  282. 'addons_name'=>$addons_name,
  283. 'title'=>$detail['title'],
  284. ];
  285. }
  286. return [];
  287. }
  288. }