<?php

namespace Agent\Lib;

use Dever;

# 批量设置商品
class Member_set
{
    public function __construct()
    {
        //Dever::load('manage/auth.init');
    }

    # 获取商品列表
    public function goods_api()
    {
        $id = Dever::input('mid');
        return Dever::load('agent/lib/set')->getData($id);
    }

    # 设置商品
    public function home_api()
    {
        $data = array();
        $data['mid'] = Dever::input('mid');
        $data['host'] = Dever::url('lib/member_set.home?mid=' . $data['mid'], 'agent');
        $data['url'] = Dever::url('lib/member_set.goods?mid=' . $data['mid'], 'agent');
        $data['submit'] = Dever::url('lib/member_set.action_cmmmit?json=1', 'agent');

        return Dever::render('setMemberGoods', $data);
    }

    # 设置生产能力
    public function action_cmmmit_api()
    {
        $goods = Dever::input('goods');
        if (!$goods) {
            Dever::alert('请传入商品');
        }
        $goods = Dever::json_decode($goods);
        $mid = Dever::input('mid');
        $desc = Dever::input('desc');

        $where['option_mid'] = $mid;
        $where['set_state'] = 2;
        Dever::db('agent/member_goods')->updates($where);

        foreach ($goods as $k => $v) {
            $type = 1;
            $type_id = $k;
            $sku_id = -1;
            if (strstr($k, '_')) {
                $temp = explode('_', $k);
                $type_id = $temp[0];
                $type = $temp[1];
            } elseif (strstr($k, '-')) {
                $temp = explode('-', $k);
                $type_id = $temp[0];
                $sku_id = $temp[1];
            }
            
            if ($v['total'] <= 0) {
                continue;
            }
            $w = array();
            $w['type'] = $type;
            $w['type_id'] = $type_id;
            $w['sku_id'] = $sku_id;
            $w['mid'] = $mid;
            
            $info = Dever::db('agent/member_goods')->one($w);
            if ($v['del'] == 2) {
                if ($info) {
                    Dever::db('agent/member_goods')->update(array('where_id' => $info['id'], 'state' => 2));
                }
            } else {
                $w['total_num'] = $v['total'];
                
                if (!$info) {
                    $state = Dever::db('agent/member_goods')->insert($w);
                    if ($state) {
                    	Dever::load('agent/lib/member_set')->log($w['mid'], '', $w['type'], $w['type_id'], $w['sku_id'], $v['total'], 0, $w['total_num'], $desc);
                    }
                } else {
                    $w['where_id'] = $info['id'];
                    $w['state'] = 1;
                    $state = Dever::db('agent/member_goods')->update($w);
                    $num = $w['total_num']-$info['total_num'];
                    if ($state && $num > 0) {
                    	Dever::load('agent/lib/member_set')->log($w['mid'], '', $w['type'], $w['type_id'], $w['sku_id'], $num, $info['total_num'], $w['total_num'], $desc);
                    }
                }
            }

            # 记录日志
        }

        return Dever::url('project/database/list&table=member_goods&project=agent&search_option_mid='.$mid, 'manage');
    }

    # 记录日志
    public function log($mid, $parent_order_num, $type, $type_id, $sku_id, $num, $pnum, $fnum, $desc = '')
    {
    	$data['mid'] = $mid;
    	$data['parent_order_num'] = $parent_order_num;
    	$data['type'] = $type;
    	$data['type_id'] = $type_id;
    	$data['sku_id'] = $sku_id;
    	$data['num'] = $num;
    	$data['pnum'] = $pnum;
    	$data['fnum'] = $fnum;
    	$data['desc'] = $desc;
    	$data['order_num'] = $this->getOrderId();

    	$id = Dever::db('agent/member_goods_log')->insert($data);

    	return $id;
    }

    /**
     * 生成日志订单号
     *
     * @return mixed
     */
    public function getOrderId()
    {
        $where['order_num'] = Dever::order('QY');
        $state = Dever::db('agent/member_goods_log')->one($where);
        if (!$state) {
            return $where['order_num'];
        } else {
            return $this->getOrderId();
        }
    }
}