common.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. // 接口公共方法
  3. use think\Db;
  4. // 更新商品库存
  5. function update_goods_stock($goods_id,$goods_no,$num){
  6. $goods_info = Db::table('store_goods')
  7. ->field('id,spec,stock')
  8. ->where(['id'=>$goods_id])
  9. ->find();
  10. $spec = json_decode($goods_info['spec'],true);
  11. foreach ($spec as $gk=>&$gv) {
  12. if($gv['goods_no'] == $goods_no) {
  13. $spec[$gk]['store_num'] = bcsub($gv['store_num'],$num) > 0 ? bcsub($gv['store_num'],$num) : 0;
  14. }
  15. }
  16. $stock = $goods_info['stock']-$num >0 ? $goods_info['stock']-$num:0;
  17. return Db::table('store_goods')->where(['id'=>$goods_id])->update(['spec'=>json_encode($spec),'stock'=>$stock]);
  18. }
  19. // 获取商品规格详情
  20. function get_goods_spec($goods_id,$goods_no)
  21. {
  22. $goods_spec = [];
  23. $goods_info = Db::table('store_goods')->field('id,spec,stock')->find($goods_id);
  24. if(empty($goods_info)) return $goods_spec;
  25. $spec = json_decode($goods_info['spec'],true);
  26. foreach ($spec as $gk=>$gv) {
  27. if($gv['goods_no'] == $goods_no) {
  28. $goods_spec = $gv;
  29. }
  30. }
  31. return $goods_spec;
  32. }
  33. // 获取退款理由
  34. function get_refund_cause()
  35. {
  36. $list = Db::table('store_order_cause')
  37. ->field('id,title')
  38. ->where(['status'=>1])
  39. ->order('sort desc , id asc')
  40. ->select();
  41. return $list;
  42. }
  43. // 更新直播状态
  44. function change_live_status()
  45. {
  46. $date_str = date("Y-m-d H:i:s");
  47. // 将活动改为进行中状态
  48. Db::table('store_live')->where([['start_at','< time',$date_str],['end_at','> time',$date_str],['status','=',2]])->update(['status'=>1]);
  49. // 将活动改为过期状态
  50. Db::table('store_live')->where([['end_at','< time',$date_str]])->update(['status'=>3]);
  51. }