VideoShopGoodsForm.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. <?php
  2. namespace app\modules\admin\models;
  3. use app\models\Attr;
  4. use app\models\AttrGroup;
  5. use app\models\Goods;
  6. use app\models\GoodsChainLevel;
  7. use app\models\GoodsPic;
  8. use app\models\ShareLevel;
  9. use app\models\StoreMini;
  10. use app\models\VideoShopGoodsExt;
  11. use EasyWeChat\Kernel\BaseClient;
  12. use yii\base\Model;
  13. use yii\helpers\Json;
  14. use app\models\Option;
  15. use app\constants\OptionSetting;
  16. class VideoShopGoodsForm extends Model
  17. {
  18. public $params;
  19. public $store_id;
  20. public $mini_id;
  21. public $miniProgram;
  22. public $name;
  23. public $id;
  24. public $ids;
  25. public $status;
  26. public $rate_type;
  27. public $rate;
  28. public $is_open;
  29. public $chain_type;
  30. public $chain_level_value;
  31. public $individual_share;
  32. public $attr_setting_type;
  33. public $share_type;
  34. public $share_commission_new_first;
  35. public $share_commission_new_second;
  36. public $share_commission_new_third;
  37. public $attr;
  38. public function rules()
  39. {
  40. return [
  41. [['name', 'ids', 'share_commission_new_first', 'share_commission_new_second', 'share_commission_new_third'], 'string'],
  42. [['id', 'mini_id', 'status', 'rate_type', 'is_open', 'individual_share', 'attr_setting_type', 'share_type', 'chain_type'], 'integer'],
  43. [['rate'], 'number'],
  44. [['chain_type'], 'default', 'value' => 1],
  45. [['params', 'chain_level_value', 'attr'], 'safe']
  46. ];
  47. }
  48. // 获取商品id列表
  49. public function syncGoodsList($next_key = '', $page_size = 10) {
  50. try {
  51. if (!$this->miniProgram) {
  52. throw new \Exception('请检查小店状态');
  53. }
  54. $data = [
  55. 'next_key' => $next_key,
  56. 'page_size' => $page_size
  57. ];
  58. $client = new BaseClient($this->miniProgram);
  59. $res = $client->httpPostJson('channels/ec/product/list/get', $data);
  60. if (!$res['errcode'] && !empty($res)) {
  61. //根据商品id同步商品
  62. // ....
  63. foreach ($res['product_ids'] as $product_id) {
  64. $goods_ext = VideoShopGoodsExt::findOne(['store_id' => $this->store_id, 'product_id' => (string)$product_id, 'mini_id' => $this->mini_id]);
  65. if ($goods_ext) {
  66. // continue;
  67. }
  68. $this->syncGoodsInfo($product_id);
  69. }
  70. //下一页
  71. if (count($res['product_ids']) >= $page_size) {
  72. $this->syncGoodsList($res['next_key']);
  73. }
  74. }
  75. return [
  76. 'code' => 0,
  77. 'msg' => '同步成功'
  78. ];
  79. } catch (\Exception $e) {
  80. return [
  81. 'code' => $e->getCode() ?: 1,
  82. 'msg' => $e->getMessage()
  83. ];
  84. }
  85. }
  86. //根据商品id同步商品
  87. public function syncGoodsInfo($product_id = 0) {
  88. $t = \Yii::$app->db->beginTransaction();
  89. try {
  90. if (!$product_id) {
  91. throw new \Exception('缺少product_id');
  92. }
  93. $store_id = $this->store_id;
  94. $mini_id = $this->mini_id;
  95. //foreach ($product_id as $item) {
  96. $data = [
  97. 'product_id' => $product_id,
  98. 'data_type' => 3
  99. ];
  100. $client = new BaseClient($this->miniProgram);
  101. $res = $client->httpPostJson('channels/ec/product/get', $data);
  102. if (!$res['errcode'] && !empty($res)) {
  103. $goodsInfo = $res['product'] ?? $res['edit_product'];
  104. $goodsInfo['min_price'] *= 0.01;
  105. $video_shop_goods = VideoShopGoodsExt::findOne(['store_id' => $store_id, 'product_id' => (string)$product_id, 'mini_id' => $mini_id]);
  106. $goods = null;
  107. $status = 1;
  108. if ($video_shop_goods && $video_shop_goods->goods_id) {
  109. $goods = Goods::findOne(['id' => $video_shop_goods->goods_id, 'is_delete' => 0]);
  110. if ($goods) {
  111. $status = $goods->status;
  112. }
  113. }
  114. if (!$goods) {
  115. $goods = new Goods();
  116. }
  117. $goods->status = $status;
  118. $goods->store_id = $this->store_id;
  119. $goods->name = $goodsInfo['title'] ?: '';
  120. $goods->price = $goodsInfo['min_price'];
  121. $goods->unit = '件';//单位
  122. $goods->cover_pic = $goodsInfo['head_imgs'][0] ?: "";
  123. $goods->original_price = $goodsInfo['min_price'];
  124. $goods->cost_price = $goodsInfo['min_price'];
  125. //拼接商品详情
  126. $desc_text = "<div>";
  127. foreach ($goodsInfo['desc_info']['imgs'] as $desc_img) {
  128. $desc_text .= "<div><img src='{$desc_img}'/></div>";
  129. }
  130. $desc_text .= "<div>{$goodsInfo['desc_info']['desc']}</div></div>";
  131. $goods->detail = $desc_text;
  132. $goods->use_attr = count($goodsInfo['skus'][0]['sku_attrs']) > 0 ? 1 : 0;
  133. $goods->goods_no = $goodsInfo['spu_code'] ?: 0;
  134. //属性
  135. if ($goodsInfo['attrs']) {
  136. $parameter_list = [];
  137. foreach ($goodsInfo['attrs'] as $parameter_) {
  138. $parameter_list[] = [
  139. 'name' => $parameter_['attr_key'],
  140. 'value' => $parameter_['attr_value']
  141. ];
  142. }
  143. $goods->parameter_list = json_encode($parameter_list, JSON_UNESCAPED_UNICODE);
  144. }
  145. if (!$goods->save()) {
  146. return [
  147. 'code' => 0,
  148. 'msg' => "导入失败"
  149. ];
  150. }
  151. //增加导入记录
  152. if (!$video_shop_goods) {
  153. $video_shop_goods = new VideoShopGoodsExt();
  154. $video_shop_goods->product_id = (string)$product_id;
  155. $video_shop_goods->store_id = $store_id;
  156. $video_shop_goods->mini_id = $mini_id;
  157. }
  158. $video_shop_goods->wx_status = $goodsInfo['status'];
  159. $goodsInfo['edit_status'] && $video_shop_goods->wx_edit_status = $goodsInfo['edit_status'];
  160. $video_shop_goods->goods_id = $goods->id;
  161. $video_shop_goods->wx_product_info = json_encode($res, JSON_UNESCAPED_UNICODE);
  162. $video_shop_goods->save();
  163. $goods_num = 0;
  164. // 获取总库存
  165. foreach ($goodsInfo['skus'] as $sku_item) {
  166. $goods_num += $sku_item['stock_num'];
  167. }
  168. $goods->goods_num = $goods_num;
  169. $goods->use_attr = 1;
  170. //判断是否使用规格
  171. if (count($goodsInfo['skus']) === 1 && empty($goodsInfo['skus'][0]['sku_attrs'])) {
  172. $goods->use_attr = 0;
  173. }
  174. $attr_result = $this->setAttr($goods, $goodsInfo['skus']);
  175. if ($attr_result['code'] === 0) {
  176. $goods->attr = json_encode($attr_result['data'], JSON_UNESCAPED_UNICODE);
  177. }
  178. $goods->save();
  179. $pic_list = $goodsInfo['head_imgs'];
  180. if ($pic_list) {
  181. GoodsPic::deleteAll(['goods_id' => $goods->id]);
  182. foreach ($pic_list as $pic_index => $pic_item) {
  183. if ($pic_index >= 9) {
  184. break;
  185. }
  186. if (!empty($pic_item)) {
  187. $pic = new GoodsPic();
  188. $pic->goods_id = $goods->id;
  189. $pic->pic_url = $pic_item;
  190. if (!$pic->save()) {
  191. continue;
  192. };
  193. }
  194. }
  195. }
  196. }
  197. //}
  198. $t->commit();
  199. return [
  200. 'code' => 0,
  201. 'msg' => '操作成功'
  202. ];
  203. } catch (\Exception $e) {
  204. debug_log([$e->getMessage()], __CLASS__);
  205. \Yii::error($e);
  206. $t->rollBack();
  207. return [
  208. 'code' => $e->getCode() ?: 1,
  209. 'msg' => $e->getMessage() . $e->getLine()
  210. ];
  211. }
  212. }
  213. public function product_h5url($product_id, $openid) {
  214. try {
  215. $data = [
  216. "openid" => $openid,
  217. "product_id" => $product_id,
  218. ];
  219. $client = new BaseClient($this->miniProgram);
  220. $res = $client->httpPostJson('channels/ec/sharer/get_sharer_product_h5url', $data);
  221. if (!$res['errcode'] && !empty($res)) {
  222. return [
  223. 'code' => 0,
  224. 'msg' => 'ok',
  225. 'data' => $res['product_h5url'],
  226. ];
  227. }
  228. return [
  229. 'code' => $res['errcode'] ?? 1,
  230. 'msg' => $res['errmsg'] ?? '系统错误',
  231. 'data' => $res,
  232. ];
  233. } catch (\Exception $e) {
  234. return [
  235. 'code' => $e->getCode() ?: 1,
  236. 'msg' => $e->getMessage() . $e->getLine()
  237. ];
  238. }
  239. }
  240. public function product_taglink($product_id, $openid) {
  241. try {
  242. $data = [
  243. "openid" => $openid,
  244. "product_id" => $product_id,
  245. ];
  246. $client = new BaseClient($this->miniProgram);
  247. $res = $client->httpPostJson('channels/ec/sharer/get_sharer_product_taglink', $data);
  248. if (!$res['errcode'] && !empty($res)) {
  249. return [
  250. 'code' => 0,
  251. 'msg' => 'ok',
  252. 'data' => $res['product_taglink'],
  253. ];
  254. }
  255. return [
  256. 'code' => $res['errcode'] ?? 1,
  257. 'msg' => $res['errmsg'] ?? '系统错误',
  258. 'data' => $res,
  259. ];
  260. } catch (\Exception $e) {
  261. return [
  262. 'code' => $e->getCode() ?: 1,
  263. 'msg' => $e->getMessage() . $e->getLine()
  264. ];
  265. }
  266. }
  267. public function product_qrcode($product_id, $openid) {
  268. try {
  269. $data = [
  270. "openid" => $openid,
  271. "product_id" => $product_id,
  272. ];
  273. $client = new BaseClient($this->miniProgram);
  274. $res = $client->httpPostJson('channels/ec/sharer/get_sharer_product_qrcode', $data);
  275. if (!$res['errcode'] && !empty($res)) {
  276. return [
  277. 'code' => 0,
  278. 'msg' => 'ok',
  279. 'data' => $res['product_qrcode'],
  280. ];
  281. }
  282. return [
  283. 'code' => $res['errcode'] ?? 1,
  284. 'msg' => $res['errmsg'] ?? '系统错误',
  285. 'data' => $res,
  286. ];
  287. } catch (\Exception $e) {
  288. return [
  289. 'code' => $e->getCode() ?: 1,
  290. 'msg' => $e->getMessage() . $e->getLine()
  291. ];
  292. }
  293. }
  294. public function product_listing($product_id, $delisting = 0) {
  295. try {
  296. $url = 'channels/ec/product/delisting';
  297. $video_shop_goods = VideoShopGoodsExt::findOne(['store_id' => $this->store_id, 'product_id' => (string)$product_id]);
  298. if(!$delisting){
  299. if($video_shop_goods['wx_edit_status'] == 7){
  300. throw new \Exception('审核中,禁止操作');
  301. }
  302. $url = 'channels/ec/product/listing';
  303. }
  304. $data = [
  305. "product_id" => $product_id,
  306. ];
  307. $miniProgram = \app\utils\Wechat\WechatMini::getWechatConfig($this->store_id, $video_shop_goods['mini_id'], 1);
  308. $client = new BaseClient($miniProgram);
  309. $res = $client->httpPostJson($url, $data);
  310. if (!$res['errcode'] && !empty($res)) {
  311. $this->miniProgram = $miniProgram;
  312. $this->syncGoodsInfo($product_id);
  313. return [
  314. 'code' => 0,
  315. 'msg' => 'ok',
  316. 'data' => $res,
  317. ];
  318. }
  319. return [
  320. 'code' => $res['errcode'] ?? 1,
  321. 'msg' => $res['errmsg'] ?? '系统错误',
  322. 'data' => $res,
  323. ];
  324. } catch (\Exception $e) {
  325. \Yii::error($e);
  326. return [
  327. 'code' => $e->getCode() ?: 1,
  328. 'msg' => $e->getMessage() . $e->getLine()
  329. ];
  330. }
  331. }
  332. public function product_stock($product_id = []) {
  333. try {
  334. $url = 'channels/ec/product/stock/batchget';
  335. $video_shop_goods = VideoShopGoodsExt::findOne(['store_id' => $this->store_id, 'product_id' => $product_id]);
  336. $data = [
  337. "product_id" => $product_id,
  338. ];
  339. $miniProgram = \app\utils\Wechat\WechatMini::getWechatConfig($this->store_id, $video_shop_goods['mini_id'], 1);
  340. $client = new BaseClient($miniProgram);
  341. $res = $client->httpPostJson($url, $data);
  342. if (!$res['errcode'] && !empty($res)) {
  343. $list = [];
  344. if($res['data']['spu_stock_list']){
  345. foreach($res['data']['spu_stock_list'] as $item){
  346. $item['__total_stock_num'] = array_sum(array_column($item['sku_stock'], 'total_stock_num'));
  347. $item['__finder_total_num'] = array_sum(array_column($item['sku_stock'], 'finder_total_num'));
  348. $item['__normal_stock_num'] = array_sum(array_column($item['sku_stock'], 'normal_stock_num'));
  349. $item['__limited_discount_stock_num'] = array_sum(array_column($item['sku_stock'], 'limited_discount_stock_num'));
  350. $list[$item['product_id']] = $item;
  351. }
  352. }
  353. return [
  354. 'code' => 0,
  355. 'msg' => 'ok',
  356. 'data' => $list,
  357. ];
  358. }
  359. return [
  360. 'code' => $res['errcode'] ?? 1,
  361. 'msg' => $res['errmsg'] ?? '系统错误',
  362. 'data' => $res,
  363. ];
  364. } catch (\Exception $e) {
  365. \Yii::error($e);
  366. return [
  367. 'code' => $e->getCode() ?: 1,
  368. 'msg' => $e->getMessage() . $e->getLine()
  369. ];
  370. }
  371. }
  372. public function setAttr($goods, $skus) {
  373. try {
  374. $attr = [];
  375. if (!$goods->use_attr) {
  376. list($default_attr, $default_attr_group) = $this->getDefaultAttr();
  377. $attr = [
  378. [
  379. 'attr_list' => [
  380. [
  381. 'attr_group_name' => $default_attr_group->attr_group_name,
  382. 'attr_id' => $default_attr->id,
  383. 'attr_name' => $default_attr->attr_name,
  384. ],
  385. ],
  386. 'num' => intval($goods->goods_num) ?: 0,
  387. 'price' => $goods->price,
  388. 'cost_price' => $goods->cost_price,
  389. 'no' => $goods->goods_no,
  390. 'is_wholesale' => 0,
  391. 'wholesale_num' => 0,
  392. 'sku_id' => $skus[0]['sku_id'],
  393. 'pic' => $skus[0]['thumb_img'] ?: $goods->cover_pic,
  394. ],
  395. ];
  396. } else {
  397. foreach ($skus as $sku) {
  398. $sku['sale_price'] *= 0.01;
  399. $sku_attrs = $sku['sku_attrs'];
  400. $sku_attr_list = [];
  401. foreach ($sku_attrs as $sku_attr) {
  402. $sku_attr_list[] = [
  403. 'attr_group_name' => $sku_attr['attr_key'],
  404. 'attr_name' => $sku_attr['attr_value']
  405. ];
  406. }
  407. $attr[] = [
  408. 'attr_list' => $sku_attr_list,
  409. 'num' => intval($sku['stock_num']) ?: 0,
  410. 'price' => $sku['sale_price'],
  411. 'cost_price' => $sku['sale_price'],
  412. 'no' => $sku['sku_code'],
  413. 'is_wholesale' => 0,
  414. 'wholesale_num' => 0,
  415. 'sku_id' => $sku['sku_id'],
  416. 'pic' => $sku['thumb_img'] ?: $goods->cover_pic,
  417. ];
  418. }
  419. }
  420. if (empty($attr) || !is_array($attr)) {
  421. return [
  422. 'code' => 1
  423. ];
  424. }
  425. $new_attr = [];
  426. foreach ($attr as $i => $item_) {
  427. // if ($item['original_price'] > 0) {
  428. // $attr_price = $item['original_price'];
  429. // } else {
  430. $attr_price = $item_['price'];
  431. // }
  432. $new_attr_item = [
  433. 'attr_list' => [],
  434. 'num' => intval($item_['num']),
  435. 'price' => sprintf('%.2f', $attr_price),
  436. 'no' => $item_['no'] ?: '',
  437. 'pic' => $item_['pic'] ?: '',
  438. 'is_wholesale' => 0,
  439. 'wholesale_num' => 0,
  440. 'cost_price' => sprintf('%.2f', $item_['cost_price'] ?: $item_['price']),
  441. 'sku_id' => $item_['sku_id']
  442. ];
  443. foreach ($item_['attr_list'] as $a) {
  444. $attr_group_model = AttrGroup::findOne(['store_id' => $this->store_id, 'attr_group_name' => $a['attr_group_name'], 'is_delete' => 0]);
  445. if (!$attr_group_model) {
  446. $attr_group_model = new AttrGroup();
  447. $attr_group_model->attr_group_name = $a['attr_group_name'];
  448. $attr_group_model->store_id = $this->store_id;
  449. $attr_group_model->is_delete = 0;
  450. $attr_group_model->save();
  451. }
  452. $attr_model = Attr::findOne(['attr_group_id' => $attr_group_model->id, 'attr_name' => $a['attr_name'], 'is_delete' => 0]);
  453. if (!$attr_model) {
  454. $attr_model = new Attr();
  455. $attr_model->attr_name = $a['attr_name'];
  456. $attr_model->attr_group_id = $attr_group_model->id;
  457. $attr_model->is_delete = 0;
  458. $attr_model->save();
  459. }
  460. $new_attr_item['attr_list'][] = [
  461. 'attr_id' => $attr_model->id,
  462. 'attr_name' => $attr_model->attr_name,
  463. ];
  464. }
  465. $new_attr[] = $new_attr_item;
  466. }
  467. return [
  468. 'code' => 0,
  469. 'data' => $new_attr
  470. ];
  471. } catch (\Exception $e) {
  472. return [
  473. 'code' => 1,
  474. 'msg' => $e->getMessage()
  475. ];
  476. }
  477. }
  478. public function getDefaultAttr() {
  479. $default_attr_name = '默认';
  480. $default_attr_group_name = '规格';
  481. $attr = Attr::findOne([
  482. 'attr_name' => $default_attr_name,
  483. 'is_delete' => 0,
  484. 'is_default' => 1,
  485. ]);
  486. $attr_group = null;
  487. if (!$attr) {
  488. $attr_group = AttrGroup::findOne([
  489. 'attr_group_name' => $default_attr_group_name,
  490. 'is_delete' => 0,
  491. ]);
  492. if (!$attr_group) {
  493. $attr_group = new AttrGroup();
  494. $attr_group->store_id = $this->store_id;
  495. $attr_group->attr_group_name = $default_attr_group_name;
  496. $attr_group->is_delete = 0;
  497. $attr_group->save(false);
  498. }
  499. $attr = new Attr();
  500. $attr->attr_group_id = $attr_group->id;
  501. $attr->attr_name = $default_attr_name;
  502. $attr->is_delete = 0;
  503. $attr->is_default = 1;
  504. $attr->save(false);
  505. } else {
  506. $attr_group = AttrGroup::findOne($attr->attr_group_id);
  507. }
  508. return [$attr, $attr_group];
  509. }
  510. //获取同步产品列表
  511. public function shopGoodsList($params = []) {
  512. try {
  513. $store_id = $this->store_id;
  514. $mini_id = $this->mini_id;
  515. $name = $this->name;
  516. $status = $this->status;
  517. $query = VideoShopGoodsExt::find()->alias('v')
  518. ->leftJoin(['g' => Goods::tableName()], 'v.goods_id = g.id')
  519. ->leftJoin(['s' => StoreMini::tableName()], 's.id = v.mini_id')
  520. ->where(['g.store_id' => $store_id, 'g.is_delete' => 0, 'v.is_delete' => 0, 's.is_cancle' => 0]);
  521. if ($mini_id) {
  522. $query->andWhere(['v.mini_id' => $mini_id]);
  523. }
  524. if ($name) {
  525. $query->andWhere(['LIKE', 'g.name', $name]);
  526. }
  527. if (in_array($status, [0, 1])) {
  528. $query->andWhere(['v.status' => $status]);
  529. }
  530. if($params['wx_status'] > 0){
  531. if($params['wx_status'] == 11){
  532. $params['wx_status'] = [0,11,13,14,15,20];
  533. }
  534. $query->andWhere(['v.wx_status' => $params['wx_status']]);
  535. }
  536. $query->select('v.id, g.name, g.cover_pic, g.rate, g.price, v.*, g.share_commission_new_first')->orderBy(['g.id' => 'DESC']);
  537. $list = pagination_make($query);
  538. foreach ($list['list'] as &$item) {
  539. $item['mini_name'] = StoreMini::findOne($item['mini_id'])->mini_nickname ?: '';
  540. $item['status'] = (int)$item['status'];
  541. $item['wx_status_name'] = VideoShopGoodsExt::wx_status_name($item['wx_status']);
  542. $item['wx_edit_status_name'] = VideoShopGoodsExt::wx_edit_status_name($item['wx_edit_status']);
  543. $item['wx_product_info'] = json_decode($item['wx_product_info'], true);
  544. $item['wx_min_price'] = sprintf("%.2f", $item['wx_product_info']['edit_product']['min_price'] / 100);
  545. $item['wx_product_stock_num'] = array_sum(array_column($item['wx_product_info']['edit_product']['skus'], 'stock_num'));
  546. $item['sale_num'] = \app\models\Order::find()->alias('o')->leftJoin(['od' => \app\models\OrderDetail::tableName()], 'o.id=od.order_id')
  547. ->rightJoin(['vo' => \app\models\VideoShopOrderExt::tableName()], 'vo.order_id=o.id')
  548. ->where(['od.goods_id' => $item['goods_id'], 'o.is_pay' => 1])
  549. ->sum('od.num') ?? 0;
  550. $rebate = $this->getShareCommissionMoneyMax(Goods::findone($item['goods_id']), $item['price']);
  551. $item['rebate'] = floatval(sprintf('%.2f', $rebate));
  552. }
  553. if($params['wx_stock'] && $list['list']){
  554. $stock = $this->product_stock(array_column($list['list'], 'product_id'));
  555. if(!$stock['code']){
  556. foreach ($list['list'] as &$list_item) {
  557. $list_item['wx_stock'] = $stock['data'][$list_item['product_id']];
  558. }
  559. }
  560. }
  561. $store_mini = StoreMini::find()->where(['store_id' => $store_id, 'fuwu_type' => 1, 'is_cancle' => 0])
  562. ->select('id, mini_nickname')->orderBy('id desc')->asArray()->all(); //
  563. return [
  564. 'code' => 0,
  565. 'msg' => 'success',
  566. 'data' => [
  567. 'data' => $list['list'],
  568. 'pageNo' => $list['pageNo'],
  569. 'totalCount' => $list['totalCount'],
  570. 'store_mini' => $store_mini
  571. ]
  572. ];
  573. } catch (\Exception $e) {
  574. return [
  575. 'code' => 1,
  576. 'msg' => $e->getMessage()
  577. ];
  578. }
  579. }
  580. public function getShareCommissionMoneyMax($goods, $level_price) {
  581. $store_id = $goods->store_id;
  582. $share_commission_new_first = json_decode($goods->share_commission_new_first, true);
  583. $share_commission_money = 0;
  584. $attr = json_decode($goods->attr, true);
  585. // TODO: 价格
  586. $item_price = max(array_column($attr, 'price'));
  587. // $item_price = doubleval($orderShare->pay_price);
  588. $rate_first = 0;
  589. if ($goods->individual_share == 1) {
  590. $rate_first = max($share_commission_new_first);
  591. $shareType = $goods->share_type;
  592. } else {
  593. $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');
  594. $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');
  595. if($first_profit1 || $first_profit0){
  596. $rate1 = $first_profit1;
  597. $rate0 = $item_price * $first_profit0 / 100;
  598. $rate_first = $rate1 > $rate0 ? $first_profit1 : $first_profit0;
  599. $shareType = $rate1 > $rate0 ? 1 : 0;
  600. }else{
  601. $setting_shara = Option::get('share_money_setting', $store_id, 'share')['value'];
  602. $setting_shara = json_decode($setting_shara, true);
  603. $rate_first = $setting_shara['level_one']['value'] ?? 0;
  604. $shareType = 0;
  605. }
  606. }
  607. if ($shareType == 1) { // 金钱
  608. $share_commission_money += $rate_first * 1;
  609. } else { // 比例
  610. $share_commission_money += $item_price * $rate_first / 100;
  611. }
  612. return sprintf("%.2f", $share_commission_money < 0.01 ? 0 : $share_commission_money);
  613. }
  614. public function setStatus() {
  615. try {
  616. $ids = $this->ids;
  617. $status = $this->status;
  618. $store_id = $this->store_id;
  619. if (!in_array($status, [0, 1])) {
  620. throw new \Exception('参数错误');
  621. }
  622. $ids = explode(',', $ids);
  623. VideoShopGoodsExt::updateAll(['status' => $status], ['id' => $ids, 'store_id' => $store_id]);
  624. return [
  625. 'code' => 0,
  626. 'msg' => '操作成功'
  627. ];
  628. } catch (\Exception $e) {
  629. return [
  630. 'code' => 1,
  631. 'msg' => $e->getMessage()
  632. ];
  633. }
  634. }
  635. public function setRateParams() {
  636. try {
  637. $params = $this->params;
  638. $store_id = $this->store_id;
  639. $goods_id = $this->id;
  640. if (!$goods_id) {
  641. throw new \Exception('缺少goods_id');
  642. }
  643. $rate_type = $this->rate_type; //分红类型 0比例 1固定金额
  644. $rate = $this->rate; //分润比例
  645. $is_open = $this->is_open; //是否开启链动
  646. $chain_level_value = $this->chain_level_value; //链动佣金设置
  647. $individual_share = $this->individual_share; //是否单独分销设置
  648. $attr_setting_type = $this->attr_setting_type; //分销设置类型 0.普通设置|1.详细设置
  649. $share_type = $this->share_type; //佣金配比 0--百分比 1--固定金额
  650. $share_commission_new_first = $this->share_commission_new_first;
  651. $share_commission_new_second = $this->share_commission_new_second;
  652. $share_commission_new_third = $this->share_commission_new_third;
  653. $attr = $this->attr;
  654. foreach ($attr as $i => &$item) {
  655. if (intval($individual_share) === 1 && intval($attr_setting_type) === 1) {
  656. $share_level = ShareLevel::find()->where(['store_id' => $store_id, 'is_delete' => ShareLevel::SHARE_NOT_DELETE, 'status' => ShareLevel::STATUS_ON])
  657. ->select('level id, name')->orderBy('level ASC')->asArray()->all();
  658. $share_level = array_merge([[
  659. 'id' => 0,
  660. 'name' => '默认等级'
  661. ]], $share_level);
  662. foreach ($share_level as $share_level_index => $share_level_) {
  663. if (!isset($item['share_commission_new_first'][$share_level_index])) {
  664. $item['share_commission_new_first'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id'];
  665. $item['share_commission_new_first'][$share_level_index]['value'] = 0;
  666. } else {
  667. foreach ($item['share_commission_new_first'] as $share_commission_new_item) {
  668. if ($share_commission_new_item['name'] === 'share_commission_level_' . $share_level_['id']) {
  669. $item['share_commission_new_first'][$share_level_index]['value'] = $share_commission_new_item['value'];
  670. }
  671. }
  672. }
  673. if (!isset($item['share_commission_new_second'][$share_level_index])) {
  674. $item['share_commission_new_second'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id'];
  675. $item['share_commission_new_second'][$share_level_index]['value'] = 0;
  676. } else {
  677. foreach ($item['share_commission_new_second'] as $share_commission_new_item) {
  678. if ($share_commission_new_item['name'] === 'share_commission_level_' . $share_level_['id']) {
  679. $item['share_commission_new_second'][$share_level_index]['value'] = $share_commission_new_item['value'];
  680. }
  681. }
  682. }
  683. if (!isset($item['share_commission_new_third'][$share_level_index])) {
  684. $item['share_commission_new_third'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id'];
  685. $item['share_commission_new_third'][$share_level_index]['value'] = 0;
  686. } else {
  687. foreach ($item['share_commission_new_third'] as $share_commission_new_item) {
  688. if ($share_commission_new_item['name'] === 'share_commission_level_' . $share_level_['id']) {
  689. $item['share_commission_new_third'][$share_level_index]['value'] = $share_commission_new_item['value'];
  690. }
  691. }
  692. }
  693. }
  694. }
  695. }
  696. $goods = Goods::findOne($goods_id);
  697. $goods->rate_type = $rate_type;
  698. $goods->rate = $rate;
  699. $goods->share_type = $share_type;
  700. $goods->attr_setting_type = $attr_setting_type;
  701. $goods->individual_share = $individual_share;
  702. $goods->share_commission_new_first = $share_commission_new_first;
  703. $goods->share_commission_new_second = $share_commission_new_second;
  704. $goods->share_commission_new_third = $share_commission_new_third;
  705. $goods->attr = json_encode($attr, JSON_UNESCAPED_UNICODE);
  706. if (!$goods->save()) {
  707. throw new \Exception(json_encode($goods->errors, JSON_UNESCAPED_UNICODE));
  708. }
  709. $goodsChainLevel = GoodsChainLevel::findOne(['goods_id' => $goods_id]);
  710. if (!$goodsChainLevel) {
  711. $goodsChainLevel = new GoodsChainLevel();
  712. $goodsChainLevel->goods_id = $goods_id;
  713. }
  714. $goodsChainLevel->type = $this->chain_type;
  715. $goodsChainLevel->is_open = $is_open;
  716. $goodsChainLevel->value = json_encode($chain_level_value);
  717. $goodsChainLevel->save();
  718. return [
  719. 'code' => 0,
  720. 'msg' => '设置成功'
  721. ];
  722. } catch (\Exception $e) {
  723. return [
  724. 'code' => 1,
  725. 'msg' => $e->getMessage()
  726. ];
  727. }
  728. }
  729. }