SeedUser.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace addons\Seed\common\models;
  3. use common\components\export\CsvTool;
  4. use common\enums\DownloadEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\common\Addons;
  7. use common\models\common\Download;
  8. use common\models\mall\MallAddons;
  9. use common\models\user\User;
  10. use Yii;
  11. use yii\db\Exception;
  12. use yii\db\Expression;
  13. /**
  14. * This is the model class for table "{{%addons_seed_user}}".
  15. *
  16. * @property int $id
  17. * @property int $mall_id 商城ID
  18. * @property int $user_id 用户ID
  19. * @property int $total_seed_num 种子金总数量
  20. * @property float $total_seed_cost 种子金总价值,单位:元
  21. * @property int $collect_seed_num 已收取种子金总数量
  22. * @property int $refund_seed_num 退款种子金总数量
  23. * @property float $total_change_money 累计转换金额,单位:元
  24. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  25. * @property int $created_at 创建时间
  26. * @property int $updated_at 修改时间
  27. */
  28. class SeedUser extends \common\models\base\BaseActiveRecord
  29. {
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public static function tableName()
  34. {
  35. return '{{%addons_seed_user}}';
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function rules()
  41. {
  42. return [
  43. [['mall_id', 'user_id', 'total_seed_num', 'collect_seed_num', 'refund_seed_num', 'status', 'created_at', 'updated_at'], 'integer'],
  44. [['total_seed_cost', 'total_change_money'], 'number'],
  45. ];
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'id' => 'ID',
  54. 'mall_id' => '商城ID',
  55. 'user_id' => '用户ID',
  56. 'total_seed_num' => '种子金总数量',
  57. 'total_seed_cost' => '种子金总价值,单位:元',
  58. 'collect_seed_num' => '已收取种子金总数量',
  59. 'refund_seed_num' => '退款种子金总数量',
  60. 'total_change_money' => '累计转换金额,单位:元',
  61. 'status' => '状态[-1:删除;0:禁用;1启用]',
  62. 'created_at' => '创建时间',
  63. 'updated_at' => '修改时间',
  64. ];
  65. }
  66. /**
  67. * 关联用户表
  68. * @Author: lun
  69. * @DateTime: 2022/7/28 0028 10:07
  70. * @Copyright: copyright (c) 2021 广东七件事集团
  71. * @return \yii\db\ActiveQuery
  72. */
  73. public function getUser()
  74. {
  75. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,username,avatar_url');
  76. }
  77. /**
  78. * 保存数据
  79. * @Author: lun
  80. * @DateTime: 2022/7/27 0027 11:44
  81. * @Copyright: copyright (c) 2021 广东七件事集团
  82. * @param $mall_id
  83. * @param $user_id
  84. * @param $params
  85. * @return array|bool
  86. */
  87. public static function setData($mall_id, $user_id, $params)
  88. {
  89. try {
  90. $model = self::findOne(['user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
  91. if (empty($model)) {
  92. $model = new self();
  93. $model->mall_id = $mall_id;
  94. $model->user_id = $user_id;
  95. $model->loadDefaultValues();
  96. }
  97. // 循环赋值
  98. foreach ($model as $key => $value) {
  99. if (isset($params[$key]) && !empty($params[$key])) $model->$key += $params[$key];
  100. }
  101. if (!$model->save()) throw new Exception($model->getErrorMessage());
  102. return $model->toArray();
  103. } catch (\Exception $e) {
  104. self::setStaticError('用户种子金:'.$e->getMessage());
  105. return false;
  106. }
  107. }
  108. /**
  109. * 导出字段
  110. * @Author: lun
  111. * @DateTime: 2022/8/5 0005 11:23
  112. * @Copyright: copyright (c) 2021 广东七件事集团
  113. * @return []
  114. */
  115. public static function fieldsList()
  116. {
  117. $fields = [
  118. 'user_id' => '用户ID',
  119. 'nickname' => '用户昵称',
  120. 'mobile' => '用户手机',
  121. 'seed_num' => '现有种子金总数量',
  122. 'total_seed_num' => '累计种子金总数量',
  123. 'total_seed_cost' => '种子金总价值',
  124. 'collect_seed_num' => '已收取种子金',
  125. 'refund_seed_num' => '退款种子金数量',
  126. 'total_change_money' => '累计转换金额',
  127. 'created_at' => '创建时间',
  128. ];
  129. return $fields;
  130. }
  131. /**
  132. * 导出处理
  133. * @Author: lun
  134. * @DateTime: 2022/8/5 0005 11:23
  135. * @Copyright: copyright (c) 2021 广东七件事集团
  136. * @param array $data
  137. * @return string
  138. */
  139. public static function exportHandle(array $data) {
  140. $download_id = $data['download_id'] ?? 0;
  141. $download = Download::findOne(['id' => $download_id]);
  142. if ($download) {
  143. try {
  144. $params = json_decode($download->params,true);
  145. $filename = $download->name;
  146. $type = $download->type;
  147. $seel_user_model = self::find();
  148. $seel_user_model->where(['mall_id' => $params['mall_id']]);
  149. !empty($params['user_id']) && $seel_user_model->where(['user_id' => $params['user_id']]);
  150. if (!empty($params['mobile'])){
  151. $user = User::find()->where(['like','mobile',$params['mobile'].'%',false])->all();
  152. $where[] = ['in', 'user_id', array_column($user,'id')];
  153. $seel_user_model->andWhere(['in', 'user_id', array_column($user,'id')]);
  154. }
  155. $seel_user_model->andWhere(['=','status',StatusEnum::ENABLED]);
  156. $seel_user_model->with(['user' => function($query){
  157. $query->select('id,nickname,username,mobile');
  158. }]);
  159. $fields = $params['fields'];
  160. $fields_arr = self::fieldsList();
  161. $have_select_field_arr = [];
  162. foreach ($fields as $value){
  163. $have_select_field_arr[]=$fields_arr[$value];
  164. }
  165. $csv = new CsvTool();
  166. $csv->filename = $filename;
  167. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  168. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  169. $csv->header = $have_select_field_arr;
  170. foreach ($seel_user_model->batch() as $seel_users) {
  171. $csv->data = array_map(function($seel_user)use($fields){
  172. $row = [];
  173. foreach ($fields as $field){
  174. switch (true){
  175. case ($field == 'nickname'):
  176. $row[] = !empty($seel_user[$field])?date('Y-m-d H:i:s',$seel_user[$field]):'';
  177. break;
  178. case ($field == 'mobile'):
  179. $row[] = !empty($seel_user['order'][$field])?date('Y-m-d H:i:s',$seel_user['order'][$field]):'';
  180. break;
  181. case ($field == 'seed_num'):
  182. $row[] = $seel_user['total_seed_num'] - $seel_user['collect_seed_num'] - $seel_user['refund_seed_num'];
  183. break;
  184. default:
  185. $row[] = !empty($seel_user[$field])?$seel_user[$field]:'';
  186. break;
  187. }
  188. }
  189. return $row;
  190. }, $seel_users);
  191. $csv->export();
  192. }
  193. $csv->updateDown($download,$params['mall_id']);
  194. return true;
  195. } catch (\Exception $e) {echo $e->getMessage();
  196. $download->status = 3;
  197. $download->save();
  198. self::setStaticError($e->getMessage());
  199. return $e->getMessage();
  200. }
  201. }
  202. }
  203. /**
  204. * 修改统计数据
  205. * @Author: lun
  206. * @DateTime: 2022/8/30 0030 10:37
  207. * @Copyright: copyright (c) 2021 广东七件事集团
  208. * @param $user_id
  209. * @param $params
  210. * @throws Exception
  211. */
  212. public static function saveStatistics($user_id, $params)
  213. {
  214. $update_data['updated_at'] = time();
  215. foreach ($params as $key => $value) {
  216. $symbol = $value >= 0 ? '+' : '-';
  217. $update_data[$key] = new Expression($key.$symbol.abs($value));
  218. }
  219. $res = self::updateAll($update_data, ['user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
  220. if (!$res) throw new Exception('修改统计数据失败');
  221. }
  222. public static function getAddons(array $params){
  223. $addons_name = $params['addons_name'];
  224. $is_install = MallAddons::isInstall($params['mall_id'], $addons_name);
  225. if($is_install){
  226. $detail = Addons::getOne([['name' =>$addons_name], ['is_online' => 1]], true,);
  227. return [
  228. 'addons_name'=>$addons_name,
  229. 'title'=>$detail['title'],
  230. ];
  231. }
  232. return [];
  233. }
  234. }