Goodup.php 41 KB

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