<?php
/**
 * 主播分佣模块
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author dongziyi <2066207038@qq.com>
 */

namespace Wxsmall\Model;
use Think\Model;
class LiveDividedModel extends Model{
    /**添加主播分佣信息
     * @param array $data=array(
            'goods_id'=>商品id,
     *      'goods_number'=>商品数量,
     *      'anchor_id'=>主播会员id,
     *      'cart_id'=>购物车id,
     *      'order_id'=>订单id,
     *      'order_goods_id'=>订单商品id,
     *      'order_status'=>订单状态,0未付款,1已付款已确认
     *      'divided_status'=>分佣状态
     *      'shop_price'=>商品价格
     *      'goods_attr'=>商品属性
     * )
     * @return array
     */
   public function add_divided($data=array()){
       $return=array('status'=>1);
        if(empty($data['goods_id'])){
            $return['status']=0;
            $return['msg']='商品不能为空';
            return $return;
        }
       if(empty($data['goods_number'])){
           $return['status']=0;
           $return['msg']='商品数量不能为空';
           return $return;
       }
       if(empty($data['anchor_id'])){
           $return['status']=0;
           $return['msg']='主播不能为空';
           return $return;
       }
       $data['balance_money']=$this->get_goods_balancemoney($data['goods_id'],$data['goods_number'],$data['shop_price']);

       $map=array();
       $map['goods_id']=$data['goods_id'];
       if($data['goods_attr']){
           $map['goods_attr']=$data['goods_attr'];
       }
       if(empty($data['order_id'])) {   //若是购物车
           $map['order_status'] = 0;
           $map['divided_status'] = 0;
           $map['order_id'] = array('elt', 0);
           $map['status'] = 1;
           $live_dcount = M("LiveDivided")->where($map)->field('id,goods_number')->find();
           if ($live_dcount) {
               $l_map = array();
               $l_map['id'] = $live_dcount['id'];
               $l_map['status'] = 1;
               $data['update_time'] = NOW_TIME;
               $data['goods_number'] = $live_dcount['goods_number'] + $data['goods_number'];
               $res = M("LiveDivided")->where($l_map)->save($data);
           } else {
               $data['add_time'] = NOW_TIME;
               $res = M("LiveDivided")->add($data);
           }
       }else{//若是有订单
           $data['add_time'] = NOW_TIME;
           $res = M("LiveDivided")->add($data);
       }
       if($res){
           $return['status']=1;
           $return['msg']='添加成功';
           return $return;
       }else{
           $return['status']=0;
           $return['msg']='添加主播分佣统计信息失败';
           return $return;
       }
       return $return;
   }

    /**获取某商品主播应得的佣金
     * @param $goods_id
     */
    public function get_goods_balancemoney($goods_id,$goods_number,$goods_price){
        if(empty($goods_id) || empty($goods_number) || empty($goods_price))return false;
        $map=array();
        $balance_rate=0;
        $map['id']=$goods_id;
        $goods_cate = M("Goods")->where($map)->getField('cat_id');
        if ($goods_cate > 0) {
            //找出某商品一级分类的佣金比
            $balance_rate = D("Common/GoodsCategory")->get_parent_pid($goods_cate);
        }
        //商品总原价
        $goods_total_price=$goods_price*$goods_number;
        //获取主播佣金比
        // $a_map=array();
        // $a_map['name']='live_balance_rate';
        // $anchor_rate=M("LiveSet")->where($a_map)->getField('value');
        $anchor_rate=getconfig('LIVE_BALANCE_RATE');
        
        //主播应得佣金
        $anchor_balance_price=format_decimal($goods_total_price*(1-$balance_rate/100)*$anchor_rate/100);
        return $anchor_balance_price;
    }

