<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\admin\controller\bbfxshop\capitalpool;

use app\admin\controller\bbfxshop\Base;
use think\Db;


class Capitalpool extends Base
{

    public $model = null;
    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\Capitalpool;
    }
    
    public function getList(){
        $params = $this->param(['page/d','limit/d','keyword/s','startTime/s','endTime/s','type/d']);
        $pindex = max(1, intval($params["page"]));
        $psize = intval($params["limit"])?intval($params["limit"]):10;
        
        
        $where = ["uniacid"=>UNIACID];
        
        $keyword = trim($params['keyword']);
        $startTime = trim($params['startTime']);
        $endTime = trim($params['endTime']);
        $type = intval($params['type']);
        
        if($keyword){
            
        }
        if($type == 1){
            $where['type'] = ['<>',99];
            $where['money'] = ['>',0];
        }
        else if($type == 3){
            $where['type'] = 99;
        }
        else{
            $where['type'] = ['<>',99];
            $where['money'] = ['<',0];
        }
        $whereTime = [];
        if($startTime){
            $startTime = strtotime($startTime." 00:00:00");
            $where['createtime'] = ['>=',$startTime];
        }
        
        if($endTime){
            $endTime = strtotime($endTime." 23:59:59");
            $whereTime['createtime'] = ['<=',$endTime];
        }

        // echo "<pre>";
        // print_r($where);die;
        
        $list = Db::name("bbfx_capitalpool_money_log")->where($where)->where($whereTime)->order("createtime desc")->limit(($pindex-1)*$psize,$psize)->select();
        $total = Db::name("bbfx_capitalpool_money_log")->where($where)->where($whereTime)->count();
        
        foreach($list as $key=>&$val){
    	    $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
            $val['money'] = abs($val['money']);
        }

        $money_total = Db::name("bbfx_capitalpool_money_log")->where($where)->where($whereTime)->sum("money");
        $money_total = round($money_total,2);
        __success("操作成功",['total'=>$total,'list'=>$list,'money_total'=>abs($money_total)]);
    }

    public function subManualAbonusConfig(){
        $params = $this->param();
        
        $config = Db::name("bbfx_capitalpool")->where(["uniacid"=>UNIACID])->find();
        
        $data = array(
            "uniacid"=>UNIACID,
            "manual_abonus"=>iserializer($params['manual_abonus']),
        );
        
    	if(!empty($config)){
          	$res = Db::name("bbfx_capitalpool")->where(["id"=>$config['id']])->update($data);;
        }
      	else{
        	$res = Db::name("bbfx_capitalpool")->insert($data);
        }
        
        
        if($res){
            \app\admin\model\AdminLog::record("","",$data,"bbfx_capitalpool");
            __success("编辑成功",$data);
        }
      	else{
      	    __success("操作成功",$data);
      	}
    }
    
    public function setAbonusReward(){
        $result = $this->model->setManualAbonusReward();
        __result($result);
    }

    public function getOrderList(){
        $params = $this->param(['page/d','limit/d','keyword/s','startTime/s','endTime/s','type/d']);
        $pindex = max(1, intval($params["page"]));
        $psize = intval($params["limit"])?intval($params["limit"]):10;
        
        $config = $this->model->getSet();
        $manual_abonus = $config['manual_abonus'];
        $goods_ids = [-1];
        if(!empty($manual_abonus)){
            $condition_goods = $manual_abonus['condition_goods'];
            if(!empty($manual_abonus)){
                foreach($condition_goods as $k => $v){
                    $id = intval($v['id']);
                    if($id > 0){
                        $goods_ids[] = $id;
                    }
                }
            }
        }
        
        
        $where = ["o.uniacid"=>UNIACID,'o.is_pay'=>1,'og.goods_id'=>['in',$goods_ids]];
        
        $keyword = trim($params['keyword']);
        $startTime = trim($params['startTime']);
        $endTime = trim($params['endTime']);
        $type = intval($params['type']);

        $whereTime = [];
        if($startTime){
            $startTime = strtotime($startTime." 00:00:00");
            $where['o.createtime'] = ['>=',$startTime];
        }
        
        if($endTime){
            $endTime = strtotime($endTime." 23:59:59");
            $whereTime['o.createtime'] = ['<=',$endTime];
        }

        $list = Db::name("bbfx_order")
            ->alias("o")->join("bbfx_order_goods og","og.order_id = o.id","LEFT")
            ->join("bbfx_user u","o.user_id = u.id","LEFT")
            ->where($where)->where($whereTime)->order("o.id desc")
            ->limit(($pindex-1)*$psize,$psize)
            ->field("o.id,o.is_manual_abonus,o.order_no,o.goods_info,o.price,o.pay_time,o.paytype,o.createtime,u.nickname,u.avatar")
            ->select();

        $total = Db::name("bbfx_order")
            ->alias("o")->join("bbfx_order_goods og","og.order_id = o.id","LEFT")
            ->join("bbfx_user u","o.user_id = u.id","LEFT")
            ->where($where)->where($whereTime)
            ->count();

        foreach($list as $key=>&$val){
    	    $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
    	    $val['pay_time'] = date("Y-m-d H:i:s",$val['pay_time']);
            $goods = iunserializer($val['goods_info']);
            unset($val['goods_info']);

            foreach($goods as $k=>&$item){
                $item['thumb'] = cdnurl($item['thumb']);
            }
            unset($item);

            $val['goods'] = $goods;
        }

        __success("操作成功",['total'=>$total,'list'=>$list]);
    }

    public function setManualAbonusOrder(){
        $id = $this->param("id/d");
        $order = Db::name("bbfx_order")->where(['id'=>$id])->find();
        if(empty($order)){
            __error("订单不存在");
        }

        if($order['is_manual_abonus'] == 1){
            $data = ['is_manual_abonus'=>0];
        }
        else{
            $data = ['is_manual_abonus'=>1];
        }

        $res = Db::name("bbfx_order")->where(['id'=>$id])->update($data);
        if($res){
            __success("操作成功",$data);
        }
        else{
            __error("操作失败");
        }
    }
}