ScoreBonusRewardLogModel.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace addons\ScoreBonus\common\models;
  3. use Yii;
  4. use common\models\base\BaseActiveRecord;
  5. use yii\behaviors\TimestampBehavior;
  6. use yii\db\Exception;
  7. use common\models\user\User;
  8. use addons\ScoreBonus\common\enums\ScoreBonusEnums;
  9. use common\models\common\Download;
  10. use common\components\export\CsvTool;
  11. use common\enums\DownloadEnum;
  12. use common\models\mall\MallAddons;
  13. use common\enums\StatusEnum;
  14. /**
  15. * This is the model class for table "{{%addon_default}}".
  16. */
  17. class ScoreBonusRewardLogModel extends BaseActiveRecord
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public static function tableName()
  23. {
  24. return '{{%addons_score_bonus_reward_log}}';
  25. }
  26. public function rules()
  27. {
  28. return [
  29. [['mall_id','user_id', 'bid', 'status', 'created_at', 'bonus_circle', 'updated_at', 'date_time', 'weight'], 'integer'],
  30. [['amounts', 'score', 'total_score', 'commission'], 'number'],
  31. [['change_type'], 'string'],
  32. ];
  33. }
  34. public static function fieldsList($field = null)
  35. {
  36. $fields = [
  37. 'user_id' => '用户ID',
  38. 'nickname' => '用户',
  39. 'commission' => '结算金额',
  40. 'score' => '结算持有积分',
  41. 'total_score' => '结算周期总积分',
  42. 'status' => '结算状态',
  43. 'created_at' => '结算时间',
  44. 'bonus_circle' => '结算周期'
  45. ];
  46. if ($field === null) return $fields;
  47. $temp = [];
  48. foreach ($field as $val) {
  49. if (isset($fields[$val])) $temp[$val] = $fields[$val];
  50. }
  51. return $temp;
  52. }
  53. /**
  54. * 保存数据
  55. * @Author: lun
  56. * @DateTime: 2023/6/2 0002 15:53
  57. * @Copyright: copyright (c) 2021 广东七件事集团
  58. * @param $params
  59. * @return bool
  60. */
  61. public static function setData(array $params)
  62. {
  63. try {
  64. $model = new self();
  65. $model->loadDefaultValues();
  66. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  67. return $model;
  68. } catch (Exception $e) {
  69. self::$static_error = $e->getMessage();
  70. return false;
  71. }
  72. }
  73. public function getUser()
  74. {
  75. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id, avatar_url, nickname, username, mobile');
  76. }
  77. public function getScoreBonus()
  78. {
  79. return $this->hasOne(ScoreBonusModel::class, ['id' => 'bid']);
  80. }
  81. public static function getAllData($params = [], $orderBy = 'id desc') {
  82. return self::find()->where($params)->orderBy($orderBy)->asArray()->all();
  83. }
  84. public static function getfieldSum($params = [], $field = 'commission') {
  85. return self::find()->where($params)->sum($field);
  86. }
  87. public static function getOneData($params = [], $orderBy = 'id desc') {
  88. return self::find()->where($params)->orderBy($orderBy)->asArray()->one();
  89. }
  90. /**
  91. * @desc:推广中心-调用
  92. * @Author: hua
  93. * @Date: 2021/11/12
  94. * @Time: 9:31
  95. * @Copyright: copyright (c) 2021 广东七件事集团
  96. * @param array $params ['user_id' => 1, 'mall_id' => 5, 'addons_name' => 'Agent']
  97. * @return array
  98. */
  99. public static function getIncomeInfo(array $params){
  100. $is_install = MallAddons::isInstall($params['mall_id'], $params['addons_name']);
  101. $data['total_commission'] = 0;
  102. $data['is_install'] = 0;
  103. $basic = \Yii::$app->services->setting->addons->get($params['mall_id'], $params['addons_name'], 'basic');
  104. $data['is_show_at_center'] = 1;
  105. if(isset($basic['is_show_at_center'])) $data['is_show_at_center'] = $basic['is_show_at_center'];
  106. if($is_install){
  107. $total_commission = self::getfieldSum(['user_id' => $params['user_id'], 'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED], 'commission');
  108. $data['total_commission'] = $total_commission??0.00;
  109. $data['is_install'] = $is_install;
  110. }
  111. return $data;
  112. }
  113. public static function exportHandle(array $data)
  114. {
  115. date_default_timezone_set('PRC');
  116. $download_id = $data['download_id'] ?? 0;
  117. $download = Download::findOne(['id' => $download_id]);
  118. if($download){
  119. try{
  120. $params = json_decode($download->params,true);
  121. // print_r($params);exit;
  122. $filename = $download->name;
  123. $type = $download->type;
  124. $mall_id = $params['mall_id'];
  125. $fields = $params['fields'];
  126. $fields_arr = self::fieldsList();
  127. $have_select_field_arr = [];
  128. // print_r($params);exit;
  129. foreach ($fields as $value){
  130. $have_select_field_arr[]=$fields_arr[$value];
  131. }
  132. $csv = new CsvTool();
  133. $csv->filename = $filename;
  134. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  135. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  136. $csv->header = $have_select_field_arr;
  137. $backend_contents_model = self::find();
  138. $backend_contents_model->where(['mall_id' => $params['mall_id'], 'bid' => $params['bid']]);
  139. if (!empty($params['user_id'])) $backend_contents_model->andWhere(['user_id' => $params['user_id']]);
  140. if (!empty($params['id_str'])) {
  141. $id_arr = explode(',', $params['id_str']);
  142. $backend_contents_model->andWhere(['in', 'id', $id_arr]);
  143. }
  144. //设置排序
  145. $order = 'id desc';
  146. if (!empty($params['orderBy'])) $order = $params['orderBy'];
  147. $backend_contents_model->orderBy($order);
  148. $backend_contents_model->with(['user']);
  149. $bonus_circle = ScoreBonusEnums::getSettleCircleMap();
  150. $data_list = [];
  151. foreach($backend_contents_model->batch() as $contents){
  152. foreach ($contents as $key => $value) {
  153. $row = [];
  154. foreach ($fields as $field){
  155. switch ($field) {
  156. case 'user_id':
  157. $row[] = $value['user_id']??'';
  158. break;
  159. case 'nickname':
  160. $row[] = $value['user']['nickname']??'';
  161. break;
  162. case 'commission':
  163. $row[] = $value['commission']??'0.00';
  164. break;
  165. case 'score':
  166. $row[] = $value['score']??'0.00';
  167. break;
  168. case 'total_score':
  169. $row[] = $value['total_score']??'0.00';
  170. break;
  171. case 'status':
  172. if ($value['status'] == 1) {
  173. $row[] = '成功';
  174. } else {
  175. $row[] = '失败';
  176. }
  177. break;
  178. case 'bonus_circle':
  179. $row[] = $bonus_circle[$value['bonus_circle']];
  180. break;
  181. case 'created_at':
  182. $row[] = date('Y-m-d H:i:s',$value['created_at'])??'';
  183. break;
  184. }
  185. }
  186. $data_list[]=$row;
  187. }
  188. }
  189. $csv->data = $data_list;
  190. $csv->export();
  191. $url = $csv->upload($params['mall_id'],$csv->relative_filename);
  192. $download->status = 2;
  193. $download->url = $url;
  194. $download->num = $csv->line_num;
  195. $download->size = $csv->file_size;
  196. $res = $download->save();
  197. return true;
  198. }catch(Exception $e){
  199. $download->status = 3;
  200. self::setStaticError($e->getMessage());
  201. return false;
  202. }
  203. }
  204. }
  205. }