AchievementAwardLog.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace addons\Achievement\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use common\models\mall\MallAddons;
  6. use common\models\user\User;
  7. use Yii;
  8. use common\models\common\Download;
  9. use common\enums\DownloadEnum;
  10. use common\components\export\CsvTool;
  11. /**
  12. * This is the model class for table "qimall_addons_achievement_award_log".
  13. *
  14. * @property int $id
  15. * @property int $mall_id
  16. * @property int $grant_id 发放规则日志id
  17. * @property int $user_id 用户id
  18. * @property float $achievement_sum 总业绩
  19. * @property float $team_achievement 团队业绩
  20. * @property float $personal_achievement 个人业绩
  21. * @property float $award_proportion 奖励比例
  22. * @property float $award_money 奖励金额
  23. * @property int|null $is_settlement 是否已结算打款 1:是
  24. * @property int|null $is_end 是否结束周期:1:是
  25. * @property float|null $award_sum 奖励总计
  26. * @property int|null $parent_team 父级团长id
  27. * @property float|null $proportion_money 比例结算金额
  28. * @property float|null $actual_money 实际应该结算金额
  29. * @property int $created_at
  30. * @property int $updated_at
  31. * @property int $status 状态[-1:删除;0:禁用;1启用]
  32. */
  33. class AchievementAwardLog extends BaseActiveRecord
  34. {
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public static function tableName()
  39. {
  40. return 'qimall_addons_achievement_award_log';
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function rules()
  46. {
  47. return [
  48. [['mall_id', 'grant_id', 'user_id', 'is_settlement', 'is_skip_settlement', 'is_end', 'parent_team', 'created_at', 'updated_at', 'status'], 'integer'],
  49. [['achievement_sum', 'team_achievement', 'personal_achievement', 'award_proportion', 'award_money', 'award_sum', 'proportion_money', 'actual_money','equal_first','equal_second','equal_money','team_equal_first','team_equal_second', 'sub_personal_achievement'], 'number'],
  50. [['equal_info'], 'string', 'max' => 255],
  51. [['equal_info'], 'default', 'value' => ''],
  52. ];
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function attributeLabels()
  58. {
  59. return [
  60. 'id' => 'ID',
  61. 'mall_id' => 'Mall ID',
  62. 'grant_id' => '发放规则日志id',
  63. 'user_id' => '用户id',
  64. 'achievement_sum' => '总业绩',
  65. 'team_achievement' => '团队业绩',
  66. 'personal_achievement' => '个人业绩',
  67. 'award_proportion' => '奖励比例',
  68. 'award_money' => '奖励金额',
  69. 'is_settlement' => '是否已结算打款 1:是',
  70. 'is_skip_settlement' => '是否不结算 0:否 1:是',
  71. 'is_end' => '是否结束周期:1:是',
  72. 'award_sum' => '奖励总计',
  73. 'parent_team' => '父级团长id',
  74. 'proportion_money' => '比例结算金额',
  75. 'actual_money' => '实际应该结算金额',
  76. 'created_at' => 'Created At',
  77. 'updated_at' => 'Updated At',
  78. 'status' => '状态[-1:删除;0:禁用;1启用]',
  79. ];
  80. }
  81. // 关联模型
  82. public function getUser()
  83. {
  84. return $this->hasOne(User::class, ['id' => 'user_id']);
  85. }
  86. public function getGrantLog()
  87. {
  88. return $this->hasOne(AchievementGrantLog::class, ['id' => 'grant_id']);
  89. }
  90. public function getUpdateLog()
  91. {
  92. return $this->hasOne(AchievementAwardLogUpdateLog::class, ['award_id' => 'id'])->andWhere(['<>','status',StatusEnum::DELETE]);
  93. }
  94. /**
  95. * @desc:推广中心-调用
  96. * @Author: hua
  97. * @Date: 2021/11/12
  98. * @Time: 9:30
  99. * @Copyright: copyright (c) 2021 广东七件事集团
  100. * @param array $params ['user_id' => 1, 'mall_id' => 5, 'addons_name' => 'Agent']
  101. * @return array
  102. */
  103. public static function getIncomeInfo(array $params)
  104. {
  105. $is_install = MallAddons::isInstall($params['mall_id'], $params['addons_name']);
  106. $data['total_commission'] = 0;
  107. $data['is_install'] = $is_install;
  108. $basic = \Yii::$app->services->setting->addons->get($params['mall_id'], $params['addons_name'], 'basic');
  109. $data['is_show_at_center'] = 1;
  110. if(isset($basic['is_show_at_center'])) $data['is_show_at_center'] = $basic['is_show_at_center'];
  111. if($is_install){
  112. $achievement = self::find()->where(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED])->sum('award_money + equal_money');
  113. $data['total_commission'] = $achievement??0;
  114. }
  115. return $data;
  116. }
  117. public static function exportHandle(array $data)
  118. {
  119. $download_id = $data['download_id'] ?? 0;
  120. $download = Download::findOne(['id' => $download_id]);
  121. if ($download) {
  122. try {
  123. $params = json_decode($download->params, true);
  124. $filename = $download->name;
  125. $type = $download->type;
  126. $have_select_field_arr = [
  127. '周期ID',
  128. '用户ID',
  129. '用户昵称',
  130. '手机号',
  131. '发放时间',
  132. '总业绩',
  133. '团队业绩',
  134. '个人业绩',
  135. '奖励比例',
  136. '平级奖励',
  137. '结算金额',
  138. '现结算金额',
  139. '结算状态',
  140. ];
  141. $csv = new CsvTool();
  142. $csv->filename = $filename;
  143. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  144. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  145. $csv->header = $have_select_field_arr;
  146. $model = self::find();
  147. $model->where(['mall_id' => $params['mall_id']]);
  148. $model->andWhere(['=', 'status', StatusEnum::ENABLED]);
  149. //拼接查询条件
  150. if (!empty($params['grant_id'])) {
  151. $model->andWhere(['=', 'grant_id', $params['grant_id']]);
  152. }
  153. $is_search = 0;
  154. $user_id_arr = [];
  155. if (!empty($params['keyword'])) {
  156. $is_search = 1;
  157. $user_list = User::lists(['where' => [['mall_id' => $params['mall_id']], [
  158. 'or',
  159. ['=', 'id', $params['keyword']],
  160. ['like', 'nickname', $params['keyword']],
  161. ['like', 'mobile', $params['keyword']]
  162. ]], 'select' => 'id']);
  163. $user_ids = array_column($user_list, 'id');
  164. if (!empty($user_id_arr)) {
  165. $user_id_arr = array_intersect($user_id_arr, $user_ids);
  166. } else {
  167. $user_id_arr = $user_ids;
  168. }
  169. }
  170. if ($is_search) {
  171. $model->andWhere(['in', 'user_id', $user_id_arr]);
  172. }
  173. $model->with(['grantLog' => function ($query) {
  174. }, 'user', 'updateLog']);
  175. $model->orderBy('id desc');
  176. //遍历组装数据
  177. foreach ($model->asArray()->batch() as $list) {
  178. $result = [];
  179. foreach ($list as $item) {
  180. $user = $item['user'];
  181. $before_award_money = $item['updateLog']['before_award_money'] ?? $item['award_money'];
  182. $result[] = [
  183. 'grant_id' => $item['grant_id'],
  184. 'user_id' => $item['user_id'],
  185. 'nickname' => $user['nickname'] ?? '-',
  186. 'mobile' => $user['mobile'] ?? '',
  187. 'settlement_time' => !empty($item['grantLog']['settlement_time']) ? date('Y-m-d H:i:s', $item['grantLog']['settlement_time']) : '',
  188. 'achievement_sum' => $item['achievement_sum'] ?? '0.00',
  189. 'team_achievement' => $item['team_achievement'] ?? '0.00',
  190. 'personal_achievement' => $item['personal_achievement'] ?? '0.00',
  191. 'award_proportion' => $item['award_proportion'],
  192. 'equal_money' => $item['equal_money'] < 0 ? '-' . $item['equal_money'] : $item['equal_money'],
  193. 'before_award_money' => $before_award_money < 0 ? '-' . $before_award_money : $before_award_money,
  194. 'award_money' => $item['award_money'] < 0 ? '-' . $item['award_money'] : $item['award_money'],
  195. 'settle_status' => $item['is_skip_settlement'] == 1 ? '不结算' : ($item['is_settlement'] == 1 ? '已结算' : '未结算')
  196. ];
  197. }
  198. $csv->data = $result;
  199. $csv->export();
  200. }
  201. $csv->updateDown($download, $params['mall_id']);
  202. return true;
  203. } catch (\Exception $e) {
  204. $download->status = 3;
  205. $res = $download->save();
  206. self::setStaticError($e->getMessage());
  207. return false;
  208. }
  209. }
  210. }
  211. public static function updateSkipSettlement($grant_id)
  212. {
  213. try {
  214. $list = self::find()
  215. ->where([
  216. 'grant_id' => $grant_id,
  217. 'is_skip_settlement' => 1,
  218. 'status' => StatusEnum::ENABLED
  219. ])
  220. ->all();
  221. if (empty($list)) {
  222. return true;
  223. }
  224. $insert_rows = [];
  225. $now = time();
  226. $t = \Yii::$app->db->beginTransaction();
  227. foreach ($list as $item) {
  228. $insert_rows = [
  229. 'mall_id' => $item['mall_id'],
  230. 'grant_id' => $item['grant_id'],
  231. 'award_id' => $item['id'],
  232. 'before_award_money' => $item['award_money'],
  233. 'after_award_money' => 0,
  234. ];
  235. $item->award_money = 0;
  236. if (!$item->save()) throw new \Exception($item->getErrorMessage());
  237. AchievementAwardLogUpdateLog::setData($insert_rows);
  238. }
  239. $t->commit();
  240. return true;
  241. } catch (\Exception $e) {
  242. $t->rollBack();
  243. self::$static_error = $e->getMessage();
  244. return false;
  245. }
  246. }
  247. }