123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- <?php
- namespace app\admin\service\goods;
- use app\model\Goods;
- use app\model\GoodsDetail;
- use app\model\GoodsLabel;
- use app\model\GoodsRunning;
- use app\model\GoodsSku;
- use app\model\SysSerial;
- use support\Db;
- use support\exception\BusinessException;
- use support\Response;
- class GoodsService
- {
- /**
- * @Desc 添加商品
- * @Author Gorden
- * @Date 2024/3/11 10:20
- *
- * @param $params
- * @return Response
- */
- public static function insert($params): Response
- {
- Db::beginTransaction();
- try {
- $params['goods_id'] = "GD" . sprintf('%016d', SysSerial::getSerial()) . random_string(8);
- // 主表
- self::mainInsert($params);
- // 商品详情表
- self::detailInsert($params);
- // 商品标签表
- self::labelInsert($params);
- // 产品运行控制信息表
- self::goodsRunningInsert($params);
- Db::commit();
- } catch (\PDOException $e) {
- return json_fail('数据写入失败~');
- } catch (BusinessException $e) {
- Db::rollBack();
- return json_fail($e->getMessage());
- } catch (\Exception $e) {
- Db::rollBack();
- return json_fail('数据写入失败~');
- }
- return json_success('success');
- }
- public static function update($params)
- {
- Db::beginTransaction();
- try {
- // 主表
- self::mainUpdate($params);
- // 商品详情表
- self::detailUpdate($params);
- // 商品标签表
- self::labelUpdate($params);
- // 产品运行控制信息表
- self::goodsRunningUpdate($params);
- Db::commit();
- } catch (BusinessException $e) {
- Db::rollBack();
- return json_fail($e->getMessage());
- } catch (\Exception $e) {
- Db::rollBack();
- return json_fail('数据更新失败~');
- }
- return json_success('success');
- }
- /**
- * @Desc 商品主表
- * @Author Gorden
- * @Date 2024/3/11 11:20
- *
- * @param $params
- * @return mixed|string
- * @throws BusinessException
- */
- public static function mainInsert($params)
- {
- try {
- $model = new Goods();
- $model->goods_id = $params['goods_id'];
- $model->join_goods_category_id = $params['join_goods_category_id'];
- $model->goods_classify = $params['goods_classify'];
- $model->goods_status = $params['goods_status'];
- $model->goods_category = $params['goods_category'];
- $model->goods_prefix = $params['goods_prefix'];
- $model->goods_name = $params['goods_name'];
- $model->goods_market_price = $params['goods_market_price'];
- $model->goods_sales_price = $params['goods_sales_price'];
- $model->goods_sku_json = !empty($params['goods_sku_json']) ? $params['goods_sku_json'] : '{}';
- $model->goods_attribute_json = !empty($params['goods_attribute_json']) ? $params['goods_attribute_json'] : '{}';
- $model->goods_service_json = !empty($params['goods_service_json']) ? $params['goods_service_json'] : '{}';
- $model->goods_title = $params['goods_title'];
- $model->goods_cover = $params['goods_cover'];
- $model->goods_on_addtimes = $params['goods_on_addtimes'] ? strtotime($params['goods_on_addtimes']) : null;
- $model->goods_sort = $params['goods_sort'];
- $model->goods_groupby = $params['goods_groupby'];
- $model->goods_remark = $params['goods_remark'];
- $model->goods_extend_json = $params['goods_extend_json'];
- $model->goods_addtimes = time();
- if ($model->save()) {
- return $model->goods_id;
- }
- // 异常
- throw new BusinessException("数据写入失败~");
- } catch (\Exception $e) {
- throw new BusinessException("数据写入失败~");
- }
- }
- /**
- * @Desc 详情表
- * @Author Gorden
- * @Date 2024/3/11 11:19
- *
- * @param $params
- * @return void
- * @throws BusinessException
- */
- public static function detailInsert($params)
- {
- try {
- $model = new GoodsDetail();
- $model->join_detail_goods_id = $params['goods_id'];
- $model->goods_detail_code_json = $params['goods_detail_code_json'];
- $model->goods_detail_slider_json = $params['goods_detail_slider_json'];
- $model->goods_detail_specs_json = $params['goods_detail_specs_json'];
- $model->goods_detail_content = $params['goods_detail_content'];
- if (!$model->save()) {
- // 异常
- throw new BusinessException("数据写入失败~");
- }
- } catch (\Exception $e) {
- throw new BusinessException("数据写入失败~");
- }
- }
- /**
- * @Desc 标签表
- * @Author Gorden
- * @Date 2024/3/11 11:32
- *
- * @param $params
- * @return void
- * @throws BusinessException
- */
- public static function labelInsert($params)
- {
- $model = new GoodsLabel();
- $model->join_label_goods_id = $params['goods_id'];
- $model->goods_label = $params['goods_label'];
- $model->goods_label_extend_json = !empty($params['goods_label_extend_json']) ? $params['goods_label_extend_json'] : '{}';
- if (!$model->save()) {
- // 异常
- throw new BusinessException('数据写入失败~');
- }
- }
- /**
- * @Desc 产品运行控制信息表
- * @Author Gorden
- * @Date 2024/3/11 11:38
- *
- * @param $params
- * @return void
- * @throws BusinessException
- */
- public static function goodsRunningInsert($params)
- {
- try {
- $model = new GoodsRunning();
- $model->join_running_goods_id = $params['goods_id'];
- $model->goods_running_storage = $params['goods_running_storage'];
- $model->goods_running_sale = $params['goods_running_sale'];
- $model->goods_running_off_type = $params['goods_running_off_type'];
- $model->goods_running_off_json = $params['goods_running_off_json'];
- if (!$model->save()) {
- throw new BusinessException('数据写入失败');
- }
- } catch (\Exception $e) {
- throw new BusinessException('数据写入失败');
- }
- }
- /**
- * @Desc SKU
- * @Author Gorden
- * @Date 2024/3/11 12:01
- *
- * @param $params
- * @return void
- * @throws BusinessException
- */
- public static function skuInsert($params)
- {
- $model = new GoodsSku();
- $model->join_sku_goods_id = $params['goods_id'];
- $model->goods_sku_status = $params['goods_sku_status'];
- $model->goods_sku_specs_json = $params['goods_sku_specs_json'];
- $model->goods_sku_title = $params['goods_sku_title'];
- $model->goods_sku_images_json = $params['goods_sku_images_json'];
- $model->goods_sku_content = $params['goods_sku_content'];
- $model->goods_sku_market_price = $params['goods_sku_market_price'];
- $model->goods_sku_sales_price = $params['goods_sku_sales_price'];
- $model->goods_sku_storage_json = $params['goods_sku_storage_json'];
- $model->goods_sku_service_json = $params['goods_sku_service_json'];
- $model->goods_sku_extend_json = $params['goods_sku_extend_json'];
- if (!$model->save()) {
- throw new BusinessException('数据写入失败~');
- }
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/3/12 8:44
- *
- * @param $params
- * @return void
- * @throws BusinessException
- */
- public static function mainUpdate($params)
- {
- try {
- $data = self::inputFilter($params, new Goods());
- $data['goods_on_addtimes'] = strtotime($data['goods_on_addtimes']);
- self::doUpdate($data['goods_id'], $data, new Goods());
- } catch (BusinessException $e) {
- throw new BusinessException($e->getMessage());
- } catch (\Exception $e) {
- throw new BusinessException('数据更新异常~');
- }
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/3/12 9:57
- *
- * @param $params
- * @return void
- * @throws BusinessException
- */
- public static function detailUpdate($params)
- {
- try {
- $data = self::inputFilter($params, new GoodsDetail());
- // 根据goods_id 查详情ID
- $detail = GoodsDetail::where('join_detail_goods_id', $params['goods_id'])->first();
- self::doUpdate($detail->goods_detail_id, $data, new GoodsDetail());
- } catch (BusinessException $e) {
- throw new BusinessException($e->getMessage());
- } catch (\Exception $e) {
- throw new BusinessException('数据更新异常~');
- }
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/3/12 9:58
- *
- * @param $params
- * @return void
- * @throws BusinessException
- */
- public static function labelUpdate($params)
- {
- try {
- $data = self::inputFilter($params, new GoodsLabel());
- // 根据goods_id 查详情ID
- $detail = GoodsLabel::where('join_label_goods_id', $params['goods_id'])->first();
- self::doUpdate($detail->goods_label_id, $data, new GoodsLabel());
- } catch (BusinessException $e) {
- throw new BusinessException($e->getMessage());
- } catch (\Exception $e) {
- throw new BusinessException('数据更新异常~');
- }
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/3/12 9:59
- *
- * @param $params
- * @return void
- * @throws BusinessException
- */
- public static function goodsRunningUpdate($params)
- {
- try {
- $data = self::inputFilter($params, new GoodsRunning());
- // 根据goods_id 查详情ID
- $detail = GoodsRunning::where('join_running_goods_id', $params['goods_id'])->first();
- self::doUpdate($detail->goods_running_id, $data, new GoodsRunning());
- } catch (BusinessException $e) {
- throw new BusinessException($e->getMessage());
- } catch (\Exception $e) {
- throw new BusinessException('数据更新异常~');
- }
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/3/12 8:45
- *
- * @param array $data
- * @param $model
- * @return array
- * @throws BusinessException
- */
- private static function inputFilter(array $data, $model): array
- {
- $table = config('database.connections.mysql.prefix') . $model->getTable();
- $allow_column = $model->getConnection()->select("desc `$table`");
- if (!$allow_column) {
- throw new BusinessException('表不存在', 2);
- }
- $columns = array_column($allow_column, 'Type', 'Field');
- foreach ($data as $col => $item) {
- if (!isset($columns[$col])) {
- unset($data[$col]);
- continue;
- }
- // 非字符串类型传空则为null
- if ($item === '' && strpos(strtolower($columns[$col]), 'varchar') === false && strpos(strtolower($columns[$col]), 'text') === false) {
- $data[$col] = null;
- }
- if (is_array($item)) {
- $data[$col] = implode(',', $item);
- }
- if ($item != '' && (strpos(strtolower($columns[$col]), 'varchar') || strpos(strtolower($columns[$col]), 'text'))) {
- $data[$col] = htmlspecialchars($item);
- }
- }
- if (empty($data['created_at'])) {
- unset($data['created_at']);
- }
- if (empty($data['updated_at'])) {
- unset($data['updated_at']);
- }
- return $data;
- }
- /**
- * @Desc 执行更新
- * @Author Gorden
- * @Date 2024/3/12 8:43
- *
- * @param $id
- * @param $data
- * @param $model
- * @return void
- */
- private static function doUpdate($id, $data, $model)
- {
- $row = $model->find($id);
- foreach ($data as $key => $val) {
- $row->{$key} = $val;
- }
- $row->save();
- }
- }
|