1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace app\common\model\qc;
- use app\api\model\member\MemberGoods;
- use app\common\model\BaseModel;
- use app\common\model\Goods;
- use app\store\model\GoodsSku;
- /**
- * 全场满件赠活动模型
- * @package app\store\model\qc
- */
- class QcMjSendGoods extends BaseModel
- {
- protected $name = 'qc_mj_send_goods';
- // 定义主键
- protected $pk = 'id';
- protected $append = ['goods_info','residue_stock'];
- public function getGoodsInfoAttr($value,$data){
- $res = Goods::alias('gd')
- ->leftJoin('goods_image gi','gd.goods_id=gi.goods_id')
- ->leftJoin('goods_sku gs','gd.goods_id=gs.goods_id')
- ->leftJoin('upload_file uf','gi.image_id=uf.file_id')
- ->where('gd.goods_id',$data['goods_id'])
- ->field('gd.goods_name,gd.goods_no,gd.goods_price_min,gd.line_price_min,gd.status,gd.sales_initial,gd.sales_actual,gd.sales_shops,(gd.sales_initial+gd.sales_actual+gd.sales_shops) as goods_sales,gi.image_id,uf.domain,uf.file_path,gs.goods_props')->find();
- $res->goods_props_obj = $res->goods_props?json_decode($res->goods_props):null;
- $res->goods_image = $res->domain.'/'.$res->file_path;
- $res->member_price = (new MemberGoods)->getGoodsMemberPrice($res->goods_id,$res->goods_price_min);
- return $res;
- }
- public function getResidueStockAttr($value,$data){
- if(isset($data['act_stock']) && isset($data['purchase_stock'])){
- $residue_stock = $data['act_stock']-$data['purchase_stock'];
- return $residue_stock>0?$residue_stock:0;
- }
- return 0;
- }
- public function add($activity_id,$data)
- {
- if (!empty($data)) {
- $insert = [];
- foreach ($data as $item) {
- // 判断是否存在
- $info = $this->where('qc_mj_send_activity_id', $activity_id)->where('goods_id', $item['goods_id'])->find();
- if (!empty($info)) { // 编辑
- $info->save([
- 'qc_mj_send_activity_id'=>$activity_id,
- 'goods_id' => $item['goods_id'],
- 'act_stock' => $item['act_stock'],
- ]);
- } else { // 新增
- $insert[] = [
- 'qc_mj_send_activity_id'=>$activity_id,
- 'goods_id' => $item['goods_id'],
- 'act_stock' => $item['act_stock'],
- ];
- }
- // 更新商品库存(只支持单规格)
- // $this->changeGoodsStocks($item['goods_id'], $act_stock);
- }
- $this->insertAll($insert);
- }
- }
- /**
- * 更新商品库存(只支持单规格)
- * @param $goods_id
- * @param $stock
- * @return mixed
- */
- public function changeGoodsStocks($goods_id, $stock)
- {
- // 取商品SKU
- $sku = GoodsSku::where('goods_id', $goods_id)->where('goods_sku_id', 0)->find();
- if (empty($sku)) {
- throw new \Exception('商品SKU信息不存在');
- }
- $stock_num = abs($stock);
- if ($sku['stock_num'] < $stock_num) {
- throw new \Exception("商品库存不足[商品id:{$goods_id}]");
- }
- if ($stock < 0) { // 增加
- Goods::where('goods_id', $goods_id)->inc('stock_total', $stock_num)->update();
- GoodsSku::where('goods_id', $goods_id)->where('goods_sku_id', 0)->inc('stock_num', $stock_num)->update();
- } else { // 减少
- Goods::where('goods_id', $goods_id)->dec('stock_total', $stock_num)->update();
- GoodsSku::where('goods_id', $goods_id)->where('goods_sku_id', 0)->dec('stock_num', $stock_num)->update();
- }
- }
- }
|