Goodup.php 18 KB

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