JuShuiTanForm.php 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models\jushuitan;
  8. use app\models\Option;
  9. use app\constants\OptionSetting;
  10. use app\models\AccountLog;
  11. use app\models\User;
  12. use app\models\SaasUser;
  13. use app\models\Cat;
  14. use app\models\Goods;
  15. use app\models\Supplier;
  16. use app\models\Attr;
  17. use app\models\AttrGroup;
  18. use app\modules\admin\models\GoodsForm;
  19. use app\models\GoodsCat;
  20. use app\models\Order;
  21. use app\models\OrderRefund;
  22. use app\models\OrderDetail;
  23. use app\models\Store;
  24. use app\models\common\CommonOrder;
  25. use app\modules\client\models\v1\BindForm;
  26. use app\modules\client\models\v1\ShareMoneyForm;
  27. use app\modules\client\models\OrderComplete;
  28. use app\models\UserShareLog;
  29. use app\models\Share;
  30. use app\models\UserShareMoney;
  31. use app\models\UserBindLog;
  32. use app\models\Level;
  33. use zmoyi\JuShuiTan\Auth\Auth;
  34. use zmoyi\JuShuiTan\Api\ApiRequest;
  35. use zmoyi\JuShuiTan\Api\Common\ServeHttp;
  36. use zmoyi\JuShuiTan\Api\Common\Util;
  37. use app\modules\admin\models\SupplierForm;
  38. use app\modules\admin\models\PlatformForm;
  39. class JuShuiTanForm extends Model
  40. {
  41. public static $baseUrl = 'https://openapi.jushuitan.com/'; //https://openapi.jushuitan.com/ https://dev-api.jushuitan.com/
  42. public static $baseUrlDev = 'https://dev-api.jushuitan.com/';
  43. public static $confs = [];
  44. public $store_id;
  45. public static $store_type = 'store';
  46. const STORE_TYPE_STORE = 'store';
  47. const STORE_TYPE_SUPPLIER = 'supplier';
  48. public static function initStoreType($type = null) {
  49. $type && self::$store_type = $type;
  50. }
  51. public static function decodeState($state = '{"type":"store","id":0}') {
  52. $res = json_decode($state, true);
  53. return $res;
  54. }
  55. public static function encodeState($id = 0, $type = 'store') {
  56. $res = json_encode(['type' => $type, 'id' => $id]);
  57. return $res;
  58. }
  59. public static function decodeOrderNo($orderNo) {
  60. $supplierOrderNo = [
  61. 'OR',
  62. 'PO',
  63. ];
  64. $orderNoHead = substr($orderNo, 0, 2);
  65. if(in_array($orderNoHead, $supplierOrderNo)){
  66. return self::STORE_TYPE_SUPPLIER;
  67. }
  68. return self::STORE_TYPE_STORE;
  69. }
  70. public static function orderStatusKey2Jst($status) {
  71. /**
  72. * 等待买家付款=WAIT_BUYER_PAY,
  73. * 等待卖家发货=WAIT_SELLER_SEND_GOODS(传此状态时实际支付金额即pay节点支付金额=应付金额ERP才会显示已付款待审核),
  74. * 等待买家确认收货=WAIT_BUYER_CONFIRM_GOODS,
  75. * 交易成功=TRADE_FINISHED,
  76. * 付款后交易关闭=TRADE_CLOSED,
  77. * 付款前交易关闭=TRADE_CLOSED_BY_TAOBAO;
  78. */
  79. $arr = [
  80. // Order::ORDER_FLOW_DEFAULT => 'WAIT_BUYER_PAY',
  81. Order::ORDER_FLOW_NO_SEND => 'WAIT_SELLER_SEND_GOODS',
  82. Order::ORDER_FLOW_SEND => 'WAIT_BUYER_CONFIRM_GOODS',
  83. Order::ORDER_FLOW_CONFIRM => 'TRADE_FINISHED',
  84. Order::ORDER_FLOW_CANCEL => 'TRADE_CLOSED',
  85. ];
  86. return $arr[$status] ?? '';
  87. }
  88. public static function api($store_id, $serveHttp, $params) {
  89. $config = self::conf($store_id);
  90. $apiRequest = new ApiRequest($config);
  91. $response = $apiRequest->request($serveHttp, $params);
  92. if(isset($response['code'])){
  93. if($response['code'] == 100){
  94. debug_log(['code==100', $response, $store_id], __CLASS__ . '.log');
  95. self::setConfExpires($store_id);
  96. }
  97. }
  98. return $response;
  99. }
  100. public static function setConfExpires($store_id = 0) {
  101. $token = Option::get(OptionSetting::JU_SHUI_TAN_TOKEN, $store_id, self::$store_type)['value'];
  102. if($token){
  103. $token = json_decode($token, true);
  104. $token['expires_in'] = time() - $token['refresh_time'];
  105. self::saveToken($store_id, $token);
  106. }
  107. }
  108. public static function conf($store_id = 0, $refresh = 0) {
  109. $cacheK = $store_id . self::$store_type;
  110. if(isset(self::$confs[$cacheK]) && !$refresh){
  111. return self::$confs[$cacheK];
  112. }
  113. $conf = Option::get(OptionSetting::JU_SHUI_TAN, 0, 'saas')['value'];
  114. if($conf){
  115. $conf = json_decode($conf, true);
  116. $conf['baseUrl'] = self::$baseUrl;
  117. if(strpos($_SERVER['SCRIPT_FILENAME'], 'quanqudao.we10.cn')){
  118. $conf['baseUrl'] = self::$baseUrlDev;
  119. }
  120. $url_callback = '';
  121. if (\Yii::$app instanceof \yii\web\Application){
  122. $url_callback = \Yii::$app->request->hostInfo . '/index.php/jushuitan/callback';
  123. }
  124. $conf['url_callback'] = $url_callback;
  125. $access_token = '';
  126. $refresh_token = '';
  127. $token = Option::get(OptionSetting::JU_SHUI_TAN_TOKEN, $store_id, self::$store_type)['value'];
  128. if($token){
  129. $token = json_decode($token, true);
  130. $expires = $token['expires_in'] + $token['refresh_time'] < time();
  131. if($token['access_token'] && !$expires){
  132. $access_token = $token['access_token'];
  133. $refresh_token = $token['refresh_token'];
  134. }
  135. if($token['is_debug']){
  136. $conf['baseUrl'] = self::$baseUrlDev;
  137. }
  138. }
  139. $conf['access_token'] = $access_token;
  140. $conf['refresh_token'] = $refresh_token;
  141. $conf['token_info'] = $token;
  142. $conf['__'] = [
  143. '$store_type' => self::$store_type,
  144. '$store_id' => $store_id,
  145. ];
  146. }
  147. self::$confs[$cacheK] = $conf;
  148. return $conf;
  149. }
  150. public static function saveConf($store_id = 0, $config = []) {
  151. $oldConf = Option::get(OptionSetting::JU_SHUI_TAN_TOKEN, $store_id, self::$store_type, '{}')['value'];
  152. $oldConf = json_decode($oldConf, true);
  153. $conf = array_merge($oldConf, $config);
  154. $set = Option::set(OptionSetting::JU_SHUI_TAN_TOKEN, json_encode($conf), $store_id, self::$store_type);
  155. self::conf($store_id, 1);
  156. return $set;
  157. }
  158. public static function saveToken($store_id = 0, $refreshToken = []) {
  159. $refreshToken['refresh_time'] = time();
  160. return self::saveConf($store_id, $refreshToken);
  161. }
  162. public static function getConf($store_id = 0, $key = '') {
  163. $config = self::conf($store_id);
  164. if(empty($config['token_info'])){
  165. return '';
  166. }
  167. return $config['token_info'][$key];
  168. }
  169. public static function code2token($store_id = 0, $code = '') {
  170. $config = self::conf($store_id);
  171. $Auth = new Auth($config);
  172. $refreshToken = $Auth->getAccessToken($code);
  173. if($refreshToken['code'] != 0){
  174. debug_log([__FUNCTION__, __LINE__, $store_id, $code, $refreshToken], __CLASS__ . '.log');
  175. return $refreshToken;
  176. }
  177. self::saveToken($store_id, $refreshToken['data']);
  178. // self::cat2Jst($store_id, 0, -1);
  179. return $refreshToken;
  180. }
  181. public static function refreshToken($store_id = 0) {
  182. if(!self::isopen($store_id)){
  183. return;
  184. }
  185. $config = self::conf($store_id);
  186. $Auth = new Auth($config);
  187. $refreshToken = $Auth->refreshToken($config['refresh_token']);
  188. debug_log([__FUNCTION__, __LINE__, $store_id, $refreshToken], __CLASS__ . '.log');
  189. if($refreshToken['code'] != 0){
  190. debug_log([__FUNCTION__, __LINE__, $store_id, $refreshToken], __CLASS__ . '.log');
  191. return $refreshToken;
  192. }
  193. self::saveToken($store_id, $refreshToken['data']);
  194. return $refreshToken;
  195. }
  196. public static function checkUrlSign($store_id, $get = []) {
  197. $config = self::conf($store_id);
  198. $Auth = new Auth($config);
  199. $sign = Util::getSign($Auth->getConfig()['app_Secret'],$get);
  200. if($sign == $get['sign']){
  201. return true;
  202. }
  203. return false;
  204. }
  205. public static function isopen($store_id = 0) {
  206. $config = self::conf($store_id);
  207. if($store_id <= 0){
  208. return $config;
  209. }
  210. return empty($config['access_token']) ? 0 : 1;
  211. }
  212. public static function createUrl($store_id = 0) {
  213. try{
  214. if(!self::isopen(0)){
  215. return [
  216. 'code' => 1,
  217. 'msg' => '未配置此功能',
  218. ];
  219. // throw new \Exception('未配置此功能');
  220. }
  221. $config = self::conf($store_id);
  222. $Auth = new Auth($config);
  223. $data = [
  224. 'state' => self::encodeState($store_id, self::$store_type),
  225. 'app_key' => $Auth->getConfig()['app_Key'],
  226. 'timestamp' => time(),
  227. 'charset' => $Auth->getConfig()['charset']
  228. ];
  229. $sign = Util::getSign($Auth->getConfig()['app_Secret'],$data);
  230. $url = $Auth->getConfig()['authUrl'] . '?sign=' . $sign . '&' . http_build_query($data);
  231. return [
  232. 'code' => 0,
  233. 'data' => $url,
  234. ];
  235. } catch (\Exception $ex) {
  236. \Yii::error($ex);
  237. return [
  238. 'code' => 1,
  239. 'msg' => $ex->getMessage(),
  240. ];
  241. }
  242. }
  243. public static function oAuth($app_key, $code, $state) {
  244. try{
  245. if($state){
  246. $state = self::decodeState($state);
  247. }
  248. $store_id = $state['id'];
  249. if($state['type'] == self::STORE_TYPE_STORE){
  250. $store = Store::findOne(['is_delete' => 0, 'id' => $store_id]);
  251. }
  252. if($state['type'] == self::STORE_TYPE_SUPPLIER){
  253. $store = Supplier::findOne(['is_delete' => 0, 'id' => $store_id]);
  254. }
  255. if (!$store || empty($app_key) || empty($code)) {
  256. \Yii::error('<====================>授权回调 参数错误, 参数为:' . json_encode(all_params()));
  257. echo '授权失败,参数错误';
  258. return;
  259. }
  260. self::initStoreType($state['type']);
  261. if(!self::checkUrlSign($store_id, all_params())){
  262. \Yii::error('<====================>授权回调 Sign参数错误, 参数为:' . json_encode(all_params()));
  263. echo '授权失败,参数错误,签名错误';
  264. return;
  265. }
  266. $res = self::code2token($store_id, $code);
  267. if ($res['code'] != 0) {
  268. \Yii::error('<====================> token解析失败, 参数为:' . json_encode($res));
  269. echo '授权失败,token解析失败,错误信息:' . $res['msg'];
  270. return;
  271. }
  272. echo '授权成功!';
  273. return;
  274. } catch (\Exception $ex) {
  275. \Yii::error($ex);
  276. echo $ex->getMessage();
  277. return [
  278. 'code' => 1,
  279. 'msg' => $ex->getMessage(),
  280. ];
  281. }
  282. }
  283. public static function gid2Jst($id) {
  284. return 'cyy_' . $id;
  285. }
  286. public static function skuid2Jst($store_id, $goods, $goods_attr_item = []) {
  287. $attr_ids = array_column($goods_attr_item['attr_list'] ?? $goods_attr_item, 'attr_id');
  288. sort($attr_ids);
  289. $sku_attr_ids = implode('_', $attr_ids);
  290. $attrs = json_decode($goods['attr'], true);
  291. $sku_attr = [];
  292. foreach($attrs as $attr){
  293. $attr_ids = array_column($attr['attr_list'], 'attr_id');
  294. sort($attr_ids);
  295. $item_attr_ids = implode('_', $attr_ids);
  296. if($item_attr_ids === $sku_attr_ids){
  297. $sku_attr = $attr;
  298. }
  299. }
  300. // $skuRule = self::getConf($store_id, 'skuRule');
  301. // if($skuRule == 0){
  302. // return self::skuid2Jst1($goods, $sku_attr);
  303. // }
  304. // if($skuRule == 2){
  305. // return self::skuid2Jst2($goods, $sku_attr);
  306. // }
  307. return self::skuid2Jst2($goods, $sku_attr);
  308. }
  309. public static function skuid2Jst1($goods, $goods_attr_item = []) {
  310. $attr_ids = array_column($goods_attr_item['attr_list'] ?? $goods_attr_item, 'attr_id');
  311. sort($attr_ids);
  312. return 'cyy_' . $goods['id'] . '_' . implode('_', $attr_ids);
  313. }
  314. public static function skuid2Jst2($goods, $goods_attr_item = []) {
  315. return $goods_attr_item['no'];
  316. }
  317. public static function skuv2Jst($id, $goods_attr_item = []) {
  318. $attr_names = array_column($goods_attr_item['attr_list'] ?? $goods_attr_item, 'attr_name', 'attr_id');
  319. return implode(';', array_values($attr_names));
  320. }
  321. public static function __queryJstGoods($store_id = 0, $goods_ids = []) {
  322. try{
  323. if(!self::isopen($store_id)){
  324. return [
  325. 'code' => 1,
  326. 'msg' => '未配置此功能',
  327. ];
  328. // throw new \Exception('未配置此功能');
  329. }
  330. if(!$goods_ids){
  331. throw new \Exception('查询参数为空');
  332. }
  333. foreach($goods_ids as &$item){
  334. $item = self::gid2Jst($item);
  335. }
  336. $data = [
  337. 'i_ids' => $goods_ids,
  338. ];
  339. $response = self::api($store_id, ServeHttp::QUERY_MALL_ITEM,$data);
  340. return $response;
  341. } catch (\Exception $ex) {
  342. \Yii::error($ex);
  343. return [
  344. 'code' => 1,
  345. 'msg' => $ex->getMessage(),
  346. ];
  347. }
  348. }
  349. public static function queryJstGoodsQty($store_id = 0, $i_ids = []) {
  350. try{
  351. if(!self::isopen($store_id)){
  352. return [
  353. 'code' => 1,
  354. 'msg' => '未配置此功能',
  355. ];
  356. // throw new \Exception('未配置此功能');
  357. }
  358. $query['i_ids'] = implode(',', $i_ids);
  359. $response = self::api($store_id, ServeHttp::QUERY_INVENTORY,$query);
  360. if($response['data'] && $response['data']['inventorys']){
  361. foreach($response['data']['inventorys'] as &$qtyList){
  362. //可用数:可用数[同步线上的库存数]=主仓实际库存-订单占有数+虚拟库存+采购在途(业务设置)+进货仓(业务设置)+销退仓库存(业务设置)
  363. $qtyList['__online_qty'] = $qtyList['qty'] - $qtyList['order_lock'] + $qtyList['virtual_qty'] + $qtyList['purchase_qty'] + $qtyList['in_qty'] + $qtyList['return_qty'];
  364. if($qtyList['__online_qty'] < 0){
  365. $qtyList['__online_qty'] = 0;
  366. }
  367. }
  368. }
  369. $response['query'] = $query;
  370. return $response;
  371. } catch (\Exception $ex) {
  372. \Yii::error($ex);
  373. return [
  374. 'code' => 1,
  375. 'msg' => $ex->getMessage(),
  376. ];
  377. }
  378. }
  379. public static function syncStoreJstGoodsQty($store_id, $goods) {
  380. //先更新对应云仓聚水潭商品
  381. if($goods->cloud_goods_id){
  382. $supplier = Supplier::findOne(['cloud_supplier_id' => $goods->cloud_supplier_id, 'is_delete' => 0, 'status' => 1]);
  383. if($supplier){
  384. self::syncSupplierJstGoodsQty($supplier->id, $goods->cloud_goods_id, $store_id, $goods);
  385. $goods->refresh();
  386. return;
  387. }
  388. }
  389. if(empty($goods->jst_goods_id)){
  390. return;
  391. }
  392. $attr_group_list = json_decode($goods->attr, true);
  393. if(empty($goods['jst_supplier_id'])){
  394. $qtyRes = self::queryJstGoodsQty($store_id, [$goods['goods_no']]);
  395. }else{
  396. // $qtyRes = self::queryJstSupplierGoodsQty($store_id, $goods['jst_supplier_id'], ['style_codes' => $goods['goods_no']]);
  397. $jstSupplierGoods = self::queryJstSupplierGoods($store_id, $goods['goods_no']);
  398. if($jstSupplierGoods['code'] != 0){
  399. return;
  400. }
  401. $qtyRes = $jstSupplierGoods['data']['__qty'];
  402. }
  403. $qtySkus = [];
  404. $updateAttrList = [];
  405. foreach($attr_group_list as &$attr_info){
  406. $qty = 0;
  407. if($qtyRes['data']['list']){
  408. foreach($qtyRes['data']['list'] as $iqty){
  409. if($iqty['item_code'] == $attr_info['no']){
  410. $qty = $iqty['stock'];
  411. break;
  412. }
  413. }
  414. }
  415. if($qtyRes['data']['inventorys']){
  416. foreach($qtyRes['data']['inventorys'] as $iqty){
  417. if($iqty['sku_id'] == $attr_info['no']){
  418. $qty = $iqty['__online_qty'];
  419. break;
  420. }
  421. }
  422. }
  423. $lockNum = self::lockLocalJstGoodsNumGet($store_id, $goods, $attr_info);
  424. $qty -= $lockNum;
  425. if($qty != $attr_info['num']){
  426. $updateAttrList[] = [
  427. 'attr_id_list' => array_column($attr_info['attr_list'], 'attr_id'),
  428. 'num' => $qty - $attr_info['num'],
  429. ];
  430. $attr_info['num'] = $qty;
  431. }
  432. }
  433. if($updateAttrList){
  434. $goods->numAdd($updateAttrList, 0);
  435. $goods['attr'] = json_encode($attr_group_list);
  436. debug_log([__FUNCTION__, '更新库存',$store_id, $goods['id'], $updateAttrList, $res, self::lockLocalJstGoodsNumList($store_id)], __CLASS__ . '.log');
  437. }else{
  438. debug_log([__FUNCTION__, '无需更新库存',$store_id, $goods['id'], $updateAttrList, $res, self::lockLocalJstGoodsNumList($store_id)], __CLASS__ . '.log');
  439. }
  440. }
  441. public static function syncSupplierJstGoodsQty($supplier_id, $goods, $store_id = 0, $store_goods = null) {
  442. self::initStoreType(self::STORE_TYPE_SUPPLIER);
  443. if(is_int($goods) || is_string($goods)){
  444. $gid = $goods;
  445. $glist = (new PlatformForm(['id' => $gid]))->goodsInfo();
  446. if($glist['code'] == 0 && $glist['data']['count'] == 1){
  447. $goods = $glist['data']['list'][0];
  448. }else{
  449. throw new \Exception('云仓接口:' . $glist['msg']);
  450. }
  451. $goods['attr'] = $goods['attrs'];
  452. }
  453. if(empty($goods['jst_goods_id'])){
  454. return $goods;
  455. }
  456. $goods['__attrs'] = $goods['attrs'];
  457. $attr_group_list = json_decode($goods['attrs'], true);
  458. if(empty($goods['jst_supplier_id'])){
  459. $qtyRes = self::queryJstGoodsQty($supplier_id, [$goods['goods_no']]);
  460. }else{
  461. // $qtyRes = self::queryJstSupplierGoodsQty($supplier_id, $goods['jst_supplier_id'], ['style_codes' => $goods['goods_no']]);
  462. $jstSupplierGoods = self::queryJstSupplierGoods($supplier_id, $goods['goods_no']);
  463. if($jstSupplierGoods['code'] != 0){
  464. return;
  465. }
  466. $qtyRes = $jstSupplierGoods['data']['__qty'];
  467. }
  468. $store_goods_attr = (array)json_decode($store_goods['attr'], true);
  469. $updateAttrListStore = 0;
  470. $qtySkus = [];
  471. $updateAttrList = [];
  472. foreach($attr_group_list as &$attr_info){
  473. $qty = 0;
  474. if($qtyRes['data']['list']){
  475. foreach($qtyRes['data']['list'] as $iqty){
  476. if($iqty['item_code'] == $attr_info['no']){
  477. $qty = $iqty['stock'];
  478. break;
  479. }
  480. }
  481. }
  482. if($qtyRes['data']['inventorys']){
  483. foreach($qtyRes['data']['inventorys'] as $iqty){
  484. if($iqty['sku_id'] == $attr_info['no']){
  485. $qty = $iqty['__online_qty'];
  486. break;
  487. }
  488. }
  489. }
  490. $lockNum = self::lockLocalJstGoodsNumGet($supplier_id, $goods, $attr_info);
  491. $qty -= $lockNum;
  492. if($qty != $attr_info['num']){
  493. $updateAttrList[] = [
  494. 'attr_id_list' => array_column($attr_info['attr_list'], 'attr_id'),
  495. 'num' => $qty - $attr_info['num'],
  496. ];
  497. $attr_info['num'] = $qty;
  498. }
  499. foreach($store_goods_attr as $si => $sattr){
  500. if($attr_info['no'] == $sattr['no']){
  501. if($attr_info['num'] != $sattr['num']){
  502. $updateAttrListStore = 1;
  503. $store_goods_attr[$si]['num'] = $attr_info['num'];
  504. }
  505. break;
  506. }
  507. }
  508. }
  509. if($updateAttrListStore || $updateAttrList){
  510. $goods['attrs'] = json_encode($attr_group_list);
  511. $form = new SupplierForm();
  512. $res = $form->setGoodsAttrNum($supplier_id, $goods['id'], $updateAttrList, $store_id);
  513. debug_log([__FUNCTION__, '更新云仓库存',$supplier_id, $goods['id'], $updateAttrList, $res, self::lockLocalJstGoodsNumList($supplier_id)], __CLASS__ . '.log');
  514. }else{
  515. debug_log([__FUNCTION__, '无需更新云仓库存',$supplier_id, $goods['id'], $updateAttrList, $res, self::lockLocalJstGoodsNumList($supplier_id)], __CLASS__ . '.log');
  516. }
  517. return $goods;
  518. }
  519. public static function queryJstGoodsList($store_id = 0, $query = [], $get_qty = 0) {
  520. try{
  521. $get_qty = $get_qty ? $get_qty : $query['get_qty'];
  522. if(!self::isopen($store_id)){
  523. return [
  524. 'code' => 1,
  525. 'msg' => '未配置此功能',
  526. ];
  527. // throw new \Exception('未配置此功能');
  528. }
  529. // $query['i_ids'] = ['cyy_2621'];
  530. // $query['item_flds'] = ['onsale'];
  531. // $query['only_item'] = true;
  532. $query['modified_begin'] = $query['modified_begin'] ?? date('Y-m-d 00:00:00', time() - 86400 * 6);
  533. $query['modified_end'] = $query['modified_end'] ?? date('Y-m-d 23:59:59');
  534. if($query['i_ids']){
  535. unset($query['modified_begin']);
  536. unset($query['modified_end']);
  537. }
  538. $response = self::api($store_id, ServeHttp::QUERY_MALL_ITEM,$query);
  539. if($response['code'] == 0 && $response['data']['data_count']){
  540. foreach($response['data']['datas'] as &$item){
  541. $item['qty'] = 0;
  542. if($get_qty){
  543. $qty = self::queryJstGoodsQty($store_id, [$item['i_id']]);
  544. $item['__qty'] = $qty;
  545. }
  546. $item['pics'] = $item['pics'] ?? [$item['pic']];
  547. $item['onsale'] = 0;
  548. if($item['skus']){
  549. foreach($item['skus'] as &$v){
  550. $v['properties_value'] = $v['properties_value'] ? : '默认';
  551. if($v['enabled'] == 1){
  552. $item['onsale'] = 1;
  553. }
  554. $v['qty'] = 0;
  555. if($get_qty && $qty['data']['inventorys']){
  556. foreach($qty['data']['inventorys'] as $sku){
  557. if($v['sku_id'] == $sku['sku_id']){
  558. $v['qty'] = $sku['__online_qty'];
  559. }
  560. }
  561. }
  562. $item['qty'] += $v['qty'];
  563. }
  564. }
  565. $item['_attr'] = self::handleAttr($item);
  566. }
  567. }
  568. $response['query'] = $query;
  569. $response['_args'] = func_get_args();
  570. $response['$get_qty'] = $get_qty;
  571. return $response;
  572. } catch (\Exception $ex) {
  573. \Yii::error($ex);
  574. return [
  575. 'code' => 1,
  576. 'msg' => $ex->getMessage(),
  577. ];
  578. }
  579. }
  580. public static function queryJstGoods($store_id = 0, $i_id = '') {
  581. $query['i_ids'] = [$i_id];
  582. $list = self::queryJstGoodsList($store_id, $query, 1);
  583. if($list['code'] != 0){
  584. return $list;
  585. }
  586. if($list['data']['datas']){
  587. return [
  588. 'code' => 0,
  589. 'data' => $list['data']['datas'][0],
  590. ];
  591. }
  592. return [
  593. 'code' => 1,
  594. 'msg' => '商品未查到1',
  595. ];
  596. }
  597. public static function queryJstSupplierGoodsList($store_id = 0, $jst_supplier_name = '', $query = [], $get_goods_info = 0, $get_qty = 0) {
  598. try{
  599. $get_qty = $get_qty ? $get_qty : $query['get_qty'];
  600. $get_goods_info = $get_goods_info ? $get_goods_info : $query['get_goods_info'];
  601. if(!self::isopen($store_id)){
  602. return [
  603. 'code' => 1,
  604. 'msg' => '未配置此功能',
  605. ];
  606. // throw new \Exception('未配置此功能');
  607. }
  608. $query['page_num'] = $query['page_num'] ?? 1;
  609. $query['page_size'] = $query['page_size'] ?? 20; //每页条数 最多100
  610. $query['sort'] = 'DESC';
  611. // $query['creator'] = '创建人';
  612. // $query['style_code_list'] = ['cyy_2621']; //款号列表 精确搜索,一次最多20个
  613. // $query['item_code_list'] = ['cyy_2621_602']; //商品编码 精确搜索,一次最多20个
  614. // $query['item_name'] = '测试商品1'; //商品名称 模糊搜索
  615. // $query['distributor_supplier_name'] = '代发供应商名称';
  616. // $query['created_start_time'] = '2022-06-30 13:37:32';
  617. // $query['created_end_time'] = '2022-06-30 13:37:32';
  618. // $query['update_start_time'] = '2022-06-30 13:37:32';
  619. // $query['update_end_time'] = '2022-06-30 13:37:32';
  620. $jst_supplier_name && $query['distributor_supplier_name'] = $jst_supplier_name;
  621. $response = self::api($store_id, '/open/api/goods/inneropen/goods/querygoodslist',$query);
  622. // $response = json_decode('{"msg": null,"code": 0,"data": {"total": 126,"has_next": true,"list": [{"perfect_platform_list": [{"perfect": true,"platform_type": "taobao"}],"creator": "徐银","min_sale_price": 7.9,"distributor_supplier_name": "湖南优点购商贸有限公司","created": "2024-03-11 12:57:55","style_code": "469865549258","item_name": "麻辣王子辣条零食网红小零食小吃批发零售独立包装休闲食品面筋","main_image_list": ["https://images.sursung.com/prod/pt/10968313/20231002/5710B7DCE9AF4838A2B241272C02DDA6_s182221_h800_w800.jpg","https://images.sursung.com/prod/pt/10968313/20231002/B51B392CDD5B4F26AF01DA4B5778CE29_s164513_h800_w800.jpg","https://images.sursung.com/prod/pt/10968313/20231002/2048ABE808B64640B6CC3C5EB5F2AFC9_s220785_h800_w800.jpg","https://images.sursung.com/prod/pt/10968313/20231002/58ED2CB6301345088CD75918D9628D5E_s144894_h800_w800.jpg","https://images.sursung.com/prod/pt/10968313/20231002/0F7409C0B4634758AF650BF412E53B92_s146490_h800_w800.jpg"],"min_cost_price": null,"item_spu_id": "C10592F30B0A4A63BD38EB4C9F52E69B","item_status": "distribution","distributor_status": true,"item_source": "FROM_SUP","distributor_co_id": "10968313","private_status": true,"updated": "2024-03-11 13:37:29","min_supply_price": 5},{"perfect_platform_list": [{"perfect": true,"platform_type": "taobao"}],"creator": "徐银","min_sale_price": 49,"distributor_supplier_name": "","created": "2024-03-07 16:55:12","style_code": "24260036030","item_name": "春季新款圆领纯棉休闲刺绣套头舒适卫衣66913 | 24260036030","main_image_list": ["https://images.sursung.com/prod/item/13492662/20240307/C4B461F6770D484896E5EC0A3C6C5275_s194132_h1280_w1280.jpg"],"min_cost_price": 23,"item_spu_id": "7A96FC0F898F40B2A69EE9F6B083AC54","item_status": "distribution","distributor_status": false,"item_source": "ERP_SPU","distributor_co_id": null,"private_status": true,"updated": "2024-03-08 13:33:01","min_supply_price": 28},{"perfect_platform_list": [{"perfect": true,"platform_type": "taobao"}],"creator": "徐银","min_sale_price": 49,"distributor_supplier_name": "","created": "2024-03-07 16:55:12","style_code": "24260046030","item_name": "春季新款圆领纯棉休闲刺绣套头舒适卫衣66901 | 24260046030","main_image_list": ["https://images.sursung.com/prod/item/13492662/20240307/96D0A81839A2493383B91ADC7B2B341C_s211325_h1280_w1280.jpg"],"min_cost_price": 23,"item_spu_id": "825D42C894DE45D1907E590F98E24338","item_status": "distribution","distributor_status": false,"item_source": "ERP_SPU","distributor_co_id": null,"private_status": true,"updated": "2024-03-08 12:25:39","min_supply_price": 28},{"perfect_platform_list": [{"perfect": true,"platform_type": "taobao"}],"creator": "徐银","min_sale_price": 49,"distributor_supplier_name": "","created": "2024-03-07 16:55:11","style_code": "24260076030","item_name": "春季新款圆领纯棉休闲刺绣套头舒适卫衣66922 | 24260076030","main_image_list": ["https://images.sursung.com/prod/item/13492662/20240307/861BF4F2E8B649CD81E65D54A3626A38_s194411_h1280_w1280.jpg"],"min_cost_price": 23,"item_spu_id": "42EE1CDE64024140847658A7268625BA","item_status": "distribution","distributor_status": false,"item_source": "ERP_SPU","distributor_co_id": null,"private_status": true,"updated": "2024-03-08 11:47:00","min_supply_price": 28},{"perfect_platform_list": [{"perfect": true,"platform_type": "taobao"}],"creator": "徐银","min_sale_price": 49,"distributor_supplier_name": "","created": "2024-03-07 16:55:11","style_code": "24260056030","item_name": "春季新款圆领纯棉休闲刺绣套头舒适卫衣66903 | 24260056030","main_image_list": ["https://images.sursung.com/prod/item/13492662/20240307/7C5F67FBFA4C43B99DDD4C530F1D6264_s204319_h1280_w1280.jpg"],"min_cost_price": 23,"item_spu_id": "656BA60B3B1B4201BBDC61B8B9B218E2","item_status": "distribution","distributor_status": false,"item_source": "ERP_SPU","distributor_co_id": null,"private_status": true,"updated": "2024-03-08 11:56:08","min_supply_price": 28},{"perfect_platform_list": [{"perfect": true,"platform_type": "taobao"}],"creator": "徐银","min_sale_price": 49,"distributor_supplier_name": "","created": "2024-03-07 16:55:11","style_code": "24260066030","item_name": "春季新款圆领纯棉休闲刺绣套头舒适卫衣66915 | 24260066030","main_image_list": ["https://images.sursung.com/prod/item/13492662/20240307/0CA44A67A75C4BA7BD45DBB9B8D47769_s226359_h1280_w1280.jpg"],"min_cost_price": 24,"item_spu_id": "891E32837A464C8088393E1372DB89E8","item_status": "distribution","distributor_status": false,"item_source": "ERP_SPU","distributor_co_id": null,"private_status": true,"updated": "2024-03-08 13:11:22","min_supply_price": 29},{"perfect_platform_list": [{"perfect": true,"platform_type": "taobao"}],"creator": "徐银","min_sale_price": 48,"distributor_supplier_name": "","created": "2024-03-07 16:53:36","style_code": "24230136030","item_name": "H*家西裤男斜纹竹炭纤维舒适商务休闲男裤A12 | 24230136030","main_image_list": ["https://images.sursung.com/prod/item/13492662/20240307/CCBE49C37EFB4FDAB99145A9A5C9E854_s186526_h1701_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/4B4D6D4BD9254F4EA488E20205A8FB96_s190233_h1631_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/50ADE8CA42BF47F3B5CAF915522A9B9A_s193943_h1649_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/CFC060FB235A4DDA85A2451CCBE066BB_s421984_h1526_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/C88B1FFDF79B451EB75AEB66ED74A8CF_s457301_h1615_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/64D05779E5A54E9D9E1E2A4F441BFDB4_s258059_h1358_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/A386322950034DB0AA233248055C7DA5_s322777_h1356_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/43A1754A18A144EF93925FE0BBD73144_s508934_h1364_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/16B5F14917624D3B82A3E8291397DF49_s215763_h685_w1260.jpg"],"min_cost_price": 21,"item_spu_id": "4D630D58552848D2ACFFFDAF2554A449","item_status": "distribution","distributor_status": false,"item_source": "ERP_SPU","distributor_co_id": null,"private_status": true,"updated": "2024-03-08 11:37:33","min_supply_price": 26},{"perfect_platform_list": [{"perfect": true,"platform_type": "taobao"}],"creator": "徐银","min_sale_price": 79,"distributor_supplier_name": "","created": "2024-03-07 16:53:36","style_code": "24310416060","item_name": "H*家夹克男双面穿字母印花抽绳连帽工装顺滑外套男春季AVA | 24310416060","main_image_list": ["https://images.sursung.com/prod/item/13492662/20240307/22423970A6DF42CF9A7CCA819E5C2038_s242182_h1378_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/4911439C485B4135BFB3703300ADF4FF_s205627_h1349_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/1286A3354E8E41BCB208B3FB40197A6B_s465277_h1398_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/4DAA39ED00604448BB31C7698CB91C8E_s96942_h667_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/A43161123E8144B4B44B4544330B11A0_s335129_h1281_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/BDDA2A2EE0934C3BBCE62BF66AB6EEB1_s510854_h1491_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/3C4FD4D970AE407CB0B5EFF8ED05275D_s450168_h2058_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/EF3CF4CDEC914E30A2DDECD4AEA14A0B_s148401_h846_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/0F7F1CA22F4B4470B86348E0FB4AF0C6_s290693_h944_w1260.jpg"],"min_cost_price": 50,"item_spu_id": "A40E0BC49E7B480A98B0CE062F244EAE","item_status": "distribution","distributor_status": false,"item_source": "ERP_SPU","distributor_co_id": null,"private_status": true,"updated": "2024-03-07 18:03:35","min_supply_price": 55},{"perfect_platform_list": [{"perfect": true,"platform_type": "taobao"}],"creator": "徐银","min_sale_price": 48,"distributor_supplier_name": "","created": "2024-03-07 16:53:36","style_code": "24230146036","item_name": "H*家休闲裤男夏季抽绳松紧腰裤腿字母运动九分裤TER | 24230146036","main_image_list": ["https://images.sursung.com/prod/item/13492662/20240307/3926F792AAEC4D1A8790CCCE1E337024_s72042_h1274_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/657759FD9120435EA6BAB2E866D6811A_s74270_h1293_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/088CD4CB2C2F467183791EC23628CB25_s238601_h1227_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/D51C4AAAA407439991DEBE965F95B09E_s247025_h1151_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/42D9A6FC263148EAA4AECE7A9CD3E80D_s222941_h1462_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/EDC20AE52B9C4A11A5572B38B99272B7_s187824_h1483_w1260.jpg","https://images.sursung.com/prod/item/13492662/20240307/67B778C72CF64F6F84A989F6E1AE0E55_s313012_h1134_w1260.jpg"],"min_cost_price": 27,"item_spu_id": "F9705E8AEC2F487A9945EA5B64E16D45","item_status": "distribution","distributor_status": false,"item_source": "ERP_SPU","distributor_co_id": null,"private_status": true,"updated": "2024-03-08 10:41:36","min_supply_price": 32},{"perfect_platform_list": [{"perfect": true,"platform_type": "taobao"}],"creator": "徐银","min_sale_price": 139,"distributor_supplier_name": "","created": "2024-03-04 11:32:07","style_code": "2431016095","item_name": "春季专柜同步款翻领外套净色时尚休闲男暗扣夹克外套ZK8922 | 2431016095","main_image_list": ["https://images.sursung.com/prod/item/13492662/20240304/3A37A5B2262244F2B2CA96DF68F4CBFB_s311700_h1536_w1536.jpg","https://images.sursung.com/prod/item/13492662/20240304/7311EFD7A64F4891ACCA55944A058A14_s320340_h1536_w1536.jpg","https://images.sursung.com/prod/item/13492662/20240304/1A55E5BA63544FEE9B6B062179236794_s533254_h1536_w1536.jpg","https://images.sursung.com/prod/item/13492662/20240304/5A5D0AA5EC024BB39B9AA020D67BB797_s566833_h1536_w1536.jpg","https://images.sursung.com/prod/item/13492662/20240304/69406076596F4CBD884CD2EC5CBC2F98_s575510_h1536_w1536.jpg","https://images.sursung.com/prod/item/13492662/20240304/91F1ACA69FF84436B53B141058BFCB3E_s577732_h1536_w1536.jpg","https://images.sursung.com/prod/item/13492662/20240304/328B235BB6C54D56BDD374D1C26BC672_s499824_h1536_w1536.jpg","https://images.sursung.com/prod/item/13492662/20240304/2D1D3CBB2F824BE886CFD2E5C9A22C2B_s364101_h1536_w1536.jpg","https://images.sursung.com/prod/item/13492662/20240304/0B7514E9F91D49A7BE5F571E57F5A86A_s202425_h995_w1260.jpg"],"min_cost_price": 85,"item_spu_id": "294A875FCE8A419E90CEB72721461527","item_status": "distribution","distributor_status": false,"item_source": "ERP_SPU","distributor_co_id": null,"private_status": true,"updated": "2024-03-04 13:58:21","min_supply_price": 90}]},"request_id": "c2d5c993a3f4432e89e3b0d5db561bb9","query": {"page_num": "1","page_size": "10","sort": "DESC"},"_args": [1,{"page_num": "1","page_size": "10","sort": "DESC"},null],"$get_qty": null}', true);
  623. if($response['code'] == 0 && $response['data']['total']){
  624. foreach($response['data']['list'] as &$item){
  625. $style_code = $item['style_code'];
  626. $supplier_co_id = 0;
  627. if($item['distributor_status']){
  628. $supplier_co_id = $item['distributor_co_id'];
  629. }
  630. $item['_supplier_co_id'] = $supplier_co_id;
  631. $item['onsale'] = 0;
  632. if($item['item_status'] == 'distribution' && $item['private_status'] == false){
  633. $item['onsale'] = 1;
  634. }
  635. $item['qty'] = 0;
  636. $item['__qty'] = [];
  637. $item['__goods_info'] = [];
  638. if($get_goods_info){
  639. $item['__goods_info'] = self::queryJstSupplierGoodsInfo($store_id, $item['item_spu_id']);
  640. $ginfo = $item['__goods_info']['data'];
  641. if($get_qty && $ginfo['item_sku_list']){
  642. if($supplier_co_id){
  643. $item_codes = array_column($ginfo['item_sku_list'], 'item_code');
  644. $item['__qty'] = self::queryJstSupplierGoodsQty($store_id, $supplier_co_id, ['item_codes' => implode(',', $item_codes)]);
  645. }else{
  646. $item['__qty'] = self::queryJstGoodsQty($store_id, [$style_code]);
  647. }
  648. $item['_attr'] = self::supplierGoodsHandleAttr($item);
  649. foreach($item['_attr'] as &$v){
  650. $item['qty'] += $v['num'];
  651. }
  652. }
  653. }
  654. }
  655. }
  656. $response['$store_type'] = self::$store_type;
  657. $response['getConf'] = self::getConf($store_id);
  658. $response['query'] = $query;
  659. $response['_args'] = func_get_args();
  660. $response['$get_qty'] = $get_qty;
  661. $response['$get_goods_info'] = $get_goods_info;
  662. return $response;
  663. } catch (\Exception $ex) {
  664. \Yii::error($ex);
  665. return [
  666. 'code' => 1,
  667. 'msg' => $ex->getMessage(),
  668. ];
  669. }
  670. }
  671. public static function queryJstSupplierGoodsInfo($store_id = 0, $item_spuId = '') {
  672. try{
  673. $k = [$store_id, $item_spuId];
  674. $v = cache()->get($k);
  675. if($v){
  676. return $v;
  677. }
  678. $query['item_spuId'] = $item_spuId;
  679. $response = self::api($store_id, '/open/api/goods/inneropen/goods/querygoodsdetail',$query);
  680. $response['query'] = $query;
  681. cache()->set($k, $response, 3600);
  682. return $response;
  683. } catch (\Exception $ex) {
  684. \Yii::error($ex);
  685. return [
  686. 'code' => 1,
  687. 'msg' => $ex->getMessage(),
  688. ];
  689. }
  690. }
  691. public static function queryJstSupplierGoodsQty($store_id = 0, $supplier_co_id = 0, $query = []) {
  692. try{
  693. $query['supplier_co_id'] = $supplier_co_id;
  694. // $query['style_codes'] = $style_code;
  695. // $query['item_codes'] = $item_code;
  696. $query['page_num'] = $query['page_num'] ?? 1;
  697. $query['page_size'] = $query['page_size'] ?? 500; //每页条数 最多500
  698. $query['order_by_key'] = $query['order_by_key'] ?? 1;
  699. $response = self::api($store_id, '/open/api/goods/inneropen/supplier/goods/querydiserpgoodsdata',$query);
  700. $response['query'] = $query;
  701. return $response;
  702. } catch (\Exception $ex) {
  703. \Yii::error($ex);
  704. return [
  705. 'code' => 1,
  706. 'msg' => $ex->getMessage(),
  707. ];
  708. }
  709. }
  710. public static function queryJstSupplierGoods($store_id = 0, $style_code = '') {
  711. $query['style_code_list'] = [$style_code];
  712. $list = self::queryJstSupplierGoodsList($store_id, '', $query, 1, 1);
  713. if($list['code'] != 0){
  714. return $list;
  715. }
  716. if($list['data']['list']){
  717. return [
  718. 'code' => 0,
  719. 'data' => $list['data']['list'][0],
  720. ];
  721. }
  722. return [
  723. 'code' => 1,
  724. 'msg' => '商品未查到',
  725. 'data' => $list,
  726. ];
  727. }
  728. public static function queryJstSupplierList($store_id = 0, $query = []) {
  729. try{
  730. $query['status'] = 2;
  731. $query['page_num'] = $query['page_num'] ?? 1;
  732. $query['page_size'] = $query['page_size'] ?? 100; //每页数量,最大100
  733. $response = self::api($store_id, '/open/api/company/inneropen/partner/channel/querymysupplier',$query);
  734. $response['query'] = $query;
  735. return $response;
  736. } catch (\Exception $ex) {
  737. \Yii::error($ex);
  738. return [
  739. 'code' => 1,
  740. 'msg' => $ex->getMessage(),
  741. ];
  742. }
  743. }
  744. public static function goods2Jst($store_id = 0, $start_id = 0, $count = 1, $upType = 0) {
  745. try{
  746. if(!self::isopen($store_id)){
  747. return [
  748. 'code' => 1,
  749. 'msg' => '未配置此功能',
  750. ];
  751. // throw new \Exception('未配置此功能');
  752. }
  753. $upload_goods = self::getConf($store_id, 'upload_goods');
  754. if(!$upload_goods){
  755. return [
  756. 'code' => 1,
  757. 'msg' => 'upload_goods close',
  758. ];
  759. }
  760. $query = Goods::find()->where(['>=', 'id', $start_id])->orderBy('id ASC');
  761. $query->andWhere(['store_id' => $store_id, 'is_delete' => 0]);
  762. $cacheKey = 'goods2Jst' . $store_id;
  763. if($count == -1){
  764. $count = $query->count();
  765. $cacheV = cache()->get($cacheKey);
  766. if($cacheV){
  767. throw new \Exception('已有队列进行中,请在' . ($cacheV - time()) . '秒后重试');
  768. }
  769. }
  770. if($count > 1){
  771. $time = round($count * 1.5);
  772. cache()->set($cacheKey, time() + $time, $time);
  773. }
  774. $icount = 5;
  775. $icount = $count < $icount ? $count : $icount;
  776. $list = $query->limit($icount)->all();
  777. $max = 50;
  778. $data = [];
  779. $upcount = 0;
  780. $upGoodsIds = [];
  781. foreach($list as $item){
  782. $c_name = null;
  783. $goods_cat = GoodsCat::findOne([ 'goods_id' => $item['id'], 'is_delete' => 0 ]);
  784. if($goods_cat){
  785. $cat = Cat::findOne($goods_cat->cat_id);
  786. $c_name = self::cname2Jst($cat);
  787. }
  788. // $supplier = Supplier::findOne(['cloud_supplier_id'=>$item['cloud_supplier_id']]);
  789. $_data = [
  790. 'batch_enabled' => false,
  791. 'i_id' => self::gid2Jst($item->id),
  792. 'name' => $item->name,
  793. 'pic' => $item->cover_pic,
  794. 'unit' => $item['unit'],
  795. 'weight' => $item['weight'],
  796. 'enabled' => $item['status'] == 1 ? 1 : -1,
  797. 'c_price' => $item['cost_price'],
  798. // 'supplier_name' => $supplier->supplier_name ?: '',
  799. // 'supplier_i_id' => $item['cloud_goods_id'],
  800. ];
  801. $c_name && $_data['c_name'] = $c_name;
  802. $skus = json_decode($item->attr, true);
  803. if($upcount && (count($data) + count($skus) >= $max)){
  804. break;
  805. }
  806. foreach(array_slice($skus, 0, 50) as $sku){
  807. $sku_id = self::skuid2Jst($store_id, $item, $sku);
  808. $sku_name = self::skuv2Jst($item->id, $sku);
  809. $_data = array_merge($_data, [
  810. 'sku_id' => $sku_id,
  811. 's_price' => $sku['price'],
  812. 'sku_pic' => $sku['pic'] ?: $item['cover_pic'],
  813. 'sku_code' => $sku['no'],
  814. 'qty' => $sku['num'],
  815. 'properties_value' => $sku_name,
  816. ]);
  817. $data[] = $_data;
  818. }
  819. $upcount++;
  820. $upGoodsIds[] = $item->id;
  821. $start_id = $item->id;
  822. if(count($data) >= $max){
  823. break;
  824. }
  825. }
  826. if($upType == 0){
  827. $updata = [
  828. 'items' => $data,
  829. ];
  830. $response = self::api($store_id, ServeHttp::UPLOAD_ITEMSKU, $updata);
  831. if($response['code']){
  832. debug_log(['goods2Jst', $response, $upGoodsIds], __CLASS__ . '.log');
  833. }
  834. if(isset($response['datas'])){
  835. foreach($response['datas'] as $item){
  836. if(!$item['issuccess']){
  837. debug_log(['goods2Jst', $item], __CLASS__ . '.log');
  838. }
  839. }
  840. }
  841. }
  842. if($upType == 1){
  843. $updata = [
  844. 'items' => $data,
  845. 'so_id' => self::gid2Jst($item->id) . time(),
  846. 'warehouse' => 1,
  847. 'is_confirm' => 1,
  848. 'type' => 'check',
  849. ];
  850. $response = self::api($store_id, ServeHttp::UPLOAD_INVENTORY_V2, $updata);
  851. if($response['code']){
  852. debug_log(['goods2Jst $upType == 1', $response, $upGoodsIds], __CLASS__ . '.log');
  853. }
  854. }
  855. if($count > 1 && ($count - $upcount > 1)){
  856. $queue = queue_push(new \app\jobs\jushuitan\Goods2JSTJob([
  857. 'id' => $store_id,
  858. 'start_id' => $start_id + 1,
  859. 'count' => $count - $upcount,
  860. 'upType' => $upType,
  861. ]), 1);
  862. }
  863. sleep(1);
  864. return $response;
  865. } catch (\Exception $ex) {
  866. \Yii::error($ex);
  867. return [
  868. 'code' => 1,
  869. 'msg' => $ex->getMessage(),
  870. ];
  871. }
  872. }
  873. public static function cname2Jst($cat, $cids = []) {
  874. $cname = 'cyy' . str_replace('-', '_', $cat['name']);
  875. if(in_array($cat['id'], $cids)){
  876. throw new \Exception('层级嵌套错误');
  877. }
  878. $cids[] = $cat['id'];
  879. if($cat['parent_id'] == 0){
  880. $name = $cname;
  881. }else{
  882. $pcat = Cat::findOne(['id' => $cat['parent_id'], 'is_delete' => 0]);
  883. if(!$pcat){
  884. throw new \Exception('父级已删除');
  885. }
  886. $name = self::cname2Jst($pcat, $cids) . '-' . $cname;
  887. }
  888. return $name;
  889. }
  890. public static function cat2Jst($store_id = 0, $start_id = 0, $count = 1) {
  891. try{
  892. if(!self::isopen($store_id)){
  893. return [
  894. 'code' => 1,
  895. 'msg' => '未配置此功能',
  896. ];
  897. // throw new \Exception('未配置此功能');
  898. }
  899. $upload_goods = self::getConf($store_id, 'upload_goods');
  900. if(!$upload_goods){
  901. return [
  902. 'code' => 1,
  903. 'msg' => 'upload_goods close',
  904. ];
  905. }
  906. $query = Cat::find()->where(['>=', 'id', $start_id])->orderBy('id ASC');
  907. $query->andWhere(['store_id' => $store_id, 'is_delete' => 0]);
  908. $msgExt = '';
  909. $cacheKey = 'cat2Jst' . $store_id;
  910. if($count == -1){
  911. $count = $query->count();
  912. $cacheV = cache()->get($cacheKey);
  913. if($cacheV){
  914. throw new \Exception('已有队列进行中,大约' . ($cacheV - time()) . '秒后执行完毕');
  915. }
  916. }
  917. if($count > 1){
  918. $time = round($count * 1.5);
  919. cache()->set($cacheKey, time() + $time, $time);
  920. $msgExt = '.大约' . $time . '秒后执行完毕';
  921. }
  922. $query->select('id, parent_id, name, sort');
  923. $list = $query->limit($count == 1 ? 1 : 2)->asArray()->all();
  924. if(count($list)){
  925. $item = $list[0];
  926. try{
  927. $c_name = self::cname2Jst($item);
  928. } catch (\Exception $ex) {
  929. debug_log(['cat2Jst$c_name$del', $item], __CLASS__ . '.log');
  930. \Yii::error([__FUNCTION__, $ex->getMessage(), $item]);
  931. $c_name = null;
  932. }
  933. if($c_name){
  934. $data = [
  935. 'c_name' => $c_name,
  936. 'sort' => $item['sort'],
  937. ];
  938. $response = self::api($store_id, '/open/jushuitan/category/upload', $data);
  939. if($response['code']){
  940. debug_log(['cat2Jst', $response, $data], __CLASS__ . '.log');
  941. }
  942. }
  943. }
  944. if($count > 1 && count($list) > 1){
  945. $queue = queue_push(new \app\jobs\jushuitan\Cat2JSTJob([
  946. 'id' => $store_id,
  947. 'start_id' => $list[1]['id'],
  948. 'count' => $count -1,
  949. ]), 1);
  950. }
  951. sleep(1);
  952. return [
  953. 'code' => 0,
  954. 'msg' => '操作成功,后台队列处理中。' . $msgExt,
  955. 'data' => $response,
  956. ];
  957. } catch (\Exception $ex) {
  958. \Yii::error($ex);
  959. return [
  960. 'code' => 1,
  961. 'msg' => $ex->getMessage(),
  962. ];
  963. }
  964. }
  965. public static function orderSingleQuery($store_id = 0, $orderNo = []) {
  966. if($orderNo){
  967. if(is_string($orderNo)){
  968. $orderNo = [$orderNo];
  969. }
  970. $shop_id = self::getConf($store_id, 'shop_id');
  971. $updata = [
  972. 'shop_id' => (int)$shop_id,
  973. 'so_ids' => $orderNo,
  974. ];
  975. $response = self::api($store_id, ServeHttp::QUERY_ORDERS_SINGLE, $updata);
  976. debug_log(['orderSingleQuery', $response, $orderNo, $updata], __CLASS__ . '.log');
  977. if($response['code']){
  978. debug_log(['orderSingleQuery', $response, $orderNo, $updata], __CLASS__ . '.log');
  979. }
  980. return $response;
  981. }
  982. return [
  983. 'code' => 0,
  984. 'msg' => '',
  985. ];
  986. }
  987. public static function orderLabelUpload($store_id = 0, $orderNo = '', $labels = [], $action_type = 1) {
  988. if($orderNo && $labels){
  989. $shop_id = self::getConf($store_id, 'shop_id');
  990. $updata = [
  991. 'shop_id' => (int)$shop_id,
  992. 'so_id' => (string)$orderNo,
  993. 'labels' => $labels,
  994. 'action_type' => (int)$action_type,
  995. ];
  996. $response = self::api($store_id, '/open/jushuitan/order/label/upload', $updata);
  997. debug_log(['orderLabelUpload', $response, $orderNo, $updata], __CLASS__ . '.log');
  998. if($response['code']){
  999. debug_log(['orderLabelUpload', $response, $orderNo, $updata], __CLASS__ . '.log');
  1000. return $response;
  1001. }
  1002. }
  1003. return [
  1004. 'code' => 0,
  1005. 'msg' => '',
  1006. ];
  1007. }
  1008. public static function order2Jst($store_id = 0, $start_id = 0, $count = 1) {
  1009. try{
  1010. if(!self::isopen($store_id)){
  1011. return [
  1012. 'code' => 1,
  1013. 'msg' => '未配置此功能',
  1014. ];
  1015. // throw new \Exception('未配置此功能' . json_encode(func_get_args()));
  1016. }
  1017. $query = Order::find()->where(['>=', 'id', $start_id])->orderBy('id ASC');
  1018. $query->andWhere(['store_id' => $store_id, 'is_delete' => 0]);
  1019. $cacheKey = 'order2Jst' . $store_id;
  1020. if($count == -1){
  1021. $count = $query->count();
  1022. $cacheV = cache()->get($cacheKey);
  1023. if($cacheV){
  1024. throw new \Exception('已有队列进行中,请在' . ($cacheV - time()) . '秒后重试');
  1025. }
  1026. }
  1027. if($count > 1){
  1028. $time = round($count * 1.5);
  1029. cache()->set($cacheKey, time() + $time, $time);
  1030. }
  1031. $shop_id = self::getConf($store_id, 'shop_id');
  1032. if(empty($shop_id)){
  1033. throw new \Exception('shop_id不存在,不能同步订单' . json_encode(func_get_args()));
  1034. }
  1035. $icount = 50;
  1036. $icount = $count < $icount ? $count : $icount;
  1037. $list = $query->limit($icount)->all();
  1038. $list = array_combine(array_column($list, 'order_no'), $list);
  1039. $max = 50;
  1040. $data = [];
  1041. $upcount = 0;
  1042. $upOrderIds = [];
  1043. foreach($list as $item){
  1044. $need_express = 1;
  1045. if($item['is_delivery'] || $item['is_offline']){
  1046. $need_express = 0;
  1047. }
  1048. $is_cod = $item['pay_type'] == Order::PAY_TYPE_COD;
  1049. $shop_status = self::orderStatusKey2Jst($item['trade_status']);
  1050. if(!$shop_status && !$is_cod){
  1051. continue;
  1052. }
  1053. $addres = json_decode($item['address_data'], true);
  1054. $ods = $item->detail;
  1055. $goodsList = [];
  1056. foreach ($ods as $od) {
  1057. $sku = json_decode($od->attr, true);
  1058. $goods = json_decode($od['goods_info'], true);
  1059. $sku_id = self::skuid2Jst($store_id, $goods, $sku);
  1060. $sku_name = self::skuv2Jst($od->goods_id, $sku);
  1061. $_data = [
  1062. 'sku_id' => $sku_id,
  1063. 'shop_sku_id' => $sku_id,
  1064. 'amount' => (float)$od['total_price'],
  1065. 'base_price' => (float)$od['total_price'],
  1066. 'qty' => $od['num'],
  1067. 'name' => $od->goods_name . '_' . $sku_name,
  1068. 'outer_oi_id' => md5($item['order_no'] . '_' . $sku_id),
  1069. ];
  1070. $goodsList[] = $_data;
  1071. }
  1072. $_data = [
  1073. 'shop_id' => (int)$shop_id,
  1074. 'so_id' => $item['order_no'],
  1075. 'order_date' => date('Y-m-d H:i:s', $item['created_at']),
  1076. 'shop_status' => $shop_status,
  1077. 'shop_buyer_id' => (string)$item['user_id'],
  1078. 'receiver_state' => $addres['province'] ?? '--',
  1079. 'receiver_city' => $addres['city'] ?? '--',
  1080. 'receiver_district' => $addres['district'] ?? '--',
  1081. 'receiver_address' => $addres['detail'] ?? '--',
  1082. 'receiver_name' => $item['name'] ?? '--',
  1083. 'receiver_phone' => $item['mobile'] ?? '--',
  1084. 'pay_amount' => (float)$item['pay_price'],
  1085. 'freight' => (float)$item['express_price'],
  1086. 'buyer_message' => $item['remark'],
  1087. 'is_cod' => $is_cod,
  1088. 'items' => $goodsList,
  1089. 'pay' => [
  1090. 'outer_pay_id' => $item['order_no'],
  1091. 'pay_date' => date('Y-m-d H:i:s', $item['pay_time']),
  1092. 'payment' => Order::PAY_TYP_NAME[$item['pay_type']] ?? '--',
  1093. 'seller_account' => '--',
  1094. 'buyer_account' => (string)$item['user_id'],
  1095. 'amount' => (float)$item['pay_price'],
  1096. ],
  1097. ];
  1098. if($item['express_no']){
  1099. $_data['l_id'] = $item['express_no'];
  1100. $_data['logistics_company'] = $item['express'] ?: '--';
  1101. }
  1102. $_data['labels'][] = 'cyy';
  1103. if($item['is_delivery']){
  1104. $_data['labels'][] = '同城配送';
  1105. }
  1106. if($item['is_offline']){
  1107. $_data['labels'][] = '自提';
  1108. }
  1109. if($is_cod){
  1110. $_data['labels'][] = '货到付款';
  1111. }
  1112. if($item['trans_status'] == 1){
  1113. $orderLabelUpload = self::orderLabelUpload($store_id, $item['order_no'], ['转单']);
  1114. }
  1115. if(!$need_express){
  1116. $_data['l_id'] = '-';
  1117. $_data['logistics_company'] = '其他';
  1118. }
  1119. $_data['labels'] = implode(',', $_data['labels']);
  1120. $data[] = $_data;
  1121. $upcount++;
  1122. $upOrderIds[] = $item->id;
  1123. $start_id = $item->id;
  1124. if(count($data) >= $max){
  1125. break;
  1126. }
  1127. }
  1128. $updata = $data;
  1129. if(empty($updata)){
  1130. throw new \Exception('提交数据为空' . json_encode(func_get_args()));
  1131. }
  1132. self::saveConf($store_id, ['upload_order_time_last' => time()]);
  1133. $response = self::api($store_id, ServeHttp::UPLOAD_ORDERS, $updata);
  1134. debug_log(['order2Jst', $response, $upOrderIds, $updata], __CLASS__ . '.log');
  1135. if($response['code']){
  1136. debug_log(['order2Jst', $response, $upOrderIds, $updata], __CLASS__ . '.log');
  1137. }
  1138. if(isset($response['data']) && isset($response['data']['datas'])){
  1139. foreach($response['data']['datas'] as $item){
  1140. if(!$item['issuccess']){
  1141. debug_log(['order2Jst', $item], __CLASS__ . '.log');
  1142. }else{
  1143. self::lockLocalJstGoodsNum($list[$item['so_id']], 1);
  1144. }
  1145. }
  1146. }
  1147. if($count > 1 && ($count - $upcount > 1)){
  1148. $queue = queue_push(new \app\jobs\jushuitan\Order2JSTJob([
  1149. 'id' => $store_id,
  1150. 'start_id' => $start_id + 1,
  1151. 'count' => $count - $upcount,
  1152. ]), 1);
  1153. }
  1154. sleep(1);
  1155. return $response;
  1156. } catch (\Exception $ex) {
  1157. \Yii::error($ex);
  1158. return [
  1159. 'code' => 1,
  1160. 'msg' => $ex->getMessage(),
  1161. ];
  1162. }
  1163. }
  1164. public static function orderRefund2Jst($store_id = 0, $start_id = 0, $count = 1) {
  1165. try{
  1166. if(!self::isopen($store_id)){
  1167. return [
  1168. 'code' => 1,
  1169. 'msg' => '未配置此功能',
  1170. ];
  1171. // throw new \Exception('未配置此功能' . json_encode(func_get_args()));
  1172. }
  1173. $query = OrderRefund::find()->where(['>=', 'id', $start_id])->orderBy('id ASC');
  1174. $query->andWhere(['store_id' => $store_id, 'is_delete' => 0]);
  1175. $cacheKey = 'orderRefund2Jst' . $store_id;
  1176. if($count == -1){
  1177. $count = $query->count();
  1178. $cacheV = cache()->get($cacheKey);
  1179. if($cacheV){
  1180. throw new \Exception('已有队列进行中,请在' . ($cacheV - time()) . '秒后重试');
  1181. }
  1182. }
  1183. if($count > 1){
  1184. $time = round($count * 1.5);
  1185. cache()->set($cacheKey, time() + $time, $time);
  1186. }
  1187. $shop_id = self::getConf($store_id, 'shop_id');
  1188. if(empty($shop_id)){
  1189. throw new \Exception('shop_id不存在,不能同步订单' . json_encode(func_get_args()));
  1190. }
  1191. $icount = 50;
  1192. $icount = $count < $icount ? $count : $icount;
  1193. $list = $query->limit($icount)->all();
  1194. $list = array_combine(array_column($list, 'order_refund_no'), $list);
  1195. $max = 50;
  1196. $data = [];
  1197. $upcount = 0;
  1198. $upOrderIds = [];
  1199. foreach($list as $item){
  1200. $o = Order::findOne($item['order_id']);
  1201. $ods = $item->detail;
  1202. $goodsList = [];
  1203. foreach ($ods as $od) {
  1204. $sku = json_decode($od->attr, true);
  1205. $goods = json_decode($od['goods_info'], true);
  1206. $sku_id = self::skuid2Jst($store_id, $goods, $sku);
  1207. $sku_name = self::skuv2Jst($od->goods_id, $sku);
  1208. $_data = [
  1209. 'sku_id' => $sku_id,
  1210. 'amount' => (float)$od['total_price'],
  1211. 'base_price' => (float)$od['total_price'],
  1212. 'qty' => $od['num'],
  1213. 'type' => '退货',
  1214. // 'name' => $od->goods_name . '_' . $sku_name,
  1215. 'outer_oi_id' => md5($o['order_no'] . '_' . $sku_id),
  1216. ];
  1217. $goodsList[] = $_data;
  1218. }
  1219. $good_status = 'BUYER_RECEIVED';
  1220. if($item['is_user_send']){
  1221. $good_status = 'BUYER_RETURNED_GOODS';
  1222. }
  1223. if($item['status'] == 1){
  1224. $good_status = 'SELLER_RECEIVED';
  1225. }
  1226. $shop_status = 'WAIT_SELLER_AGREE';
  1227. if($item['is_agree']){
  1228. $shop_status = 'WAIT_BUYER_RETURN_GOODS';
  1229. }
  1230. if($item['is_user_send']){
  1231. $shop_status = 'WAIT_SELLER_CONFIRM_GOODS';
  1232. }
  1233. if($item['status'] == OrderRefund::STATUS_REFUND_AGREE){
  1234. $shop_status = 'SUCCESS';
  1235. }
  1236. if($item['status'] == OrderRefund::STATUS_REFUSE){
  1237. $shop_status = 'SELLER_REFUSE_BUYER';
  1238. }
  1239. if($item['is_user_cancel'] == 1){
  1240. $shop_status = 'CLOSED';
  1241. }
  1242. $_data = [
  1243. 'shop_id' => (int)$shop_id,
  1244. 'outer_as_id' => $item['order_refund_no'],
  1245. 'so_id' => $o['order_no'],
  1246. 'type' => '普通退货',
  1247. 'shop_status' => $shop_status,
  1248. 'good_status' => $good_status,
  1249. 'question_type' => '普通退货',
  1250. 'refund' => (float)$o['pay_price'],
  1251. 'payment' => 0.00,
  1252. 'items' => $goodsList,
  1253. ];
  1254. if($item['user_send_express_no']){
  1255. $_data['l_id'] = $item['user_send_express_no'];
  1256. $_data['logistics_company'] = $item['user_send_express'];
  1257. }
  1258. $data[] = $_data;
  1259. $upcount++;
  1260. $upOrderIds[] = $item->id;
  1261. $start_id = $item->id;
  1262. if(count($data) >= $max){
  1263. break;
  1264. }
  1265. }
  1266. $updata = $data;
  1267. if(empty($updata)){
  1268. throw new \Exception('提交数据为空' . json_encode(func_get_args()));
  1269. }
  1270. $response = self::api($store_id, ServeHttp::UPLOAD_AFTERSALE, $updata);
  1271. debug_log(['orderRefund2Jst', $response, $upOrderIds, $updata], __CLASS__ . '.log');
  1272. if($response['code']){
  1273. debug_log(['orderRefund2Jst', $response, $upOrderIds, $updata], __CLASS__ . '.log');
  1274. }
  1275. if(isset($response['data']) && isset($response['data']['datas'])){
  1276. foreach($response['data']['datas'] as $item){
  1277. if(!$item['issuccess']){
  1278. debug_log(['orderRefund2Jst', $item], __CLASS__ . '.log');
  1279. }else{
  1280. // self::lockLocalJstGoodsNum($list[$item['so_id']], 1);
  1281. }
  1282. }
  1283. }
  1284. if($count > 1 && ($count - $upcount > 1)){
  1285. $queue = queue_push(new \app\jobs\jushuitan\OrderRefund2JSTJob([
  1286. 'id' => $store_id,
  1287. 'start_id' => $start_id + 1,
  1288. 'count' => $count - $upcount,
  1289. ]), 1);
  1290. }
  1291. sleep(1);
  1292. return $response;
  1293. } catch (\Exception $ex) {
  1294. \Yii::error($ex);
  1295. return [
  1296. 'code' => 1,
  1297. 'msg' => $ex->getMessage(),
  1298. ];
  1299. }
  1300. }
  1301. public static function afterGoodsSave($goods) {
  1302. try{
  1303. if(!self::isopen($goods->store_id)){
  1304. return [
  1305. 'code' => 1,
  1306. 'msg' => '未配置此功能',
  1307. ];
  1308. // throw new \Exception('未配置此功能');
  1309. }
  1310. $cacheKey = 'executeGoods2JST' . $goods->id;
  1311. $cacheV = cache()->get($cacheKey);
  1312. if($cacheV){
  1313. return;
  1314. }
  1315. $second = 10;
  1316. cache()->set($cacheKey, 1, $second);
  1317. $queue = queue_push(new \app\jobs\jushuitan\Goods2JSTJob([
  1318. 'id' => $goods->store_id,
  1319. 'start_id' => $goods->id,
  1320. ]), $second);
  1321. $queue = queue_push(new \app\jobs\jushuitan\Goods2JSTJob([
  1322. 'id' => $goods->store_id,
  1323. 'start_id' => $goods->id,
  1324. 'upType' => 1,
  1325. ]), $second);
  1326. return $queue;
  1327. } catch (\Exception $ex) {
  1328. \Yii::error($ex);
  1329. return [
  1330. 'code' => 1,
  1331. 'msg' => $ex->getMessage(),
  1332. ];
  1333. }
  1334. }
  1335. public static function afterCatSave($cat) {
  1336. try{
  1337. if(!self::isopen($cat->store_id)){
  1338. return [
  1339. 'code' => 1,
  1340. 'msg' => '未配置此功能',
  1341. ];
  1342. // throw new \Exception('未配置此功能');
  1343. }
  1344. $cacheKey = 'executeCat2JST' . $cat->id;
  1345. $cacheV = cache()->get($cacheKey);
  1346. if($cacheV){
  1347. return;
  1348. }
  1349. $second = 10;
  1350. cache()->set($cacheKey, 1, $second);
  1351. $queue = queue_push(new \app\jobs\jushuitan\Cat2JSTJob([
  1352. 'id' => $cat->store_id,
  1353. 'start_id' => $cat->id,
  1354. ]), $second);
  1355. return $queue;
  1356. } catch (\Exception $ex) {
  1357. \Yii::error($ex);
  1358. return [
  1359. 'code' => 1,
  1360. 'msg' => $ex->getMessage(),
  1361. ];
  1362. }
  1363. }
  1364. public static function lockLocalJstGoodsNumList($store_id) {
  1365. $list = [];
  1366. $cacheKeyList = 'lockLocalJstGoodsNumList' . self::$store_type . $store_id;
  1367. $cacheListV = cache()->get($cacheKeyList) ? : [];
  1368. foreach ($cacheListV as $gid) {
  1369. if(self::$store_type == self::STORE_TYPE_STORE){
  1370. $goods = Goods::findOne($gid);
  1371. if(!$goods['jst_goods_id']){
  1372. continue;
  1373. }
  1374. }
  1375. if(self::$store_type == self::STORE_TYPE_SUPPLIER){
  1376. $glist = (new PlatformForm(['id' => $gid]))->goodsInfo();
  1377. if($glist['code'] == 0 && $glist['data']['count'] == 1){
  1378. $goods = $glist['data']['list'][0];
  1379. }
  1380. if(!$goods['jst_goods_id']){
  1381. continue;
  1382. }
  1383. $goods['attr'] = $goods['attrs'];
  1384. }
  1385. $gattr = json_decode($goods['attr'], true);
  1386. foreach($gattr as $gattr_item){
  1387. $gattr_ids = array_column($gattr_item['attr_list'], 'attr_id');
  1388. sort($gattr_ids);
  1389. $cacheKey = 'lockLocalJstGoodsNum' . self::$store_type . $store_id . '_' . $goods['id'] . '_' . implode('_', $gattr_ids);
  1390. $cacheV = cache()->get($cacheKey) ? : 0;
  1391. if($cacheV){
  1392. $list[] = [
  1393. 'attr_ids' => implode(',', $gattr_ids),
  1394. 'no' => $gattr_item['no'],
  1395. 'i_ids' => self::$store_type == self::STORE_TYPE_STORE ? self::skuid2Jst($goods['store_id'], $goods, $gattr_item) : $gattr_item['no'],
  1396. 'cacheV' => $cacheV,
  1397. ];
  1398. }
  1399. }
  1400. }
  1401. debug_log([__FUNCTION__, $list], __CLASS__ . '.log');
  1402. return [
  1403. 'code' => 0,
  1404. 'data' => $list,
  1405. ];
  1406. }
  1407. public static function lockLocalJstGoodsNumGet($store_id, $goods, $gattr_item) {
  1408. $gattr_ids = array_column($gattr_item['attr_list'], 'attr_id');
  1409. sort($gattr_ids);
  1410. $cacheKey = 'lockLocalJstGoodsNum' . self::$store_type . $store_id . '_' . $goods['id'] . '_' . implode('_', $gattr_ids);
  1411. $cacheV = cache()->get($cacheKey);
  1412. return $cacheV > 0 ? $cacheV : 0;
  1413. }
  1414. public static function lockLocalJstGoodsNum($order, $unlock = 0, $changedAttributes = []) {
  1415. try{
  1416. $store_id = $order->store_id;
  1417. debug_log([__FUNCTION__, $order->id, '锁库存逻辑', $unlock, $changedAttributes, self::lockLocalJstGoodsNumList($store_id)], __CLASS__ . '.log');
  1418. $order->refresh();
  1419. $od = $order->detail;
  1420. if(!$od){
  1421. debug_log([__FUNCTION__, $order->id, '订单商品还没录入,跳出'], __CLASS__ . '.log');
  1422. return;
  1423. }
  1424. // \Yii::error($od);
  1425. $cacheKeyList = 'lockLocalJstGoodsNumList' . self::$store_type . $store_id;
  1426. $cacheListV = cache()->get($cacheKeyList) ? : [];
  1427. $cacheKeyOrder = 'lockLocalJstGoodsNumOrder' . self::$store_type . $store_id . '_' . $order->id;
  1428. $cacheOrderV = cache()->get($cacheKeyOrder);
  1429. $cacheTtl = 60 * 25;
  1430. if($unlock){
  1431. if(empty($cacheOrderV)){
  1432. debug_log([__FUNCTION__, $order->id, $cacheKeyOrder, $cacheOrderV, '缓存不存在,无需解锁'], __CLASS__ . '.log');
  1433. return;
  1434. }else{
  1435. cache()->delete($cacheKeyOrder);
  1436. }
  1437. }else{
  1438. if($cacheOrderV){
  1439. debug_log([__FUNCTION__, $order->id, $cacheOrderV, '缓存已存在,无需加锁'], __CLASS__ . '.log');
  1440. return;
  1441. }else{
  1442. if($changedAttributes){
  1443. debug_log([__FUNCTION__, $order->id, $changedAttributes, '订单创建时缓存未初始化,无需操作'], __CLASS__ . '.log');
  1444. return;
  1445. }
  1446. debug_log([__FUNCTION__, $cacheKeyOrder, '锁关联订单', self::lockLocalJstGoodsNumList($store_id)], __CLASS__ . '.log');
  1447. cache()->set($cacheKeyOrder, 1, $cacheTtl);
  1448. }
  1449. }
  1450. foreach($od as $item){
  1451. $goods = json_decode($item['goods_info'], true);
  1452. if(!$goods['jst_goods_id']){
  1453. continue;
  1454. }
  1455. $attr = json_decode($item['attr'], true);
  1456. $attr_ids = array_column($attr, 'attr_id');
  1457. sort($attr_ids);
  1458. $gattr = json_decode($goods['attr'], true);
  1459. foreach($gattr as $gattr_item){
  1460. $gattr_ids = array_column($gattr_item['attr_list'], 'attr_id');
  1461. sort($gattr_ids);
  1462. if(implode('_', $attr_ids) == implode('_', $gattr_ids)){
  1463. $cacheKey = 'lockLocalJstGoodsNum' . self::$store_type . $store_id . '_' . $goods['id'] . '_' . implode('_', $attr_ids);
  1464. $cacheV = cache()->get($cacheKey) ? : 0;
  1465. if($unlock){
  1466. if(empty($cacheV)){
  1467. debug_log([__FUNCTION__, $order->id, $cacheV, '缓存不存在,无需解锁'], __CLASS__ . '.log');
  1468. return;
  1469. }else{
  1470. $val = $cacheV - $item['num'];
  1471. cache()->set($cacheKey, $val, $cacheTtl);
  1472. if($val <= 0){
  1473. cache()->set($cacheKeyList, array_unique(array_diff($cacheListV, [$goods['id']])), $cacheTtl);
  1474. }
  1475. }
  1476. }else{
  1477. cache()->set($cacheKey, $cacheV + $item['num'], $cacheTtl);
  1478. cache()->set($cacheKeyList, array_unique(array_merge($cacheListV, [$goods['id']])), $cacheTtl);
  1479. }
  1480. debug_log([__FUNCTION__, $cacheKey, cache()->get($cacheKey), $cacheKeyList, cache()->get($cacheKeyList),self::lockLocalJstGoodsNumList($store_id)], __CLASS__ . '.log');
  1481. break;
  1482. }
  1483. }
  1484. }
  1485. } catch (\Exception $ex) {
  1486. \Yii::error($ex);
  1487. return [
  1488. 'code' => 1,
  1489. 'msg' => $ex->getMessage(),
  1490. ];
  1491. }
  1492. }
  1493. public static function afterOrderSave($order, $changedAttributes = []) {
  1494. try{
  1495. if(!self::isopen($order->store_id)){
  1496. return [
  1497. 'code' => 1,
  1498. 'msg' => '未配置此功能',
  1499. ];
  1500. // throw new \Exception('未配置此功能');
  1501. }
  1502. self::lockLocalJstGoodsNum($order, 0, $changedAttributes);
  1503. $is_cod = $order['pay_type'] == Order::PAY_TYPE_COD;
  1504. $shop_status = self::orderStatusKey2Jst($order['trade_status']);
  1505. if(!$shop_status && !$is_cod){
  1506. return [
  1507. 'code' => 1,
  1508. 'msg' => '不需要上传',
  1509. ];
  1510. }
  1511. if(!self::isopen($order->store_id)){
  1512. return [
  1513. 'code' => 1,
  1514. 'msg' => '未配置此功能',
  1515. ];
  1516. // throw new \Exception('未配置此功能');
  1517. }
  1518. $cacheKey = 'executeOrder2JST' . $order->id;
  1519. $cacheV = cache()->get($cacheKey);
  1520. if($cacheV){
  1521. return;
  1522. }
  1523. $second = 10;
  1524. cache()->set($cacheKey, 1, $second);
  1525. $queue = queue_push(new \app\jobs\jushuitan\Order2JSTJob([
  1526. 'id' => $order->store_id,
  1527. 'start_id' => $order->id,
  1528. ]), $second);
  1529. return $queue;
  1530. } catch (\Exception $ex) {
  1531. \Yii::error($ex);
  1532. return [
  1533. 'code' => 1,
  1534. 'msg' => $ex->getMessage(),
  1535. ];
  1536. }
  1537. }
  1538. public static function afterOrderRefundSave($insert, $changedAttributes, $orderRefund) {
  1539. try{
  1540. if(!self::isopen($orderRefund->store_id)){
  1541. return [
  1542. 'code' => 1,
  1543. 'msg' => '未配置此功能',
  1544. ];
  1545. // throw new \Exception('未配置此功能');
  1546. }
  1547. // if(!$insert){
  1548. // return [
  1549. // 'code' => 1,
  1550. // 'msg' => '不需要上传',
  1551. // ];
  1552. // }
  1553. $cacheKey = 'executeOrderRefund2JST' . $orderRefund->id;
  1554. $cacheV = cache()->get($cacheKey);
  1555. if($cacheV){
  1556. return;
  1557. }
  1558. $second = 2;
  1559. cache()->set($cacheKey, 1, $second);
  1560. $queue = queue_push(new \app\jobs\jushuitan\OrderRefund2JSTJob([
  1561. 'id' => $orderRefund->store_id,
  1562. 'start_id' => $orderRefund->id,
  1563. ]), $second);
  1564. return $queue;
  1565. } catch (\Exception $ex) {
  1566. \Yii::error($ex);
  1567. return [
  1568. 'code' => 1,
  1569. 'msg' => $ex->getMessage(),
  1570. ];
  1571. }
  1572. }
  1573. public static function lockLocalJstGoodsNumSupplier($order, $unlock = 0, $changedAttributes = []) {
  1574. try{
  1575. $supplier = Supplier::findOne(['cloud_supplier_id' => $order->supplier_id]);
  1576. $store_id = $supplier->id;
  1577. if(!self::isopen($store_id)){
  1578. return [
  1579. 'code' => 1,
  1580. 'msg' => '未配置此功能',
  1581. ];
  1582. // throw new \Exception('未配置此功能' . json_encode(func_get_args()));
  1583. }
  1584. debug_log([__FUNCTION__, $order->id, '锁库存逻辑', $unlock, $changedAttributes, self::lockLocalJstGoodsNumList($store_id)], __CLASS__ . '.log');
  1585. try{
  1586. //批发单刷新
  1587. if($order instanceof \yii\base\Model){
  1588. $order->refresh();
  1589. }
  1590. } catch (\Exception $ex) {
  1591. }
  1592. $od = $order->detail;
  1593. if(!$od){
  1594. debug_log([__FUNCTION__, $order->id, '订单商品还没录入,跳出'], __CLASS__ . '.log');
  1595. return;
  1596. }
  1597. // \Yii::error($od);
  1598. $cacheKeyList = 'lockLocalJstGoodsNumList' . self::$store_type . $store_id;
  1599. $cacheListV = cache()->get($cacheKeyList) ? : [];
  1600. $cacheKeyOrder = 'lockLocalJstGoodsNumOrder' . self::$store_type . $store_id . '_' . $order->id;
  1601. $cacheOrderV = cache()->get($cacheKeyOrder);
  1602. $cacheTtl = 60 * 25;
  1603. if($unlock){
  1604. if(empty($cacheOrderV)){
  1605. debug_log([__FUNCTION__, $order->id, $cacheKeyOrder, $cacheOrderV, '缓存不存在,无需解锁'], __CLASS__ . '.log');
  1606. return;
  1607. }else{
  1608. cache()->delete($cacheKeyOrder);
  1609. }
  1610. }else{
  1611. if($cacheOrderV){
  1612. debug_log([__FUNCTION__, $order->id, $cacheOrderV, '缓存已存在,无需加锁'], __CLASS__ . '.log');
  1613. return;
  1614. }else{
  1615. if($changedAttributes){
  1616. debug_log([__FUNCTION__, $order->id, $changedAttributes, '订单创建时缓存未初始化,无需操作'], __CLASS__ . '.log');
  1617. return;
  1618. }
  1619. debug_log([__FUNCTION__, $cacheKeyOrder, '锁关联订单', self::lockLocalJstGoodsNumList($store_id)], __CLASS__ . '.log');
  1620. cache()->set($cacheKeyOrder, 1, $cacheTtl);
  1621. }
  1622. }
  1623. foreach($od as $item){
  1624. $glist = (new PlatformForm(['id' => $item['goods_id']]))->goodsInfo();
  1625. if($glist['code'] == 0 && $glist['data']['count'] == 1){
  1626. $goods = $glist['data']['list'][0];
  1627. }
  1628. if(!$goods['jst_goods_id']){
  1629. continue;
  1630. }
  1631. //兼容批发、转单数据
  1632. $attr = is_array($item['attr']) ? $item['attr'] : json_decode($item['attr'], true);
  1633. $attr_ids = array_column($attr['attr_list'] ?? $attr, 'attr_id');
  1634. sort($attr_ids);
  1635. $gattr = json_decode($goods['attrs'], true);
  1636. foreach($gattr as $gattr_item){
  1637. $gattr_ids = array_column($gattr_item['attr_list'], 'attr_id');
  1638. sort($gattr_ids);
  1639. if(implode('_', $attr_ids) == implode('_', $gattr_ids)){
  1640. $cacheKey = 'lockLocalJstGoodsNum' . self::$store_type . $store_id . '_' . $goods['id'] . '_' . implode('_', $attr_ids);
  1641. $cacheV = cache()->get($cacheKey) ? : 0;
  1642. if($unlock){
  1643. if(empty($cacheV)){
  1644. debug_log([__FUNCTION__, $order->id, $cacheV, '缓存不存在,无需解锁'], __CLASS__ . '.log');
  1645. return;
  1646. }else{
  1647. $val = $cacheV - $item['num'];
  1648. cache()->set($cacheKey, $val, $cacheTtl);
  1649. if($val <= 0){
  1650. cache()->set($cacheKeyList, array_unique(array_diff($cacheListV, [$goods['id']])), $cacheTtl);
  1651. }
  1652. }
  1653. }else{
  1654. cache()->set($cacheKey, $cacheV + $item['num'], $cacheTtl);
  1655. cache()->set($cacheKeyList, array_unique(array_merge($cacheListV, [$goods['id']])), $cacheTtl);
  1656. }
  1657. debug_log([__FUNCTION__, $cacheKey, cache()->get($cacheKey), $cacheKeyList, cache()->get($cacheKeyList),self::lockLocalJstGoodsNumList($store_id)], __CLASS__ . '.log');
  1658. break;
  1659. }
  1660. }
  1661. }
  1662. } catch (\Exception $ex) {
  1663. \Yii::error($ex);
  1664. return [
  1665. 'code' => 1,
  1666. 'msg' => $ex->getMessage(),
  1667. ];
  1668. }
  1669. }
  1670. public static function afterPurchaseOrderSave($order, $insert = false, $changedAttributes = []) {
  1671. try{
  1672. if(!$insert){
  1673. return;
  1674. }
  1675. self::initStoreType(self::STORE_TYPE_SUPPLIER);
  1676. self::lockLocalJstGoodsNumSupplier($order, 0, $changedAttributes);
  1677. } catch (\Exception $ex) {
  1678. \Yii::error($ex);
  1679. return [
  1680. 'code' => 1,
  1681. 'msg' => $ex->getMessage(),
  1682. ];
  1683. }
  1684. }
  1685. public static function cloudTransitOrder($order_no = 'PO123123') {
  1686. $order = null;
  1687. try{
  1688. $orderTransit = \app\models\OrderTransit::findOne(['cloud_order_no' => $order_no]);
  1689. $orderList = (new SupplierForm(['order_id' => (string)$orderTransit['cloud_order_id']]))->supplierPurchaseOrderList($orderTransit['cloud_supplier_id'], 1);
  1690. if($orderList['code'] == 0 && $orderList['data']['count']){
  1691. $order = $orderList['data']['list'][0];
  1692. $goods_list = $order['goods_list'];
  1693. $order = json_decode(json_encode($order));
  1694. $order->detail = $goods_list;
  1695. $order->id = $order->order_no;
  1696. }
  1697. } catch (\Exception $ex) {
  1698. \Yii::error($ex);
  1699. }
  1700. return $order;
  1701. }
  1702. public static function afterTransitOrderSave($orderTransit, $insert = false, $changedAttributes = []) {
  1703. try{
  1704. if(!$insert){
  1705. return;
  1706. }
  1707. self::initStoreType(self::STORE_TYPE_SUPPLIER);
  1708. $cloudOrder = self::cloudTransitOrder($orderTransit['cloud_order_no']);
  1709. self::lockLocalJstGoodsNumSupplier($cloudOrder);
  1710. } catch (\Exception $ex) {
  1711. \Yii::error($ex);
  1712. return [
  1713. 'code' => 1,
  1714. 'msg' => $ex->getMessage(),
  1715. ];
  1716. }
  1717. }
  1718. public static function supplierOrder2Jst($supplier_id = 0, $start_time = 0) {
  1719. try{
  1720. self::initStoreType(self::STORE_TYPE_SUPPLIER);
  1721. if(!self::isopen($supplier_id)){
  1722. return [
  1723. 'code' => 1,
  1724. 'msg' => '未配置此功能',
  1725. ];
  1726. // throw new \Exception('未配置此功能' . json_encode(func_get_args()));
  1727. }
  1728. $shop_id = self::getConf($supplier_id, 'shop_id');
  1729. if(empty($shop_id)){
  1730. return [
  1731. 'code' => 1,
  1732. 'msg' => 'shop_id不存在,不能同步订单',
  1733. ];
  1734. // throw new \Exception('shop_id不存在,不能同步订单' . json_encode(func_get_args()));
  1735. }
  1736. if($start_time == 0){
  1737. $upload_order_time_last = self::getConf($supplier_id, 'upload_order_time_last');
  1738. if($upload_order_time_last){
  1739. $start_time = $upload_order_time_last;
  1740. }else{
  1741. $start_time = time() - 600;
  1742. }
  1743. }
  1744. $form = new SupplierForm();
  1745. $supplierOrder = $form->supplierOrderList2Jst($supplier_id, $start_time);
  1746. if($supplierOrder['code']){
  1747. throw new \Exception('获取订单失败,' . $supplierOrder['msg'] . json_encode(func_get_args()));
  1748. }
  1749. if(empty($supplierOrder['data']['list'])){
  1750. return [
  1751. 'code' => 0,
  1752. 'msg' => '订单数据为空',
  1753. ];
  1754. }
  1755. $max = 50;
  1756. $arr = array_chunk($supplierOrder['data']['list'], $max);
  1757. foreach($arr as $list){
  1758. $list = array_combine(array_column($list, 'so_id'), $list);
  1759. $data = [];
  1760. $upOrderIds = [];
  1761. foreach($list as $_data){
  1762. $up = 1;
  1763. $_data['labels'] = 'cyy,' . $_data['labels'];
  1764. $_data['shop_id'] = (int)$shop_id;
  1765. $_data['is_cod'] = (bool)$_data['is_cod'];
  1766. $_data['pay_amount'] = (float)$_data['pay_amount'];
  1767. $_data['freight'] = (float)$_data['freight'];
  1768. $_data['pay']['amount'] = (float)$_data['pay']['amount'];
  1769. foreach($_data['items'] as &$item){
  1770. if(empty($item['sku_id'])){
  1771. $up = 0;
  1772. debug_log(['supplierOrder2Jst', 'sku_id为空', $_data['so_id']], __CLASS__ . '.log');
  1773. break;
  1774. }
  1775. $item['outer_oi_id'] = md5($item['outer_oi_id']);
  1776. $item['qty'] = (int)$item['qty'];
  1777. $item['amount'] = (float)$item['amount'];
  1778. $item['base_price'] = (float)$item['base_price'];
  1779. }
  1780. if(!$up){
  1781. continue;
  1782. }
  1783. $data[] = $_data;
  1784. $upOrderIds[] = $_data['so_id'];
  1785. }
  1786. $updata = $data;
  1787. if(empty($updata)){
  1788. return [
  1789. 'code' => 0,
  1790. 'msg' => '提交数据为空',
  1791. ];
  1792. throw new \Exception('提交数据为空' . json_encode(func_get_args()));
  1793. }
  1794. self::saveConf($supplier_id, ['upload_order_time_last' => time()]);
  1795. $response = self::api($supplier_id, ServeHttp::UPLOAD_ORDERS, $updata);
  1796. debug_log(['supplierOrder2Jst', $response, $upOrderIds, $updata], __CLASS__ . '.log');
  1797. if($response['code']){
  1798. debug_log(['supplierOrder2Jst', $response, $upOrderIds, $updata], __CLASS__ . '.log');
  1799. }
  1800. if(isset($response['data']) && isset($response['data']['datas'])){
  1801. foreach($response['data']['datas'] as $item){
  1802. if(!$item['issuccess']){
  1803. debug_log(['supplierOrder2Jst', $item], __CLASS__ . '.log');
  1804. }else{
  1805. //批发
  1806. $order2 = \app\models\PurchaseOrder::findOne(['cloud_order_no' => $item['so_id']]);
  1807. if($order2){
  1808. $order = \app\models\PurchaseOrder::findOne(['order_no' => $order2['order_no']]);
  1809. }
  1810. if(!$order){
  1811. //转单
  1812. $order = self::cloudTransitOrder($item['so_id']);
  1813. }
  1814. self::lockLocalJstGoodsNumSupplier($order, 1);
  1815. }
  1816. }
  1817. }
  1818. // sleep(1);
  1819. }
  1820. return $response;
  1821. } catch (\Exception $ex) {
  1822. \Yii::error($ex);
  1823. return [
  1824. 'code' => 1,
  1825. 'msg' => $ex->getMessage(),
  1826. ];
  1827. }
  1828. }
  1829. public static function checkGoodsImport2Store($store_id, $params) {
  1830. try {
  1831. $id = $params['i_id'];
  1832. $goods = Goods::find()->where(['jst_goods_id' => trim($id), 'store_id' => $store_id, 'is_delete' => 0])->select(['id'])->limit(1)->one();
  1833. return [
  1834. 'code' => 0,
  1835. 'data' => $goods,
  1836. ];
  1837. } catch (\Exception $e) {
  1838. return [
  1839. 'code' => 1,
  1840. 'msg' => $e->getMessage()
  1841. ];
  1842. }
  1843. }
  1844. // 添加店铺商品
  1845. public static function jstGoods2Store($store_id, $params)
  1846. {
  1847. $goods = self::queryJstGoods($store_id, $params['i_id']);
  1848. if ($goods['code'] != 0) {
  1849. return $goods;
  1850. }
  1851. $goods = $goods['data'];
  1852. $store_goods = Goods::find()->where(['jst_goods_id' => trim($goods['i_id']), 'store_id' => $store_id, 'is_delete' => 0])->select(['id'])->limit(1)->one();
  1853. $model = new GoodsForm(['id' => $store_goods['id']]);
  1854. $model->jst_goods_id = $goods['i_id'];
  1855. $model->status = 1;
  1856. $model->store_id = $store_id;
  1857. $model->name = $params['name'];
  1858. $model->price = $params['price'];
  1859. $model->cost_price = $params['cost_price'];
  1860. $model->original_price = $params['original_price'];
  1861. $model->goods_num = $goods['qty'];
  1862. $model->goods_no = $goods['i_id'];
  1863. $model->use_attr = 1;
  1864. $attr = $params['attr'];
  1865. foreach($attr as &$item){
  1866. foreach($goods['skus'] as $sku){
  1867. if($sku['sku_id'] == $item['no']){
  1868. $item['num'] = $sku['qty'];
  1869. }
  1870. }
  1871. }
  1872. $model->attr = $attr;
  1873. $model->cat_id = $params['cat'];
  1874. $model->service = '';
  1875. $model->content = '';
  1876. $model->product_type = 0;
  1877. $model->form = ['is_form' => 0];
  1878. $model->cover_pic = $goods['pic'];
  1879. $pics = [];
  1880. foreach($goods['pics'] as $pic){
  1881. $pics[] = ['pic_url' => $pic];
  1882. }
  1883. $model->goods_pic_list = $pics;
  1884. $res = $model->save();
  1885. if($res['code'] != 0){
  1886. \Yii::error([__FUNCTION__, $res, $goods, $params, $model]);
  1887. return $res;
  1888. }
  1889. return $res;
  1890. }
  1891. // 批量添加店铺商品
  1892. public static function jstGoods2StoreBatch($store_id, $params)
  1893. {
  1894. try{
  1895. $i_ids = explode(',', $params['i_ids']);
  1896. foreach($i_ids as $i_id){
  1897. $goods = self::queryJstGoods($store_id, $i_id);
  1898. if ($goods['code'] != 0) {
  1899. return $goods;
  1900. }
  1901. $goods = $goods['data'];
  1902. $attr = $goods['_attr'];
  1903. $store_goods = Goods::find()->where(['jst_goods_id' => trim($goods['i_id']), 'store_id' => $store_id, 'is_delete' => 0])->select(['id'])->limit(1)->one();
  1904. $model = new GoodsForm(['id' => $store_goods['id']]);
  1905. $model->jst_goods_id = $goods['i_id'];
  1906. $model->status = 1;
  1907. $model->store_id = $store_id;
  1908. $model->name = $goods['name'];
  1909. $model->price = $goods['market_price'] ?: 0;
  1910. $model->cost_price = $goods['s_price'] ?: 0;
  1911. $model->original_price = $goods['market_price'] ?: 0;
  1912. $model->goods_num = $goods['qty'];
  1913. $model->goods_no = $goods['i_id'];
  1914. $model->use_attr = 1;
  1915. $model->attr = $attr;
  1916. $model->cat_id = $params['cat'];
  1917. $model->service = '';
  1918. $model->content = '';
  1919. $model->product_type = 0;
  1920. $model->form = ['is_form' => 0];
  1921. $model->cover_pic = $goods['pic'];
  1922. $pics = [];
  1923. foreach($goods['pics'] as $pic){
  1924. $pics[] = ['pic_url' => $pic];
  1925. }
  1926. $model->goods_pic_list = $pics;
  1927. $res = $model->save();
  1928. if($res['code'] != 0){
  1929. \Yii::error([__FUNCTION__, $res, $goods, $params, $model]);
  1930. return $res;
  1931. }
  1932. }
  1933. return [
  1934. 'code' => 0,
  1935. 'msg' => '操作成功'
  1936. ];
  1937. } catch (\Exception $e) {
  1938. return [
  1939. 'code' => 1,
  1940. 'msg' => $e->getMessage() . $e->getLine()
  1941. ];
  1942. }
  1943. }
  1944. public static function commonHandleStore($supplier_id, $data, $id = 0) {
  1945. $supplier = Supplier::findOne(['id' => $supplier_id, 'is_delete' => 0]);
  1946. if (!$supplier) {
  1947. return [
  1948. 'code' => 1,
  1949. 'msg' => '供货商未找到',
  1950. ];
  1951. }
  1952. $supplierForm = new SupplierForm();
  1953. $supplierForm->attributes = $data;
  1954. $supplierForm->token_supplier_id = $supplier->cloud_supplier_id;
  1955. $setGoods = $supplierForm->setGoods();
  1956. if ($setGoods['code'] == 1) {
  1957. return $setGoods;
  1958. }
  1959. // 提交审核
  1960. $supplierForm = new SupplierForm();
  1961. $supplierForm->attributes = ['goods_id' => $setGoods['data']['goods_id']];
  1962. $supplierForm->token_supplier_id = $supplier->cloud_supplier_id;
  1963. $submitAudit = $supplierForm->auditSubmit();
  1964. if ($submitAudit['code'] == 1) {
  1965. return $submitAudit;
  1966. }
  1967. // 审核商品
  1968. $form = new PlatformForm();
  1969. $form->attributes = [
  1970. 'id' => $submitAudit['data']['goods_id'],
  1971. 'status' => 1,
  1972. ];
  1973. return $form->auditHandle();
  1974. }
  1975. //attr参数包含店铺和供应商所需参数
  1976. public static function handleAttr(&$goods) {
  1977. $_attr = [];
  1978. $skus = $goods['skus'];
  1979. $image = $goods['pic'];
  1980. foreach ($goods['skus'] as &$sku) {
  1981. $attr_list = [
  1982. [
  1983. 'attr_group_name' => '规格',
  1984. 'attr_name' => $sku['properties_value']
  1985. ]
  1986. ];
  1987. $attr = [
  1988. 'attr_list' => $attr_list,
  1989. 'num' => $sku['qty'],
  1990. 'price' => sprintf('%.2f', $sku['market_price']),
  1991. 'cost_price' => sprintf('%.2f', $sku['sale_price']),
  1992. 'no' => $sku['sku_id'],
  1993. 'pic' => $sku['pic'] == '' ? $image : $sku['pic'],
  1994. 'share_commission_first' => '',
  1995. 'share_commission_second' => '',
  1996. 'share_commission_third' => '',
  1997. 'wholesale_price' => sprintf('%.2f', $sku['sale_price']),
  1998. 'original_price' => sprintf('%.2f', $sku['market_price']),
  1999. ];
  2000. if(self::$store_type == self::STORE_TYPE_SUPPLIER){
  2001. $attr['price'] = sprintf('%.2f', $sku['sale_price']);
  2002. }
  2003. $_attr[] = $attr;
  2004. $sku['_attr'] = $attr;
  2005. }
  2006. return $_attr;
  2007. }
  2008. public static function checkGoodsImport2Supplier($params) {
  2009. try {
  2010. $id = $params['i_id'];
  2011. $cloud_store_token = get_platform_token();
  2012. $goods_url = "/cloud/user/getJstGoods";
  2013. $goods_data = [];
  2014. $goods_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  2015. $goods_data['jst_goods_id'] = $id; //默认为 1
  2016. $domain = (new OptionSetting)->getCloudDomainName();
  2017. $goods_data_info = cloud_post($domain . $goods_url,$goods_data);
  2018. $goods_data_info = json_decode($goods_data_info,true);
  2019. if($goods_data_info['code'] != 0){
  2020. throw new \Exception($goods_data_info['msg']);
  2021. }else{
  2022. return $goods_data_info;
  2023. }
  2024. } catch (\Exception $e) {
  2025. return [
  2026. 'code' => 1,
  2027. 'msg' => $e->getMessage()
  2028. ];
  2029. }
  2030. }
  2031. // 添加云仓商品
  2032. public static function jstGoods2Supplier($supplier_id, $params)
  2033. {
  2034. $goods = self::queryJstGoods($supplier_id, $params['i_id']);
  2035. if ($goods['code'] != 0) {
  2036. return $goods;
  2037. }
  2038. $goods = $goods['data'];
  2039. $attr = $params['attr'];
  2040. foreach($attr as &$item){
  2041. foreach($goods['skus'] as $sku){
  2042. if($sku['sku_id'] == $item['no']){
  2043. $item['num'] = $sku['qty'];
  2044. }
  2045. }
  2046. }
  2047. $data = [
  2048. 'cat_id' => \implode(',', $params['cat']),
  2049. 'name' => $params['name'],
  2050. 'goods_no' => (string)$params['i_id'],
  2051. 'g_s_pic_url' => $goods['pic'],
  2052. 'desc' => $params['name'],
  2053. 'g_price' => $params['price'],
  2054. 'g_wholesale_price' => $params['wholesale_price'],
  2055. 'g_wholesale_ladder_rebate' => '[]',
  2056. 'g_wholesale_rebate_switch' => '0',
  2057. 'weight' => '',
  2058. 'g_original_price' => $params['original_price'],
  2059. 'g_unit' => $goods['unit'],
  2060. 'g_shop_count' => 1,
  2061. 'num' => $goods['qty'],
  2062. 'use_attr' => '1',
  2063. 'g_pic_list' => $goods['pics'],
  2064. 'g_send_type' => '0',
  2065. 'attrs' => $attr,
  2066. 'is_change' => 1,
  2067. 'jst_goods_id' => $params['i_id'],
  2068. 'market_price' => sprintf('%.2f', $goods['market_price']),
  2069. ];
  2070. return self::commonHandleSupplier($supplier_id, $data, $params['i_id']);
  2071. }
  2072. // 批量添加云仓商品
  2073. public static function jstGoods2SupplierBatch($supplier_id, $params)
  2074. {
  2075. try{
  2076. $i_ids = explode(',', $params['i_ids']);
  2077. foreach($i_ids as $i_id){
  2078. $goods = self::queryJstGoods($supplier_id, $i_id);
  2079. if ($goods['code'] != 0) {
  2080. return $goods;
  2081. }
  2082. $goods = $goods['data'];
  2083. $attr = $goods['_attr'];
  2084. $data = [
  2085. 'cat_id' => \implode(',', $params['cat']),
  2086. 'name' => $goods['name'],
  2087. 'goods_no' => (string)$i_id,
  2088. 'g_s_pic_url' => $goods['pic'],
  2089. 'desc' => $goods['name'],
  2090. 'g_price' => sprintf('%.2f', $goods['s_price']),
  2091. 'g_wholesale_price' => sprintf('%.2f', $goods['s_price']),
  2092. 'g_wholesale_ladder_rebate' => '[]',
  2093. 'g_wholesale_rebate_switch' => '0',
  2094. 'weight' => '',
  2095. 'g_original_price' => sprintf('%.2f', $goods['s_price']),
  2096. 'g_unit' => $goods['unit'],
  2097. 'g_shop_count' => 1,
  2098. 'num' => $goods['qty'],
  2099. 'use_attr' => '1',
  2100. 'g_pic_list' => $goods['pics'],
  2101. 'g_send_type' => '0',
  2102. 'attrs' => $attr,
  2103. 'is_change' => 1,
  2104. 'jst_goods_id' => $i_id,
  2105. 'market_price' => sprintf('%.2f', $goods['market_price']),
  2106. ];
  2107. $result = self::commonHandleSupplier($supplier_id, $data, $i_id);
  2108. if (intval($result['code']) !== 0) {
  2109. throw new \Exception($result['msg']);
  2110. }
  2111. }
  2112. return [
  2113. 'code' => 0,
  2114. 'msg' => '操作成功'
  2115. ];
  2116. } catch (\Exception $e) {
  2117. return [
  2118. 'code' => 1,
  2119. 'msg' => $e->getMessage() . $e->getLine()
  2120. ];
  2121. }
  2122. }
  2123. // 添加店铺商品
  2124. public static function jstSupplierGoods2Store($store_id, $params)
  2125. {
  2126. $goods = self::queryJstSupplierGoods($store_id, $params['i_id']);
  2127. if ($goods['code'] != 0) {
  2128. return $goods;
  2129. }
  2130. $goods = $goods['data'];
  2131. $store_goods = Goods::find()->where(['jst_goods_id' => trim($goods['style_code']), 'store_id' => $store_id, 'is_delete' => 0])->select(['id'])->limit(1)->one();
  2132. $model = new GoodsForm(['id' => $store_goods['id']]);
  2133. $model->jst_goods_id = $goods['style_code'];
  2134. $model->jst_supplier_id = $goods['_supplier_co_id'] ?: 0;
  2135. $model->status = 1;
  2136. $model->store_id = $store_id;
  2137. $model->name = $params['name'];
  2138. $model->price = $params['price'];
  2139. $model->cost_price = $params['cost_price'];
  2140. $model->original_price = $params['original_price'];
  2141. $model->goods_num = $goods['qty'];
  2142. $model->goods_no = $goods['style_code'];
  2143. $model->use_attr = 1;
  2144. $attr = $params['attr'];
  2145. // foreach($attr as &$item){
  2146. // foreach($goods['skus'] as $sku){
  2147. // if($sku['sku_id'] == $item['no']){
  2148. // $item['num'] = $sku['qty'];
  2149. // }
  2150. // }
  2151. // }
  2152. $model->attr = $attr;
  2153. $model->cat_id = $params['cat'];
  2154. $model->service = '';
  2155. $model->content = '';
  2156. $model->product_type = 0;
  2157. $model->form = ['is_form' => 0];
  2158. $model->cover_pic = $goods['main_image_list'][0] ?? '';
  2159. $pics = [];
  2160. foreach($goods['main_image_list'] as $pic){
  2161. $pics[] = ['pic_url' => $pic];
  2162. }
  2163. $model->goods_pic_list = $pics;
  2164. $model->content = $goods['_desc'];
  2165. if($goods['__goods_info'] && $goods['__goods_info']['data']){
  2166. $model->video_url = $goods['__goods_info']['data']['item_photo']['item_video'] ?: $goods['__goods_info']['data']['item_photo']['goods_video'];
  2167. }
  2168. $res = $model->save();
  2169. if($res['code'] != 0){
  2170. \Yii::error([__FUNCTION__, $res, $goods, $params, $model]);
  2171. return $res;
  2172. }
  2173. return $res;
  2174. }
  2175. // 批量添加店铺商品
  2176. public static function jstSupplierGoods2StoreBatch($store_id, $params)
  2177. {
  2178. try{
  2179. $i_ids = explode(',', $params['i_ids']);
  2180. foreach($i_ids as $i_id){
  2181. $goods = self::queryJstSupplierGoods($store_id, $i_id);
  2182. if ($goods['code'] != 0) {
  2183. return $goods;
  2184. }
  2185. $goods = $goods['data'];
  2186. $attr = $goods['_attr'];
  2187. $store_goods = Goods::find()->where(['jst_goods_id' => trim($goods['style_code']), 'store_id' => $store_id, 'is_delete' => 0])->select(['id'])->limit(1)->one();
  2188. $model = new GoodsForm(['id' => $store_goods['id']]);
  2189. $model->jst_goods_id = $goods['style_code'];
  2190. $model->jst_supplier_id = $goods['_supplier_co_id'] ?: 0;
  2191. $model->status = 1;
  2192. $model->store_id = $store_id;
  2193. $model->name = $goods['item_name'];
  2194. $model->price = $goods['min_sale_price'];
  2195. $model->cost_price = $goods['min_supply_price'];
  2196. $model->original_price = $goods['min_sale_price'];
  2197. $model->goods_num = $goods['qty'];
  2198. $model->goods_no = $goods['style_code'];
  2199. $model->use_attr = 1;
  2200. // $attr = $params['attr'];
  2201. // foreach($attr as &$item){
  2202. // foreach($goods['skus'] as $sku){
  2203. // if($sku['sku_id'] == $item['no']){
  2204. // $item['num'] = $sku['qty'];
  2205. // }
  2206. // }
  2207. // }
  2208. $model->attr = $attr;
  2209. $model->cat_id = $params['cat'];
  2210. $model->service = '';
  2211. $model->content = '';
  2212. $model->product_type = 0;
  2213. $model->form = ['is_form' => 0];
  2214. $model->cover_pic = $goods['main_image_list'][0] ?? '';
  2215. $pics = [];
  2216. foreach($goods['main_image_list'] as $pic){
  2217. $pics[] = ['pic_url' => $pic];
  2218. }
  2219. $model->goods_pic_list = $pics;
  2220. $model->content = $goods['_desc'];
  2221. if($goods['__goods_info'] && $goods['__goods_info']['data']){
  2222. $model->video_url = $goods['__goods_info']['data']['item_photo']['item_video'] ?: $goods['__goods_info']['data']['item_photo']['goods_video'];
  2223. }
  2224. $res = $model->save();
  2225. if($res['code'] != 0){
  2226. \Yii::error([__FUNCTION__, $res, $goods, $params, $model]);
  2227. return $res;
  2228. }
  2229. }
  2230. return [
  2231. 'code' => 0,
  2232. 'msg' => '操作成功'
  2233. ];
  2234. } catch (\Exception $e) {
  2235. return [
  2236. 'code' => 1,
  2237. 'msg' => $e->getMessage() . $e->getLine()
  2238. ];
  2239. }
  2240. }
  2241. // 添加云仓商品
  2242. public static function jstSupplierGoods2Supplier($supplier_id, $params)
  2243. {
  2244. $supplierGoods = self::queryJstSupplierGoods($supplier_id, $params['i_id']);
  2245. if($supplierGoods['code']){
  2246. return $supplierGoods;
  2247. }
  2248. $attr = $params['attr'];
  2249. $data = [
  2250. 'cat_id' => \implode(',', $params['cat']),
  2251. 'name' => $params['name'],
  2252. 'goods_no' => (string)$params['i_id'],
  2253. 'g_s_pic_url' => $supplierGoods['data']['main_image_list'][0] ?? '',
  2254. 'desc' => $supplierGoods['data']['_desc'],
  2255. 'g_price' => $params['price'],
  2256. 'g_wholesale_price' => $params['wholesale_price'],
  2257. 'g_wholesale_ladder_rebate' => '[]',
  2258. 'g_wholesale_rebate_switch' => '0',
  2259. 'weight' => '',
  2260. 'g_original_price' => $params['original_price'],
  2261. 'g_unit' => '',
  2262. 'g_shop_count' => 1,
  2263. 'num' => $supplierGoods['data']['qty'],
  2264. 'use_attr' => '1',
  2265. 'g_pic_list' => $supplierGoods['data']['main_image_list'] ?: [],
  2266. 'g_send_type' => '0',
  2267. 'attrs' => $attr,
  2268. 'is_change' => 1,
  2269. 'jst_goods_id' => $params['i_id'],
  2270. 'jst_supplier_id' => $supplierGoods['data']['_supplier_co_id'] ?: 0,
  2271. 'market_price' => sprintf('%.2f', $supplierGoods['data']['min_sale_price']),
  2272. ];
  2273. if($supplierGoods['data']['__goods_info'] && $supplierGoods['data']['__goods_info']['data']){
  2274. $data['video_url'] = $supplierGoods['data']['__goods_info']['data']['item_photo']['item_video'] ?: $supplierGoods['data']['__goods_info']['data']['item_photo']['goods_video'];
  2275. }
  2276. return self::commonHandleSupplier($supplier_id, $data, $params['i_id']);
  2277. }
  2278. // 批量添加云仓商品
  2279. public static function jstSupplierGoods2SupplierBatch($supplier_id, $params)
  2280. {
  2281. try{
  2282. $i_ids = explode(',', $params['i_ids']);
  2283. $res = [];
  2284. foreach($i_ids as $i_id){
  2285. $supplierGoods = self::queryJstSupplierGoods($supplier_id, $i_id);
  2286. if($supplierGoods['code']){
  2287. return $supplierGoods;
  2288. }
  2289. $attr = $supplierGoods['data']['_attr'];
  2290. $data = [
  2291. 'cat_id' => \implode(',', $params['cat']),
  2292. 'name' => $supplierGoods['data']['item_name'],
  2293. 'goods_no' => (string)$i_id,
  2294. 'g_s_pic_url' => $supplierGoods['data']['main_image_list'][0] ?? '',
  2295. 'desc' => $supplierGoods['data']['_desc'],
  2296. 'g_price' => sprintf('%.2f', $supplierGoods['data']['min_supply_price']),
  2297. 'g_wholesale_price' => sprintf('%.2f', $supplierGoods['data']['min_supply_price']),
  2298. 'g_wholesale_ladder_rebate' => '[]',
  2299. 'g_wholesale_rebate_switch' => '0',
  2300. 'weight' => '',
  2301. 'g_original_price' => sprintf('%.2f', $supplierGoods['data']['min_sale_price']),
  2302. 'g_unit' => '',
  2303. 'g_shop_count' => 1,
  2304. 'num' => $supplierGoods['data']['qty'],
  2305. 'use_attr' => '1',
  2306. 'g_pic_list' => $supplierGoods['data']['main_image_list'] ?: [],
  2307. 'g_send_type' => '0',
  2308. 'attrs' => $attr,
  2309. 'is_change' => 1,
  2310. 'jst_goods_id' => $i_id,
  2311. 'jst_supplier_id' => $supplierGoods['data']['_supplier_co_id'] ?: 0,
  2312. 'market_price' => sprintf('%.2f', $supplierGoods['data']['min_sale_price']),
  2313. ];
  2314. if($supplierGoods['data']['__goods_info'] && $supplierGoods['data']['__goods_info']['data']){
  2315. $data['video_url'] = $supplierGoods['data']['__goods_info']['data']['item_photo']['item_video'] ?: $supplierGoods['data']['__goods_info']['data']['item_photo']['goods_video'];
  2316. }
  2317. $result = self::commonHandleSupplier($supplier_id, $data, $i_id);
  2318. $res[] = $result;
  2319. if (intval($result['code']) !== 0) {
  2320. throw new \Exception($result['msg']);
  2321. }
  2322. }
  2323. return [
  2324. 'code' => 0,
  2325. 'msg' => '操作成功',
  2326. 'ress' => $res,
  2327. ];
  2328. } catch (\Exception $e) {
  2329. return [
  2330. 'code' => 1,
  2331. 'ress' => $res,
  2332. 'msg' => $e->getMessage() . $e->getLine()
  2333. ];
  2334. }
  2335. }
  2336. public static function commonHandleSupplier($supplier_id, $data, $id = 0) {
  2337. $supplier = Supplier::findOne(['id' => $supplier_id, 'is_delete' => 0]);
  2338. if (!$supplier) {
  2339. return [
  2340. 'code' => 1,
  2341. 'msg' => '供货商未找到',
  2342. ];
  2343. }
  2344. $_res = ['query' => $data];
  2345. $supplierForm = new SupplierForm();
  2346. $supplierForm->attributes = $data;
  2347. $supplierForm->token_supplier_id = $supplier->cloud_supplier_id;
  2348. $setGoods = $supplierForm->setGoods();
  2349. if ($setGoods['code'] == 1) {
  2350. return array_merge($setGoods, $_res);
  2351. }
  2352. // 提交审核
  2353. $supplierForm = new SupplierForm();
  2354. $supplierForm->attributes = ['goods_id' => $setGoods['data']['goods_id']];
  2355. $supplierForm->token_supplier_id = $supplier->cloud_supplier_id;
  2356. $submitAudit = $supplierForm->auditSubmit();
  2357. if ($submitAudit['code'] == 1) {
  2358. return array_merge($submitAudit, $_res);
  2359. }
  2360. // 审核商品
  2361. $form = new PlatformForm();
  2362. $form->attributes = [
  2363. 'id' => $submitAudit['data']['goods_id'],
  2364. 'status' => 1,
  2365. ];
  2366. return array_merge($form->auditHandle(), $_res);
  2367. }
  2368. //attr参数包含店铺和供应商所需参数
  2369. public static function supplierGoodsHandleAttr(&$supplierGoods) {
  2370. $_attr = [];
  2371. $image = $supplierGoods['main_image_list'][0];
  2372. $qty = $supplierGoods['__qty'];
  2373. $ginfo = $supplierGoods['__goods_info']['data'];
  2374. $desc = $ginfo['describe'] ?? '';
  2375. $imgs = $ginfo['item_photo']['item_detail_images'];
  2376. if($imgs){
  2377. $desc .= '<br><img src="' . implode('"/><img src="', $imgs) . '"/>';
  2378. }
  2379. $supplierGoods['_desc'] = $desc;
  2380. foreach ($supplierGoods['__goods_info']['data']['item_sku_list'] as &$sku) {
  2381. $attr_list = [
  2382. [
  2383. 'attr_group_name' => $ginfo['first_spec_name'],
  2384. 'attr_name' => $sku['first_spec_value_name'],
  2385. ],
  2386. ];
  2387. if($ginfo['second_spec_name']){
  2388. $attr_list[] = [
  2389. 'attr_group_name' => $ginfo['second_spec_name'],
  2390. 'attr_name' => $sku['second_spec_value_name'],
  2391. ];
  2392. }
  2393. $num = 0;
  2394. if($qty['data']['list']){
  2395. foreach($qty['data']['list'] as $iqty){
  2396. if($iqty['item_code'] == $sku['item_code']){
  2397. $num = $iqty['stock'];
  2398. break;
  2399. }
  2400. }
  2401. }
  2402. if($qty['data']['inventorys']){
  2403. foreach($qty['data']['inventorys'] as $iqty){
  2404. if($iqty['sku_id'] == $sku['item_code']){
  2405. $num = $iqty['qty'];
  2406. break;
  2407. }
  2408. }
  2409. }
  2410. // $attr_list = [
  2411. // [
  2412. // 'attr_group_name' => '规格',
  2413. // 'attr_name' => $sku['properties_value']
  2414. // ]
  2415. // ];
  2416. $attr = [
  2417. 'attr_list' => $attr_list,
  2418. 'num' => $num,
  2419. 'price' => sprintf('%.2f', $sku['sale_price']),
  2420. 'cost_price' => sprintf('%.2f', $sku['supply_price']),
  2421. 'wholesale_price' => sprintf('%.2f', $sku['supply_price']),
  2422. 'original_price' => sprintf('%.2f', $sku['sale_price']),
  2423. 'no' => $sku['item_code'],
  2424. 'weight' => $sku['weight'],
  2425. 'pic' => $sku['pic'] == '' ? $image : $sku['pic'],
  2426. 'share_commission_first' => '',
  2427. 'share_commission_second' => '',
  2428. 'share_commission_third' => '',
  2429. ];
  2430. if(self::$store_type == self::STORE_TYPE_SUPPLIER){
  2431. $attr['price'] = sprintf('%.2f', $sku['supply_price']);
  2432. }
  2433. $sku['qty'] = $attr['num'];
  2434. $sku['_attr'] = $attr;
  2435. $_attr[] = $attr;
  2436. }
  2437. return $_attr;
  2438. }
  2439. }