Goodup.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. <?php
  2. namespace app\youzan\logic;
  3. use app\admin\common\User;
  4. use app\admin\model\ActionLog;
  5. use app\admin\model\GoodTax;
  6. use app\admin\model\ProcessOrder;
  7. use app\txx\model\YzGood;
  8. use app\youzan\model\PlatformYouzan;
  9. use think\Exception;
  10. use think\facade\Cache;
  11. use think\facade\Db;use think\facade\Log;
  12. //商品处理层
  13. class Goodup
  14. {
  15. private static $offline_result_info_id = '0024';//从有赞平台下架商品的原因编码,注意环境不同,编码可能不同,生产环境是0024,测试环境是0021
  16. //提交商品上线
  17. public static function create(array $data = [])
  18. {
  19. $isonline = [];
  20. $i = 0;
  21. Db::startTrans();
  22. try {
  23. $all_list = Db::name("platform_youzan")
  24. ->where(["platform_id" => $data['platform_id'], "is_del" => PlatformYouzan::$del_normal])
  25. ->whereIn('spuCode', array_column($data['good_data'], 'spuCode'))
  26. ->column('id,exam_status', 'spuCode');
  27. $all_nake = Db::name("good_nake")
  28. ->where(["min_num" => 1, "is_del" => 0])
  29. ->whereIn('spuCode', array_column($data['good_data'], 'spuCode'))
  30. ->column('id', 'spuCode');
  31. $market = array_column( $data['good_info'],'market_url','spuCode');
  32. foreach ($data['good_data'] as $value) {
  33. // $count = Db::name("good_nake")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->count();
  34. //校验是否存在起订量为1的信息
  35. if (empty($all_nake[$value['spuCode']])) {
  36. $god = Db::name("good_basic")
  37. ->field('id,good_name,good_thumb_img,chargerid')
  38. ->where(["spuCode" => $value['spuCode'], "is_del" => 0])
  39. ->find();
  40. if ($god) {
  41. $tp = [];
  42. $tp['spuCode'] = $value['spuCode'];
  43. $tp['good_name'] = $god['good_name'];
  44. $tp['good_img'] = $god['good_thumb_img'];
  45. // $tp['exam_status'] = $ist['exam_status'];
  46. $spec = Db::name("good_spec")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->select()->toArray();
  47. $speclist = [];
  48. $speclist = [];
  49. if (!empty($spec)) {
  50. foreach ($spec as $val) {
  51. $temp = [];
  52. $temp['id'] = $val['id'];
  53. $temp['spuCode'] = $val['spuCode'];
  54. $temp['spec_id'] = $val['spec_id'];
  55. $temp['spec_value_id'] = $val['spec_value_id'];
  56. $temp['is_del'] = $val['is_del'];
  57. $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
  58. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  59. $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
  60. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  61. $speclist[] = $temp;
  62. }
  63. }
  64. $tp["speclist"] = empty($speclist) ? [] : $speclist;
  65. $is_no_1[] = $tp;
  66. }
  67. }
  68. if (!empty($is_no_1)) {
  69. Db::rollback();
  70. return app_show(1010, "存在起订量不为1的商品", $is_no_1);
  71. }
  72. //校验是否存在上线
  73. $ist = isset($all_list[$value['spuCode']]) ? $all_list[$value['spuCode']] : [];
  74. if ($ist) {
  75. $god = Db::name("good_basic")
  76. ->field('id,good_name,good_thumb_img,chargerid')
  77. ->where(["spuCode" => $value['spuCode'], "is_del" => 0])
  78. ->find();
  79. if ($god) {
  80. $tp = [];
  81. $tp['spuCode'] = $value['spuCode'];
  82. $tp['good_name'] = $god['good_name'];
  83. $tp['good_img'] = $god['good_thumb_img'];
  84. $tp['exam_status'] = $ist['exam_status'];
  85. $spec = Db::name("good_spec")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->select()->toArray();
  86. $speclist = [];
  87. if (!empty($spec)) {
  88. foreach ($spec as $val) {
  89. $temp = [];
  90. $temp['id'] = $val['id'];
  91. $temp['spuCode'] = $val['spuCode'];
  92. $temp['spec_id'] = $val['spec_id'];
  93. $temp['spec_value_id'] = $val['spec_value_id'];
  94. $temp['is_del'] = $val['is_del'];
  95. $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
  96. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  97. $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
  98. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  99. $speclist[] = $temp;
  100. }
  101. }
  102. $tp["speclist"] = empty($speclist) ? [] : $speclist;
  103. $isonline[] = $tp;
  104. }
  105. }
  106. if (!empty($isonline)) {
  107. Db::rollback();
  108. return app_show(1009, "存在已上线产品", $isonline);
  109. }
  110. //改变编码规则,将原来的skuCode后两位换成序列号
  111. //str_pad字符串填充
  112. $skuCode = makeNo("SN");
  113. $skuCode = substr($skuCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  114. $tmp = [
  115. "platform_id" => $data['platform_id'],
  116. "online_reason" => $data['online_reason'],
  117. "online_remark" => $data['online_remark'],
  118. "spuCode" => $value['spuCode'],
  119. "skuCode" => $skuCode,
  120. "exam_status" => PlatformYouzan::$exam_status_0,
  121. "is_del" => PlatformYouzan::$del_normal,
  122. "createrid" => $data['createrid'],
  123. "creater" => $data['creater'],
  124. "updaterid" => $data['createrid'],
  125. 'market_url'=>$market[$value['spuCode']]??'',
  126. "updater" => $data['creater'],
  127. "addtime" => date("Y-m-d H:i:s"),
  128. "updatetime" => date("Y-m-d H:i:s")
  129. ];
  130. $i++;
  131. //修改状态,添加待办
  132. $create = Db::name("platform_youzan")->insert($tmp, true);
  133. if ($create > 0) {
  134. Db::name("good_platform")->insert([
  135. "spuCode" => $value['spuCode'],
  136. "skuCode" => $skuCode,
  137. 'platform_code' => $data['platform_id'],
  138. "online_reason" => $data['online_reason'],
  139. "online_remark" => $data['online_remark'],
  140. "exam_status" => 0,
  141. 'is_online' => 0,
  142. "status" => 1,
  143. "is_del" => 0,
  144. "addtime" => date("Y-m-d H:i:s"),
  145. "updatetime" => date("Y-m-d H:i:s"),
  146. "creater" => $data['creater'],
  147. "createrid" => $data['createrid'],
  148. ]);
  149. ActionLog::logAdd(['id' => $data['createrid'], 'nickname' => $data['creater']], [
  150. "order_code" => $skuCode,
  151. "status" => $tmp['exam_status'],//这里的status是之前的值
  152. "action_remark" => '',//备注
  153. "action_type" => "create"//新建create,编辑edit,更改状态status
  154. ], "YZSX", $tmp['exam_status'], $tmp);
  155. ProcessOrder::AddProcess(['id' => $data['createrid'], 'nickname' => $data['creater']], [
  156. "order_type" => 'YZSX',
  157. "order_code" => $skuCode,
  158. "order_id" => $create,
  159. "order_status" => $tmp['exam_status'],
  160. "before_status" => $tmp['exam_status'],
  161. 'holder_id' => Db::name('good_basic')->where(["spuCode" => $value['spuCode'], "is_del" => 0])->value('chargerid', 0),
  162. ]);
  163. $iso = Db::name("good")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->find();
  164. if ($iso == false) {
  165. unset($value['id']);
  166. $stock_num = Db::name('good_stock')->alias("a")
  167. ->leftJoin("warehouse_info b","a.wsm_code=b.wsm_code")
  168. ->where(['a.is_del' => 0, 'a.spuCode' =>$value['spuCode'],"b.wsm_type"=>[2,5]])->sum('usable_stock');
  169. $value['usable_stock'] =$stock_num;
  170. $value['addtime']=date("Y-m-d H:i:s");
  171. $value['updatetime']=date("Y-m-d H:i:s");
  172. } else {
  173. $value['id'] = $iso['id'];
  174. $value['updatetime'] = date("Y-m-d H:i:s");
  175. }
  176. $up = Db::name("good")->strict(false)->save($value);
  177. if (!$up) {
  178. Db::rollback();
  179. return error_show(1004, "提交失败");
  180. }
  181. } else {
  182. Db::rollback();
  183. return error_show(1004, "提交失败");
  184. }
  185. }
  186. Db::commit();
  187. return app_show(0, "提交成功");
  188. } catch (\Exception $e) {
  189. Db::rollback();
  190. return error_show(1004, $e->getMessage());
  191. }
  192. }
  193. //审核并将商品推送到有赞平台
  194. public static function youzanGoodUpOnline(int $platform_youzan_id = 0, array $data = [],int $uid=0,string $uname='')
  195. {
  196. Db::startTrans();
  197. try {
  198. $db = new PlatformYouzan();
  199. $rs = $db
  200. ->field('a.id,a.platform_id,a.spuCode,a.skuCode,a.exam_status,gb.good_name,gb.weight,gb.good_img,gb.cat_id,gb.good_info_img,gb.chargerid')
  201. ->alias('a')
  202. ->where(['a.id' => $platform_youzan_id, 'a.is_del' => $db::$del_normal])
  203. ->leftJoin('good_basic gb', 'gb.spuCode=a.spuCode')
  204. ->findOrEmpty();
  205. if ($rs->isEmpty()) throw new Exception('该商品上线记录不存在');
  206. if ($rs->exam_status != $db::$exam_status_0) throw new Exception('该商品并不是待审核状态');
  207. $old_exam_status = $rs->exam_status;
  208. // $userinfo = GetUserInfo($data['token']);
  209. //platform_youzan 更新信息
  210. $update_data = [
  211. 'exam_status' => $data['exam_status'],
  212. 'updaterid' => $uid,//isset($userinfo['data']['id']) ? $userinfo['data']['id'] : 0,
  213. 'updater' => $uname,//isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname'] : '',
  214. 'updatetime' => date('Y-m-d H:i:s'),
  215. 'distribution'=>implode(',',$data['distribution']),
  216. ];
  217. //good_platform 更新信息
  218. $update_good_platform_data = ['updatetime' => date('Y-m-d H:i:s')];
  219. $res = true;
  220. //审核通过
  221. if ($data['exam_status'] == $db::$exam_status_1) {
  222. // $ladder_info = Db::name('good_ladder')
  223. // ->field('id,cost_fee')
  224. // ->where('id', $data['good_ladder_id'])
  225. // ->where('is_del', 0)
  226. // ->find();
  227. //
  228. // if (empty($ladder_info)) throw new Exception('该商品阶梯记录不存在');
  229. //运费,单位分,整数
  230. $nake_info = Db::name('good_nake')
  231. // ->where(['spuCode' => $rs->spuCode, 'is_del' => 0])
  232. // ->where('min_num', '<=', $data['start_sale_num'])
  233. ->field('id,delivery_fee,cost_fee')
  234. ->where('id', $data['good_ladder_id'])
  235. ->findOrEmpty();
  236. $post_fee = isset($nake_info['delivery_fee']) ? $nake_info['delivery_fee'] : 0;
  237. $desc = explode(',', $rs->good_info_img);
  238. $desc_string = [];
  239. foreach ($desc as $value) {
  240. $desc_string[] = "<img src='$value' />";
  241. }
  242. //计算系统售价
  243. $sale_price = self::getSalePrice([
  244. 'spuCode' => $rs->spuCode,
  245. 'platform_id' => $rs->platform_id,
  246. 'min_num' => $data['start_sale_num'],
  247. 'relaComNo' => $data['relaComNo'],
  248. // 'platform_id' => $rs->platform_id
  249. ]);
  250. $update_data = array_merge($update_data, [
  251. 'yz_cat_id' => $data['yz_cat_id'],
  252. 'is_support_barter' => $data['is_support_barter'],
  253. 'sale_price' => $sale_price,
  254. 'cost_fee' => $nake_info['cost_fee'] ?? 0,
  255. 'final_price' => $data['final_price'],
  256. 'start_sale_num' => $data['start_sale_num'],
  257. 'reject_reason' => '',
  258. 'post_fee' => $post_fee,
  259. 'desc' => implode('', $desc_string),//商品描述
  260. 'good_ladder_id' => $data['good_ladder_id'],
  261. 'tags' => implode(',', $data['tags']),
  262. ]);
  263. //调有赞项目的接口,从有赞平台上线该商品
  264. $curl_data = [
  265. 'item_no' => $rs->skuCode,//商品自定义编码 skuCode
  266. 'item_type' => $data['item_type'],//0实物商品
  267. 'title' => $rs->good_name,//商品标题
  268. 'is_support_barter' => $data['is_support_barter'],//是否支持换货。1:支持;0:不支持
  269. 'desc' => implode('', $desc_string),//商品描述
  270. 'item_weight' => $rs->weight,
  271. 'category_id' => $rs->cat_id,
  272. 'auto_listing_time' => $data['auto_listing_time'],//0立即售出,传值表示定时(要大于当前时间戳)
  273. 'stock_deduct_mode' => 0,//0拍下减库存
  274. 'is_display' => 1,//1上架商品
  275. // 'quantity' => '1111111111',//库存数,库存数不管,用户自己去有赞平台调整这个数
  276. 'hide_stock' => 0,//0显示库存,1不显示库存
  277. 'origin' => $data['origin'],//系统售价
  278. 'price' => $data['final_price'],//最后售价
  279. 'uid' => $uid,//isset($userinfo['data']['id']) ? $userinfo['data']['id'] : 0,
  280. 'nickname' => $uname,//isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname'] : '',
  281. 'good_img' => $rs->good_img,//图片集合
  282. 'post_fee' => $post_fee,//运费,单位分,整数
  283. 'sell_point' => '',//商品卖点信息
  284. 'yz_cat_id' => $data['yz_cat_id'],//有赞类目id
  285. 'start_sale_num' => $data['start_sale_num'],//商品起售数,默认为1
  286. 'tags' => implode(',', $data['tags']),//标签id,多个以,分割
  287. 'distribution'=>$data['distribution'],
  288. ];
  289. $host = config('app.yz_domain');
  290. $responses = curl_request($host . 'api/yz_goodup', $curl_data);
  291. $response = json_decode($responses, true);
  292. Log::info("[".date('Y-m-d H:i:s')."] {$rs->skuCode} 接口yz_goodup:".$responses);
  293. if (!isset($response['code'])||$response['code'] != 0) {
  294. //上架失败
  295. $res = false;
  296. $update_data = array_merge($update_data, ['exam_status' => $db::$exam_status_7, 'offline_fail_reason' => $response['message']]);
  297. } else {
  298. //上架成功
  299. $update_data = array_merge($update_data, ['exam_status' => $db::$exam_status_6, 'offline_fail_reason' => '', 'online_time' => date('Y-m-d H:i:s')]);
  300. if ($response['data']['plat_code'] != '') {
  301. $update_data['plat_code'] = $response['data']['plat_code'];
  302. $update_good_platform_data = array_merge($update_good_platform_data, ['plat_code' => $response['data']['plat_code']]);
  303. }
  304. $update_good_platform_data = array_merge($update_good_platform_data, ['exam_status' =>3, 'online_time' => date('Y-m-d H:i:s'), 'is_online' => 1]);
  305. }
  306. } else {
  307. $update_data['reject_reason'] = $data['reject_reason'];
  308. $update_good_platform_data['exam_status'] = 5;
  309. }
  310. //更新
  311. $db->where('id', $platform_youzan_id)
  312. ->where('exam_status', '=', $db::$exam_status_0)
  313. ->save($update_data);
  314. Db::name('good_platform')
  315. ->where(['is_del' => 0, 'spuCode' => $rs->spuCode, 'skuCode' => $rs->skuCode, 'platform_code' => $rs->platform_id, 'exam_status' => 0])
  316. ->update($update_good_platform_data);
  317. //添加审核记录
  318. ActionLog::logAdd([
  319. 'id' => $uid,//isset($userinfo['data']['id']) ? $userinfo['data']['id'] : 0,
  320. 'nickname' => $uname//isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname'] : ''
  321. ], [
  322. "order_code" => $rs->skuCode,//编号
  323. "status" => $old_exam_status,
  324. "action_remark" => '',//备注
  325. "action_type" => "status"//新建create,编辑edit,更改状态status
  326. ], 'YZSX', $update_data['exam_status'], $update_data);
  327. ProcessOrder::AddProcess([
  328. 'id' => $uid,//isset($userinfo['data']['id']) ? $userinfo['data']['id'] : 0,
  329. 'nickname' => $uname,//isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname'] : ''
  330. ], [
  331. "order_type" => 'YZSX',
  332. "order_code" => $rs->skuCode,//咨询单详情编号
  333. "order_id" => $rs->id,
  334. "order_status" => $update_data['exam_status'],
  335. "before_status" => $old_exam_status,
  336. 'holder_id' => $rs->chargerid,
  337. ]);
  338. if ($res) {
  339. Db::commit();
  340. return app_show(0, '操作成功');
  341. } else throw new Exception('上架失败,' . $response['message']);
  342. } catch (Exception $exception) {
  343. Db::rollback();
  344. return error_show(1005, $exception->getMessage());
  345. }
  346. }
  347. //计算系统售价
  348. private static function getSalePrice(array $data = [])
  349. {
  350. $spuCode = $data['spuCode'];
  351. $platid = $data['platform_id'];
  352. $platform = Db::name("platform")->where(["id" => $platid, "is_del" => 0])->find();
  353. $num = $data['min_num'];
  354. if ($platform['platform_type'] != 1) throw new Exception('平台为非对接平台');
  355. $good = Db::name("good_basic")->where(["spuCode" => $spuCode, "is_del" => 0])->find();
  356. if ($good == false) throw new Exception('商品数据未找到');
  357. $nakelist = Db::name("good_nake")
  358. ->where(['spuCode' => $spuCode, "is_del" => 0])
  359. ->where("min_num", "<=", $num)
  360. ->order("min_num desc")
  361. ->find();
  362. if ($nakelist == false) {
  363. $nakelist = Db::name("good_nake")->where(['spuCode' => $spuCode, "is_del" => 0])->order("min_num asc")->find();
  364. //非库存品的话,继续校验最小起订量
  365. //库存品的话,不足采购起订量的时候,取采购最小起订量
  366. if ($good['is_stock'] == 0) throw new Exception("起订量不足{$nakelist['min_num']}");
  367. }
  368. //计算毛利率
  369. $budget = get_budget($good['cat_id'], $data['relaComNo'], $data['platform_id']);
  370. $top_cat_id = made($good['cat_id']);//获取所有分类
  371. $top_cat_id = isset($top_cat_id[0]['id']) ? $top_cat_id[0]['id'] : 0;//获取顶级分类id
  372. $sale_cost_fee = 0;
  373. if ($good['is_gold_price'] == 1 && $top_cat_id == 6) {
  374. // $saleprice = $good['noble_weight']*$good["cgd_gold_price"] + $nakelist['cost_fee']/(1-$budget)*$good['noble_weight']+$nakelist['mark_fee']+$nakelist['package_fee']+$nakelist['cert_fee']+$nakelist['nake_fee']+$nakelist['delivery_fee'];
  375. //系统售价=(贵金属重量*供应商采购金价 + 工艺费*贵金属重量+加标费+包装费+证书费+成本裸价+运费+其他费用)/(1-成本售价/100)
  376. $saleprice = ($good['noble_weight'] * $good["cgd_gold_price"] + $nakelist['cost_fee'] * $good['noble_weight'] + $nakelist['mark_fee'] + $nakelist['package_fee'] + $nakelist['cert_fee'] + $nakelist['nake_fee'] + $nakelist['delivery_fee'] + $nakelist['other_fee']) / (1 - $budget);
  377. //计算工艺费
  378. //销售工艺费=(( 商品重量* 供应商采购金价 + 采购成本工艺费* 商品重量+包装费+加标费+证书费+产品裸价+其他费用)/(1-成本售价/100)-(包装费+加标费+证书费+产品裸价0+运费+其他费用) )/商品重量-供应商采购金价
  379. $sale_cost_fee = (($good['noble_weight'] * $good["cgd_gold_price"] + $nakelist['cost_fee'] * $good['noble_weight'] + $nakelist['package_fee'] + $nakelist['mark_fee'] + $nakelist['cert_fee'] + $nakelist['nake_fee'] + $nakelist['other_fee'] + $nakelist['delivery_fee']) / (1 - $budget) - ($nakelist['package_fee'] + $nakelist['mark_fee'] + $nakelist['cert_fee'] + $nakelist['nake_fee'] + $nakelist['delivery_fee'] + $nakelist['other_fee'])) / $good['noble_weight'] - $good["cgd_gold_price"];
  380. } else {
  381. // $saleprice = ($nakelist['mark_fee'] + $nakelist['package_fee'] + $nakelist['cert_fee'] + $nakelist['delivery_fee'] + $nakelist['nake_fee']) / (1 - $budget);
  382. // (加标费+包装费+证书费+成本裸价+运费)/(1-成本售价/100)=系统售价
  383. // $saleprice = (加标费 + 包装费 + 证书费 + 运费 + 成本裸价+其他费用) / (1 - $budget);
  384. $saleprice = $nakelist['nake_total'] / (1 - $budget);
  385. }
  386. return round($saleprice, 2);
  387. }
  388. //有赞商品重新上线(上线失败的、下线后又重新上线的)
  389. public static function goodupOnlineAgain(int $platform_youzan_id = 0, string $token = '')
  390. {
  391. Db::startTrans();
  392. try {
  393. $db = new PlatformYouzan();
  394. $rs = $db
  395. ->field('id,spuCode,skuCode,platform_id,exam_status')
  396. ->where(['id' => $platform_youzan_id, 'is_del' => $db::$del_normal])
  397. ->whereIn('exam_status', [$db::$exam_status_7, $db::$exam_status_8])
  398. ->findOrEmpty();
  399. if ($rs->isEmpty()) return error_show(1005, '该商品上线记录不存在或不允许重新上线');
  400. $userinfo = GetUserInfo($token);
  401. $uid = isset($userinfo['data']['id']) ? $userinfo['data']['id'] : 0;
  402. $nickname = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname'] : '';
  403. $old_status = $rs->exam_status;
  404. //更新
  405. $db
  406. ->where('id', $platform_youzan_id)
  407. ->save([
  408. 'exam_status' => $db::$exam_status_0,
  409. 'updaterid' => $uid,
  410. 'updater' => $nickname,
  411. 'updatetime' => date('Y-m-d H:i:s'),
  412. ]);
  413. //更新
  414. Db::name('good_platform')
  415. ->where(['is_del' => 0, 'spuCode' => $rs->spuCode, 'skuCode' => $rs->skuCode, 'platform_code' => $rs->platform_id])
  416. ->where('exam_status', '<>', 0)
  417. ->update(['exam_status' => 0, 'updatetime' => date('Y-m-d H:i:s')]);
  418. $stn = ["order_code" => $rs->skuCode, "status" => $old_status, "action_remark" => '', "action_type" => "status"];
  419. ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], $stn, "YZSX", $db::$exam_status_0, ['id' => $platform_youzan_id]);
  420. $process = ["order_code" => $rs->skuCode, "order_id" => $rs->id, "order_status" => $db::$exam_status_0, "order_type" => 'YZSX', "before_status" => $old_status, 'holder_id' => $uid];
  421. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], $process);
  422. Db::commit();
  423. return app_show(0, '操作成功');
  424. } catch (Exception $exception) {
  425. Db::rollback();
  426. return error_show(1005, $exception->getMessage());
  427. }
  428. }
  429. //商品上线详情
  430. public static function getYzGoodupInfo(array $param = [])
  431. {
  432. $data = Db::name('platform_youzan')
  433. ->where('id', $param['platform_youzan_id'])
  434. ->whereOr('skuCode', $param['skuCode'])
  435. ->findOrEmpty();
  436. if (empty($data)) return error_show(1005, '查询不到该记录');
  437. $good_data = Db::name("good_basic")
  438. ->withoutField('createrid,creater,is_del,addtime,updatetime')
  439. ->where(["is_del" => 0, "spuCode" => $data['spuCode']])
  440. ->findOrEmpty();
  441. if (empty($good_data)) return error_show(1004, "未找到商品数据");
  442. $data = array_merge($data, $good_data);
  443. $plat = Db::name("platform")->where(["id" => $data['platform_id']])->find();
  444. // $data['skuCode'] = $data['skuCode'];
  445. $data['platform_code'] = $data['platform_id'];
  446. $data['platform_code_en'] = isset($plat['platform_code']) ? $plat['platform_code'] : "";
  447. $data['platform_name'] = isset($plat['platform_name']) ? $plat['platform_name'] : "";
  448. // $data['online_reason'] = $good_platform['online_reason'];
  449. // $data['online_time'] = $data['online_time'];
  450. // $data['online_remark'] = $good_platform['online_remark'];
  451. // $data['exam_status'] = $good_platform['exam_status'];
  452. // $data['is_online'] = $good_platform['is_online'];
  453. // $data['plat_code'] = $data['plat_code'];
  454. // $data['exclusive'] = makeExcluse($data['is_exclusive']);
  455. $unit = Db::name("unit")->where(["id" => $data['good_unit']])->find();
  456. $data['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  457. $data['cat_info'] = made($data['cat_id'], []);
  458. $data['yz_cat_info'] = made_youzan($data['yz_cat_id'], []);
  459. $spec = Db::name("good_spec")->where(["spuCode" => $data['spuCode'], "is_del" => 0])->select()->toArray();
  460. // $supplier = Db::name("supplier")->where(["code" => $data['supplierNo']])->find();
  461. $names = User::getIns()->handle('getCodeAndName',['code'=>[$data['supplierNo'],$data['companyNo']]]);
  462. $data['supplierName'] = $names['data'][$data['supplierNo']]??'';
  463. // $data['noble_name'] = isset($data['noble_metal']) && $data['noble_metal'] != 0 ? $this->noble[$data['noble_metal']] : "";
  464. // $company = Db::name("business")->where(["companyNo" => $data['companyNo']])->find();
  465. $data['company'] = $names['data'][$data['companyNo']]??'';//isset($company['company']) ? $company['company'] : "";
  466. if ($data['brand_id'] != 0) {
  467. $brand = Db::name("brand")->where(["id" => $data['brand_id']])->find();
  468. $data["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  469. } else {
  470. $data["brand_name"] = "";
  471. $data["brand_id"] = "";
  472. }
  473. $data['origin_place_cn'] = "";
  474. $data['delivery_place_cn'] = "";
  475. if ($data['delivery_place'] !== "") {
  476. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  477. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $data['delivery_place']);
  478. $data['delivery_place_cn'] = GetAddr(json_encode($place));
  479. }
  480. if ($data['origin_place'] !== "") {
  481. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  482. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $data['origin_place']);
  483. $data['origin_place_cn'] = GetAddr(json_encode($place));
  484. }
  485. $excluse = makeExcluse($data['is_exclusive']);
  486. $data['exclusive'] = $excluse;
  487. $data["good_info_img"] = $data['good_info_img'] != "" ? $data['good_info_img'] : [];
  488. $data["good_img"] = $data['good_img'] != "" ? $data['good_img'] : [];
  489. $speclist = [];
  490. if (!empty($spec)) {
  491. foreach ($spec as $value) {
  492. $temp = [];
  493. $temp['id'] = $value['id'];
  494. $temp['spuCode'] = $value['spuCode'];
  495. $temp['spec_id'] = $value['spec_id'];
  496. $temp['spec_value_id'] = $value['spec_value_id'];
  497. $temp['is_del'] = $value['is_del'];
  498. $sp = Db::name("specs")->where(["id" => $value['spec_id']])->find();
  499. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  500. $spv = Db::name("spec_value")->where(["id" => $value['spec_value_id']])->find();
  501. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  502. $speclist[] = $temp;
  503. }
  504. }
  505. $data["speclist"] = empty($speclist) ? [] : $speclist;
  506. $ladder = Db::name("good_ladder")->where(['skuCode' => $data['skuCode'], "is_del" => 0])->select()->toArray();
  507. $data["ladderlist"] = !empty($ladder) ? $ladder : [];
  508. $nakelist = Db::name("good_nake")->where(['spuCode' => $data['spuCode'], "is_del" => 0])->select()->toArray();
  509. $data['tax_info']=GoodTax::with(['Tax','OutTax'])->where(['spuCode'=>$data['spuCode']])->findOrEmpty();
  510. $gold_price = 0;
  511. if ($data['is_gold_price'] == 1 && $data['cost_fee'] != 0 && $data['good_price'] == 0) {
  512. $gold_price = Db::name("gold_price1")->where(["type" => $data['noble_metal'], "status" => 1, "is_del" => 0])->order("addtime desc")->value('price', 0);
  513. }
  514. $data['gold_price'] = $gold_price;
  515. $nakearry = [];
  516. if (!empty($nakelist)) {
  517. //实时金价
  518. foreach ($nakelist as $value) {
  519. $value['sale_price'] = $value['nake_total'];
  520. //计算最终售价
  521. if ($data['is_gold_price'] == 1 && $data['cost_fee'] != 0 && $data['good_price'] == 0) {
  522. //普通商品:直接用财务提交的售价字段;
  523. //贵金属( 商品重量* 最新金价 + 工艺费(财务定价提交的)* 商品重量+包装费+加标费+证书费+产品裸价+其他费用)=商品库商品直接下单的售价
  524. $value['sale_price'] = ($data['weight'] * $gold_price + $value['cost_fee'] * $data['weight'] + $value['package_fee'] + $value['mark_fee'] + $value['cert_fee'] + $value['nake_fee'] + $value['other_fee']);
  525. }
  526. $nakearry[] = $value;
  527. }
  528. }
  529. $proof = Db::name("good_proof")->where(["spuCode" => $data['spuCode'], "is_del" => 0])->order("updatetime desc")->field("id,proof_type,proof_url")->find();
  530. $data['proof'] = isset($proof) && $proof != false ? $proof : [];
  531. $data["nakelist"] = $nakearry;
  532. //补充采购员字段
  533. //成本表里的创建人,也是线上商品的采购人,为了防止混淆,给creater取个别名
  534. $temp_basic_info = Db::name('good_basic')
  535. ->field('id,createrid purchase_id,creater purchase')
  536. ->where(['spuCode' => $data['spuCode'], 'is_del' => 0])
  537. ->find();
  538. $data['purchase_id'] = $temp_basic_info['purchase_id'];
  539. $data['purchase'] = $temp_basic_info['purchase'];
  540. //补充商品详情链接字段
  541. $data['detail_url'] = YzGood::where(['is_del' => 0, 'item_no' => $data['skuCode']])
  542. ->value('detail_url', '');
  543. //格式化配送方式
  544. $data['distribution'] = explode(',', $data['distribution']);
  545. return app_show(0, "获取成功", $data);
  546. }
  547. //有赞商品下线
  548. public static function youzanGoodOffline(int $platform_youzan_id = 0, array $data = [])
  549. {
  550. Db::startTrans();
  551. try {
  552. $db = new PlatformYouzan();
  553. $rs = $db
  554. ->field('id,spuCode,skuCode,exam_status,platform_id')
  555. ->where(['id' => $platform_youzan_id, 'is_del' => $db::$del_normal])
  556. ->findOrEmpty();
  557. if ($rs->isEmpty()) throw new Exception('该记录不存在');
  558. if ($rs->exam_status == $db::$exam_status_8) throw new Exception('该商品已下线,无法重复下线');
  559. $userinfo = GetUserInfo($data['token']);
  560. $old_status = $rs->exam_status;
  561. $curl_data = [
  562. 'item_no' => $rs->skuCode,//商品自定义编码 skuCode
  563. 'uid' => isset($userinfo['data']['id']) ? $userinfo['data']['id'] : 0,
  564. 'uname' => isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname'] : '',
  565. ];
  566. if ($rs->exam_status == $db::$exam_status_6) {
  567. //已上线的要调用有赞接口下线
  568. //调有赞项目的接口,从有赞平台下线该商品
  569. $host = config('app.yz_domain');
  570. $response = curl_request($host . 'api/yz_goodup_offline', $curl_data);
  571. Log::info('['.date('Y-m-d H:i:s')."] {$rs->skuCode} 接口offline:".$response);
  572. $response = json_decode($response, true);
  573. if ($response['code'] != 0) throw new Exception($response['message']);
  574. }
  575. $db::where('id', $platform_youzan_id)
  576. ->update([
  577. 'exam_status' => $db::$exam_status_8,
  578. 'offline_reason_id' => $data['offline_reason_id'],
  579. 'offline_remark' => $data['offline_remark'],
  580. 'updaterid' => isset($userinfo['data']['id']) ? $userinfo['data']['id'] : 0,
  581. 'updater' => isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname'] : '',
  582. 'updatetime' => date('Y-m-d H:i:s')
  583. ]);
  584. //更新
  585. Db::name('good_platform')
  586. ->where(['is_del' => 0, 'spuCode' => $rs->spuCode, 'skuCode' => $rs->skuCode, 'platform_code' => $rs->platform_id])
  587. ->where('exam_status', '<>', 5)
  588. ->update(['exam_status' => 5, 'updatetime' => date('Y-m-d H:i:s')]);
  589. //下线记录表增加记录
  590. Db::name('good_offline_log_youzan')
  591. ->insert([
  592. 'spuCode' => $rs->spuCode,
  593. 'skuCodes' => $rs->skuCode,
  594. 'good_name' => Db::name("good_basic")->where('spuCode', $rs->spuCode)->value('good_name', ''),
  595. 'offline_reason' => $data['offline_reason_id'],
  596. 'offline_remark' => $data['offline_remark'],
  597. 'createrid' => isset($userinfo['data']['id']) ? $userinfo['data']['id'] : 0,
  598. 'creater' => isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname'] : '',
  599. 'addtime' => date('Y-m-d H:i:s')
  600. ]);
  601. $stn = ["order_code" => $rs->skuCode, "status" => $old_status, "action_remark" => '', "action_type" => "status"];
  602. ActionLog::logAdd(['id' => $curl_data['uid'], ['nickname' => $curl_data['uname']]], $stn, "YZSX", $db::$exam_status_8, $data);
  603. $process = ["order_code" => $rs->skuCode, "order_id" => $rs->id, "order_status" => $db::$exam_status_8, "order_type" => 'YZSX', "before_status" => $old_status, 'holder_id' => $curl_data['uid']];
  604. ProcessOrder::AddProcess(['id' => $curl_data['uid'], ['nickname' => $curl_data['uname']]], $process);
  605. Db::commit();
  606. return app_show(0, '操作成功');
  607. } catch (Exception $exception) {
  608. Db::rollback();
  609. return error_show(1005, $exception->getMessage());
  610. }
  611. }
  612. //有赞下线商品列表
  613. public static function youzanOfflineList(array $data = [])
  614. {
  615. $where = [];
  616. if ($data['start_date'] != '' && $data['end_date'] != '') $where[] = ['addtime', 'between', [$data['start_date'], $data['end_date']]];
  617. if ($data['creater'] != '') $where[] = ['creater', 'like', '%' . $data['creater'] . '%'];
  618. $count = Db::name('good_offline_log_youzan')->where($where)->count('id');
  619. $list = Db::name('good_offline_log_youzan')
  620. ->field('id,spuCode,good_name,creater,addtime')
  621. ->where($where)
  622. ->order('addtime', 'desc')
  623. ->page($data['page'], $data['size'])
  624. ->select()
  625. ->toArray();
  626. return app_show(0, "获取成功", ['list' => $list, 'count' => $count]);
  627. }
  628. //有赞下线商品详情
  629. public static function youzanOfflineDetail(int $id = 0)
  630. {
  631. $res = Db::name('good_offline_log_youzan')
  632. ->alias('gol')
  633. ->field('gol.*,ri.result')
  634. ->where('gol.id', $id)
  635. ->leftJoin('result_info ri', 'ri.result_code=gol.offline_reason')
  636. ->find();
  637. $res['children_list'] = Db::name('platform_youzan')
  638. ->alias('gp')
  639. ->field('gp.id,gp.skuCode,p.platform_name,g.creater purchease,gp.creater')
  640. ->where(['gp.spuCode' => $res['spuCode']])
  641. ->whereIn('gp.skuCode', $res['skuCodes'])
  642. ->leftJoin('platform p', 'p.id=gp.platform_id')
  643. ->leftJoin('good g', 'g.spuCode=gp.spuCode')
  644. ->select()
  645. ->toArray();
  646. return app_show(0, "获取成功", $res);
  647. }
  648. //商品下架(有赞平台操作)
  649. public static function goodOffLineByYouzan(string $item_no = '')
  650. {
  651. Db::startTrans();
  652. try {
  653. Db::table('wsm_good_platform')
  654. ->where(['is_del' => 0, 'skuCode' => $item_no])
  655. ->update([
  656. 'exam_status' => 5,
  657. 'is_online' => 0,
  658. 'updatetime' => date('Y-m-d H:i:s'),
  659. ]);
  660. $good_info = Db::table('wsm_platform_youzan')
  661. ->alias('py')
  662. ->field('py.id,py.spuCode,gb.good_name')
  663. ->leftJoin('good_basic gb', 'gb.spuCode=py.spuCode')
  664. ->where(['py.is_del' => 0, 'py.exam_status' => 6, 'py.skuCode' => $item_no])
  665. ->find();
  666. if (!empty($good_info)) {
  667. //更新采销上线记录表
  668. Db::table('wsm_platform_youzan')
  669. ->where('id', $good_info['id'])
  670. ->update([
  671. 'exam_status' => 8,
  672. 'offline_remark' => '有赞平台操作下架',
  673. 'offline_reason_id' => self::$offline_result_info_id,
  674. 'updaterid' => 0,
  675. 'updater' => 'youzan',
  676. 'updatetime' => date('Y-m-d H:i:s'),
  677. ]);
  678. //下线记录
  679. Db::table('wsm_good_offline_log_youzan')
  680. ->insert([
  681. 'spuCode' => isset($good_info['spuCode']) ? $good_info['spuCode'] : '',
  682. 'skuCodes' => $item_no,
  683. 'good_name' => isset($good_info['good_name']) ? $good_info['good_name'] : '',
  684. 'offline_reason' => self::$offline_result_info_id,
  685. 'offline_remark' => '有赞平台操作下架',
  686. 'createrid' => 0,
  687. 'creater' => 'youzan',
  688. 'addtime' => date('Y-m-d H:i:s'),
  689. ]);
  690. }
  691. Db::commit();
  692. return app_show(0, '操作成功');
  693. } catch (Exception $exception) {
  694. Db::rollback();
  695. return error_show(0, '操作失败,' . $exception->getMessage());
  696. }
  697. }
  698. //商品删除(有赞平台操作)
  699. public static function goodDeleteByYouzan(string $item_no = '')
  700. {
  701. Db::startTrans();
  702. try {
  703. Db::table('wsm_good_platform')
  704. ->where(['is_del' => 0, 'skuCode' => $item_no])
  705. ->update([
  706. 'is_del' => 1,//是否删除,0未删除,1删除
  707. 'updatetime' => date('Y-m-d H:i:s'),
  708. ]);
  709. //更新采销上线记录表
  710. Db::table('wsm_platform_youzan')
  711. ->where(['is_del' => 0, 'skuCode' => $item_no])
  712. ->update([
  713. 'is_del' => 1,//是否删除,0未删除,1删除
  714. 'updaterid' => 0,
  715. 'updater' => 'youzan',
  716. 'updatetime' => date('Y-m-d H:i:s'),
  717. ]);
  718. Db::commit();
  719. return app_show(0, '操作成功');
  720. } catch (Exception $exception) {
  721. Db::rollback();
  722. return error_show(1005, '操作失败,' . $exception->getMessage());
  723. }
  724. }
  725. }