    /**添加主播订单
     * @param $cart_ids
     * @param $order_ids
     */
    public function save_order_divided($cart_ids,$order_ids){
        $return=array('status'=>1);
        if(empty($cart_ids) || empty($order_ids)){
            $return['status']=0;
            $return['msg']="参数错误";
            return $return;
        }
        if(strstr($order_ids,',')){
            $order_ids=array_filter(explode(',',$order_ids));
        }
        $map=$o_map=array();
        $map['cart_id']=array('in',$cart_ids);
        $anchor_goods=M("LiveDivided")->where($map)->field('cart_id,goods_id')->select();
        $o_map['order_id']=array('in',$order_ids);
        $order_goods=M("OrderGoods")->where($o_map)->field('id,goods_id,goods_number,goods_price,order_id')->select();
        foreach(array_filter($order_goods) as $key=>$val){
            foreach(array_filter($anchor_goods) as $ak=>$av){
                if($val['goods_id']==$av['goods_id']){
                    $s_map=$s_data=array();
                    $s_map['goods_id']=$av['goods_id'];
                    $s_map['cart_id']=$av['cart_id'];
                    $s_data['order_id']=$val['order_id'];
                    $s_data['order_goods_id']=$val['id'];
                    $s_data['goods_number']=$val['goods_number'];
                    $balance_price=$this->get_goods_balancemoney($val['goods_id'],$val['goods_number'],$val['goods_price']);
                    $s_data['balance_money']=$balance_price;
                    $s_data['update_time']=NOW_TIME;
                    M("LiveDivided")->where($s_map)->save($s_data);
                }
            }
        }
        return $return;
    }

    /**更改主播订单状态
     * @param $order_ids
     */
    public function save_order_status($order_ids){
        $return=array('status'=>1);
        if(empty($order_ids)){
            $return['status']=0;
            $return['msg']='参数错误';
            return $return;
        }
        $map=array();
        $map['order_id']=$order_ids;
        $map['order_status']=0;
        $map['divided_status']=0;
        $map['status']=1;
        $divided_count=M("LiveDivided")->where($map)->count();
        if($divided_count){
            $map=$data=array();
            $map['order_id']=$order_ids;
            $map['status']=1;
            $data['order_status']=1;
            $data['divided_status']=1;
            $res=M("LiveDivided")->where($map)->save($data);
            if($res){
                $return['status']=1;
                $return['msg']='修改成功';
                return $return;
            }else{
                $return['status']=0;
                $return['msg']='修改失败';
                return $return;
            }
        }
        return $return;
    }

    /**确认收货后
     * @param $order_id
     */
    public function anchor_deivided($order_id){
        $return=array('status'=>1);
        if(empty($order_id)){
            $return['status']=0;
            $return['msg']='参数错误';
            return $return;
        }
        //修改主播订单状态
        $a_map=array();
        $a_map['order_id']=$order_id;
        $a_map['status']=1;
        $a_map['order_status']=1;
        $a_data=array();
        $a_data['order_status']=3;
       $res=M("LiveDivided")->where($a_map)->save($a_data);
        if($res){
            //计算主播总的应得佣金
            $a_map=array();
            $a_map['order_id']=$order_id;
            $a_map['status']=1;
            $a_map['order_status']=3;
            $total_money=M("LiveDivided")->where($a_map)->sum("balance_money");
            //获取主播会员
            $anchor_id=M("LiveDivided")->where($a_map)->getField('anchor_id');
            $anchor_name=idtoname($anchor_id,"Member",'mobile');
            account_change($anchor_id,$total_money,1,5,'主播'.$anchor_name.'推荐商品,得到分成');
            //修改分佣状态
            $a_map=array();
            $a_map['order_id']=$order_id;
            $a_map['status']=1;
            $a_map['order_status']=3;
            $save_data=array();
            $save_data['divided_status']=2;
            $save_data['divided_time']=NOW_TIME;
           M("LiveDivided")->where($a_map)->save($save_data);
        }else{
            $return['status']=0;
            $return['msg']='修改主播订单收货状态失败';
            return $return;
        }
        return $return;
    }
}