Good.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace app\abutment\logic;
  3. use think\Exception;
  4. use think\facade\Db;
  5. class Good
  6. {
  7. //获取商品列表
  8. public static function list(array $param = [])
  9. {
  10. $db = Db::name('good_basic')
  11. ->alias('a')
  12. ->where(['a.supplierNo' => request()->supplier['supplierNo'], 'a.is_del' => 0]);
  13. // ->where('createrid', request()->user['id'])//创建人都取的是供应商的负责人,后面商品的创建人会转化成订单的采购员
  14. if ($param['keyword'] != '') $db->whereLike('spuCode|good_name', '%' . $param['keyword'] . '%');
  15. $count = $db->count('id');
  16. $list = $db
  17. ->field('a.id,a.spuCode,a.good_thumb_img,a.good_name,a.cat_id,"" cat_name,a.brand_id,b.brand_name,a.companyNo,c.company,a.addtime')
  18. ->leftJoin('brand b', 'b.id=a.brand_id')
  19. ->leftJoin('business c', 'c.companyNo=a.companyNo')
  20. ->withAttr('cat_name', function ($val, $data) {
  21. return made($data['cat_id']);
  22. })
  23. ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
  24. ->page($param['page'], $param['size'])
  25. ->select()
  26. ->toArray();
  27. return json_show(0, '请求成功', ['count' => $count, 'list' => $list]);
  28. }
  29. //创建商品
  30. public static function createGood(string $spucode = '', array $data = [], array $speclist = [], array $good_ladder = [])
  31. {
  32. Db::startTrans();
  33. try {
  34. $in = Db::name("good_basic")->insertGetId($data);
  35. if ($in) {
  36. // if ($speclist !== "" && !empty($speclist)) {
  37. $temp = [];
  38. foreach ($speclist as $value) {
  39. $lemp = [];
  40. $lemp['spuCode'] = $spucode;
  41. $lemp['spec_id'] = $value['spec_id'];
  42. $lemp['spec_value_id'] = $value['spec_value_id'];
  43. $lemp['addtime'] = date("Y-m-d H:i:s");
  44. $lemp['updatetime'] = date("Y-m-d H:i:s");
  45. $temp[] = $lemp;
  46. }
  47. if ($temp) Db::name("good_spec")->insertAll($temp);
  48. // }
  49. // $catinfo = Db::name("cat")->where(["id"=>$cat_id])->find();
  50. // $budget = isset($catinfo['order_rate']) ? $catinfo['order_rate']/100:0;
  51. $top_cat_id = made($data['cat_id']);//获取所有分类
  52. $top_cat_id = isset($top_cat_id[0]['id']) ? $top_cat_id[0]['id'] : 0;//获取顶级分类id
  53. if ($good_ladder !== "" && !empty($good_ladder)) {
  54. $temp = [];
  55. foreach ($good_ladder as $value) {
  56. $lemp = [];
  57. $lemp['spuCode'] = $spucode;
  58. $lemp['min_num'] = $value['min_num'];
  59. $lemp['nake_fee'] = $value['nake_fee'];
  60. $lemp['cost_fee'] = $value['cost_fee'];
  61. $lemp['delivery_fee'] = $value['delivery_fee'];
  62. $lemp['cert_fee'] = $value['cert_fee'];
  63. $lemp['mark_fee'] = $value['mark_fee'];
  64. $lemp['package_fee'] = $value['package_fee'];
  65. $lemp['other_fee'] = $value['other_fee'];
  66. if ($data['is_gold_price'] == 1 && $top_cat_id == 6) {
  67. //$data['is_gold_price']==1 表示启用实时金价,cat_id==6表示贵金属
  68. $gold = Db::name("gold_price1")->where(["type" => $data['noble_metal'], "status" => 1, "is_del" => 0])->order("addtime desc")->find();
  69. //$lemp['nake_total'] = $data['noble_weight'] * $gold['price'] + $value['cost_fee'] * $data['noble_weight'] + $value['mark_fee'] + $value['package_fee'] + $value['cert_fee'] + $value['nake_fee'] + $value['delivery_fee'] + $value['other_fee'];
  70. //成本合计=贵金属重量*供应商采购金价 + 工艺费*贵金属重量+加标费+包装费+证书费+成本裸价+运费+其他费用
  71. //成本总计启用实时金价时,采用供应商实时金价
  72. $lemp['nake_total'] = $data['noble_weight'] * $gold['price'] + $value['cost_fee'] * $data['noble_weight'] + $value['mark_fee'] + $value['package_fee'] + $value['cert_fee'] + $value['nake_fee'] + $value['delivery_fee'] + $value['other_fee'];
  73. } else $lemp['nake_total'] = $value['nake_fee'] + $value['delivery_fee'] + $value['cert_fee'] + $value['mark_fee'] + $value['package_fee'] + $value['other_fee'];
  74. // $lemp['creater_id'] = $createrid;
  75. // $lemp['creater'] = $creater;
  76. $lemp['is_del'] = 0;
  77. $lemp['addtime'] = date("Y-m-d H:i:s");
  78. $lemp['updatetime'] = date("Y-m-d H:i:s");
  79. $temp[] = $lemp;
  80. }
  81. $count = Db::name("good_nake")->insertAll($temp);
  82. if ($count == 0) throw new Exception('商品规格值创建失败');
  83. }
  84. Db::commit();
  85. return json_show(0, "商品创建成功", ['spuCode' => $spucode]);
  86. } else throw new Exception('商品创建失败');
  87. } catch (Exception $e) {
  88. Db::rollback();
  89. return json_show(1004, $e->getMessage());
  90. }
  91. }
  92. //修改商品基础信息
  93. public static function updateGoodBasicsInfo(string $spuCode = '', array $data = [])
  94. {
  95. $res = Db::name("good_basic")
  96. ->field('id')
  97. ->where('is_del', 0)
  98. ->where('spuCode', $spuCode)
  99. ->find();
  100. if (empty($res)) return json_show(1005, '该条商品数据不存在');
  101. if (isset($data['delivery_place'])) $data['delivery_place'] = implode(',', $data['delivery_place']);
  102. if (isset($data['origin_place'])) $data['origin_place'] = implode(',', $data['origin_place']);
  103. if (isset($data['good_img'])) $data['good_img'] = implode(',', $data['good_img']);
  104. if (isset($data['good_info_img'])) $data['good_info_img'] = implode(',', $data['good_info_img']);
  105. $data['updatetime'] = date('Y-m-d H:i:s');
  106. $rs = Db::name('good_basic')
  107. ->where('id', $res['id'])
  108. ->update($data);
  109. return $rs ? json_show(0, '修改商品基础信息成功') : json_show(1005, '修改商品基础信息失败');
  110. }
  111. //修改商品价格信息
  112. public static function updateGoodPriceInfo(string $spuCode = '', array $data = [])
  113. {
  114. $res = Db::name("good_basic")
  115. ->field('id')
  116. ->where('is_del', 0)
  117. ->where('spuCode', $spuCode)
  118. ->find();
  119. if (empty($res)) return json_show(1005, '该条商品数据不存在');
  120. //校验该商品是否上线,上线的话不允许修改价格相关信息
  121. $rs = Db::name('good_platform')
  122. ->field('id')
  123. ->where(['is_del' => 0, 'spuCode' => $spuCode, 'exam_status' => 6])
  124. ->findOrEmpty();
  125. if (!empty($rs)) return json_show(1005, '不允许修改上线中的商品价格信息');
  126. $data['updatetime'] = date("Y-m-d H:i:s");
  127. // "is_step" => count($good_ladder) > 1 ? 1 : 0,//如果阶梯价个数大于1,说明启用阶梯价
  128. $rs = Db::name('good_basic')
  129. ->strict(false)
  130. ->where('id', $res['id'])
  131. ->update($data);
  132. return $rs ? json_show(0, '修改商品价格信息成功') : json_show(1005, '修改商品价格信息失败');
  133. }
  134. //获取商品详情
  135. public static function read(string $spuCode = '')
  136. {
  137. $res = Db::name("good_basic")
  138. ->withoutField('good_code,createrid,creater,field_change')
  139. ->where('is_del', 0)
  140. ->where('spuCode', $spuCode)
  141. ->withAttr('good_img', function ($val) {
  142. return explode(',', $val);
  143. })
  144. ->withAttr('good_info_img', function ($val) {
  145. return explode(',', $val);
  146. })
  147. ->findOrEmpty();
  148. return json_show(0, '获取商品详情成功', $res);
  149. }
  150. }