Goodup.php 38 KB

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