| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829 |
- <?php
- namespace app\modules\admin\models;
- use app\models\Attr;
- use app\models\AttrGroup;
- use app\models\Goods;
- use app\models\GoodsChainLevel;
- use app\models\GoodsPic;
- use app\models\ShareLevel;
- use app\models\StoreMini;
- use app\models\VideoShopGoodsExt;
- use EasyWeChat\Kernel\BaseClient;
- use yii\base\Model;
- use yii\helpers\Json;
- use app\models\Option;
- use app\constants\OptionSetting;
- class VideoShopGoodsForm extends Model
- {
- public $params;
- public $store_id;
- public $mini_id;
- public $miniProgram;
- public $name;
- public $id;
- public $ids;
- public $status;
- public $rate_type;
- public $rate;
- public $is_open;
- public $chain_type;
- public $chain_level_value;
- public $individual_share;
- public $attr_setting_type;
- public $share_type;
- public $share_commission_new_first;
- public $share_commission_new_second;
- public $share_commission_new_third;
- public $attr;
- public function rules()
- {
- return [
- [['name', 'ids', 'share_commission_new_first', 'share_commission_new_second', 'share_commission_new_third'], 'string'],
- [['id', 'mini_id', 'status', 'rate_type', 'is_open', 'individual_share', 'attr_setting_type', 'share_type', 'chain_type'], 'integer'],
- [['rate'], 'number'],
- [['chain_type'], 'default', 'value' => 1],
- [['params', 'chain_level_value', 'attr'], 'safe']
- ];
- }
- // 获取商品id列表
- public function syncGoodsList($next_key = '', $page_size = 10) {
- try {
- if (!$this->miniProgram) {
- throw new \Exception('请检查小店状态');
- }
- $data = [
- 'next_key' => $next_key,
- 'page_size' => $page_size
- ];
- $client = new BaseClient($this->miniProgram);
- $res = $client->httpPostJson('channels/ec/product/list/get', $data);
- if (!$res['errcode'] && !empty($res)) {
- //根据商品id同步商品
- // ....
- foreach ($res['product_ids'] as $product_id) {
- $goods_ext = VideoShopGoodsExt::findOne(['store_id' => $this->store_id, 'product_id' => (string)$product_id, 'mini_id' => $this->mini_id]);
- if ($goods_ext) {
- // continue;
- }
- $this->syncGoodsInfo($product_id);
- }
- //下一页
- if (count($res['product_ids']) >= $page_size) {
- $this->syncGoodsList($res['next_key']);
- }
- }
- return [
- 'code' => 0,
- 'msg' => '同步成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => $e->getCode() ?: 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //根据商品id同步商品
- public function syncGoodsInfo($product_id = 0) {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!$product_id) {
- throw new \Exception('缺少product_id');
- }
- $store_id = $this->store_id;
- $mini_id = $this->mini_id;
- //foreach ($product_id as $item) {
- $data = [
- 'product_id' => $product_id,
- 'data_type' => 3
- ];
- $client = new BaseClient($this->miniProgram);
- $res = $client->httpPostJson('channels/ec/product/get', $data);
- if (!$res['errcode'] && !empty($res)) {
- $goodsInfo = $res['product'] ?? $res['edit_product'];
- $goodsInfo['min_price'] *= 0.01;
- $video_shop_goods = VideoShopGoodsExt::findOne(['store_id' => $store_id, 'product_id' => (string)$product_id, 'mini_id' => $mini_id]);
- $goods = null;
- $status = 1;
- if ($video_shop_goods && $video_shop_goods->goods_id) {
- $goods = Goods::findOne(['id' => $video_shop_goods->goods_id, 'is_delete' => 0]);
- if ($goods) {
- $status = $goods->status;
- }
- }
- if (!$goods) {
- $goods = new Goods();
- }
- $goods->status = $status;
- $goods->store_id = $this->store_id;
- $goods->name = $goodsInfo['title'] ?: '';
- $goods->price = $goodsInfo['min_price'];
- $goods->unit = '件';//单位
- $goods->cover_pic = $goodsInfo['head_imgs'][0] ?: "";
- $goods->original_price = $goodsInfo['min_price'];
- $goods->cost_price = $goodsInfo['min_price'];
- //拼接商品详情
- $desc_text = "<div>";
- foreach ($goodsInfo['desc_info']['imgs'] as $desc_img) {
- $desc_text .= "<div><img src='{$desc_img}'/></div>";
- }
- $desc_text .= "<div>{$goodsInfo['desc_info']['desc']}</div></div>";
- $goods->detail = $desc_text;
- $goods->use_attr = count($goodsInfo['skus'][0]['sku_attrs']) > 0 ? 1 : 0;
- $goods->goods_no = $goodsInfo['spu_code'] ?: 0;
- //属性
- if ($goodsInfo['attrs']) {
- $parameter_list = [];
- foreach ($goodsInfo['attrs'] as $parameter_) {
- $parameter_list[] = [
- 'name' => $parameter_['attr_key'],
- 'value' => $parameter_['attr_value']
- ];
- }
- $goods->parameter_list = json_encode($parameter_list, JSON_UNESCAPED_UNICODE);
- }
- if (!$goods->save()) {
- return [
- 'code' => 0,
- 'msg' => "导入失败"
- ];
- }
- //增加导入记录
- if (!$video_shop_goods) {
- $video_shop_goods = new VideoShopGoodsExt();
- $video_shop_goods->product_id = (string)$product_id;
- $video_shop_goods->store_id = $store_id;
- $video_shop_goods->mini_id = $mini_id;
- }
- $video_shop_goods->wx_status = $goodsInfo['status'];
- $goodsInfo['edit_status'] && $video_shop_goods->wx_edit_status = $goodsInfo['edit_status'];
- $video_shop_goods->goods_id = $goods->id;
- $video_shop_goods->wx_product_info = json_encode($res, JSON_UNESCAPED_UNICODE);
- $video_shop_goods->save();
- $goods_num = 0;
- // 获取总库存
- foreach ($goodsInfo['skus'] as $sku_item) {
- $goods_num += $sku_item['stock_num'];
- }
- $goods->goods_num = $goods_num;
- $goods->use_attr = 1;
- //判断是否使用规格
- if (count($goodsInfo['skus']) === 1 && empty($goodsInfo['skus'][0]['sku_attrs'])) {
- $goods->use_attr = 0;
- }
- $attr_result = $this->setAttr($goods, $goodsInfo['skus']);
- if ($attr_result['code'] === 0) {
- $goods->attr = json_encode($attr_result['data'], JSON_UNESCAPED_UNICODE);
- }
- $goods->save();
- $pic_list = $goodsInfo['head_imgs'];
- if ($pic_list) {
- GoodsPic::deleteAll(['goods_id' => $goods->id]);
- foreach ($pic_list as $pic_index => $pic_item) {
- if ($pic_index >= 9) {
- break;
- }
- if (!empty($pic_item)) {
- $pic = new GoodsPic();
- $pic->goods_id = $goods->id;
- $pic->pic_url = $pic_item;
- if (!$pic->save()) {
- continue;
- };
- }
- }
- }
- }
- //}
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch (\Exception $e) {
- debug_log([$e->getMessage()], __CLASS__);
- \Yii::error($e);
- $t->rollBack();
- return [
- 'code' => $e->getCode() ?: 1,
- 'msg' => $e->getMessage() . $e->getLine()
- ];
- }
- }
- public function product_h5url($product_id, $openid) {
- try {
- $data = [
- "openid" => $openid,
- "product_id" => $product_id,
- ];
- $client = new BaseClient($this->miniProgram);
- $res = $client->httpPostJson('channels/ec/sharer/get_sharer_product_h5url', $data);
- if (!$res['errcode'] && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => 'ok',
- 'data' => $res['product_h5url'],
- ];
- }
- return [
- 'code' => $res['errcode'] ?? 1,
- 'msg' => $res['errmsg'] ?? '系统错误',
- 'data' => $res,
- ];
- } catch (\Exception $e) {
- return [
- 'code' => $e->getCode() ?: 1,
- 'msg' => $e->getMessage() . $e->getLine()
- ];
- }
- }
- public function product_taglink($product_id, $openid) {
- try {
- $data = [
- "openid" => $openid,
- "product_id" => $product_id,
- ];
- $client = new BaseClient($this->miniProgram);
- $res = $client->httpPostJson('channels/ec/sharer/get_sharer_product_taglink', $data);
- if (!$res['errcode'] && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => 'ok',
- 'data' => $res['product_taglink'],
- ];
- }
- return [
- 'code' => $res['errcode'] ?? 1,
- 'msg' => $res['errmsg'] ?? '系统错误',
- 'data' => $res,
- ];
- } catch (\Exception $e) {
- return [
- 'code' => $e->getCode() ?: 1,
- 'msg' => $e->getMessage() . $e->getLine()
- ];
- }
- }
- public function product_qrcode($product_id, $openid) {
- try {
- $data = [
- "openid" => $openid,
- "product_id" => $product_id,
- ];
- $client = new BaseClient($this->miniProgram);
- $res = $client->httpPostJson('channels/ec/sharer/get_sharer_product_qrcode', $data);
- if (!$res['errcode'] && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => 'ok',
- 'data' => $res['product_qrcode'],
- ];
- }
- return [
- 'code' => $res['errcode'] ?? 1,
- 'msg' => $res['errmsg'] ?? '系统错误',
- 'data' => $res,
- ];
- } catch (\Exception $e) {
- return [
- 'code' => $e->getCode() ?: 1,
- 'msg' => $e->getMessage() . $e->getLine()
- ];
- }
- }
- public function product_listing($product_id, $delisting = 0) {
- try {
- $url = 'channels/ec/product/delisting';
- $video_shop_goods = VideoShopGoodsExt::findOne(['store_id' => $this->store_id, 'product_id' => (string)$product_id]);
- if(!$delisting){
- if($video_shop_goods['wx_edit_status'] == 7){
- throw new \Exception('审核中,禁止操作');
- }
- $url = 'channels/ec/product/listing';
- }
- $data = [
- "product_id" => $product_id,
- ];
- $miniProgram = \app\utils\Wechat\WechatMini::getWechatConfig($this->store_id, $video_shop_goods['mini_id'], 1);
- $client = new BaseClient($miniProgram);
- $res = $client->httpPostJson($url, $data);
- if (!$res['errcode'] && !empty($res)) {
- $this->miniProgram = $miniProgram;
- $this->syncGoodsInfo($product_id);
- return [
- 'code' => 0,
- 'msg' => 'ok',
- 'data' => $res,
- ];
- }
- return [
- 'code' => $res['errcode'] ?? 1,
- 'msg' => $res['errmsg'] ?? '系统错误',
- 'data' => $res,
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => $e->getCode() ?: 1,
- 'msg' => $e->getMessage() . $e->getLine()
- ];
- }
- }
- public function product_stock($product_id = []) {
- try {
- $url = 'channels/ec/product/stock/batchget';
- $video_shop_goods = VideoShopGoodsExt::findOne(['store_id' => $this->store_id, 'product_id' => $product_id]);
- $data = [
- "product_id" => $product_id,
- ];
- $miniProgram = \app\utils\Wechat\WechatMini::getWechatConfig($this->store_id, $video_shop_goods['mini_id'], 1);
- $client = new BaseClient($miniProgram);
- $res = $client->httpPostJson($url, $data);
- if (!$res['errcode'] && !empty($res)) {
- $list = [];
- if($res['data']['spu_stock_list']){
- foreach($res['data']['spu_stock_list'] as $item){
- $item['__total_stock_num'] = array_sum(array_column($item['sku_stock'], 'total_stock_num'));
- $item['__finder_total_num'] = array_sum(array_column($item['sku_stock'], 'finder_total_num'));
- $item['__normal_stock_num'] = array_sum(array_column($item['sku_stock'], 'normal_stock_num'));
- $item['__limited_discount_stock_num'] = array_sum(array_column($item['sku_stock'], 'limited_discount_stock_num'));
- $list[$item['product_id']] = $item;
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'ok',
- 'data' => $list,
- ];
- }
- return [
- 'code' => $res['errcode'] ?? 1,
- 'msg' => $res['errmsg'] ?? '系统错误',
- 'data' => $res,
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => $e->getCode() ?: 1,
- 'msg' => $e->getMessage() . $e->getLine()
- ];
- }
- }
- public function setAttr($goods, $skus) {
- try {
- $attr = [];
- if (!$goods->use_attr) {
- list($default_attr, $default_attr_group) = $this->getDefaultAttr();
- $attr = [
- [
- 'attr_list' => [
- [
- 'attr_group_name' => $default_attr_group->attr_group_name,
- 'attr_id' => $default_attr->id,
- 'attr_name' => $default_attr->attr_name,
- ],
- ],
- 'num' => intval($goods->goods_num) ?: 0,
- 'price' => $goods->price,
- 'cost_price' => $goods->cost_price,
- 'no' => $goods->goods_no,
- 'is_wholesale' => 0,
- 'wholesale_num' => 0,
- 'sku_id' => $skus[0]['sku_id'],
- 'pic' => $skus[0]['thumb_img'] ?: $goods->cover_pic,
- ],
- ];
- } else {
- foreach ($skus as $sku) {
- $sku['sale_price'] *= 0.01;
- $sku_attrs = $sku['sku_attrs'];
- $sku_attr_list = [];
- foreach ($sku_attrs as $sku_attr) {
- $sku_attr_list[] = [
- 'attr_group_name' => $sku_attr['attr_key'],
- 'attr_name' => $sku_attr['attr_value']
- ];
- }
- $attr[] = [
- 'attr_list' => $sku_attr_list,
- 'num' => intval($sku['stock_num']) ?: 0,
- 'price' => $sku['sale_price'],
- 'cost_price' => $sku['sale_price'],
- 'no' => $sku['sku_code'],
- 'is_wholesale' => 0,
- 'wholesale_num' => 0,
- 'sku_id' => $sku['sku_id'],
- 'pic' => $sku['thumb_img'] ?: $goods->cover_pic,
- ];
- }
- }
- if (empty($attr) || !is_array($attr)) {
- return [
- 'code' => 1
- ];
- }
- $new_attr = [];
- foreach ($attr as $i => $item_) {
- // if ($item['original_price'] > 0) {
- // $attr_price = $item['original_price'];
- // } else {
- $attr_price = $item_['price'];
- // }
- $new_attr_item = [
- 'attr_list' => [],
- 'num' => intval($item_['num']),
- 'price' => sprintf('%.2f', $attr_price),
- 'no' => $item_['no'] ?: '',
- 'pic' => $item_['pic'] ?: '',
- 'is_wholesale' => 0,
- 'wholesale_num' => 0,
- 'cost_price' => sprintf('%.2f', $item_['cost_price'] ?: $item_['price']),
- 'sku_id' => $item_['sku_id']
- ];
- foreach ($item_['attr_list'] as $a) {
- $attr_group_model = AttrGroup::findOne(['store_id' => $this->store_id, 'attr_group_name' => $a['attr_group_name'], 'is_delete' => 0]);
- if (!$attr_group_model) {
- $attr_group_model = new AttrGroup();
- $attr_group_model->attr_group_name = $a['attr_group_name'];
- $attr_group_model->store_id = $this->store_id;
- $attr_group_model->is_delete = 0;
- $attr_group_model->save();
- }
- $attr_model = Attr::findOne(['attr_group_id' => $attr_group_model->id, 'attr_name' => $a['attr_name'], 'is_delete' => 0]);
- if (!$attr_model) {
- $attr_model = new Attr();
- $attr_model->attr_name = $a['attr_name'];
- $attr_model->attr_group_id = $attr_group_model->id;
- $attr_model->is_delete = 0;
- $attr_model->save();
- }
- $new_attr_item['attr_list'][] = [
- 'attr_id' => $attr_model->id,
- 'attr_name' => $attr_model->attr_name,
- ];
- }
- $new_attr[] = $new_attr_item;
- }
- return [
- 'code' => 0,
- 'data' => $new_attr
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function getDefaultAttr() {
- $default_attr_name = '默认';
- $default_attr_group_name = '规格';
- $attr = Attr::findOne([
- 'attr_name' => $default_attr_name,
- 'is_delete' => 0,
- 'is_default' => 1,
- ]);
- $attr_group = null;
- if (!$attr) {
- $attr_group = AttrGroup::findOne([
- 'attr_group_name' => $default_attr_group_name,
- 'is_delete' => 0,
- ]);
- if (!$attr_group) {
- $attr_group = new AttrGroup();
- $attr_group->store_id = $this->store_id;
- $attr_group->attr_group_name = $default_attr_group_name;
- $attr_group->is_delete = 0;
- $attr_group->save(false);
- }
- $attr = new Attr();
- $attr->attr_group_id = $attr_group->id;
- $attr->attr_name = $default_attr_name;
- $attr->is_delete = 0;
- $attr->is_default = 1;
- $attr->save(false);
- } else {
- $attr_group = AttrGroup::findOne($attr->attr_group_id);
- }
- return [$attr, $attr_group];
- }
- //获取同步产品列表
- public function shopGoodsList($params = []) {
- try {
- $store_id = $this->store_id;
- $mini_id = $this->mini_id;
- $name = $this->name;
- $status = $this->status;
- $query = VideoShopGoodsExt::find()->alias('v')
- ->leftJoin(['g' => Goods::tableName()], 'v.goods_id = g.id')
- ->leftJoin(['s' => StoreMini::tableName()], 's.id = v.mini_id')
- ->where(['g.store_id' => $store_id, 'g.is_delete' => 0, 'v.is_delete' => 0, 's.is_cancle' => 0]);
- if ($mini_id) {
- $query->andWhere(['v.mini_id' => $mini_id]);
- }
- if ($name) {
- $query->andWhere(['LIKE', 'g.name', $name]);
- }
- if (in_array($status, [0, 1])) {
- $query->andWhere(['v.status' => $status]);
- }
- if($params['wx_status'] > 0){
- if($params['wx_status'] == 11){
- $params['wx_status'] = [0,11,13,14,15,20];
- }
- $query->andWhere(['v.wx_status' => $params['wx_status']]);
- }
- $query->select('v.id, g.name, g.cover_pic, g.rate, g.price, v.*, g.share_commission_new_first')->orderBy(['g.id' => 'DESC']);
- $list = pagination_make($query);
- foreach ($list['list'] as &$item) {
- $item['mini_name'] = StoreMini::findOne($item['mini_id'])->mini_nickname ?: '';
- $item['status'] = (int)$item['status'];
- $item['wx_status_name'] = VideoShopGoodsExt::wx_status_name($item['wx_status']);
- $item['wx_edit_status_name'] = VideoShopGoodsExt::wx_edit_status_name($item['wx_edit_status']);
- $item['wx_product_info'] = json_decode($item['wx_product_info'], true);
- $item['wx_min_price'] = sprintf("%.2f", $item['wx_product_info']['edit_product']['min_price'] / 100);
- $item['wx_product_stock_num'] = array_sum(array_column($item['wx_product_info']['edit_product']['skus'], 'stock_num'));
- $item['sale_num'] = \app\models\Order::find()->alias('o')->leftJoin(['od' => \app\models\OrderDetail::tableName()], 'o.id=od.order_id')
- ->rightJoin(['vo' => \app\models\VideoShopOrderExt::tableName()], 'vo.order_id=o.id')
- ->where(['od.goods_id' => $item['goods_id'], 'o.is_pay' => 1])
- ->sum('od.num') ?? 0;
-
- $rebate = $this->getShareCommissionMoneyMax(Goods::findone($item['goods_id']), $item['price']);
- $item['rebate'] = floatval(sprintf('%.2f', $rebate));
- }
- if($params['wx_stock'] && $list['list']){
- $stock = $this->product_stock(array_column($list['list'], 'product_id'));
- if(!$stock['code']){
- foreach ($list['list'] as &$list_item) {
- $list_item['wx_stock'] = $stock['data'][$list_item['product_id']];
- }
- }
- }
- $store_mini = StoreMini::find()->where(['store_id' => $store_id, 'fuwu_type' => 1, 'is_cancle' => 0])
- ->select('id, mini_nickname')->orderBy('id desc')->asArray()->all(); //
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount'],
- 'store_mini' => $store_mini
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function getShareCommissionMoneyMax($goods, $level_price) {
- $store_id = $goods->store_id;
- $share_commission_new_first = json_decode($goods->share_commission_new_first, true);
- $share_commission_money = 0;
- $attr = json_decode($goods->attr, true);
- // TODO: 价格
- $item_price = max(array_column($attr, 'price'));
- // $item_price = doubleval($orderShare->pay_price);
- $rate_first = 0;
- if ($goods->individual_share == 1) {
- $rate_first = max($share_commission_new_first);
- $shareType = $goods->share_type;
- } else {
- $first_profit1 = ShareLevel::find()->where(['profit_type' => 1, 'store_id' => $store_id, 'is_delete' => ShareLevel::SHARE_NOT_DELETE,'status' => ShareLevel::STATUS_ON])->max('first_profit');
- $first_profit0 = ShareLevel::find()->where(['profit_type' => 0, 'store_id' => $store_id, 'is_delete' => ShareLevel::SHARE_NOT_DELETE,'status' => ShareLevel::STATUS_ON])->max('first_profit');
- if($first_profit1 || $first_profit0){
- $rate1 = $first_profit1;
- $rate0 = $item_price * $first_profit0 / 100;
- $rate_first = $rate1 > $rate0 ? $first_profit1 : $first_profit0;
- $shareType = $rate1 > $rate0 ? 1 : 0;
- }else{
- $setting_shara = Option::get('share_money_setting', $store_id, 'share')['value'];
- $setting_shara = json_decode($setting_shara, true);
- $rate_first = $setting_shara['level_one']['value'] ?? 0;
- $shareType = 0;
- }
- }
- if ($shareType == 1) { // 金钱
- $share_commission_money += $rate_first * 1;
- } else { // 比例
- $share_commission_money += $item_price * $rate_first / 100;
- }
- return sprintf("%.2f", $share_commission_money < 0.01 ? 0 : $share_commission_money);
- }
- public function setStatus() {
- try {
- $ids = $this->ids;
- $status = $this->status;
- $store_id = $this->store_id;
- if (!in_array($status, [0, 1])) {
- throw new \Exception('参数错误');
- }
- $ids = explode(',', $ids);
- VideoShopGoodsExt::updateAll(['status' => $status], ['id' => $ids, 'store_id' => $store_id]);
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function setRateParams() {
- try {
- $params = $this->params;
- $store_id = $this->store_id;
- $goods_id = $this->id;
- if (!$goods_id) {
- throw new \Exception('缺少goods_id');
- }
- $rate_type = $this->rate_type; //分红类型 0比例 1固定金额
- $rate = $this->rate; //分润比例
- $is_open = $this->is_open; //是否开启链动
- $chain_level_value = $this->chain_level_value; //链动佣金设置
- $individual_share = $this->individual_share; //是否单独分销设置
- $attr_setting_type = $this->attr_setting_type; //分销设置类型 0.普通设置|1.详细设置
- $share_type = $this->share_type; //佣金配比 0--百分比 1--固定金额
- $share_commission_new_first = $this->share_commission_new_first;
- $share_commission_new_second = $this->share_commission_new_second;
- $share_commission_new_third = $this->share_commission_new_third;
- $attr = $this->attr;
- foreach ($attr as $i => &$item) {
- if (intval($individual_share) === 1 && intval($attr_setting_type) === 1) {
- $share_level = ShareLevel::find()->where(['store_id' => $store_id, 'is_delete' => ShareLevel::SHARE_NOT_DELETE, 'status' => ShareLevel::STATUS_ON])
- ->select('level id, name')->orderBy('level ASC')->asArray()->all();
- $share_level = array_merge([[
- 'id' => 0,
- 'name' => '默认等级'
- ]], $share_level);
- foreach ($share_level as $share_level_index => $share_level_) {
- if (!isset($item['share_commission_new_first'][$share_level_index])) {
- $item['share_commission_new_first'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id'];
- $item['share_commission_new_first'][$share_level_index]['value'] = 0;
- } else {
- foreach ($item['share_commission_new_first'] as $share_commission_new_item) {
- if ($share_commission_new_item['name'] === 'share_commission_level_' . $share_level_['id']) {
- $item['share_commission_new_first'][$share_level_index]['value'] = $share_commission_new_item['value'];
- }
- }
- }
- if (!isset($item['share_commission_new_second'][$share_level_index])) {
- $item['share_commission_new_second'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id'];
- $item['share_commission_new_second'][$share_level_index]['value'] = 0;
- } else {
- foreach ($item['share_commission_new_second'] as $share_commission_new_item) {
- if ($share_commission_new_item['name'] === 'share_commission_level_' . $share_level_['id']) {
- $item['share_commission_new_second'][$share_level_index]['value'] = $share_commission_new_item['value'];
- }
- }
- }
- if (!isset($item['share_commission_new_third'][$share_level_index])) {
- $item['share_commission_new_third'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id'];
- $item['share_commission_new_third'][$share_level_index]['value'] = 0;
- } else {
- foreach ($item['share_commission_new_third'] as $share_commission_new_item) {
- if ($share_commission_new_item['name'] === 'share_commission_level_' . $share_level_['id']) {
- $item['share_commission_new_third'][$share_level_index]['value'] = $share_commission_new_item['value'];
- }
- }
- }
- }
- }
- }
- $goods = Goods::findOne($goods_id);
- $goods->rate_type = $rate_type;
- $goods->rate = $rate;
- $goods->share_type = $share_type;
- $goods->attr_setting_type = $attr_setting_type;
- $goods->individual_share = $individual_share;
- $goods->share_commission_new_first = $share_commission_new_first;
- $goods->share_commission_new_second = $share_commission_new_second;
- $goods->share_commission_new_third = $share_commission_new_third;
- $goods->attr = json_encode($attr, JSON_UNESCAPED_UNICODE);
- if (!$goods->save()) {
- throw new \Exception(json_encode($goods->errors, JSON_UNESCAPED_UNICODE));
- }
- $goodsChainLevel = GoodsChainLevel::findOne(['goods_id' => $goods_id]);
- if (!$goodsChainLevel) {
- $goodsChainLevel = new GoodsChainLevel();
- $goodsChainLevel->goods_id = $goods_id;
- }
- $goodsChainLevel->type = $this->chain_type;
- $goodsChainLevel->is_open = $is_open;
- $goodsChainLevel->value = json_encode($chain_level_value);
- $goodsChainLevel->save();
- return [
- 'code' => 0,
- 'msg' => '设置成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|