Goodup.php 40 KB

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