ScoreBonusModel.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 addons\ScoreBonus\common\enums\ScoreBonusEnums;
  8. use common\enums\StatusEnum;
  9. use common\models\common\Download;
  10. use common\components\export\CsvTool;
  11. use common\enums\DownloadEnum;
  12. /**
  13. * This is the model class for table "{{%addon_default}}".
  14. */
  15. class ScoreBonusModel extends BaseActiveRecord
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public static function tableName()
  21. {
  22. return '{{%addons_score_bonus}}';
  23. }
  24. public function rules()
  25. {
  26. return [
  27. [['mall_id','bonus_circle', 'status', 'created_at', 'num', 'updated_at', 'date_time', 'bonus_num', 'settle_method', 'total_weight'], 'integer'],
  28. [['bonus_price', 'settle_price', 'total_score'], 'number'],
  29. [['num_text'], 'string'],
  30. ];
  31. }
  32. public static function fieldsList($field = null)
  33. {
  34. $fields = [
  35. 'num' => '期数',
  36. 'bonus_num' => '分红用户数',
  37. 'settle_price' => '结算金额',
  38. 'bonus_price' => '周期分红池总额',
  39. 'total_score' => '周期总积分',
  40. 'status' => '结算状态',
  41. 'bonus_circle' => '结算周期',
  42. 'created_at' => '结算时间',
  43. ];
  44. if ($field === null) return $fields;
  45. $temp = [];
  46. foreach ($field as $val) {
  47. if (isset($fields[$val])) $temp[$val] = $fields[$val];
  48. }
  49. return $temp;
  50. }
  51. /**
  52. * 保存数据
  53. * @Author: lun
  54. * @DateTime: 2023/6/2 0002 15:53
  55. * @Copyright: copyright (c) 2021 广东七件事集团
  56. * @param $params
  57. * @return bool
  58. */
  59. public static function setData(array $params)
  60. {
  61. try {
  62. $model = new self();
  63. $model->loadDefaultValues();
  64. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  65. return $model;
  66. } catch (Exception $e) {
  67. self::$static_error = $e->getMessage();
  68. return false;
  69. }
  70. }
  71. public static function getDataOne($params = [], $orderBy = 'id desc')
  72. {
  73. return self::find()->where($params)->orderBy($orderBy)->asArray()->one();
  74. }
  75. public static function exportHandle(array $data)
  76. {
  77. date_default_timezone_set('PRC');
  78. $download_id = $data['download_id'] ?? 0;
  79. $download = Download::findOne(['id' => $download_id]);
  80. if($download){
  81. try{
  82. $params = json_decode($download->params,true);
  83. // print_r($params);exit;
  84. $filename = $download->name;
  85. $type = $download->type;
  86. $mall_id = $params['mall_id'];
  87. $fields = $params['fields'];
  88. $fields_arr = self::fieldsList();
  89. $have_select_field_arr = [];
  90. // print_r($params);exit;
  91. foreach ($fields as $value){
  92. $have_select_field_arr[]=$fields_arr[$value];
  93. }
  94. $csv = new CsvTool();
  95. $csv->filename = $filename;
  96. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  97. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  98. $csv->header = $have_select_field_arr;
  99. $backend_contents_model = self::find();
  100. $backend_contents_model->where(['mall_id' => $params['mall_id']]);
  101. if (isset($params['status']) && is_numeric($params['status'])) $backend_contents_model->andWhere(['status' => $params['status']]);
  102. if (!empty($params['start'])) $backend_contents_model->andWhere(['>=', 'created_at', strtotime($params['start'])]);
  103. if (!empty($params['end'])) $backend_contents_model->andWhere(['<=', 'created_at', strtotime($params['end'])]);
  104. if (!empty($params['num_text'])) $backend_contents_model->andWhere(['like', 'num_text', $params['num_text']]);
  105. if (!empty($params['id_str'])) {
  106. $id_arr = explode(',', $params['id_str']);
  107. $backend_contents_model->andWhere(['in', 'id', $id_arr]);
  108. }
  109. //设置排序
  110. $order = 'id desc';
  111. if (!empty($params['orderBy'])) $order = $params['orderBy'];
  112. $backend_contents_model->orderBy($order);
  113. $bonus_circle = ScoreBonusEnums::getSettleCircleMap();
  114. $data_list = [];
  115. foreach($backend_contents_model->batch() as $contents){
  116. foreach ($contents as $key => $value) {
  117. $row = [];
  118. foreach ($fields as $field){
  119. switch ($field) {
  120. case 'num':
  121. if ($value['num'] < 10) {
  122. $value['num'] = '0' . $value['num'];
  123. }
  124. $row[] = $value['date_time'].$value['num'].'期'??'';
  125. break;
  126. case 'bonus_num':
  127. $row[] = $value['bonus_num']??'0';
  128. break;
  129. case 'settle_price':
  130. $row[] = $value['settle_price']??'0.00';
  131. break;
  132. case 'bonus_price':
  133. $row[] = $value['bonus_price']??'0.00';
  134. break;
  135. case 'total_score':
  136. $row[] = $value['total_score']??'0.00';
  137. break;
  138. case 'status':
  139. if ($value['status'] == 1) {
  140. $row[] = '成功';
  141. } else {
  142. $row[] = '失败';
  143. }
  144. break;
  145. case 'bonus_circle':
  146. $row[] = $bonus_circle[$value['bonus_circle']];
  147. break;
  148. case 'created_at':
  149. $row[] = date('Y-m-d H:i:s',$value['created_at'])??'';
  150. break;
  151. }
  152. }
  153. $data_list[]=$row;
  154. }
  155. }
  156. $csv->data = $data_list;
  157. $csv->export();
  158. $url = $csv->upload($params['mall_id'],$csv->relative_filename);
  159. $download->status = 2;
  160. $download->url = $url;
  161. $download->num = $csv->line_num;
  162. $download->size = $csv->file_size;
  163. $res = $download->save();
  164. return true;
  165. }catch(Exception $e){
  166. $download->status = 3;
  167. self::setStaticError($e->getMessage());
  168. return false;
  169. }
  170. }
  171. }
  172. }