| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?php
- namespace addons\CloudStock\common\models;
- use common\enums\PayTypeEnum;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\goods\Goods;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\user\User;
- use common\components\export\CsvTool;
- use common\enums\DownloadEnum;
- use common\models\common\Download;
- use Yii;
- /**
- * This is the model class for table "{{%addons_cloud_stock_buying_order".
- *
- * @property int $id 自增id
- * @property int $mall_id 商城id
- * @property int $user_id 买货用户id
- * @property float $after_distribution_price 商品的实际支付金额扣除其他分润后的金额;未扣奖励前货款;
- * @property int $order_id 订单id;order表
- * @property int $order_detail_id 订单详情id;order_detail表
- * @property int $goods_id 商品id
- * @property int $num 商品数量
- * @property int $is_return 是否有退库存
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- * @property int $payment_type
- */
- class CloudStockBuyingOrder extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_cloud_stock_buying_order}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'goods_id', 'num'], 'required'],
- [['mall_id', 'user_id', 'order_id', 'order_detail_id', 'goods_id', 'num', 'is_return', 'status', 'created_at', 'updated_at','payment_type'], 'integer'],
- [['after_distribution_price'], 'number'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '自增id',
- 'mall_id' => '商城id',
- 'user_id' => '买货用户id',
- 'after_distribution_price' => '商品的实际支付金额扣除其他分润后的金额;未扣奖励前货款;',
- 'order_id' => '订单id;order表',
- 'order_detail_id' => '订单详情id;order_detail表',
- 'goods_id' => '商品id',
- 'num' => '商品数量',
- 'is_return' => '是否有退库存',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'payment_type' => '支付方式',
- ];
- }
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id'])->select(['id', 'nickname', 'avatar_url', 'mobile']);
- }
- public function getOrder()
- {
- return $this->hasOne(Order::class, ['id' => 'order_id'])->select(['order_no', 'order_status', 'id','is_feedback','pay_money','payment_type']);
- }
- public function getOrderDetail()
- {
- return $this->hasOne(OrderDetail::class, ['id' => 'order_detail_id'])->select(['price', 'is_feedback', 'id']);
- }
- public function getGoods()
- {
- return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select(['id', 'goods_name', 'cover_pic']);
- }
- public static function setData(array $params)
- {
- try {
- if(!empty($params['id'])){
- $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]]);
- if (empty($model)) throw new \Exception('服务不存在或已删除');
- }else{
- $model = new self();
- $model->loadDefaultValues();
- }
- if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- return $model;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 导出字段数组
- * @Author: hua
- * @DateTime: 2021/10/11 18:06
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \string[][]
- */
- public static function fieldsLists($field = null){
- $fields = [
- 'order_id' => '订单ID',
- 'user_id' => '买家用户id',
- 'nickname' => '用户昵称',
- 'goods_name' => '商品名称',
- 'goods_num' => '购买商品数量',
- 'created_at' => '订单创建时间',
- 'order_no' => '订单编号',
- 'pay_money' => '实际支付金额',
- 'after_distribution_price' => '货款金额',
- 'order_status' => '订单状态',
- 'payment_type' => '支付方式',
- ];
- if ($field === null) return $fields;
- $temp = [];
- foreach ($field as $val) {
- if (isset($fields[$val])) $temp[$val] = $fields[$val];
- }
- return $temp;
- }
- /**
- * 导出
- */
- public static function exportOrder(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 = isset($params['fields']) ? $params['fields'] : null;
- $exportField = self::fieldsLists($fields);
- $exportField = array_keys($exportField);
- $where[] = ['=', 'mall_id', $params['mall_id']];
- $where[] = ['in', 'status', [StatusEnum::DISABLED, StatusEnum::ENABLED]];
- if (!empty($params['keyword'])) {
- $user_list = User::find()->where(['or',
- ['id' => $params['keyword']],
- // ['mobile' => $params['keyword']],
- ['nickname' => $params['keyword']]])->select('id')->asArray()->all();
- $user_id_arr = array_column($user_list, 'id');
- $where[] = ['user_id' => $user_id_arr];
- }
- if (!empty($params['goods_name'])) {
- $goods_list = Goods::lists(['where' => [['like', 'goods_name', $params['goods_name']]]]);
- if ($goods_list) {
- $goods_id_arr = array_column($goods_list, 'id');
- $where[] = ['goods_id' => $goods_id_arr];
- }
- }
- if (!empty($params['payment_type'])) $where[] = ['=', 'payment_type', $params['payment_type'] ];
- if (!empty($params['start'])) $where[] = ['>=', 'created_at', strtotime($params['start'])];
- if (!empty($params['end'])) $where[] = ['<=', 'created_at', strtotime($params['end'])];
- if (!empty($params['id'])) {
- $where[] = ['=', 'id', $params['id']];
- }
- $wheres['order'] = 'id desc';
- $wheres['with'] = ['user', 'order', 'orderDetail', 'goods'];
- $wheres['where'] = $where;
- $model = self::find();
- self::paramPack($wheres, $model);
- $list = [];
- $exportheader = [];
- $isSetHeader = true;
- //所有团队部分修改
- $i = 1;
- $page_size = 1000;
- $payment_type_list= PayTypeEnum::getMap();
- while ($log_list = $model->asArray()->offset(($i - 1) * $page_size)->limit($page_size)->all()){
- foreach ($log_list as $l){
- $temp = [];
- //按顺序组装
- if(in_array('id', $fields)) {
- $temp['id'] = $l['id'];
- $isSetHeader && array_push($exportheader, '订单ID');
- }
- if(in_array('user_id', $fields)) {
- $temp['user_id'] = $l['user_id'];
- $isSetHeader && array_push($exportheader, '买家用户id');
- }
- if(in_array('nickname', $fields)) {
- $temp['nickname'] = $l['user']['nickname'];
- $isSetHeader && array_push($exportheader, '用户昵称');
- }
- if(in_array('goods_name', $fields)) {
- $temp['goods_name'] = $l['goods']['goods_name'];
- $isSetHeader && array_push($exportheader, '商品名称');
- }
- if(in_array('num', $fields)) {
- $temp['num'] = $l['num'];
- $isSetHeader && array_push($exportheader, '购买商品数量');
- }
- if(in_array('created_at', $fields)) {
- $temp['created_at'] = date('Y-m-d H:i:s', $l['created_at']);
- $isSetHeader && array_push($exportheader, '订单创建时间');
- }
- if(in_array('order_no', $fields)) {
- $temp['order_no'] = $l['order']['order_no'];
- $isSetHeader && array_push($exportheader, '订单编号');
- }
- if(in_array('pay_money', $fields)) {
- $temp['pay_money'] = $l['order']['pay_money'];
- $isSetHeader && array_push($exportheader, '实际支付金额');
- }
- if(in_array('after_distribution_price', $fields)) {
- $temp['after_distribution_price'] = $l['after_distribution_price'];
- $isSetHeader && array_push($exportheader, '货款金额');
- }
- if(in_array('order_status', $fields)) {
- switch($l['order']['order_status']) {
- case 0:
- $temp['order_status'] = '待支付';
- break;
- case 1:
- $temp['order_status'] = '待发货(已付款)';
- break;
- case 2:
- $temp['order_status'] = '已发货';
- break;
- case 3:
- $temp['order_status'] = '已收货';
- break;
- case 4:
- $temp['order_status'] = '已完成';
- break;
- default:
- $temp['order_status'] = '';
- };
- $isSetHeader && array_push($exportheader, '订单状态');
- }
- if(in_array('payment_type',$fields)){
- $isSetHeader && array_push($exportheader, '支付方式');
- $temp['payment_type']=$payment_type_list[$l['order']['payment_type']]??'';
- }
- $list[] = $temp;
- $isSetHeader = false;
- }
- $i++;
- }
- if (!empty($list)) {
- $csv = new CsvTool();
- $csv->filename = $filename;
- $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
- $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
- $csv->header = $exportheader;
- $csv->data = $list;
- $csv->export();
- $csv->updateDown($download, $params['mall_id']);
- }
- }catch (\Exception $e) {
- $download->status = 3;
- $res = $download->save();
- var_dump($e->getMessage().$e->getFile().$e->getLine());
- self::setStaticError($e->getMessage());
- return $e->getMessage();
- }
- }
- }
- }
|