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