123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- namespace app\common\model;
- use think\Model;
- use think\Db;
- /**
- * 模型
- */
- class OrderGoods extends Model
- {
- // 表名
- protected $name = 'shop_order_goods';
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = '';
- // 追加属性
- protected $append = [
- ];
- protected static $tagCount = 0;
- public function getGoodsImageAttr($value, $data)
- {
- $value = $value ?: ($data['image'] ?? '/assets/addons/shop/img/noimage.jpg');
- return cdnurl($value, true);
- }
- //销量增
- public static function setGoodsSalesInc($order_sn)
- {
- $list = (new self)->where('order_sn', $order_sn)->select();
- // 启动事务
- Db::startTrans();
- try {
- foreach ($list as $item) {
- $goods = $item->goods;
- $sku = $item->sku;
- if ($goods) {
- $goods->setInc('sales', $item->nums);
- }
- if ($sku) {
- $sku->setInc('sales', $item->nums);
- }
- }
- // 提交事务
- Db::commit();
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- }
- return true;
- }
- //销量减
- public static function setGoodsSalesDec($order_sn)
- {
- $list = (new self)->where('order_sn', $order_sn)->select();
- // 启动事务
- Db::startTrans();
- try {
- foreach ($list as $item) {
- $goods = $item->goods;
- $sku = $item->sku;
- if ($goods) {
- $goods->setDec('sales', $item->nums);
- }
- if ($sku) {
- $sku->setDec('sales', $item->nums);
- }
- }
- // 提交事务
- Db::commit();
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- }
- return true;
- }
- //库存增
- public static function setGoodsStocksInc($order_sn)
- {
- $list = (new self)->where('order_sn', $order_sn)->select();
- // 启动事务
- Db::startTrans();
- try {
- foreach ($list as $item) {
- $goods = $item->goods;
- $sku = $item->sku;
- if ($sku) {
- $sku->setInc('stocks', $item->nums);
- }
- if ($goods) {
- $goods->setInc('stocks', $item->nums);
- }
- }
- // 提交事务
- Db::commit();
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- }
- return true;
- }
- //库存减
- public static function setGoodsStocksDec($order_sn)
- {
- $list = (new self)->where('order_sn', $order_sn)->select();
- // 启动事务
- Db::startTrans();
- try {
- foreach ($list as $item) {
- $goods = $item->goods;
- $sku = $item->sku;
- if ($sku) {
- $sku->setDec('stocks', $item->nums);
- }
- if ($goods) {
- $goods->setDec('stocks', $item->nums);
- }
- }
- // 提交事务
- Db::commit();
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- }
- return true;
- }
- public function getGoodsSkuAttrAttr($value, $data)
- {
- return json_decode($value, true);
- }
- public function goods()
- {
- return $this->belongsTo('Goods', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(1);
- }
- public function Sku()
- {
- return $this->belongsTo('Sku', 'goods_sku_id', 'id', [], 'LEFT');
- }
- public function Order()
- {
- return $this->hasOne('Order', 'order_sn', 'order_sn', [], 'LEFT');
- }
- }
|