WechatVideoShopCommission.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. namespace addons\WechatVideoShop\common\models;
  3. use addons\WechatVideoShop\common\enums\WechatVideoShopEnums;
  4. use common\components\export\CsvTool;
  5. use common\enums\DownloadEnum;
  6. use common\enums\StatusEnum;
  7. use common\models\common\Download;
  8. use common\models\order\OrderDetail;
  9. use common\models\user\User;
  10. use common\models\order\Order;
  11. use Yii;
  12. /**
  13. * This is the model class for table "qimall_addons_wechat_video_shop_commission".
  14. *
  15. * @property int $id ID
  16. * @property int $mall_id 商城ID
  17. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  18. * @property int $created_at 创建时间
  19. * @property int $updated_at 更新事件
  20. * @property int $user_id 用户
  21. * @property int $buy_user_id 买家
  22. * @property int $order_id 订单ID
  23. * @property string $order_no 订单编号
  24. * @property float $order_amount 交易金额
  25. * @property string $remark 佣金备注
  26. * @property int $settle_status 结算状态
  27. * @property int $commission 佣金
  28. * @property int $commission_type 佣金类型1佣金2积分
  29. * @property int $goods_id 佣金类型1佣金2积分
  30. * @property int $shop_id 佣金类型1佣金2积分
  31. */
  32. class WechatVideoShopCommission extends \common\models\base\BaseActiveRecord
  33. {
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static function tableName()
  38. {
  39. return 'qimall_addons_wechat_video_shop_commission';
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function rules()
  45. {
  46. return [
  47. [['mall_id', 'status', 'created_at', 'updated_at', 'user_id', 'buy_user_id', 'order_id',
  48. 'settle_status', 'commission', 'commission_type', 'goods_id', 'shop_id'], 'integer'],
  49. [['order_amount'], 'number'],
  50. [['order_no'], 'string', 'max' => 30],
  51. [['remark'], 'string', 'max' => 1000],
  52. ];
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function attributeLabels()
  58. {
  59. return [
  60. 'id' => 'ID',
  61. 'mall_id' => 'Mall ID',
  62. 'status' => 'Status',
  63. 'created_at' => 'Created At',
  64. 'updated_at' => 'Updated At',
  65. 'user_id' => 'User ID',
  66. 'buy_user_id' => 'Buy User ID',
  67. 'order_id' => 'Order ID',
  68. 'order_no' => 'Order No',
  69. 'order_amount' => 'Order Amount',
  70. 'remark' => 'Remark',
  71. 'settle_status' => 'Settle Status',
  72. 'commission' => 'Commission',
  73. 'commission_type' => 'Commission Type',
  74. ];
  75. }
  76. public function getUser()
  77. {
  78. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id, username, nickname, avatar_url, mobile');
  79. }
  80. public function getSharer()
  81. {
  82. return $this->hasOne(WechatVideoShopSharer::class, ['user_id' => 'user_id']);
  83. }
  84. public function getBuyUser()
  85. {
  86. return $this->hasOne(User::class, ['id' => 'buy_user_id'])->select('id, username, nickname, avatar_url, mobile');
  87. }
  88. public function getOrder()
  89. {
  90. return $this->hasOne(Order::class, ['id' => 'order_id']);
  91. }
  92. /**
  93. * 导出字段
  94. */
  95. public static function exportFieldsList($field = null)
  96. {
  97. return [
  98. 'user_id' => '用户ID',
  99. 'nickname' => '昵称',
  100. 'mobile' => '手机号',
  101. 'created_at' => '时间',
  102. 'commission' => '佣金',
  103. 'commission_type' => '佣金类型',
  104. 'settle_status' => '结算状态',
  105. 'level' => '分销员等级',
  106. 'order_no' => '订单号',
  107. 'order_amount' => '订单金额',
  108. 'goods_name' => '商品名称'
  109. ];
  110. }
  111. public static function fieldsList($field = null)
  112. {
  113. $fields = [
  114. 'user_id' => '用户ID',
  115. 'nickname' => '昵称',
  116. 'mobile' => '手机号',
  117. 'created_at' => '时间',
  118. 'commission' => '佣金',
  119. 'commission_type' => '佣金类型',
  120. 'settle_status' => '结算状态',
  121. 'level' => '分销员等级',
  122. 'order_no' => '订单号',
  123. 'order_amount' => '订单金额',
  124. 'goods_name' => '商品名称'
  125. ];
  126. if ($field === null) return $fields;
  127. $temp = [];
  128. foreach ($field as $val) {
  129. if (isset($fields[$val])) $temp[$val] = $fields[$val];
  130. }
  131. return $temp;
  132. }
  133. /**
  134. * @name:
  135. * @msg: 处理导出数据,这个方法是异步队列处理时使用的,请别修改
  136. * @param {array} $data
  137. * @return bool
  138. */
  139. public static function exportHandle(array $data)
  140. {
  141. $download_id = $data['download_id'] ?? 0;
  142. $download = Download::findOne(['id' => $download_id]);
  143. if ($download) {
  144. try {
  145. $params = json_decode($download->params, true);
  146. $filename = $download->name;
  147. $type = $download->type;
  148. $fields = $params['fields'];
  149. $fields_arr = self::fieldsList();
  150. $have_select_field_arr = [];
  151. foreach ($fields as $value) {
  152. $have_select_field_arr[] = $fields_arr[$value];
  153. }
  154. $csv = new CsvTool();
  155. $csv->filename = $filename;
  156. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  157. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  158. $csv->header = $have_select_field_arr;
  159. $model = self::find();
  160. $model->where(['mall_id' => $params['mall_id']]);
  161. $model->andWhere(['>=', 'status', StatusEnum::DISABLED]);
  162. $user_id_arr = [];
  163. //拼接查询条件
  164. if (!empty($params['level'])) $model->andWhere(['=', 'level', $params['level']]);
  165. if (!empty($params['keyword'])) {
  166. $condition['where'] = [[
  167. 'or',
  168. ['like', 'nickname', $params['keyword']],
  169. ['like', 'mobile', $params['keyword']],
  170. ['id' => $params['keyword']]
  171. ]];
  172. $user_list = User::lists($condition);
  173. $user_id_arr = array_column($user_list, 'id');
  174. }
  175. if ($user_id_arr) {
  176. $model->andWhere(['in', 'user_id', $user_id_arr]);
  177. }
  178. if ($params['date_start']) {
  179. $model->andWhere(['>=', 'created_at', $params['date_start']]);
  180. }
  181. if ($params['date_end']) {
  182. $model->andWhere(['<=', 'created_at', $params['date_end']]);
  183. }
  184. if ($params['order_no']) {
  185. $order_list = Order::lists([
  186. 'where' => [
  187. ['mall_id' => $params['mall_id']],
  188. ['like', 'order_no', $params['order_no']]
  189. ],
  190. 'select' => 'id',
  191. ]);
  192. $model->andWhere(['order_id' => array_column($order_list, 'id') ?? []]);
  193. }
  194. $model->with(['user', 'sharer' => function($query) use($params) {
  195. $query->andWhere(['mall_id' => $params['mall_id']]);
  196. }]);
  197. $levels = WechatVideoShopLevel::lists([
  198. 'where' => [
  199. ['mall_id' => $params['mall_id']],
  200. ],
  201. 'index' => 'level'
  202. ]);
  203. //遍历组装数据
  204. foreach ($model->asArray()->batch() as $list) {
  205. $order_detail_list = OrderDetail::lists([
  206. 'where' => [
  207. ['mall_id' => $params['mall_id']],
  208. ['order_id' => array_column($list, 'order_id') ?? []]
  209. ],
  210. 'select' => 'id, goods_name, order_id',
  211. 'index' => 'order_id',
  212. 'with' => [
  213. 'order' => function ($query) {
  214. return $query->select('id, order_no');
  215. }
  216. ]
  217. ]);
  218. //查询上级昵称数组
  219. $csv->data = array_map(function ($agent) use ($fields, $order_detail_list, $params, $levels) {
  220. $row = [];
  221. //遍历所选字段
  222. foreach ($fields as $field) {
  223. if (in_array($field, ['created_at'])) {
  224. $row[] = !empty($agent[$field]) ? date('Y-m-d H:i:s', $agent[$field]) : '';
  225. } elseif (in_array($field, ['nickname', 'mobile'])) {
  226. $row[] = !empty($agent['user'][$field]) ? $agent['user'][$field] : '';
  227. } elseif (in_array($field, ['level'])) {
  228. $row[] = !empty($levels[$agent['sharer']['level']]) ? $levels[$agent['sharer']['level']]['name'] : '';
  229. } elseif (in_array($field, ['order_no'])) {
  230. $row[] = isset($order_detail_list[$agent['order_id']]) ? $order_detail_list[$agent['order_id']]['order']['order_no'] : '';
  231. } elseif (in_array($field, ['goods_name'])) {
  232. $row[] = isset($order_detail_list[$agent['order_id']]) ? $order_detail_list[$agent['order_id']]['goods_name'] : '';
  233. } elseif (in_array($field, ['commission'])) {
  234. $row[] = !empty($agent[$field]) ? bcmul($agent[$field], 0.01, 2) : 0;
  235. } elseif (in_array($field, ['commission_type'])) {
  236. $row[] = !empty($agent[$field]) ? WechatVideoShopEnums::getRewardTypeMap($params['mall_id'], $agent[$field]) : '未知类型';
  237. } elseif (in_array($field, ['settle_status'])) {
  238. $row[] = !empty($agent[$field]) ? (WechatVideoShopEnums::SETTLE_STATUS_REMARKS[$agent[$field]] ?? '未知状态') : '未知状态';
  239. } else {
  240. $row[] = !empty($agent[$field]) ? $agent[$field] : '';
  241. }
  242. }
  243. return $row;
  244. }, $list);
  245. $csv->export();
  246. }
  247. $csv->updateDown($download, $params['mall_id']);
  248. return true;
  249. } catch (\Exception $e) {
  250. $download->status = 3;
  251. $res = $download->save();
  252. var_dump($e->getMessage());
  253. self::setStaticError($e->getMessage());
  254. return false;
  255. }
  256. }
  257. }
  258. }