123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <?php
- namespace app\abutment\logic;
- use think\Exception;
- use think\facade\Db;
- class Good
- {
- //获取商品列表
- public static function list(array $param = [])
- {
- $db = Db::name('good_basic')
- ->alias('a')
- ->where(['a.supplierNo' => request()->supplier['supplierNo'], 'a.is_del' => 0]);
- // ->where('createrid', request()->user['id'])//创建人都取的是供应商的负责人,后面商品的创建人会转化成订单的采购员
- if ($param['keyword'] != '') $db->whereLike('spuCode|good_name', '%' . $param['keyword'] . '%');
- $count = $db->count('id');
- $list = $db
- ->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')
- ->leftJoin('brand b', 'b.id=a.brand_id')
- ->leftJoin('business c', 'c.companyNo=a.companyNo')
- ->withAttr('cat_name', function ($val, $data) {
- return made($data['cat_id']);
- })
- ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
- ->page($param['page'], $param['size'])
- ->select()
- ->toArray();
- return json_show(0, '请求成功', ['count' => $count, 'list' => $list]);
- }
- //创建商品
- public static function createGood(string $spucode = '', array $data = [], array $speclist = [], array $good_ladder = [])
- {
- Db::startTrans();
- try {
- $in = Db::name("good_basic")->insertGetId($data);
- if ($in) {
- // if ($speclist !== "" && !empty($speclist)) {
- $temp = [];
- foreach ($speclist as $value) {
- $lemp = [];
- $lemp['spuCode'] = $spucode;
- $lemp['spec_id'] = $value['spec_id'];
- $lemp['spec_value_id'] = $value['spec_value_id'];
- $lemp['addtime'] = date("Y-m-d H:i:s");
- $lemp['updatetime'] = date("Y-m-d H:i:s");
- $temp[] = $lemp;
- }
- if ($temp) Db::name("good_spec")->insertAll($temp);
- // }
- // $catinfo = Db::name("cat")->where(["id"=>$cat_id])->find();
- // $budget = isset($catinfo['order_rate']) ? $catinfo['order_rate']/100:0;
- $top_cat_id = made($data['cat_id']);//获取所有分类
- $top_cat_id = isset($top_cat_id[0]['id']) ? $top_cat_id[0]['id'] : 0;//获取顶级分类id
- if ($good_ladder !== "" && !empty($good_ladder)) {
- $temp = [];
- foreach ($good_ladder as $value) {
- $lemp = [];
- $lemp['spuCode'] = $spucode;
- $lemp['min_num'] = $value['min_num'];
- $lemp['nake_fee'] = $value['nake_fee'];
- $lemp['cost_fee'] = $value['cost_fee'];
- $lemp['delivery_fee'] = $value['delivery_fee'];
- $lemp['cert_fee'] = $value['cert_fee'];
- $lemp['mark_fee'] = $value['mark_fee'];
- $lemp['package_fee'] = $value['package_fee'];
- $lemp['other_fee'] = $value['other_fee'];
- if ($data['is_gold_price'] == 1 && $top_cat_id == 6) {
- //$data['is_gold_price']==1 表示启用实时金价,cat_id==6表示贵金属
- $gold = Db::name("gold_price1")->where(["type" => $data['noble_metal'], "status" => 1, "is_del" => 0])->order("addtime desc")->find();
- //$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'];
- //成本合计=贵金属重量*供应商采购金价 + 工艺费*贵金属重量+加标费+包装费+证书费+成本裸价+运费+其他费用
- //成本总计启用实时金价时,采用供应商实时金价
- $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'];
- } else $lemp['nake_total'] = $value['nake_fee'] + $value['delivery_fee'] + $value['cert_fee'] + $value['mark_fee'] + $value['package_fee'] + $value['other_fee'];
- // $lemp['creater_id'] = $createrid;
- // $lemp['creater'] = $creater;
- $lemp['is_del'] = 0;
- $lemp['addtime'] = date("Y-m-d H:i:s");
- $lemp['updatetime'] = date("Y-m-d H:i:s");
- $temp[] = $lemp;
- }
- $count = Db::name("good_nake")->insertAll($temp);
- if ($count == 0) throw new Exception('商品规格值创建失败');
- }
- Db::commit();
- return json_show(0, "商品创建成功", ['spuCode' => $spucode]);
- } else throw new Exception('商品创建失败');
- } catch (Exception $e) {
- Db::rollback();
- return json_show(1004, $e->getMessage());
- }
- }
- //修改商品基础信息
- public static function updateGoodBasicsInfo(string $spuCode = '', array $data = [])
- {
- $res = Db::name("good_basic")
- ->field('id')
- ->where('is_del', 0)
- ->where('spuCode', $spuCode)
- ->find();
- if (empty($res)) return json_show(1005, '该条商品数据不存在');
- if (isset($data['delivery_place'])) $data['delivery_place'] = implode(',', $data['delivery_place']);
- if (isset($data['origin_place'])) $data['origin_place'] = implode(',', $data['origin_place']);
- if (isset($data['good_img'])) $data['good_img'] = implode(',', $data['good_img']);
- if (isset($data['good_info_img'])) $data['good_info_img'] = implode(',', $data['good_info_img']);
- $data['updatetime'] = date('Y-m-d H:i:s');
- $rs = Db::name('good_basic')
- ->where('id', $res['id'])
- ->update($data);
- return $rs ? json_show(0, '修改商品基础信息成功') : json_show(1005, '修改商品基础信息失败');
- }
- //修改商品价格信息
- public static function updateGoodPriceInfo(string $spuCode = '', array $data = [])
- {
- $res = Db::name("good_basic")
- ->field('id')
- ->where('is_del', 0)
- ->where('spuCode', $spuCode)
- ->find();
- if (empty($res)) return json_show(1005, '该条商品数据不存在');
- //校验该商品是否上线,上线的话不允许修改价格相关信息
- $rs = Db::name('good_platform')
- ->field('id')
- ->where(['is_del' => 0, 'spuCode' => $spuCode, 'exam_status' => 6])
- ->findOrEmpty();
- if (!empty($rs)) return json_show(1005, '不允许修改上线中的商品价格信息');
- $data['updatetime'] = date("Y-m-d H:i:s");
- // "is_step" => count($good_ladder) > 1 ? 1 : 0,//如果阶梯价个数大于1,说明启用阶梯价
- $rs = Db::name('good_basic')
- ->strict(false)
- ->where('id', $res['id'])
- ->update($data);
- return $rs ? json_show(0, '修改商品价格信息成功') : json_show(1005, '修改商品价格信息失败');
- }
- //获取商品详情
- public static function read(string $spuCode = '')
- {
- $res = Db::name("good_basic")
- ->withoutField('good_code,createrid,creater,field_change')
- ->where('is_del', 0)
- ->where('spuCode', $spuCode)
- ->withAttr('good_img', function ($val) {
- return explode(',', $val);
- })
- ->withAttr('good_info_img', function ($val) {
- return explode(',', $val);
- })
- ->findOrEmpty();
- return json_show(0, '获取商品详情成功', $res);
- }
- }
|