<?php

namespace Factory\Lib;

use Dever;

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

    # 获取工厂
    public function search_api()
    {
        return Dever::search('factory/info');
    }

    # 获取商品列表
    public function goods_api()
    {
        $factory_id = Dever::input('factory_id', 1);
        return Dever::outDiy(Dever::load('goods/lib/set')->getGoodsList($factory_id, 'factory_id', 'factory/goods_sku'));
    }

    # 设置生产能力
    public function home_api()
    {
        $data = array();
        $data['factory_id'] = Dever::input('id', 1);
        $data['host'] = Dever::url('lib/set.home', 'factory');
        $data['url'] = Dever::url('lib/set.goods?factory_id=' . $data['factory_id'], 'factory');
        $data['search'] = Dever::url('lib/set.search?yes='.$data['factory_id'].'&json=1', 'factory');
        $data['submit'] = Dever::url('lib/set.action_commit?json=1', 'factory');

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

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

        $where['option_factory_id'] = $factory_id;
        $where['set_state'] = 2;
        Dever::db('factory/goods')->updates($where);

        Dever::db('factory/goods_sku')->updates($where);

        foreach ($goods as $k => $v) {
            $temp = explode('-', $k);
            $goods_id = $temp[0];
            if (isset($temp[1])) {
                $sku_id = $temp[1];
            } else {
                $sku_id = -1;
            }
            $goods_info = Dever::db('goods/info')->one($goods_id);
            $w = array();
            $w['goods_id'] = $goods_id;
            $w['factory_id'] = $factory_id;
            $info = Dever::db('factory/goods')->one($w);

            $w['status'] = $goods_info['status'];
            $w['p_price'] = $v['p_price'];
            if (!$info) {
                Dever::db('factory/goods')->insert($w);
            } else {
                $w['where_id'] = $info['id'];
                $w['state'] = 1;
                Dever::db('factory/goods')->update($w);
            }

            $w = array();
            $w['goods_id'] = $goods_id;
            $w['factory_id'] = $factory_id;
            $w['sku_id'] = $sku_id;
            $info = Dever::db('factory/goods_sku')->one($w);

            $w['status'] = $goods_info['status'];
            $w['p_price'] = $v['p_price'];
            if (!$info) {
                Dever::db('factory/goods_sku')->insert($w);
            } else {
                $w['where_id'] = $info['id'];
                $w['state'] = 1;
                Dever::db('factory/goods_sku')->update($w);
            }
        }

        return Dever::url('project/database/list&project=factory&&table=info&menu=factory&search_option_state=1', 'manage');
    }
}