| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490 |
- <?php
- namespace addons\CloudStock\common\models;
- use common\enums\PayTypeEnum;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\common\PaymentTrade;
- use common\models\common\ScoreLog;
- use common\models\goods\Goods;
- use common\models\order\Order;
- use common\models\user\User;
- use common\components\export\CsvTool;
- use common\enums\DownloadEnum;
- use common\models\common\Download;
- use addons\CloudStock\common\enums\CloudStockEnum;
- use Exception;
- use Yii;
- /**
- * This is the model class for table "{{%addons_cloud_stock_fill_order".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $user_id 补货代理商用户id
- * @property int $trade_id 支付流水记录 id
- * @property float $pay_money 支付金额
- * @property string $order_no 补货订单编号
- * @property string $trade_no 支付流水号
- * @property int $is_pay 是否支付
- * @property int $goods_id 商品id
- * @property int $num 补货商品数量
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- * @property int $payment_type
- * @property int $price_difference_order_id 价差礼包订单id
- * @property int $price_difference_order_detail_id 价差礼包订单详情id
- * @property int $batch_fill_order_id 批量补货订单id
- */
- class CloudStockFillOrder extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_cloud_stock_fill_order}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'order_no', 'goods_id'], 'required'],
- [['mall_id', 'user_id', 'trade_id', 'is_pay', 'goods_id', 'num', 'status', 'created_at', 'updated_at', 'payment_type',
- 'price_difference_order_id', 'price_difference_order_detail_id', 'batch_fill_order_id'], 'integer'],
- [['pay_money'], 'number'],
- [['order_no', 'trade_no'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'user_id' => '补货代理商用户id',
- 'trade_id' => '支付流水记录 id',
- 'pay_money' => '支付金额',
- 'order_no' => '补货订单编号',
- 'trade_no' => '支付流水号',
- 'is_pay' => '是否支付',
- 'goods_id' => '商品id',
- 'num' => '补货商品数量',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'payment_type' => '支付方式',
- 'batch_fill_order_id' => '批量补货订单ID',
- ];
- }
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id'])->select(['id', 'nickname', 'avatar_url', 'mobile', 'parent_id']);
- }
- public function getTrade()
- {
- return $this->hasOne(Trande::class, ['id' => 'trade_id'])->select(['id', 'nickname', 'avatar_url', 'mobile']);
- }
- public function getScore()
- {
- return $this->hasOne(ScoreLog::class, ['source_table_id' => 'trade_id'])->select(['change_num']);
- }
- public function getGoods()
- {
- return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select(['id', 'goods_name', 'cover_pic', 'price']);
- }
- public static function setData(int $mall_id, array $data)
- {
- if (!empty($data['id'])) {
- $model = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['id']]);
- if (empty($model)) {
- self::$static_error = '数据异常';
- return false;
- }
- } else {
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $mall_id;
- }
- foreach ($data as $key => $val) {
- $model->$key = $val;
- }
- // dd($model);
- if (!$model->save()) {
- self::$static_error = '保存数据失败:' . $model->getErrorMessage();
- return false;
- }
- return $model;
- }
- public static function check($params): bool
- {
- $order_no_arr = $params['order_no_arr'];
- $payment_order_list = $params['payment_order_list'];
- foreach($order_no_arr as $order_no){
- if(substr($order_no,0,3) == 'CSP'){
- $counts = CloudStockAgentOrder::find()->where(['order_no'=>$order_no_arr,'send_status'=>CloudStockEnum::PICK_ORDER_STATUS3])->count();
- }else{
- $counts = self::find()->where(['order_no'=>$order_no_arr,'is_pay'=>0])->count();
- }
- }
- if ($counts != count($payment_order_list)) {
- self::$static_error = '补货订单不一致';
- return false;
- }
- return true;
- }
- /**
- * 导出字段数组
- * @Author: hua
- * @DateTime: 2021/10/11 18:06
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \string[][]
- */
- public static function fieldsLists($field = null){
- $fields = [
- 'id' => '订单ID',
- 'user_id' => '代理商用户id',
- 'nickname' => '代理商用户昵称',
- 'goods_name' => '商品名称',
- 'fill_parent' => '补货上级',
- 'num' => '补货数量',
- 'created_at' => '订单创建时间',
- 'order_no' => '订单编号',
- 'trade_no' => '支付流水号',
- 'pay_money' => '实际支付金额',
- 'payment_type' => '支付方式',
- 'is_pay' => '是否支付',
- ];
- 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]];
- $where[] = ['=', 'price_difference_order_id', 0]; // 只显示补货订单
- 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 (isset($params['is_pay']) && is_numeric($params['is_pay'])) {
- $where[] = ['=', 'is_pay', $params['is_pay']];
- }
- 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', '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()){
- if(in_array('payment_type',$fields)){
- $trade_nos = array_column($log_list,'trade_no');
- $payment_trade_arr = PaymentTrade::lists(['where'=>[['out_trade_no'=>$trade_nos]],'index'=>'out_trade_no','select'=>'out_trade_no,pay_type as payment_type']);
- $fill_order_ids = [];
- foreach ($log_list as $val){
- if(empty($val['trade_no'])){
- $fill_order_ids[]=$val['id'];
- }
- }
- if(!empty($fill_order_ids)){
- $fill_order_relations = CloudStockFillOrderRelation::lists(['where'=>[['fill_order_id'=>$fill_order_ids]],'index'=>'fill_order_id','select'=>'fill_order_id,order_id']);
- $order_ids = array_column($fill_order_relations,'order_id');
- $order_arr = Order::lists(['where'=>[['id'=>$order_ids]],'index'=>'id','select'=>'id,payment_type']);
- }
- }
- // 得到补货上级
- [
- 'deduction_user_id_arr' => $deduction_user_id_arr,
- 'user_list' => $user_list
- ] = self::getFillParentList($params['mall_id'], array_column($log_list, 'id'));
- 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('fill_parent', $fields)) { // 补货上级
- // 得到补货上级
- $fromUser = [];
- if (isset($deduction_user_id_arr[$l['id']])) {
- $deductionUserIds = array_unique($deduction_user_id_arr[$l['id']]);
- foreach ($deductionUserIds as $uid) {
- if (!isset($user_list[$uid])) {
- continue;
- }
- $fromUser[] = sprintf(
- '补货用户ID:"%s" 补货用户昵称:"%s"',
- $uid,
- $user_list[$uid]['nickname']
- );
- }
- }
- $temp['fill_parent'] = implode(' ,', $fromUser);
- $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_no'];
- $isSetHeader && array_push($exportheader, '订单编号');
- }
- if(in_array('trade_no', $fields)) {
- $temp['trade_no'] = $l['trade_no'];
- $isSetHeader && array_push($exportheader, '支付流水号');
- }
- if(in_array('pay_money', $fields)) {
- $temp['pay_money'] = $l['pay_money'];
- $isSetHeader && array_push($exportheader, '实际支付金额');
- }
- if(in_array('is_pay', $fields)) {
- if($l['is_pay'] == 1){
- $temp['is_pay'] = '已支付';
- }else{
- $temp['is_pay'] = '未支付';
- }
- $isSetHeader && array_push($exportheader, '是否已支付');
- }
- if(in_array('payment_type',$fields)){
- $isSetHeader && array_push($exportheader, '支付方式');
- if(!empty($l['trade_no'])){
- $payment_type = $payment_trade_arr[$l['trade_no']]['payment_type']??'';
- }else{
- if(isset($fill_order_relations)&&isset($fill_order_relations[$l['id']])){
- $order_id = $fill_order_relations[$l['id']]['order_id'];
- if(isset($order_arr)&&isset($order_arr[$order_id])){
- $payment_type = $order_arr[$order_id]['payment_type'];
- }
- }
- }
- $temp['payment_type'] = '';
- if(isset($payment_type)){
- $temp['payment_type']=$payment_type_list[$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();
- }
- }
- }
- public function fixOrder(){
- $i = 1;
- $page_size = 1000;
- while ($list = CloudStockBuyingOrder::find()->where(['payment_type' => 0])
- ->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
- $order_ids = array_column($list, 'order_id');
- $order_list = Order::lists([
- 'where' => [
- ['id' => $order_ids]
- ],
- 'index' => 'id'
- ]);
- foreach ($list as $buy_order) {
- if (isset($order_list[$buy_order['order_id']])) {
- $order = $order_list[$buy_order['order_id']];
- $buy_order->payment_type = $order['payment_type'];
- $res = $buy_order->save();
- var_dump($res);
- }
- }
- $i++;
- }
- $i = 1;
- $page_size = 1000;
- while ($list = CloudStockFillOrder::find()->where(['payment_type' => 0])
- ->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
- $trade_ids = array_column($list, 'trade_id');
- $paymentTrade_list = \common\models\common\PaymentTrade::lists([
- 'where' => [
- ['id' => $trade_ids]
- ],
- 'index' => 'id'
- ]);
- foreach ($list as $fill_order) {
- if (isset($paymentTrade_list[$fill_order['trade_id']])) {
- $paymentTrade = $paymentTrade_list[$fill_order['trade_id']];
- $fill_order->payment_type = $paymentTrade['pay_type'];
- $res = $fill_order->save();
- var_dump($res);
- }
- }
- $i++;
- }
- }
- /**
- * 获取订单的补货上级
- *
- * @param int $mallId
- * @param array $order_id_arr
- *
- * @return array
- */
- private static function getFillParentList(int $mallId, array $order_id_arr)
- {
- $deduction_list = CloudStockDeductionDetail::lists([
- 'where' => [
- ['mall_id' => $mallId],
- ['order_id' => $order_id_arr],
- ['order_type' => CloudStockEnum::STOCK_ORDER_TYPE_REPLENISH],
- ['changes_type' => CloudStockEnum::CHANGE_TYPE_DEDUTION_FILL]
- ],
- 'select' => 'user_id,order_id',
- ]
- );
- $deduction_user_ids = array_column($deduction_list, 'user_id');
- $deduction_user_id_arr = [];
- foreach ($deduction_list as $val) {
- $deduction_user_id_arr[$val['order_id']][] = $val['user_id'];
- }
- $user_list = User::lists([
- 'where' => [
- ['id' => $deduction_user_ids]
- ],
- 'index' => 'id',
- 'select' => 'id,nickname,mobile,avatar_url',
- ]
- );
- $user_list[0] = [
- 'id' => 0,
- 'nickname' => '平台',
- 'mobile' => '',
- 'avatar_url' => '',
- ];
- return [
- 'deduction_user_id_arr' => $deduction_user_id_arr,
- 'user_list' => $user_list
- ];
- }
- public static function getOrderByNo($params) {
- try {
- $info = self::getOne([
- ['mall_id' => $params['mall_id']],
- ['order_no' => $params['order_no']]
- ], true, ['user', 'goods']);
- $data = [
- 'user' => $info['user'],
- 'pay_money' => $info['pay_money'],
- 'order_no' => $info['order_no'],
- 'id' => $info['id'],
- ];
- $data['product'][] = [
- 'pic_url' => $info['goods']['cover_pic'],
- 'goods_name' => $info['goods']['goods_name'],
- 'goods_attr_name' => '',
- 'price' => $info['goods']['price'],
- 'num' => $info['num'],
- ];
- return $data;
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- }
|