| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <?php
- namespace addons\WechatVideoShop\common\models;
- use addons\WechatVideoShop\common\enums\WechatVideoShopEnums;
- use common\components\export\CsvTool;
- use common\enums\DownloadEnum;
- use common\enums\StatusEnum;
- use common\models\common\Download;
- use common\models\order\OrderDetail;
- use common\models\user\User;
- use common\models\order\Order;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_wechat_video_shop_commission".
- *
- * @property int $id ID
- * @property int $mall_id 商城ID
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 更新事件
- * @property int $user_id 用户
- * @property int $buy_user_id 买家
- * @property int $order_id 订单ID
- * @property string $order_no 订单编号
- * @property float $order_amount 交易金额
- * @property string $remark 佣金备注
- * @property int $settle_status 结算状态
- * @property int $commission 佣金
- * @property int $commission_type 佣金类型1佣金2积分
- * @property int $goods_id 佣金类型1佣金2积分
- * @property int $shop_id 佣金类型1佣金2积分
- */
- class WechatVideoShopCommission extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'qimall_addons_wechat_video_shop_commission';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'status', 'created_at', 'updated_at', 'user_id', 'buy_user_id', 'order_id',
- 'settle_status', 'commission', 'commission_type', 'goods_id', 'shop_id'], 'integer'],
- [['order_amount'], 'number'],
- [['order_no'], 'string', 'max' => 30],
- [['remark'], 'string', 'max' => 1000],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'user_id' => 'User ID',
- 'buy_user_id' => 'Buy User ID',
- 'order_id' => 'Order ID',
- 'order_no' => 'Order No',
- 'order_amount' => 'Order Amount',
- 'remark' => 'Remark',
- 'settle_status' => 'Settle Status',
- 'commission' => 'Commission',
- 'commission_type' => 'Commission Type',
- ];
- }
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id'])->select('id, username, nickname, avatar_url, mobile');
- }
- public function getSharer()
- {
- return $this->hasOne(WechatVideoShopSharer::class, ['user_id' => 'user_id']);
- }
- public function getBuyUser()
- {
- return $this->hasOne(User::class, ['id' => 'buy_user_id'])->select('id, username, nickname, avatar_url, mobile');
- }
- public function getOrder()
- {
- return $this->hasOne(Order::class, ['id' => 'order_id']);
- }
- /**
- * 导出字段
- */
- public static function exportFieldsList($field = null)
- {
- return [
- 'user_id' => '用户ID',
- 'nickname' => '昵称',
- 'mobile' => '手机号',
- 'created_at' => '时间',
- 'commission' => '佣金',
- 'commission_type' => '佣金类型',
- 'settle_status' => '结算状态',
- 'level' => '分销员等级',
- 'order_no' => '订单号',
- 'order_amount' => '订单金额',
- 'goods_name' => '商品名称'
- ];
- }
- public static function fieldsList($field = null)
- {
- $fields = [
- 'user_id' => '用户ID',
- 'nickname' => '昵称',
- 'mobile' => '手机号',
- 'created_at' => '时间',
- 'commission' => '佣金',
- 'commission_type' => '佣金类型',
- 'settle_status' => '结算状态',
- 'level' => '分销员等级',
- 'order_no' => '订单号',
- 'order_amount' => '订单金额',
- 'goods_name' => '商品名称'
- ];
- if ($field === null) return $fields;
- $temp = [];
- foreach ($field as $val) {
- if (isset($fields[$val])) $temp[$val] = $fields[$val];
- }
- return $temp;
- }
- /**
- * @name:
- * @msg: 处理导出数据,这个方法是异步队列处理时使用的,请别修改
- * @param {array} $data
- * @return bool
- */
- public static function exportHandle(array $data)
- {
- $download_id = $data['download_id'] ?? 0;
- $download = Download::findOne(['id' => $download_id]);
- if ($download) {
- try {
- $params = json_decode($download->params, true);
- $filename = $download->name;
- $type = $download->type;
- $fields = $params['fields'];
- $fields_arr = self::fieldsList();
- $have_select_field_arr = [];
- foreach ($fields as $value) {
- $have_select_field_arr[] = $fields_arr[$value];
- }
- $csv = new CsvTool();
- $csv->filename = $filename;
- $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
- $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
- $csv->header = $have_select_field_arr;
- $model = self::find();
- $model->where(['mall_id' => $params['mall_id']]);
- $model->andWhere(['>=', 'status', StatusEnum::DISABLED]);
- $user_id_arr = [];
- //拼接查询条件
- if (!empty($params['level'])) $model->andWhere(['=', 'level', $params['level']]);
- if (!empty($params['keyword'])) {
- $condition['where'] = [[
- 'or',
- ['like', 'nickname', $params['keyword']],
- ['like', 'mobile', $params['keyword']],
- ['id' => $params['keyword']]
- ]];
- $user_list = User::lists($condition);
- $user_id_arr = array_column($user_list, 'id');
- }
- if ($user_id_arr) {
- $model->andWhere(['in', 'user_id', $user_id_arr]);
- }
- if ($params['date_start']) {
- $model->andWhere(['>=', 'created_at', $params['date_start']]);
- }
- if ($params['date_end']) {
- $model->andWhere(['<=', 'created_at', $params['date_end']]);
- }
- if ($params['order_no']) {
- $order_list = Order::lists([
- 'where' => [
- ['mall_id' => $params['mall_id']],
- ['like', 'order_no', $params['order_no']]
- ],
- 'select' => 'id',
- ]);
- $model->andWhere(['order_id' => array_column($order_list, 'id') ?? []]);
- }
- $model->with(['user', 'sharer' => function($query) use($params) {
- $query->andWhere(['mall_id' => $params['mall_id']]);
- }]);
- $levels = WechatVideoShopLevel::lists([
- 'where' => [
- ['mall_id' => $params['mall_id']],
- ],
- 'index' => 'level'
- ]);
- //遍历组装数据
- foreach ($model->asArray()->batch() as $list) {
- $order_detail_list = OrderDetail::lists([
- 'where' => [
- ['mall_id' => $params['mall_id']],
- ['order_id' => array_column($list, 'order_id') ?? []]
- ],
- 'select' => 'id, goods_name, order_id',
- 'index' => 'order_id',
- 'with' => [
- 'order' => function ($query) {
- return $query->select('id, order_no');
- }
- ]
- ]);
- //查询上级昵称数组
- $csv->data = array_map(function ($agent) use ($fields, $order_detail_list, $params, $levels) {
- $row = [];
- //遍历所选字段
- foreach ($fields as $field) {
- if (in_array($field, ['created_at'])) {
- $row[] = !empty($agent[$field]) ? date('Y-m-d H:i:s', $agent[$field]) : '';
- } elseif (in_array($field, ['nickname', 'mobile'])) {
- $row[] = !empty($agent['user'][$field]) ? $agent['user'][$field] : '';
- } elseif (in_array($field, ['level'])) {
- $row[] = !empty($levels[$agent['sharer']['level']]) ? $levels[$agent['sharer']['level']]['name'] : '';
- } elseif (in_array($field, ['order_no'])) {
- $row[] = isset($order_detail_list[$agent['order_id']]) ? $order_detail_list[$agent['order_id']]['order']['order_no'] : '';
- } elseif (in_array($field, ['goods_name'])) {
- $row[] = isset($order_detail_list[$agent['order_id']]) ? $order_detail_list[$agent['order_id']]['goods_name'] : '';
- } elseif (in_array($field, ['commission'])) {
- $row[] = !empty($agent[$field]) ? bcmul($agent[$field], 0.01, 2) : 0;
- } elseif (in_array($field, ['commission_type'])) {
- $row[] = !empty($agent[$field]) ? WechatVideoShopEnums::getRewardTypeMap($params['mall_id'], $agent[$field]) : '未知类型';
- } elseif (in_array($field, ['settle_status'])) {
- $row[] = !empty($agent[$field]) ? (WechatVideoShopEnums::SETTLE_STATUS_REMARKS[$agent[$field]] ?? '未知状态') : '未知状态';
- } else {
- $row[] = !empty($agent[$field]) ? $agent[$field] : '';
- }
- }
- return $row;
- }, $list);
- $csv->export();
- }
- $csv->updateDown($download, $params['mall_id']);
- return true;
- } catch (\Exception $e) {
- $download->status = 3;
- $res = $download->save();
- var_dump($e->getMessage());
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
- }
|