| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\constants\Kefu;
- use app\models\Coupon;
- use app\models\Cat;
- use app\models\Goods;
- use app\models\GoodsCat;
- use app\models\ActivityRebateOrderN;
- use app\models\ActivityRebateOrderNLog;
- use yii\base\Model;
- use app\models\Option;
- use app\constants\OptionSetting;
- use app\models\Order;
- use app\models\Mch;
- use app\models\MchAudit;
- use app\models\MchCommonCat;
- use app\models\User;
- use app\models\SaasUser;
- use app\models\Admin;
- use app\models\Cash;
- use app\models\MchCat;
- use app\models\AccountLog;
- use app\models\MchAccountLog;
- use app\utils\OrderNo;
- use app\models\District;
- class MchForm extends Model {
- public $store_id;
- public $mch_id;
- public $saas_id;
- public $user_id;
- public $user_name;
- public $username;
- public $password;
- public $realname;
- public $tel;
- public $mch_common_cat_id;
- public $wechat_name;
- public $is_open;
- public $logo;
- public $transfer_rate;
- public $sort;
- public $province_id;
- public $city_id;
- public $district_id;
- public $address;
- public $longitude;
- public $latitude;
- public $begin_time;
- public $end_time;
- public $saas_user_name;
- public $mch_audit;
- public $mobile;
- public $is_show;
- public $id;
- public $ids;
- public $name;
- public $status;
- public $order_no;
- public $goods_id;
- public $goods_name;
- public $is_delete;
- public $parent_id;
- public $pic_url;
- public $reason;
- public $home_type;
- public $lng;
- public $lat;
- public $dis;
- public $orderByDis;
- public $orderByAi;
- public $form;
- public $kefu_id;
- public $kefu_auto_reply_img;
- public $kefu_auto_reply_text;
- public $brands_id;
- public function rules() {
- return [
- [['status', 'id','brands_id'], 'integer'],
- [['ids', 'name'], 'string'],
- [['store_id', 'mch_id', 'goods_id', 'goods_name', 'saas_id', 'user_id', 'is_delete', 'user_name', 'order_no', 'mobile'], 'safe'],
- [['realname', 'tel', 'mch_common_cat_id', 'wechat_name', 'is_open', 'logo', 'transfer_rate',
- 'sort', 'province_id', 'city_id', 'district_id', 'address', 'longitude', 'latitude',
- 'username', 'password', 'mch_audit', 'is_show', 'begin_time', 'end_time', 'saas_user_name',
- 'lng', 'lat', 'dis', 'orderByDis', 'orderByAi',
- 'form',
- 'parent_id', 'pic_url', 'reason', 'home_type', 'kefu_id', 'kefu_auto_reply_img', 'kefu_auto_reply_text'],
- 'safe'
- ],
- ];
- }
- public function init() {
- parent::init();
- if (empty($this->store_id)) {
- $this->store_id = get_store_id();
- }
- }
- public function mchSetting() {
- $setting = json_decode(Option::get(OptionSetting::MCH_SETTING, $this->store_id, 'store', '{}')['value'], true);
- $ret = [
- 'mch_reg_form' => $setting['mch_reg_form'] ?? [],
- 'mch_reg_text' => $setting['mch_reg_text'] ?? '',
- 'mch_goods_audit' => $setting['mch_goods_audit'] ?? 0,
- 'mch_order_notice' => $setting['mch_order_notice'] ?? 0,
- 'mch_cash_type' => $setting['mch_cash_type'] ?? [0],
- 'mch_cash_rate' => $setting['mch_cash_rate'] ?? 0,
- 'show_mch_mobile' => !isset($setting['show_mch_mobile']) ? 1 : $setting['show_mch_mobile'],
- 'show_mch_address' => !isset($setting['show_mch_address']) ? 1 : $setting['show_mch_address'],
- ];
- if(!is_array($ret['mch_cash_type'])){
- $ret['mch_cash_type'] = [$ret['mch_cash_type']];
- }
- return $ret;
- }
- public function mchSettingSave($conf = []) {
- Option::set(OptionSetting::MCH_SETTING, json_encode($conf), $this->store_id, 'store');
- }
- public function mchList() {
- try {
- $is_delete = 0;
- if ($this->is_delete == 1) {
- $is_delete = 1;
- }
- $query = Mch::find()->with(['mchBrands'])->where(['is_delete' => $is_delete, 'store_id' => $this->store_id]);
-
- if (!is_null($this->is_open) && $this->is_open > -1) {
- $query->andWhere(['is_open' => $this->is_open]);
- }
- if (!is_null($this->mch_common_cat_id) && $this->mch_common_cat_id > 0) {
- $query->andWhere(['mch_common_cat_id' => $this->mch_common_cat_id]);
- }
- if ($this->mch_id > 0) {
- $query->andWhere(['id' => $this->mch_id]);
- }
- if (!empty($this->name)) {
- $query->andWhere(['like', 'name', trim($this->name)]);
- }
- if (!empty($this->realname)) {
- $query->andWhere(['like', 'realname', trim($this->realname)]);
- }
- if (!empty($this->tel)) {
- $query->andWhere(['like', 'tel', trim($this->tel)]);
- }
- if (!empty($this->begin_time)) {
- $query->andWhere(['>=', 'created_at', strtotime($this->begin_time)]);
- }
- if (!empty($this->end_time)) {
- $query->andWhere(['<=', 'created_at', strtotime($this->end_time)]);
- }
- if($this->brands_id > -1){
- $query->andWhere(['brands_id' => $this->brands_id]);
- }
- if($this->district_id > 0){
- $query->andWhere(['district_id' => $this->district_id]);
- }
- $query->select('*');
- $query->orderBy('id DESC');
- if($this->orderByAi){
- $query->orderBy('account_money_total DESC, sort DESC, id ASC');
- }
-
-
- if($this->lng && $this->lat){
- $queryDis = 'ROUND(st_distance_sphere(point('. $this->lng .', '. $this->lat .'), point(longitude, latitude)), 2)';
- if($this->dis){
- $query->andWhere($queryDis . " < :dis", [':dis' => $this->dis]);
- }
- $query->addSelect([$queryDis . ' dis']);
- // $query->andWhere(['AND', '`longitude` > 0 AND `latitude` > 0']);
- if($this->orderByDis){
- $query->orderBy('dis ASC, sort DESC');
- }
- }
- $pagination = pagination_make($query);
- $cats = MchCommonCat::find()->where(['store_id' => $this->store_id])->indexBy('id')->all();
- foreach ($pagination['list'] as &$item) {
- $item['mch_common_cat_name'] = $cats[$item['mch_common_cat_id']] ? $cats[$item['mch_common_cat_id']]['name'] : '-';
- $saasUser = SaasUser::findOne([$item['saas_user_id']]);
- $item['saas_user'] = $saasUser;
- $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
- $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
-
- $admin = Admin::findOne(['type_id' => $item['id'], 'type' => Admin::ADMIN_TYPE_MCH, 'is_delete' => 0]);
- if($admin->store_id != $item['store_id']){
- $admin->store_id = $item['store_id'];
- $admin->save();
- }
- $item['admin'] = $admin;
-
- if(isset($item['dis'])){
- $item['dis_str'] = $item['dis'] > 1000 ? (round($item['dis'] / 1000, 2) . 'km') : (round($item['dis'], 0) . 'm');
- }
-
- $goods_count = Goods::find()->where(['mch_id' => $item['id'], 'is_delete' => 0])->count();
- $item['goods_count'] = $goods_count ?: 0;
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $pagination,
- // 'q' => $query->createCommand()->getRawSql(),
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function diyMchSelectList(){
- $query = Mch::find()->where(['store_id' => $this->store_id,'is_delete' => 0])
- ->select('id,name,logo,mch_common_cat_id');
- if($this->name){
- $query->andWhere(['like','name', $this->name]);
- }
- if($this->mch_common_cat_id){
- $query->andWhere(['mch_common_cat_id' => $this->mch_common_cat_id]);
- }
- $result = pagination_make($query);
- foreach($result['list'] as &$item){
- // 搜索入住商商品6个
- $item['goods_list'] = Goods::find()->where(['mch_id' => $item['id'],'is_delete'=>0,'status' => 1])->select('id,name,cover_pic,original_price,price')->limit(6)->asArray()->all();
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $result,
- ];
- }
- public function mchSelectList() {
- $list = Mch::find()->where(['store_id' => $this->store_id])
- ->select('id,name,logo,mch_common_cat_id,is_delete')->indexBy('id')->asArray()->all();
- foreach($list as &$item){
- $item['name'] = $item['id'] . '.' . $item['name'] . ($item['is_delete'] ? '(已删)' : '');
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $list,
- ];
- }
- public function mchInfo() {
- $mch = Mch::findOne($this->mch_id);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $mch,
- 'dis' => \app\utils\Tools::getDistance($mch->latitude, $mch->longitude, $this->lat, $this->lng),
- 'homeTypes' => $this->mchHomeList(),
- ];
- }
- public function mchHomeList() {
- $urlPre = '';
- if (\Yii::$app instanceof \yii\web\Application){
- $urlPre = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl;
- }
- $homeTypes = [
- $urlPre . '/web/v1/statics/images/mch-home/home1.png',
- $urlPre . '/web/v1/statics/images/mch-home/home2.png',
- $urlPre . '/web/v1/statics/images/mch-home/home3.png',
- ];
- return $homeTypes;
- }
- public function mchHomeSave() {
- $mch = Mch::findOne(['id' => $this->id]);
- $mch->home_type = (int)$this->home_type;
- if (!$mch->save()) {
- \Yii::error([__METHOD__, $mch->attributes]);
- throw new \Exception('保存失败。' . array_shift($mch->getFirstErrors()));
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功!',
- ];
- }
- public function mchSave() {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $mch = $this->id ? Mch::findOne(['id' => $this->id, 'store_id' => $this->store_id]) : new Mch();
- if (empty($mch)) {
- throw new \Exception('参数错误' . $this->id);
- }
- if (isset($this->store_id)) {
- $mch->store_id = $this->store_id;
- }
- if (isset($this->user_id)) {
- $user = User::findOne($this->user_id);
- if (empty($user)) {
- throw new \Exception('用户id错误。' . $this->user_id);
- }
- $saasUser = SaasUser::findOne(['mobile' => $user->binding]);
- $mch->user_id = $this->user_id;
- $mch->saas_user_id = $saasUser->id;
- }
- if (isset($this->realname)) {
- $mch->realname = $this->realname;
- }
- if (isset($this->tel)) {
- $mch->tel = $this->tel;
- }
- if (isset($this->mch_common_cat_id)) {
- $mch->mch_common_cat_id = $this->mch_common_cat_id;
- }
- if (isset($this->wechat_name)) {
- $mch->wechat_name = $this->wechat_name;
- }
- if (isset($this->name)) {
- $mch->name = $this->name;
- }
- if (isset($this->is_open)) {
- $mch->is_open = $this->is_open;
- }
- if (isset($this->logo)) {
- $mch->logo = $this->logo;
- }
- if (isset($this->transfer_rate)) {
- $mch->transfer_rate = $this->transfer_rate;
- }
- if (isset($this->sort)) {
- $mch->sort = $this->sort;
- }
- if (isset($this->brands_id) && $this->brands_id > 0) {
- $mch->brands_id = $this->brands_id;
- }
- $mch->service_tel = '';
- if (isset($this->province_id) && $this->province_id) {
- $district = District::find()->where(['id' => [$this->province_id, $this->city_id, $this->district_id]])->select('name')->column();
- $district = implode('', $district);
- $mch->district = $district;
- $mch->province_id = $this->province_id;
- $mch->city_id = $this->city_id;
- $mch->district_id = $this->district_id;
- $mch->address = $this->address;
- $mch->longitude = $this->longitude;
- $mch->latitude = $this->latitude;
- } else {
- $mch->province_id = 0;
- $mch->city_id = 0;
- $mch->district_id = 0;
- $mch->address = '';
- }
- if (!$mch->save()) {
- \Yii::error([__METHOD__, $mch->attributes]);
- throw new \Exception('保存失败。' . array_shift($mch->getFirstErrors()));
- }
- $this->mchAdminSave($this->id ? 0 : 1, $mch, trim($this->username), trim($this->password), $saasUser->id);
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功!',
- 'data' => $mch,
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function mchAdminSave($isAdd, $mch, $username, $password, $saas_user_id) {
- $mch_id = $mch->id;
- if ($isAdd) {
- if (empty($username)) {
- throw new \Exception('请输入登陆账户');
- }
- $is = Admin::find()->where(['username' => $username, 'is_delete' => 0])->one();
- if ($is) {
- throw new \Exception('账户名称已经存在!请更换' . $username);
- }
- $admin = new Admin();
- $admin->access_token = \Yii::$app->security->generateRandomString();
- $admin->username = $username;
- $admin->name = $username;
- $admin->store_id = $mch->store_id;
- $admin->type_id = $mch_id;
- $admin->type = Admin::ADMIN_TYPE_MCH;
- } else {
- if (empty($password)) {
- return;
- }
- $admin = Admin::findOne(['type_id' => $mch_id, 'type' => Admin::ADMIN_TYPE_MCH, 'username' => $username, 'is_delete' => 0]);
- if (empty($admin)) {
- throw new \Exception('账户不存在');
- }
- }
- $admin->password = \Yii::$app->security->generatePasswordHash($password ?: '123456');
- $admin->saas_user_id = $saas_user_id;
- if (!$admin->save()) {
- throw new \Exception('账户保存失败!' . $username . array_shift($admin->getFirstErrors()));
- }
- }
- public function mchOpen() {
- try {
- $is_open = $this->is_open;
- $id = $this->id;
- if (!is_array($id)) {
- $id = explode(',', $id);
- }
- foreach ($id as $item) {
- $mch = Mch::findOne($item);
- if (!$mch) {
- throw new \Exception('参数错误' . $item);
- }
- $mch->is_open = $is_open;
- if (!$mch->save()) {
- throw new \Exception('保存失败。' . $item . array_shift($mch->getFirstErrors()));
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function mchDel() {
- try {
- $id = $this->id;
- if (!is_array($id)) {
- $id = explode(',', $id);
- }
- foreach ($id as $item) {
- $mch = Mch::findOne($item);
- if (!$mch) {
- throw new \Exception('参数错误' . $item);
- }
- $mch->is_delete = 1;
- if (!$mch->save()) {
- throw new \Exception('保存失败。' . $item . array_shift($mch->getFirstErrors()));
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function mchAuditList() {
- try {
- $is_delete = 0;
- if ($this->is_delete == 1) {
- $is_delete = 1;
- }
- $query = MchAudit::find()->where(['is_delete' => $is_delete, 'store_id' => $this->store_id]);
- if (!is_null($this->status) && $this->status > -1) {
- $query->andWhere(['status' => $this->status]);
- }
- if (!is_null($this->mch_common_cat_id) && $this->mch_common_cat_id > 0) {
- $query->andWhere(['mch_common_cat_id' => $this->mch_common_cat_id]);
- }
- if ($this->mch_id > 0) {
- $query->andWhere(['mch_id' => $this->mch_id]);
- }
- if ($this->user_id > 0) {
- $query->andWhere(['user_id' => $this->user_id]);
- }
- if (!empty($this->saas_user_name)) {
- $query->andWhere(['saas_user_id' => SaasUser::find()->select('id')->where(['like', 'name', trim($this->saas_user_name)])]);
- }
- if (!empty($this->name)) {
- $query->andWhere(['like', 'name', trim($this->name)]);
- }
- if (!empty($this->realname)) {
- $query->andWhere(['like', 'realname', trim($this->realname)]);
- }
- if (!empty($this->tel)) {
- $query->andWhere(['like', 'tel', trim($this->tel)]);
- }
- if (!empty($this->begin_time)) {
- $query->andWhere(['>=', 'created_at', strtotime($this->begin_time)]);
- }
- if (!empty($this->end_time)) {
- $query->andWhere(['<=', 'created_at', strtotime($this->end_time)]);
- }
- $query->orderBy('id DESC');
- $pagination = pagination_make($query);
- $cats = MchCommonCat::find()->where(['store_id' => $this->store_id])->indexBy('id')->all();
- foreach ($pagination['list'] as &$item) {
- $item['mch_common_cat_name'] = $cats[$item['mch_common_cat_id']] ? $cats[$item['mch_common_cat_id']]['name'] : '-';
- $saasUser = SaasUser::findOne([$item['saas_user_id']]);
- $item['saas_user'] = $saasUser;
- $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
- $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $pagination,
- 'q' => $query->createCommand()->getRawSql(),
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function mchAuditStatus() {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $reason = $this->reason;
- $status = $this->status;
- $id = $this->id;
- if (!is_array($id)) {
- $id = explode(',', $id);
- }
- foreach ($id as $item) {
- $mchAudit = MchAudit::findOne($item);
- if (!$mchAudit) {
- throw new \Exception('参数错误' . $item);
- }
- $mchAudit->reason = $reason ?: '';
- $mchAudit->status = $status;
- if (!$mchAudit->save()) {
- throw new \Exception('保存失败。' . $item . array_shift($mchAudit->getFirstErrors()));
- }
- if ($status == 1) {
- $mchSave = (new MchForm([
- 'username' => $mchAudit['username'],
- 'password' => $mchAudit['password'],
- 'user_id' => $mchAudit['user_id'],
- 'name' => $mchAudit['name'],
- 'logo' => $mchAudit['logo'],
- 'realname' => $mchAudit['realname'],
- 'tel' => $mchAudit['tel'],
- 'mch_common_cat_id' => $mchAudit['mch_common_cat_id'],
- 'wechat_name' => $mchAudit['wechat_name'],
- 'form' => $mchAudit['form'],
- 'province_id' => $mchAudit['province_id'],
- 'city_id' => $mchAudit['city_id'],
- 'district_id' => $mchAudit['district_id'],
- 'address' => $mchAudit['address'],
- 'longitude' => $mchAudit['longitude'],
- 'latitude' => $mchAudit['latitude'],
- ]))->mchSave();
- if ($mchSave['code']) {
- throw new \Exception('商户创建失败。' . $mchSave['msg']);
- }
- $mchAudit->mch_id = $mchSave['data']['id'];
- if (!$mchAudit->save()) {
- throw new \Exception('审核关联失败。' . $item . array_shift($mchAudit->getFirstErrors()));
- }
- }
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => 'success',
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function mchReg() {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $has = MchAudit::find()->where(['user_id' => $this->user_id, 'status' => 0])->one();
- if($has){
- throw new \Exception('禁止重复申请。');
- }
- $mch = new MchAudit();
- $mch->store_id = $this->store_id;
- $mch->user_id = $this->user_id;
- $mch->saas_user_id = $this->saas_id;
- $mch->realname = $this->realname;
- $mch->tel = $this->tel;
- $mch->mch_common_cat_id = $this->mch_common_cat_id;
- $mch->wechat_name = $this->wechat_name;
- $mch->username = $this->username;
- $mch->password = $this->password;
- $mch->form = is_array($this->form) ? json_encode($this->form) : $this->form;
- if (isset($this->name)) {
- $mch->name = $this->name;
- }
- if (isset($this->logo)) {
- $mch->logo = $this->logo;
- }
- if (isset($this->province_id) && $this->province_id) {
- $district = District::find()->where(['id' => [$this->province_id, $this->city_id, $this->district_id]])->select('name')->column();
- $district = implode('', $district);
- $mch->district = $district;
- $mch->province_id = $this->province_id;
- $mch->city_id = $this->city_id;
- $mch->district_id = $this->district_id;
- $mch->address = $this->address;
- $mch->longitude = $this->longitude;
- $mch->latitude = $this->latitude;
- }
- if (!$mch->save()) {
- \Yii::error([__METHOD__, $mch->attributes]);
- throw new \Exception('保存失败。' . array_shift($mch->getFirstErrors()));
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功!',
- 'data' => $mch,
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function mchAuditDel() {
- try {
- $id = $this->id;
- if (!is_array($id)) {
- $id = explode(',', $id);
- }
- foreach ($id as $item) {
- $mchAudit = MchAudit::findOne($item);
- if (!$mchAudit) {
- throw new \Exception('参数错误' . $item);
- }
- $mchAudit->is_delete = 1;
- if (!$mchAudit->save()) {
- throw new \Exception('保存失败。' . $item . array_shift($mchAudit->getFirstErrors()));
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function mchCommonCatList() {
- try {
- $is_delete = 0;
- if ($this->is_delete == 1) {
- $is_delete = 1;
- }
- $query = MchCommonCat::find()->where(['is_delete' => $is_delete, 'store_id' => $this->store_id]);
- if (!is_null($this->is_show) && $this->is_show > -1) {
- $query->andWhere(['is_show' => $this->is_show]);
- }
- if (!empty($this->name)) {
- $query->andWhere(['like', 'name', trim($this->name)]);
- }
- if (!empty($this->begin_time)) {
- $query->andWhere(['>=', 'created_at', strtotime($this->begin_time)]);
- }
- if (!empty($this->end_time)) {
- $query->andWhere(['<=', 'created_at', strtotime($this->end_time)]);
- }
- $query->orderBy('sort DESC, id DESC');
- $list = $query->asArray()->all();
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => ['list' => $list],
- // 'q' => $query->createCommand()->getRawSql(),
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function mchCommonCatSelectList($indexBy = 1) {
- $query = MchCommonCat::find()->where(['is_delete' => 0, 'store_id' => $this->store_id])
- ->select('id,name,is_show');
- if($indexBy){
- $query->indexBy('id');
- }
- $list = $query->orderBy('sort DESC, id DESC')->asArray()->all();
- foreach($list as &$item){
- if(!$item['is_show']){
- $item['name'] .= '(已隐藏)';
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $list,
- ];
- }
- public function mchCommonCatSave() {
- try {
- $mchCommonCat = $this->id ? MchCommonCat::findOne(['id' => $this->id, 'store_id' => $this->store_id]) : new MchCommonCat();
- if (empty($mchCommonCat)) {
- throw new \Exception('参数错误' . $this->id);
- }
- $mchCommonCat->name = $this->name;
- $mchCommonCat->sort = $this->sort ?: 0;
- $mchCommonCat->is_show = $this->is_show;
- $mchCommonCat->store_id = $this->store_id;
- if (!$mchCommonCat->save()) {
- \Yii::error([__METHOD__, $mchCommonCat->attributes]);
- throw new \Exception('保存失败。' . array_shift($mchCommonCat->getFirstErrors()));
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function mchCommonCatShow() {
- try {
- $is_show = $this->is_show;
- $id = $this->id;
- if (!is_array($id)) {
- $id = explode(',', $id);
- }
- foreach ($id as $item) {
- $mchCommonCat = MchCommonCat::findOne($item);
- if (!$mchCommonCat) {
- throw new \Exception('参数错误' . $item);
- }
- $mchCommonCat->is_show = $is_show;
- if (!$mchCommonCat->save()) {
- throw new \Exception('保存失败。' . $item . array_shift($mchCommonCat->getFirstErrors()));
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function mchCommonCatDel() {
- try {
- $id = $this->id;
- if (!is_array($id)) {
- $id = explode(',', $id);
- }
- foreach ($id as $item) {
- $mchCommonCat = MchCommonCat::findOne($item);
- if (!$mchCommonCat) {
- throw new \Exception('参数错误' . $item);
- }
- $mchCommonCat->is_delete = 1;
- if (!$mchCommonCat->save()) {
- throw new \Exception('保存失败。' . $item . array_shift($mchCommonCat->getFirstErrors()));
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function mchGoodsAudit() {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $status = $this->status;
- $id = $this->id;
- if (!is_array($id)) {
- $id = explode(',', $id);
- }
- foreach ($id as $item) {
- $goods = Goods::findOne($item);
- if (!$goods) {
- throw new \Exception('参数错误' . $item);
- }
- $goods->mch_audit = 0;
- $goods->mch_audit_time = 0;
- if ($status == 1) {
- $goods->status = Goods::STATUS_NORMAL;
- }
- if (!$goods->save()) {
- throw new \Exception('保存失败。' . $item . array_shift($goods->getFirstErrors()));
- }
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => 'success',
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
-
-
-
-
-
-
-
-
-
- public static function goodsCatList($mch_id = 1, $is_show = -1){
- $res = [];
- $query = MchCat::find()->where([
- 'is_delete' => 0,
- 'mch_id' => $mch_id
- ])->orderBy(['sort'=>SORT_ASC]);
- if ($is_show > -1) {
- $query->andWhere([
- 'is_show' => $is_show
- ]);
- }
- $cat_list = $query->asArray()->all();
- if ($cat_list) {
- $res = buildTree($cat_list);
- }
- return $res;
- }
- public function goodsCatSave(){
- $cat = $this->id ? MchCat::findOne(['id' => $this->id, 'mch_id' => $this->mch_id]) : new MchCat();
- if (empty($cat)) {
- throw new \Exception('参数错误' . $this->id);
- }
- $cat->parent_id = $this->parent_id;
- $cat->is_show = $this->is_show;
- if ($this->is_show == 0 && $this->id) {
- // 产品分类隐藏同时下架该分类下所有商品
- $goods_ids = MchGoodsCat::find()->where(['cat_id' => $this->id])->select('goods_id')->asArray()->all();
- Goods::updateAll(['status' => Goods::STATUS_DISABLE],['in', 'id', array_column($goods_ids, 'goods_id')]);
- }
- $cat->name = $this->name;
- $cat->pic_url = $this->pic_url;
- $cat->sort = $this->sort;
- $cat->mch_id = $this->mch_id;
- if ($cat->save()) {
- return [
- 'code' => 0,
- 'msg' => '提交成功'
- ];
- } else {
- return [
- 'code' => 0,
- 'msg' => '保存失败' . array_shift($cat->getFirstErrors()),
- ];
- }
- }
- public function goodsCatStatus($id, $status = 1){
- try {
- if (!is_array($id)) {
- $id = explode(',', $id);
- }
- foreach ($id as $item) {
- $mchCat = MchCat::findOne($item);
- if (!$mchCat) {
- throw new \Exception('参数错误' . $item);
- }
- if($status == 0 || $status == 1){
- $mchCat->is_show = $status;
- }else{
- $mchCat->is_delete = 1;
- }
- if (!$mchCat->save()) {
- throw new \Exception('保存失败。' . $item . array_shift($mchCat->getFirstErrors()));
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
-
-
-
-
- //发放佣金
- public static function accountMoneyAdd($order) {
- if(!$order){
- return [
- 'code' => 1,
- 'msg' => '入驻商订单不存在或已发放',
- ];
- }
- $mch_id = $order->mch_id;
- if(!$mch_id){
- return [
- 'code' => 404,
- 'msg' => '非入驻商订单',
- ];
- }
- $order_id = $order->id;
- $t = \Yii::$app->db->beginTransaction();
- try{
- $order->mch_is_price = 1;
- $order->mch_is_price_time = time();
- $save = $order->save();
- if(!$save){
- throw new \Exception('入驻商佣金发放状态修改失败' . array_shift($order->getFirstErrors()));
- }
- $mch = Mch::findOne($mch_id);
- $pay_price = $order instanceof \app\plugins\scanCodePay\models\Order ? $order->total_price : $order->pay_price;
- $profit = sprintf("%.2f", $pay_price * $mch->transfer_rate / 100);
- $amount = $pay_price - $profit;
-
- $desc = [];
-
- //
- $share_price = (float)$order->first_price + (float)$order->second_price + (float)$order->third_price;
- if($share_price > 0){
- $amount -= $share_price;
- $desc[] = '分销佣金:' . $share_price;
- }
-
- //同城配送费用
- if(isset($order->is_delivery) && $order->is_delivery){
- $delivery_info = \app\models\DeliveryInfo::findOne(['order_no' => $order->order_no]);
- $express_price = (float)$delivery_info['fee'];
- if($express_price > 0){
- $amount -= $express_price;
- $desc[] = '同城配送费用:' . $express_price;
- }
- }
- $desc = implode(';', $desc);
- $amount = $amount <= 0 ? 0 : $amount;
- $saveLog = MchAccountLog::saveLog($mch_id, $order->user_id, $amount, AccountLog::LOG_TYPE_INCOME, $order_id, $order->order_no, $desc, $profit);
- if($saveLog['code']){
- throw new \Exception($saveLog['msg']);
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => 'ok',
- ];
- } catch (\Exception $ex) {
- debug_log([__METHOD__, __LINE__, $order->id, $ex->getMessage()], __CLASS__ . '.log');
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '操作失败,' . $ex->getMessage(),
- ];
- }
- }
- //佣金
- public function accountLog() {
- try {
- $query = MchAccountLog::find()->where(['mch_id' => $this->mch_id]);
- if ($this->order_no) {
- $query->andWhere(['like', 'order_no', $this->order_no]);
- }
- if (!empty($this->begin_time)) {
- $query->andWhere(['>=', 'created_at', strtotime($this->begin_time)]);
- }
- if (!empty($this->end_time)) {
- $query->andWhere(['<=', 'created_at', strtotime($this->end_time)]);
- }
- $query->orderBy('id DESC');
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$item) {
- $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $pagination,
- 'mch' => Mch::findOne($this->mch_id),
- // 'q' => $query->createCommand()->getRawSql(),
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //提现
- public function mchCashSubmit($price, $type, $user_id = 0, $name = '', $account = '', $bank = '') {
- $mch_id = $this->mch_id;
- $exit = Cash::find()->andWhere(['=', 'status', 0])->andWhere(['mch_id' => $mch_id, 'cash_type' => Cash::IS_CASH_TYPE_MCH])->exists();
- if ($exit) {
- // return [
- // 'code' => 1,
- // 'msg' => '尚有未完成的提现申请'
- // ];
- }
- $mch = Mch::findOne($mch_id);
- $conf = $this->mchSetting();
- if (!$price) {
- return [
- 'code' => 1,
- 'msg' => '参数非法1',
- 'f' => func_get_args(),
- 'c' => $conf,
- ];
- }
- $service_charge = $price * $conf['mch_cash_rate'] / 100;
- $t = \Yii::$app->db->beginTransaction();
- try {
- $cash = new Cash();
- $cash->order_no = OrderNo::getOrderNo(OrderNo::ORDER_CASH);
- $cash->is_delete = 0;
- $cash->status = 0;
- $cash->mch_id = $mch_id;
- $cash->price = $price;
- $cash->created_at = time();
- $cash->user_id = $user_id;
- $cash->store_id = $mch->store_id;
- $cash->type = $type;
- $cash->name = $name;
- $cash->mobile = $account;
- $cash->bank_name = $bank;
- $cash->pay_time = 0;
- $cash->service_charge = $conf['mch_cash_rate'];
- $cash->cash_type = Cash::IS_CASH_TYPE_MCH;
- if (!$cash->save()) {
- \Yii::error([__METHOD__, $cash->getErrors()]);
- throw new \Exception('入驻商提现保存失败' . array_shift($cash->getFirstErrors()));
- }
- $saveLog = MchAccountLog::saveLog($mch_id, (int)$user_id, $price, AccountLog::LOG_TYPE_EXPEND, 0, $cash->order_no, '提现', $service_charge);
- if($saveLog['code']){
- throw new \Exception($saveLog['msg']);
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- debug_log([__METHOD__, __LINE__, $e->getMessage()], __CLASS__ . '.log');
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '操作失败,' . $e->getMessage()
- ];
- }
- }
- public function getKefuInfo()
- {
- $mch_id = $this->mch_id;
- $mch = Mch::findOne($mch_id);
- if (!$mch) {
- return [
- 'code' => 1,
- 'msg' => '数据不存在',
- ];
- }
- $kefu_info = json_decode($mch->kefu_info ?: '', true);
- if (empty($kefu_info)) {
- $kefu_info = [
- 'kefu_id' => '',
- 'kefu_auto_reply_img' => '',
- 'kefu_auto_reply_text' => '',
- ];
- }
- $is_show_kefu_download = Option::get('is_show_kefu_download', 0, 'saas', 1)['value'];
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data'=> [
- 'kefu_id' => $kefu_info['kefu_id'] ?: '',
- 'kefu_auto_reply_img' => $kefu_info['kefu_auto_reply_img'] ?: '',
- 'kefu_auto_reply_text' => $kefu_info['kefu_auto_reply_text'] ?: '',
- 'is_show_kefu_download' => intval($is_show_kefu_download)
- ]
- ];
- }
- public function setKefuInfo()
- {
- $kefu_id = $this->kefu_id;
- $kefu_auto_reply_img = $this->kefu_auto_reply_img;
- $kefu_auto_reply_text = $this->kefu_auto_reply_text;
- $mch_id = $this->mch_id;
- $mch = Mch::findOne($mch_id);
- if (!$mch) {
- return [
- 'code' => 1,
- 'msg' => '数据不存在',
- ];
- }
- $kefu_info = [
- 'kefu_id' => $kefu_id ?: '',
- 'kefu_auto_reply_img' => $kefu_auto_reply_img ?: '',
- 'kefu_auto_reply_text' => $kefu_auto_reply_text ?: '',
- ];
- if (!empty($kefu_id)) {
- if (!preg_match('/^1[3456789]\d{9}$/i', $kefu_id)) {
- return [
- 'code' => 1,
- 'msg' => '客服手机号格式错误',
- ];
- }
- $url = Kefu::API_HOST;
- $host = \str_replace(['http://', 'https://'], '', \Yii::$app->request->hostInfo);
- $res = http_post($url . '/api/regKefu', [
- 'form_params' => [
- 'token' => \Yii::$app->getKefuApiToken(),
- 'host' => $host,
- 'mobile' => $kefu_id,
- 'kefu_auto_reply_img' => $kefu_auto_reply_img,
- 'kefu_auto_reply_text' => $kefu_auto_reply_text,
- ]
- ]);
- if ($res->getStatusCode() != 200) {
- return [
- 'code' => 1,
- 'msg' => '请求出错!',
- ];
- }
- $content = json_decode((string)$res->getBody(), true);
- if ($content['code'] == 0) {
- $mch->kefu_info = json_encode($kefu_info, JSON_UNESCAPED_UNICODE);
- $mch->save();
- }
- } else {
- $mch->kefu_info = json_encode($kefu_info, JSON_UNESCAPED_UNICODE);
- $mch->save();
- $content = [
- 'code' => 0,
- 'msg' => '保存成功',
- ];
- }
- return $content;
- }
-
-
-
- //入驻商访客key
- public static function cacheKeyVisit($mch_id, $date = '') {
- $key = implode('_', ['mch_visit_date', $mch_id, $date]);
- return $key;
- }
- //入驻商访客
- public function mchVisit($date = '', $add = 0) {
- $key = self::cacheKeyVisit($this->mch_id, $date);
- $val = cache()->get($key);
- if($add){
- $val += $add;
- cache()->set($key, $val, 86400 * 30);
- }
- return $val;
- }
- }
|