Goodup.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. <?php
  2. namespace app\youzan\logic;
  3. use app\admin\model\ActionLog;
  4. use app\admin\model\ProcessOrder;
  5. use app\youzan\model\PlatformYouzan;
  6. use think\Exception;
  7. use think\facade\Cache;
  8. use think\facade\Db;
  9. //商品处理层
  10. class Goodup
  11. {
  12. //提交商品上线
  13. public static function create(array $data = [])
  14. {
  15. $isonline = [];
  16. $i = 0;
  17. Db::startTrans();
  18. try {
  19. foreach ($data['good_data'] as $value) {
  20. $count = Db::name("good_nake")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->count();
  21. $ist = Db::name("good_platform")->where(["spuCode" => $value['spuCode'], "platform_code" => $data['platform_id'], "is_del" => 0])->find();
  22. if ($ist) {
  23. $god = Db::name("good_basic")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->find();
  24. if ($god) {
  25. $tp = [];
  26. $tp['spuCode'] = $value['spuCode'];
  27. $tp['good_name'] = $god['good_name'];
  28. $tp['good_img'] = $god['good_thumb_img'];
  29. $tp['exam_status'] = $ist['exam_status'];
  30. $spec = Db::name("good_spec")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->select()->toArray();
  31. $speclist = [];
  32. if (!empty($spec)) {
  33. foreach ($spec as $val) {
  34. $temp = [];
  35. $temp['id'] = $val['id'];
  36. $temp['spuCode'] = $val['spuCode'];
  37. $temp['spec_id'] = $val['spec_id'];
  38. $temp['spec_value_id'] = $val['spec_value_id'];
  39. $temp['is_del'] = $val['is_del'];
  40. $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
  41. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  42. $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
  43. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  44. $speclist[] = $temp;
  45. }
  46. }
  47. $tp["speclist"] = empty($speclist) ? [] : $speclist;
  48. $isonline[] = $tp;
  49. }
  50. }
  51. if (!empty($isonline)) {
  52. return app_show(1009, "存在已上线产品", $isonline);
  53. }
  54. //改变编码规则,将原来的skuCode后两位换成序列号
  55. //str_pad字符串填充
  56. $skuCode = makeNo("SN");
  57. $skuCode = substr($skuCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  58. $tmp = [
  59. "platform_id" => $data['platform_id'],
  60. "online_reason" => $data['online_reason'],
  61. "online_remark" => $data['online_remark'],
  62. "spuCode" => $value['spuCode'],
  63. "skuCode" => $skuCode,
  64. "exam_status" => PlatformYouzan::$exam_status_0,
  65. "is_del" => PlatformYouzan::$del_normal,
  66. "createrid" => $data['createrid'],
  67. "creater" => $data['creater'],
  68. "updaterid" => $data['createrid'],
  69. "updater" => $data['creater'],
  70. "addtime" => date("Y-m-d H:i:s"),
  71. "updatetime" => date("Y-m-d H:i:s")
  72. ];
  73. $i++;
  74. //修改状态,添加待办
  75. $create = Db::name("platform_youzan")->insert($tmp, true);
  76. if ($create > 0) {
  77. ActionLog::logAdd(['id' => $data['createrid'], 'nickname' => $data['creater']], [
  78. "order_code" => $skuCode,
  79. "status" => $tmp['exam_status'],//这里的status是之前的值
  80. "action_remark" => '',//备注
  81. "action_type" => "create"//新建create,编辑edit,更改状态status
  82. ], "SPSX", $tmp['exam_status'], $tmp);
  83. ProcessOrder::AddProcess(['id' => $data['createrid'], 'nickname' => $data['creater']], [
  84. "order_type" => 'SPSX',
  85. "order_code" => $skuCode,
  86. "order_id" => $create,
  87. "order_status" => $tmp['exam_status'], "before_status" => $tmp['exam_status']
  88. ]);
  89. $iso = Db::name("good")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->find();
  90. if ($iso == false) {
  91. unset($value['id']);
  92. $value['addtime'] = date("Y-m-d H:i:s");
  93. $value['updatetime'] = date("Y-m-d H:i:s");
  94. } else {
  95. $value['id'] = $iso['id'];
  96. $value['updatetime'] = date("Y-m-d H:i:s");
  97. }
  98. $up = Db::name("good")->save($value);
  99. if (!$up) {
  100. Db::rollback();
  101. return error_show(1004, "提交失败");
  102. }
  103. } else {
  104. Db::rollback();
  105. return error_show(1004, "提交失败");
  106. }
  107. }
  108. Db::commit();
  109. return app_show(0, "提交成功");
  110. } catch (\Exception $e) {
  111. Db::rollback();
  112. return error_show(1004, $e->getMessage());
  113. }
  114. }
  115. //审核并将商品推送到有赞平台
  116. public static function youzanGoodUpOnline(int $platform_youzan_id = 0, array $data = [])
  117. {
  118. $db = new PlatformYouzan();
  119. $rs = $db
  120. ->field('a.id,a.spuCode,a.skuCode,a.exam_status,gb.good_name,gb.weight,gb.good_img,gb.cat_id,gb.good_remark')
  121. ->alias('a')
  122. ->where(['a.id' => $platform_youzan_id, 'a.is_del' => $db::$del_normal])
  123. ->leftJoin('good_basic gb', 'gb.spuCode=a.spuCode')
  124. ->findOrEmpty();
  125. if ($rs->isEmpty()) return error_show(1005, '该商品上线记录不存在');
  126. if ($rs->exam_status != $db::$exam_status_0) return error_show(1005, '该商品并不是待审核状态');
  127. $ladder_info = Db::name('good_ladder')
  128. ->field('id,min_num,sale_price,cost_fee')
  129. ->where('id', $data['good_ladder_id'])
  130. ->where('is_del', 0)
  131. ->find();
  132. if (empty($ladder_info)) return error_show(1005, '该商品阶梯记录不存在');
  133. $userinfo = GetUserInfo($data['token']);
  134. $update_data = [
  135. 'exam_status' => $data['exam_status'],
  136. 'updaterid' => isset($userinfo['data']['id']) ? $userinfo['data']['id'] : 0,
  137. 'updater' => isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname'] : '',
  138. 'updatetime' => date('Y-m-d H:i:s'),
  139. ];
  140. $res = true;
  141. if ($data['exam_status'] == $db::$exam_status_1) {
  142. //运费,单位分,整数
  143. $post_fee = Db::name('good_nake')
  144. ->where(['spuCode' => $rs->spuCode, 'is_del' => 0])
  145. ->where('min_num', '<=', $data['start_sale_num'])
  146. ->value('delivery_fee', 0);
  147. //审核通过
  148. $update_data = array_merge($update_data, [
  149. 'yz_cat_id' => $data['yz_cat_id'],
  150. 'is_support_barter' => $data['is_support_barter'],
  151. 'sale_price' => $ladder_info['sale_price'],
  152. 'cost_fee' => $ladder_info['cost_fee'],
  153. 'final_price' => $data['final_price'],
  154. 'start_sale_num' => $data['start_sale_num'],
  155. 'reject_reason' => '',
  156. 'post_fee' => $post_fee,
  157. 'desc' => $rs->good_remark,//商品描述
  158. 'good_ladder_id' => $data['good_ladder_id'],
  159. 'tags' => implode(',', $data['tags']),
  160. ]);
  161. //调有赞项目的接口,从有赞平台下线该商品
  162. $curl_data = [
  163. 'item_no' => $rs->skuCode,//商品自定义编码 skuCode
  164. 'item_type' => 0,//0实物商品
  165. 'title' => $rs->good_name,//商品标题
  166. 'is_support_barter' => $data['is_support_barter'],//是否支持换货。1:支持;0:不支持
  167. 'desc' => $rs->good_remark,//商品描述
  168. 'item_weight' => $rs->weight,
  169. 'category_id' => $rs->cat_id,
  170. 'auto_listing_time' => '0',//0立即售出,传值表示定时(要大于当前时间戳)
  171. 'stock_deduct_mode' => 0,//0拍下减库存
  172. 'is_display' => 1,//1上架商品
  173. 'quantity' => '1111111111',//库存数,先随便写个数,待 @戴 确定
  174. 'hide_stock' => 0,//0显示库存,1不显示库存
  175. 'origin' => $ladder_info['sale_price'],//系统售价
  176. 'price' => $data['final_price'],//最后售价
  177. 'uid' => isset($userinfo['data']['id']) ? $userinfo['data']['id'] : 0,
  178. 'nickname' => isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname'] : '',
  179. 'good_img' => $rs->good_img,//图片集合
  180. 'post_fee' => $post_fee,//运费,单位分,整数
  181. 'sell_point' => '',//商品卖点信息
  182. 'yz_cat_id' => $data['yz_cat_id'],//有赞类目id
  183. 'start_sale_num' => $data['start_sale_num'],//商品起售数,默认为1
  184. 'tags' => $data['tags'],//标签id,多个以,分割
  185. ];
  186. $host = config('app.yz_domain');
  187. $response = curl_request($host . 'api/yz_goodup', $curl_data);
  188. $response = json_decode($response, true);
  189. if ($response['code'] != 0) {
  190. //上架失败
  191. $res = false;
  192. $update_data = array_merge($update_data, ['exam_status' => $db::$exam_status_7, 'offline_fail_reason' => $response['message']]);
  193. } else {
  194. //上架成功
  195. $update_data = array_merge($update_data, ['exam_status' => $db::$exam_status_6, 'offline_fail_reason' => '', 'plat_code' => $response['data']['plat_code']]);
  196. }
  197. } else $update_data['reject_reason'] = $data['reject_reason'];
  198. //更新
  199. $db->where('id', $platform_youzan_id)
  200. ->where('exam_status', '=', $db::$exam_status_0)
  201. ->save($update_data);
  202. return $res ? app_show(0, '操作成功') : error_show(1005, '上架失败,' . $response['message']);
  203. }
  204. //有赞商品重新上线(上线失败的、下线后又重新上线的)
  205. public static function goodupOnlineAgain(int $platform_youzan_id = 0, string $token = '')
  206. {
  207. $db = new PlatformYouzan();
  208. $rs = $db
  209. ->field('id')
  210. ->where(['id' => $platform_youzan_id, 'is_del' => $db::$del_normal])
  211. ->whereIn('exam_status', [$db::$exam_status_7, $db::$exam_status_8])
  212. ->findOrEmpty();
  213. if ($rs->isEmpty()) return error_show(1005, '该商品上线记录不存在或不允许重新上线');
  214. $userinfo = GetUserInfo($token);
  215. //更新
  216. $res = $db->where('id', $platform_youzan_id)
  217. ->save([
  218. 'exam_status' => $db::$exam_status_0,
  219. 'updaterid' => isset($userinfo['data']['id']) ? $userinfo['data']['id'] : 0,
  220. 'updater' => isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname'] : '',
  221. 'updatetime' => date('Y-m-d H:i:s'),
  222. ]);
  223. return $res ? app_show(0, '操作成功') : error_show(1005, '重新发起上线失败');
  224. }
  225. //商品上线详情
  226. public static function getYzGoodupInfo(int $platform_youzan_id = 0)
  227. {
  228. $data = Db::name('platform_youzan')
  229. ->where('id', $platform_youzan_id)
  230. ->find();
  231. if (empty($data)) return error_show(1005, '查询不到该记录');
  232. $good_data = Db::name("good")
  233. ->withoutField('createrid,creater,is_del,addtime,updatetime')
  234. ->where(["is_del" => 0, "spuCode" => $data['spuCode']])
  235. ->find();
  236. if ($good_data == false) return error_show(1004, "未找到商品数据");
  237. $data = array_merge($data, $good_data);
  238. $plat = Db::name("platform")->where(["id" => $data['platform_id']])->find();
  239. // $data['skuCode'] = $data['skuCode'];
  240. $data['platform_code'] = $data['platform_id'];
  241. $data['platform_code_en'] = isset($plat['platform_code']) ? $plat['platform_code'] : "";
  242. $data['platform_name'] = isset($plat['platform_name']) ? $plat['platform_name'] : "";
  243. // $data['online_reason'] = $good_platform['online_reason'];
  244. // $data['online_time'] = $data['online_time'];
  245. // $data['online_remark'] = $good_platform['online_remark'];
  246. // $data['exam_status'] = $good_platform['exam_status'];
  247. // $data['is_online'] = $good_platform['is_online'];
  248. // $data['plat_code'] = $data['plat_code'];
  249. // $data['exclusive'] = makeExcluse($data['is_exclusive']);
  250. $unit = Db::name("unit")->where(["id" => $data['good_unit']])->find();
  251. $data['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  252. $data['cat_info'] = made($data['cat_id'], []);
  253. $data['yz_cat_info'] = made_youzan($data['yz_cat_id'], []);
  254. $spec = Db::name("good_spec")->where(["spuCode" => $data['spuCode'], "is_del" => 0])->select()->toArray();
  255. $supplier = Db::name("supplier")->where(["code" => $data['supplierNo']])->find();
  256. $data['supplierName'] = isset($supplier['name']) ? $supplier['name'] : "";
  257. // $data['noble_name'] = isset($data['noble_metal']) && $data['noble_metal'] != 0 ? $this->noble[$data['noble_metal']] : "";
  258. $company = Db::name("business")->where(["companyNo" => $data['companyNo']])->find();
  259. $data['company'] = isset($company['company']) ? $company['company'] : "";
  260. if ($data['brand_id'] != 0) {
  261. $brand = Db::name("brand")->where(["id" => $data['brand_id']])->find();
  262. $data["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  263. } else {
  264. $data["brand_name"] = "";
  265. $data["brand_id"] = "";
  266. }
  267. $data['origin_place_cn'] = "";
  268. $data['delivery_place_cn'] = "";
  269. if ($data['delivery_place'] !== "") {
  270. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  271. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $data['delivery_place']);
  272. $data['delivery_place_cn'] = GetAddr(json_encode($place));
  273. }
  274. if ($data['origin_place'] !== "") {
  275. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  276. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $data['origin_place']);
  277. $data['origin_place_cn'] = GetAddr(json_encode($place));
  278. }
  279. $excluse = makeExcluse($data['is_exclusive']);
  280. $data['exclusive'] = $excluse;
  281. $data["good_info_img"] = $data['good_info_img'] != "" ? $data['good_info_img'] : [];
  282. $data["good_img"] = $data['good_img'] != "" ? $data['good_img'] : [];
  283. $speclist = [];
  284. if (!empty($spec)) {
  285. foreach ($spec as $value) {
  286. $temp = [];
  287. $temp['id'] = $value['id'];
  288. $temp['spuCode'] = $value['spuCode'];
  289. $temp['spec_id'] = $value['spec_id'];
  290. $temp['spec_value_id'] = $value['spec_value_id'];
  291. $temp['is_del'] = $value['is_del'];
  292. $sp = Db::name("specs")->where(["id" => $value['spec_id']])->find();
  293. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  294. $spv = Db::name("spec_value")->where(["id" => $value['spec_value_id']])->find();
  295. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  296. $speclist[] = $temp;
  297. }
  298. }
  299. $data["speclist"] = empty($speclist) ? [] : $speclist;
  300. $ladder = Db::name("good_ladder")->where(['skuCode' => $data['skuCode'], "is_del" => 0])->select()->toArray();
  301. $data["ladderlist"] = !empty($ladder) ? $ladder : [];
  302. $nakelist = Db::name("good_nake")->where(['spuCode' => $data['spuCode'], "is_del" => 0])->select()->toArray();
  303. // $catinfo = Db::name("cat")->where(["id"=>$data['cat_id']])->find();
  304. // $cat_top_list = $data['yz_cat_info'];
  305. // $cat_top_id = isset($cat_top_list[0]['id']) ? $cat_top_list[0]['id'] : 0;
  306. // $budget = isset($catinfo['order_rate']) ? $catinfo['order_rate']/100:0;
  307. $gold_price = 0;
  308. if ($data['is_gold_price'] == 1 && $data['cost_fee'] != 0 && $data['good_price'] == 0) {
  309. $gold_price = Db::name("gold_price1")->where(["type" => $data['noble_metal'], "status" => 1, "is_del" => 0])->order("addtime desc")->value('price', 0);
  310. }
  311. $data['gold_price'] = $gold_price;
  312. $nakearry = [];
  313. if (!empty($nakelist)) {
  314. //实时金价
  315. foreach ($nakelist as $value) {
  316. $value['sale_price'] = $value['nake_total'];
  317. //计算最终售价
  318. if ($data['is_gold_price'] == 1 && $data['cost_fee'] != 0 && $data['good_price'] == 0) {
  319. //普通商品:直接用财务提交的售价字段;
  320. //贵金属( 商品重量* 最新金价 + 工艺费(财务定价提交的)* 商品重量+包装费+加标费+证书费+产品裸价+其他费用)=商品库商品直接下单的售价
  321. $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']);
  322. }
  323. $nakearry[] = $value;
  324. }
  325. }
  326. $proof = Db::name("good_proof")->where(["spuCode" => $data['spuCode'], "is_del" => 0])->order("updatetime desc")->field("id,proof_type,proof_url")->find();
  327. $data['proof'] = isset($proof) && $proof != false ? $proof : [];
  328. $data["nakelist"] = $nakearry;
  329. //补充采购员字段
  330. //成本表里的创建人,也是线上商品的采购人,为了防止混淆,给creater取个别名
  331. $temp_basic_info = Db::name('good_basic')
  332. ->field('id,createrid purchase_id,creater purchase')
  333. ->where(['spuCode' => $data['spuCode'], 'is_del' => 0])
  334. ->find();
  335. $data['purchase_id'] = $temp_basic_info['purchase_id'];
  336. $data['purchase'] = $temp_basic_info['purchase'];
  337. return app_show(0, "获取成功", $data);
  338. }
  339. //有赞商品下线
  340. public static function youzanGoodOffline(int $platform_youzan_id = 0, array $data = [])
  341. {
  342. Db::startTrans();
  343. try {
  344. $db = new PlatformYouzan();
  345. $rs = $db
  346. ->field('id,skuCode,exam_status')
  347. ->where(['id' => $platform_youzan_id, 'is_del' => $db::$del_normal])
  348. ->findOrEmpty();
  349. if ($rs->isEmpty()) throw new Exception('该记录不存在');
  350. if ($rs->exam_status == $db::$exam_status_8) throw new Exception('该商品已下线,无法重复下线');
  351. $userinfo = GetUserInfo($data['token']);
  352. $curl_data = [
  353. 'item_no' => $rs->skuCode,//商品自定义编码 skuCode
  354. 'uid' => isset($userinfo['data']['id']) ? $userinfo['data']['id'] : 0,
  355. 'nickname' => isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname'] : '',
  356. ];
  357. if ($rs->exam_status == $db::$exam_status_6) {
  358. //已上线的要调用有赞接口下线
  359. //调有赞项目的接口,从有赞平台下线该商品
  360. $host = config('app.yz_domain');
  361. $response = curl_request($host . 'api/yz_goodup_offline', $curl_data);
  362. $response = json_decode($response, true);
  363. if ($response['code'] != 0) throw new Exception($response['message']);
  364. }
  365. $db::where('id', $platform_youzan_id)
  366. ->update([
  367. 'exam_status' => $db::$exam_status_8,
  368. 'offline_reason_id' => $data['offline_reason_id'],
  369. 'offline_remark' => $data['offline_remark'],
  370. 'updaterid' => isset($userinfo['data']['id']) ? $userinfo['data']['id'] : 0,
  371. 'updater' => isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname'] : '',
  372. 'updatetime' => date('Y-m-d H:i:s')
  373. ]);
  374. Db::commit();
  375. return app_show(0, '操作成功');
  376. } catch (Exception $exception) {
  377. Db::rollback();
  378. return error_show(1005, $exception->getMessage());
  379. }
  380. }
  381. //有赞下线商品列表
  382. public static function youzanOfflineList(array $data = [])
  383. {
  384. $where = [['pyz.exam_status', '=', PlatformYouzan::$exam_status_8]];//已下线
  385. if ($data['start_date'] != '' && $data['end_date'] != '') $where[] = ['pyz.addtime', 'between', [$data['start_date'], $data['end_date']]];
  386. if ($data['creater'] != '') $where[] = ['pyz.creater', 'like', '%' . $data['creater'] . '%'];
  387. $count = Db::name('platform_youzan')
  388. ->alias('pyz')
  389. ->leftJoin('good g', 'g.spuCode=pyz.spuCode')
  390. ->where($where)
  391. ->count('pyz.id');
  392. $list = PlatformYouzan::field('pyz.id,pyz.id platform_youzan_id,pyz.spuCode,g.good_name,pyz.updater,pyz.updatetime')
  393. ->alias('pyz')
  394. ->leftJoin('good g', 'g.spuCode=pyz.spuCode')
  395. ->where($where)
  396. ->order('pyz.addtime', 'desc')
  397. ->page($data['page'], $data['size'])
  398. ->select()
  399. ->toArray();
  400. return app_show(0, "获取成功", ['list' => $list, 'count' => $count]);
  401. }
  402. //有赞下线商品详情
  403. public static function youzanOfflineDetail(int $platform_youzan_id = 0)
  404. {
  405. $info = PlatformYouzan::where('pyz.id', $platform_youzan_id)
  406. ->field('pyz.*,g.good_name,r.result,p.platform_name')
  407. ->alias('pyz')
  408. ->leftJoin('good g', 'g.spuCode=pyz.spuCode')
  409. ->leftJoin('result_info r', 'r.result_code=pyz.offline_reason_id')
  410. ->leftJoin('platform p', 'p.id=pyz.platform_id')
  411. ->findOrEmpty();
  412. return app_show(0, "获取成功", $info);
  413. }
  414. }