123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- // 接口公共方法
- use think\Db;
- // 更新商品库存
- function update_goods_stock($goods_id,$goods_no,$num){
- $goods_info = Db::table('store_goods')
- ->field('id,spec,stock')
- ->where(['id'=>$goods_id])
- ->find();
- $spec = json_decode($goods_info['spec'],true);
- foreach ($spec as $gk=>&$gv) {
- if($gv['goods_no'] == $goods_no) {
- $spec[$gk]['store_num'] = bcsub($gv['store_num'],$num) > 0 ? bcsub($gv['store_num'],$num) : 0;
- }
- }
- $stock = $goods_info['stock']-$num >0 ? $goods_info['stock']-$num:0;
- return Db::table('store_goods')->where(['id'=>$goods_id])->update(['spec'=>json_encode($spec),'stock'=>$stock]);
- }
- // 获取退款理由
- function get_refund_cause()
- {
- $list = Db::table('order_refund_cause')
- ->field('id,title')
- ->where(['status'=>1])
- ->order('sort desc , id asc')
- ->select();
- return $list;
- }
- //获取当月日历
- function get_month_days(){
- $date = date('Y-m-d');
- $start_time = strtotime($date);
- $total_month_day = date('t', $start_time);// 当月天数
- $month_data = [];
- $base_data = date("Y-m");
- for($i=1;$i<=$total_month_day;$i++) {
- $c = $i;
- if($c < 10) $c ='0'.$i;
- $month_data[] = [
- 'date_str'=>$base_data.'-'.$c,
- 'date' => $i,
- 'prefix_date' => $c,
- 'week' => date('w',strtotime($base_data.'-'.$c))
- ];
- }
- return $month_data;
- }
- function get_order_sn(){
- $order_id_main = date('YmdHis') . rand(10000000,99999999);
- $order_id_len = strlen($order_id_main);
- $order_id_sum = 0;
- for($i=0; $i<$order_id_len; $i++){
- $order_id_sum += (int)(substr($order_id_main,$i,1));
- }
- $osn = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100,2,'0',STR_PAD_LEFT);
- return $osn;
- }
- /**
- *随机抽奖
- * @param array $data
- * @param string $field
- * @param int $num
- * @return array ['抽中的键'=>'抽中次数']
- */
- function draw_lottery ($data = [],$field='',$num = 1,$is_clip=0)
- {
- $draw_data = [];
- foreach ($data as $k=>$v)
- {
- if($is_clip && $k== 0) continue;
- if($v[$field] > 0)
- {
- for($i=1;$i<= $v[$field];$i++) {
- $draw_data[] = $k.'_key_'.$i;
- }
- }
- }
- $rand_key = array_rand($draw_data,$num);
- $draw_key = [];
- if($num == 1) {
- $sel_key = explode( '_key_',$draw_data[$rand_key])[0];
- $draw_key[$sel_key] = 1;
- }else{
- for ($a=0;$a<$num;$a++) {
- $sel_key = explode( '_key_',$draw_data[$rand_key[$a]])[0];
- if(isset($draw_key[$sel_key])) {
- $draw_key[$sel_key]++;
- }else{
- $draw_key[$sel_key] = 1;
- }
- }
- }
- return $draw_key;
- }
- /**
- *指定抽奖
- * @param array $data
- * @param string $field
- * @param int $num
- * @param int $stock 剩余库存
- * @return array ['抽中的键'=>'抽中次数']
- */
- function point_lottery ($data = [],$field='',$num = 1,$check_num = 0)
- {
- $draw_data = [];
- $draw_key = [];
- foreach ($data as $k=>$v) {
- // 指定前三个中奖
- if(in_array($k,[0,1,2]) && isset($v['point_num'])) {
- // 剩余库存 > 0 && 售卖数量>= 指定数量 && 可以抽奖
- if( $v['store_num'] > 0 && $check_num >= $v['point_num'] && $num > 0){
- $draw_key[$k] = 1;
- $num = $num - 1;
- }
- }elseif($v[$field] > 0 ) {
- for($i=1;$i<= $v[$field];$i++) {
- $draw_data[] = $k.'_key_'.$i;
- }
- }
- }
- if($num == 0) return $draw_key;// 不可以抽奖
- $rand_key = array_rand($draw_data,$num);
- if($num == 1) {
- $sel_key = explode( '_key_',$draw_data[$rand_key])[0];
- $draw_key[$sel_key] = 1;
- }else{
- for ($a=0;$a<$num;$a++) {
- $sel_key = explode( '_key_',$draw_data[$rand_key[$a]])[0];
- if(isset($draw_key[$sel_key])) {
- $draw_key[$sel_key]++;
- }else{
- $draw_key[$sel_key] = 1;
- }
- }
- }
- return $draw_key;
- }
- function cdraw_lottery($data,$draw_key,$user_id=0,$sel_key)
- {
- $keys = max(array_keys($draw_key));
- if($data[$sel_key]['store_num'] > 0 && $keys != $sel_key) {
- $draw_key[$keys] = $draw_key[$keys] - 1;
- if($draw_key[$keys] == 0) unset($draw_key[$keys]);
- if(isset($draw_key[$sel_key])) {
- $draw_key[$sel_key] = $draw_key[$sel_key]+1;
- }else{
- $draw_key[$sel_key] = 1;
- }
- }
- return $draw_key;
- }
- //获取运费
- function get_freight($num)
- {
- $first_freight = floatval(sysconf('first_freight_price')) > 0 ? floatval(sysconf('first_freight_price')):0;
- $later_freight = floatval(sysconf('later_freight_price')) > 0 ? floatval(sysconf('later_freight_price')):0;
- $all_freight = bcadd($first_freight,bcmul($num-1,$later_freight,2),2);
- return ['all_freight'=>$all_freight,'first_freight'=>$first_freight,'later_freight'=>$later_freight];
- }
- // 生成消息记录
- function send_user_message($user_id,$type_id,$content){
- $msg_data =[
- 'user_id' => $user_id,
- 'type_id' => $type_id,
- 'content' => $content,
- 'create_at' => date('Y-m-d H:i:s'),
- ];
- Db::table('user_message')->insert($msg_data);
- return Db::getLastInsID();
- }
- // 自动分解魔玩柜
- function resolve_magic($user_id = 0)
- {
- $time = time();
- $where = [];
- $where[] = ['num','>',0];
- $where[] = ['past_int','<',$time];
- if($user_id) $where[] = ['user_id','=',$user_id];
- $magic_case = Db::table('magic_case')
- ->field('id,user_id,num,crystal')
- ->where($where)
- ->select();
- foreach ($magic_case as $value) {
- // 扣减魔玩柜变更记录
- $crystal = bcmul($value['num'],$value['crystal'],2);
- $log_data= [
- 'user_id' => $value['user_id'],
- 'create_at' => date('Y-m-d H:i:s'),
- 'num' => $value['num'],
- 'case_id' => $value['id'],
- 'crystal' =>$crystal ,
- ];
- //更新明信片余额
- $member_info = Db::table('store_member')->field('id,crystal_cash')->find($value['user_id']);
- // 扣减魔玩柜的数量
- Db::startTrans();
- $res1= Db::table('magic_case')->where('id',$value['id'])->update(['num'=>0]);
- $res2 = Db::table('store_member')->where('id',$value['user_id'])->update(['crystal_cash'=>bcadd($member_info['crystal_cash'],$crystal,2)]);
- Db::table('magic_resolve')->insert($log_data);// 分解日志
- // 余额日志
- crystal_log($value['user_id'],$crystal,'魔玩柜分解',3);
- if($res1 && $res2) {
- Db::commit();
- }else{
- Db::rollback();
- }
- }
- }
- // 运费券过期
- function user_coupon_over($user_id = 0)
- {
- $where = [];
- $where[] = ['status','=',1];
- $where[] = ['past_at','< time',date('Y-m-d H:i:s')];
- if($user_id) $where[] = ['user_id','=',$user_id];
- Db::table('user_coupon_list')
- ->where($where)
- ->update(['status'=>3]);
- }
|