<?php namespace Place\Lib;
use Dever;
use Place;
class Resource
{
    public $app;
    public $type;
    protected $db;
    protected $where;
    protected $set;
    public function __construct($type)
    {
        $this->type = $type;
        if (!$this->type) {
            Dever::error('资源参数错误');
        }
        if (isset(Dever::config('setting')['type'][$this->type])) {
            $this->app = Dever::config('setting')['type'][$this->type];
            $this->db = Dever::db('info', $this->app);
        } else {
            Dever::error('资源参数错误');
        }
        $this->where = array('status' => 1);
        $this->set['col'] = Dever::load('info', $this->app)->getCol();
    }

    # 获取列表
    public function getList($cate = false, $id = false, $num = 10)
    {
        if ($cate) {
            $cate = explode(',', $cate);
            $this->where['cate_parent_id'] = $cate[0];
            if (isset($cate[1])) {
                $this->where['cate_child_id'] = $cate[0];
            }
        }
        $name = Dever::input('search');
        if ($name) {
            $this->where['name'] = array('like', $name);
        }
        if ($id) {
            $this->set['limit'] = '0,6';
            $this->where['id'] = array('!=', $id);
        } else {
            $this->set['num'] = $num;
        }
        $info = $this->db->select($this->where, $this->set);
        $result = array();
        foreach ($info as $k => $v) {
            $v = $this->handleInfo($v);
            $result[] = $v;
        }
        return $result;
    }

    public function getInfo($id, $state = true)
    {
        if (!$id) {
            Dever::error('资源不存在');
        }
        $this->where['id'] = $id;
        $info = $this->db->find($this->where);
        if (!$info) {
            if ($state) {
                Dever::error('资源不存在');
            }
            return false;
        }
        return $this->handleInfo($info);
    }

    # 下单
    public function pay_commit(){}
    public function pay($info)
    {
        $order = array();
        $order['uid'] = Place::$uid;
        $order['name'] = $info['name'];
        $prefix = strtoupper(substr($this->app, 0, 2));
        if ($this->type == 3) {
            $order['cash'] = $info['value'];
        } else {
            $order['cash'] = ($info['value']/Place::$info['score_per']);
        }
        $order['order_num'] = Dever::load('util', 'api')->createNumber($prefix, 'place/order');
        $order['notify'] = 'place/callback.resource|' . Dever::input('p') . '|' . $order['order_num'] . '|' . $this->type;

        $data['pay'] = Dever::load('account', 'place')->pay($order);
        if ($data['pay'] && empty($data['pay']['link'])) {
            $order['status'] = 1;
            $order['type'] = $this->type;
            $order['type_id'] = $info['id'];
            $data['order_id'] = Dever::db('order', 'place')->insert($order);
            $data['order_num'] = $order['order_num'];
            if (isset($info['detail'])) {
                $detail['order_id'] = $data['order_id'];
                foreach ($info['detail'] as $k => $v) {
                    $detail['type'] = $this->type;
                    $detail['type_id'] = $v['id'];
                    if (isset($v['sku_id'])) {
                        $detail['sku_id'] = $v['sku_id'];
                    }
                    if (isset($v['sku_name'])) {
                        $detail['sku_name'] = $v['sku_name'];
                    }
                    $detail['pic'] = $v['pic'];
                    $detail['cash'] = $v['cash'];
                    $detail['num'] = $v['num'];
                    $detail['name'] = $v['name'];
                    $detail['pic'] = $v['pic'];
                    Dever::db('order_detail', 'place')->insert($detail);
                }
            }
        }
        return $data;
    }

