QcMjSendGoods.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace app\common\model\qc;
  3. use app\api\model\member\MemberGoods;
  4. use app\common\model\BaseModel;
  5. use app\common\model\Goods;
  6. use app\store\model\GoodsSku;
  7. /**
  8. * 全场满件赠活动模型
  9. * @package app\store\model\qc
  10. */
  11. class QcMjSendGoods extends BaseModel
  12. {
  13. protected $name = 'qc_mj_send_goods';
  14. // 定义主键
  15. protected $pk = 'id';
  16. protected $append = ['goods_info','residue_stock'];
  17. public function getGoodsInfoAttr($value,$data){
  18. $res = Goods::alias('gd')
  19. ->leftJoin('goods_image gi','gd.goods_id=gi.goods_id')
  20. ->leftJoin('goods_sku gs','gd.goods_id=gs.goods_id')
  21. ->leftJoin('upload_file uf','gi.image_id=uf.file_id')
  22. ->where('gd.goods_id',$data['goods_id'])
  23. ->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();
  24. $res->goods_props_obj = $res->goods_props?json_decode($res->goods_props):null;
  25. $res->goods_image = $res->domain.'/'.$res->file_path;
  26. $res->member_price = (new MemberGoods)->getGoodsMemberPrice($res->goods_id,$res->goods_price_min);
  27. return $res;
  28. }
  29. public function getResidueStockAttr($value,$data){
  30. if(isset($data['act_stock']) && isset($data['purchase_stock'])){
  31. $residue_stock = $data['act_stock']-$data['purchase_stock'];
  32. return $residue_stock>0?$residue_stock:0;
  33. }
  34. return 0;
  35. }
  36. public function add($activity_id,$data)
  37. {
  38. if (!empty($data)) {
  39. $insert = [];
  40. foreach ($data as $item) {
  41. // 判断是否存在
  42. $info = $this->where('qc_mj_send_activity_id', $activity_id)->where('goods_id', $item['goods_id'])->find();
  43. if (!empty($info)) { // 编辑
  44. $info->save([
  45. 'qc_mj_send_activity_id'=>$activity_id,
  46. 'goods_id' => $item['goods_id'],
  47. 'act_stock' => $item['act_stock'],
  48. ]);
  49. } else { // 新增
  50. $insert[] = [
  51. 'qc_mj_send_activity_id'=>$activity_id,
  52. 'goods_id' => $item['goods_id'],
  53. 'act_stock' => $item['act_stock'],
  54. ];
  55. }
  56. // 更新商品库存(只支持单规格)
  57. // $this->changeGoodsStocks($item['goods_id'], $act_stock);
  58. }
  59. $this->insertAll($insert);
  60. }
  61. }
  62. /**
  63. * 更新商品库存(只支持单规格)
  64. * @param $goods_id
  65. * @param $stock
  66. * @return mixed
  67. */
  68. public function changeGoodsStocks($goods_id, $stock)
  69. {
  70. // 取商品SKU
  71. $sku = GoodsSku::where('goods_id', $goods_id)->where('goods_sku_id', 0)->find();
  72. if (empty($sku)) {
  73. throw new \Exception('商品SKU信息不存在');
  74. }
  75. $stock_num = abs($stock);
  76. if ($sku['stock_num'] < $stock_num) {
  77. throw new \Exception("商品库存不足[商品id:{$goods_id}]");
  78. }
  79. if ($stock < 0) { // 增加
  80. Goods::where('goods_id', $goods_id)->inc('stock_total', $stock_num)->update();
  81. GoodsSku::where('goods_id', $goods_id)->where('goods_sku_id', 0)->inc('stock_num', $stock_num)->update();
  82. } else { // 减少
  83. Goods::where('goods_id', $goods_id)->dec('stock_total', $stock_num)->update();
  84. GoodsSku::where('goods_id', $goods_id)->where('goods_sku_id', 0)->dec('stock_num', $stock_num)->update();
  85. }
  86. }
  87. }