| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- <?php
- namespace addons\Happiness\common\models;
- use addons\MiniMall\common\enums\RedisKeyEnum;
- use addons\TeamLeader\common\models\UserPartitionRelationship;
- use common\components\export\CsvTool;
- use common\enums\DownloadEnum;
- use common\enums\OrderStatusEnum;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\helpers\ArrayHelper;
- use common\helpers\StringHelper;
- use common\models\base\BaseActiveRecord;
- use common\models\common\Download;
- use common\models\user\User;
- use Yii;
- use Exception;
- /**
- * This is the model class for table "qimall_addons_happiness_cashier_order".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $store_id 门店id
- * @property int $user_id 会员id
- * @property string $order_no 订单编号
- * @property int $pay_time 付款时间
- * @property int $pay_status 支付状态
- * @property float $money 应付金额
- * @property float $pay_money 实付金额
- * @property int|null $payment_type 支付类型
- * @property float $amount_received 实收金额
- * @property float $service_money 服务费
- * @property string $remark 备注
- * @property string $extra 额外信息:例如优惠信息,渠道分红,经销分红
- * @property int $status 状态[0禁用 1启用 -1删除]
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- */
- class HappinessCashierOrder extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_happiness_cashier_order}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'store_id', 'user_id', 'pay_status', 'pay_time', 'payment_type', 'status', 'created_at', 'updated_at'], 'integer'],
- [['money', 'pay_money', 'amount_received', 'service_money'], 'number'],
- [['order_no'], 'string', 'max' => 100],
- [['remark', 'extra'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'store_id' => '门店id',
- 'user_id' => '会员id',
- 'order_no' => '订单编号',
- 'pay_status' => '支付状态',
- 'pay_time' => '付款时间',
- 'money' => '应付金额',
- 'pay_money' => '实付金额',
- 'payment_type' => '支付类型',
- 'amount_received' => '实收金额',
- 'service_money' => '服务费',
- 'remark' => '备注',
- 'extra' => '额外信息:例如优惠信息,渠道分红,经销分红',
- 'status' => '状态[0禁用 1启用 -1删除]',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- ];
- }
- public static function fieldsList($is_true=false)
- {
- $data= [
- 'user_id' => '会员ID',
- 'nickname' => '昵称',
- 'pay_time' => '付款时间',
- 'money' => '应付金额',
- 'pay_money' => '实付金额',
- 'extra' => '优惠信息',
- 'amount_received' => '实收金额',
- 'remark' => '备注',
- ];
- if($is_true){
- $data['store_name'] = '门店名称';
- }
- return $data;
- }
- public function getStore()
- {
- return $this->hasOne(HappinessStore::class, ['id' => 'store_id']);
- }
- /**
- * 异步导出至下载中心
- * @DateTime 2021-11-12 16:01:25
- * @param array $data
- * @copyright: Copyright (c) 2020 广东七件事集团
- */
- 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;
- $model = self::find();
- $model->where(['mall_id' => $params['mall_id']]);
- $model->andWhere(['store_id' => $params['store_id']]);
- if (!empty($params['start'])) $model->andWhere(['>=', 'pay_time', strtotime($params['start'])]);
- if (!empty($params['end'])) $model->andWhere(['<=', 'pay_time', strtotime($params['end'])]);
- if (!empty($params['min'])) $model->andWhere(['>=', 'pay_money', $params['min']]);
- if (!empty($params['max'])) $model->andWhere(['<=', 'pay_money', $params['max']]);
- if (!empty($params['keyword'])) {
- $user_list = User::lists(['where' => [['mall_id' => $params['mall_id']], [
- 'or',
- ['like', 'username', $params['keyword']],
- ['like', 'nickname', $params['keyword']],
- ]], 'select' => 'id']);
- $user_ids = array_column($user_list, 'id');
- $model->andWhere(['in', 'user_id', $user_ids]);
- }
- $model->with(['user']);
- $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;
- foreach ($model->batch() as $orders) {
- // $users 是一个包含100条或小于100条用户表数据的数组
- empty($csv->header) && $csv->header = array_keys($orders[0]);
- $csv->data = array_map(function ($order) use ($fields) {
- $row = [];
- foreach ($fields as $field) {
- if ($field == 'nickname') {
- $row[] = $order['user']['nickname'];
- } else if ($field == 'extra') {
- $extra = json_decode($order['extra'], true);
- $extra_content = '';
- $row[] = $extra_content;
- } else if ($field == 'pay_time') {
- $row[] = !empty($order['pay_time']) ? date('Y-m-d H:i:s', $order['pay_time']) : '';
- } else {
- if ((isset($order[$field]))) $row[] = $order[$field];
- }
- }
- return $row;
- }, $orders);
- $csv->export();
- }
- $url = $csv->upload($params['mall_id'], $csv->relative_filename);
- $download->status = 2;
- $download->url = $url;
- $download->num = $csv->line_num;
- $download->size = $csv->file_size;
- $res = $download->save();
- return true;
- } catch (\PHPUnit\Util\Exception $e) {
- $download->status = 3;
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id']);
- }
- public function getDetail()
- {
- return $this->hasMany(HappinessCashierOrderDetail::class, ['cashier_order_id' => 'id']);
- }
- public static function setData(array $params)
- {
- try {
- if (!empty($params['id'])) {
- $model = self::findOne(['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;
- }
- }
-
- //生成收银台订单
- public static function createOrder($data)
- {
- $t = Yii::$app->db->beginTransaction();
- try {
- $params['order_no'] = StringHelper::generateOrderNo('C');
- $order_trade_info[] = [
- 'order_no' => $params['order_no'],
- 'amount' => $data['money'],
- 'title' => $data['title'],
- 'notify_class' => 'addons\Happiness\common\logic\notify\CashierNotify',
- 'order_type' => 'cashier_order',
- ];
- //创建订单
- $params['pay_status'] = OrderStatusEnum::NOT_PAY;
- $params['service_money'] = 0;
- $params['amount_received'] = 0;
- $params['money']=0;
- $params['pay_money'] = $data['money'];
- $params['store_id'] = $data['store_id'];
- $params['mall_id'] = $data['mall_id'];
- $params['user_id'] = $data['user_id'];
- $store_cashier_order = self::setData($params);
- if ($store_cashier_order == false) throw new \Exception(self::getStaticError());
-
- $res = \Yii::$app->services->pay->createTrade($params['mall_id'], $params['user_id'], $order_trade_info);
- if ($res == false) throw new \Exception('生成支付流水失败');
- //生成支付流水
- $arr['transaction'] = $res;
- $t->commit();
- return $arr;
- } catch (\Exception $e) {
- $t->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
-
- }
- public static function pay($params)
- {
- try {
- $trans = Yii::$app->db->beginTransaction();
- $model = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED]]);
- if (empty($model)) throw new \Exception('订单不存在或已删除');
- if ($model->pay_status != OrderStatusEnum::NOT_PAY) throw new Exception('订单已经被处理,无法进行支付');
- if ($model->pay_status == StatusEnum::ENABLED) throw new Exception('订单已支付,请不要重复支付');
-
- $model->payment_type = $params['pay_type'];
- $model->pay_status = StatusEnum::ENABLED;
- $model->amount_received = $model->pay_money;
- $model->pay_time = time();
- $res = $model->save();
- if ($res === false) throw new Exception('修改订单失败:' . $model->getErrorMessage());
- //HappinessCashierOrderDetail::updateAll(['order_status' => OrderStatusEnum::ACCOMPLISH],
- // ['cashier_order_id' => $model['id'], 'order_status' => OrderStatusEnum::NOT_PAY]);
- $trans->commit();
- // 触发订单支付事件,事务提交完成后触发
- //$event = new CashierOrderPaidEvent($model['mall_id']);
- //$order['id'] = $model['id'];
- //$order['operator_id'] = $params['operator_id'] ?? 0;
- //$order['operator_name'] = $params['operator_name'] ?? '';
- //Yii::$app->services->events->dispatch($event, $order, $event->listeners);
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
-
- public static function scanCode($params)
- {
- try {
- $add_user_data = [
- 'mall_id' => $params['mall_id'],
- 'user_id' => $params['user_id'],
- 'store_id' => $params['store_id'],
- ];
- $res = HappinessStoreUser::setData($add_user_data);
- if ($res == false) throw new \Exception('添加店铺会员失败');
- $store = HappinessStore::getOne([['id' => $params['store_id']]]);
- if (empty($params['user']['parent_id']) && !empty($store['scan_code_bind'])) {
- $user_id = $params['user_id'];
- $parent_id = $store['user_id'];
- $param = [
- 'user_id' => $user_id,
- 'parent_id' => $parent_id,
- ];
- $parent = User::getOne([['id' => $parent_id]]);
- $user = $params['user'];
- if($user['id']!=$parent_id){
- $parent_id_arr = UserPartitionRelationship::getParentIdArr($store['user_id']);
- if(!in_array($user['id'],$parent_id_arr)){
- $user->parent_id = $parent_id;
- $user->second_parent_id = $parent['parent_id'] ?? 0;
- $user->third_parent_id = $parent['second_parent_id'] ?? 0;
- $res = $user->save();
- if ($res === false) throw new $user->getErrorMessage();
- $param['is_admin'] = 0;
- $param['ip'] = ArrayHelper::get_client_ip();
- $param['operator_user_id'] = $param['user_id'];
- $res = \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK_CHANGE_RELATIONSHIP, RabbitMqEnum::CHANGE_RELATIONSHIP_QUEUE, $param, UserPartitionRelationship::class, 'changeRelationship');
- if ($res === false) throw new \PHPUnit\Util\Exception(UserPartitionRelationship::getStaticError());
- }
- }
- }
- return ['id' => $store['id'], 'store_name' => $store['store_name'], 'logo_img' => $store['logo_img']];
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function close($params,$force = false)
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- $cond = ['mall_id' => $params['mall_id'], 'status' => [StatusEnum::ENABLED]];
- if (isset($params['id'])) {
- $cond['id'] = $params['id'];
- } else {
- $cond['order_no'] = $params['order_no'];
- }
- isset($params['user_id']) && $cond['user_id'] = $params['user_id'];
- $model = self::findOne($cond);
- if (empty($model)) throw new Exception('订单不存在或已删除');
- // 判断订单是否已经被关闭
- if ($model->status == StatusEnum::DELETE) throw new Exception('订单已经关闭');
- // 判断是否是未支付的订单
- if ($force == false && $model->pay_status != OrderStatusEnum::NOT_PAY) throw new Exception('订单已经被处理');
- $model->status = StatusEnum::DELETE;
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- $trans->commit();
- return true;
- } catch (\Exception $e) {
- $trans->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- }
|