common.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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('order_refund_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],['status','=',2]])->update(['status'=>1]);
  49. }
  50. //获取当月日历
  51. function get_month_days(){
  52. $date = date('Y-m-d');
  53. $start_time = strtotime($date);
  54. $total_month_day = date('t', $start_time);// 当月天数
  55. $month_data = [];
  56. $base_data = date("Y-m");
  57. for($i=1;$i<=$total_month_day;$i++) {
  58. $c = $i;
  59. if($c < 10) $c ='0'.$i;
  60. $month_data[] = [
  61. 'date_str'=>$base_data.'-'.$c,
  62. 'date' => $i,
  63. 'prefix_date' => $c,
  64. 'week' => date('w',strtotime($base_data.'-'.$c))
  65. ];
  66. }
  67. return $month_data;
  68. }