    # 同步资源，关联资源
    public function relation($place_id, $data, $relation)
    {
        $where['channel_place_id'] = $place_id;
        $where['channel_type'] = $data['type'];
        $where['channel_type_id'] = $data['type_id'];
        $info = Dever::db('resource_relation', 'place')->find($where);
        if ($info) {
            if ($info['status'] == 2) {
                Dever::db('resource_relation', 'place')->update($info['id'], array('status' => 1));
                $update['source'] = 2;
                Dever::db('info', $this->app, false)->update($info['type_id'], $update);
                # 同步sku
                $this->relationSku($info['type_id'], $relation);
            }
        } else {
            $parent_id = $child_id = 0;
            foreach ($relation['cate'] as $k => $v) {
                unset($v['id']);
                unset($v['cdate']);
                if (!$parent_id) {
                    $v['parent_id'] = 0;
                    $parent_id = $this->createCate($v);
                } else {
                    $v['parent_id'] = $parent_id;
                    $child_id = $this->createCate($v);
                }
            }
            $cate[] = $relation['info']['cate_parent_id'] = $parent_id;
            if ($child_id) {
                $cate[] = $relation['info']['cate_child_id'] = $child_id;
            }
            $relation['info']['cate'] = implode(',', $cate);
            $relation['info']['source'] = 2;
            unset($relation['info']['id']);
            $info_id = Dever::db('info', $this->app, false)->insert($relation['info']);
            if ($info_id) {
                # 同步sku
                $this->relationSku($info_id, $relation);

                $where['type'] = $data['type'];
                $where['type_id'] = $info_id;
                $where['channel_price'] = $data['price'];
                $where['status'] = 1;
                Dever::db('resource_relation', 'place')->insert($where);
            }
        }
    }

    # 同步sku
    public function relationSku($info_id, $relation)
    {
        if (isset($relation['sku']) && $relation['sku']) {
            $spec_value = array();
            $spec_db = Dever::db('spec', $this->app, false);
            $spec_value_db = Dever::db('spec_value', $this->app, false);
            $sku_db = Dever::db('sku', $this->app, false);
            $spec_db->delete(array('info_id' => $info_id));
            $spec_value_db->delete(array('info_id' => $info_id));
            $sku_db->delete(array('info_id' => $info_id));
            foreach ($relation['spec'] as $v) {
                unset($v['id']);
                unset($v['cdate']);
                $v['info_id'] = $info_id;
                $spec_id = $spec_db->insert($v);
                foreach ($v['value'] as $v1) {
                    $id = $v1['id'];
                    unset($v1['id']);
                    unset($v1['cdate']);
                    $v1['info_id'] = $info_id;
                    $v1['spec_id'] = $spec_id;
                    $value_id = $spec_value_db->insert($v1);
                    $spec_value[$id] = $value_id;
                }
            }
            
            foreach ($relation['sku'] as $v) {
                unset($v['id']);
                unset($v['cdate']);
                $key = explode(',', $v['key']);
                $v['key'] = array();
                foreach ($key as $v1) {
                    if (isset($spec_value[$v1])) {
                        $v['key'][] = $spec_value[$v1];
                    }
                }
                $v['key'] = implode(',', $v['key']);
                $sku_db->insert($v);
            }
        }
    }

    # 删除关联关系
    public function relationDel($place_id, $data)
    {
        $where['channel_place_id'] = $place_id;
        $where['channel_type'] = $data['type'];
        $where['channel_type_id'] = $data['type_id'];
        $where['status'] = 1;
        $info = Dever::db('resource_relation', 'place')->find($where);
        if ($info) {
            Dever::db('resource_relation', 'place')->update($info['id'], array('status' => 2));
            Dever::db('info', $this->app, false)->update($info['type_id'], array('source' => 1));
        }
    }

    # 创建分类
    private function createCate($v)
    {
        $db = Dever::db('cate', $this->app, false);
        $info = $db->find(array('key' => $v['key']));
        if (!$info) {
            $id = $db->insert($v);
        } else {
            $id = $info['id'];
        }
        return $id;
    }

    private function handleInfo($info)
    {
        $info = Dever::load('info', $this->app)->getInfo($info);
        $info['pic'] = explode(',', $info['pic']);
        $info['cdate_str'] = date('Y-m-d H:i:s', $info['cdate']);
        if (Place::$uid && Place::$user['client_id'] > 0) {
            $info['client'] = Dever::load('client', 'place')->get($info, $this->app, $this->type, '进货');
        }
        $info['price'] = Dever::load('price', 'place')->get($info, $this->app, $this->type, '下载');

        if (isset($info['content'])) {
            if (isset($info['password'])) {
                unset($info['password']);
            }
            # 查看详情信息
            if (Place::$uid) {
                $act = new Act('collect');
                $info['collect'] = 2;
                $collect = $act->getInfo();
                if ($collect) {
                    $info['collect'] = 1;
                }
            }
            $info['content'] = htmlspecialchars_decode($info['content']);
        } else {
            $info['pic'] = $info['pic'][0] ?? '';
        }
        return $info;
    }
}