<?php
namespace app\admin\controller\ump;
use app\admin\controller\AuthController;
use app\admin\model\system\SystemGroupData;
use app\models\store\StoreWholesale as Wholesale;
use crmeb\services\{UtilService,JsonService as Json,UtilService as Util};


class StoreWholesale extends  AuthController
{
    public function index()
    {
        $this->assign('countSeckill', Wholesale::getWholeCount());
        $this->assign('seckillId', Wholesale::getWholeIdAll());
        return $this->fetch();
    }
    public function save_excel()
    {
        $where = Util::getMore([
            ['status', ''],
            ['store_name', '']
        ]);
        Wholesale::SaveExcel($where);
    }

    /**
     * 异步获取砍价数据
     */
    public function getlist()
    {
        $where = UtilService::getMore([
            ['page', 1],
            ['limit', 20],
            ['status', ''],
            ['store_name', '']
        ]);
        $seckillList = Wholesale::systemPage($where);
        if (is_object($seckillList['list'])) $seckillList['list'] = $seckillList['list']->toArray();
        $data = $seckillList['list']['data'];

        foreach ($data as $k => $v) {
            $end_time = $v['stop_time'];
            if ($end_time) {
                $config = SystemGroupData::get($v['time_id']);
                if ($config) {
                    $arr = json_decode($config->value, true);
                    $start_hour = intval($arr['time']['value']);
                    $continued = intval($arr['continued']['value']);
                    $end_hour = $start_hour + $continued;
                    $end_time = $end_time . ' ' . $end_hour . ':00:00';
                }
            }
            $data[$k]['_stop_time'] = $end_time;
        }
        return Json::successlayui(['count' => $seckillList['list']['total'], 'data' => $data]);
    }
    public function create($id=0)
    {
        $this->assign('id', (int)$id);
        return $this->fetch();
    }
    /**
     * 获取产品详细信息
     * @param int $id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get_product_info($id = 0)
    {
        $data['productInfo'] = [];
        if ($id) {
            $productInfo = Wholesale::get($id);
            $data['productInfo'] = $productInfo;
        }
        $data['time_id'] = sys_data('whole_time');
        return Json::successful($data);
    }

    /**
     * 保存新建的资源
     *
     *
     */
    public function save($id)
    {
        $data = Util::postMore([
            'title',
            'info',
            'keyword',
            ['unit_name', '件'],
            ['image', []],
            ['images', []],
            ['price',0],
            ['cost',0],
            ['postage', 0],
            ['ot_price', 0],
            ['give_integral', 0],
            ['sort',0],
            ['stock', 0],
            ['description', ''],
            ['start_time', ''],
            ['stop_time', ''],
            ['time_id', 0],
            ['is_hot', 0],
            ['is_news', 0],
            ['num', 1],
            ['is_show',1]
        ]);
        if (count($data['image']) < 1) return Json::fail('请上传产品图片');
        if (count($data['images']) < 1) return Json::fail('请上传产品轮播图');
        $data['image'] = $data['image'][0];
        $data['images'] = json_encode($data['images']);
        Wholesale::beginTrans();
        if ($id) {
            unset($data['sales']);
            $attr_res =  Wholesale::edit($data, $id);

            if ($attr_res) {
                Wholesale::commitTrans();
                return Json::success('修改成功!');
            } else {
                Wholesale::rollbackTrans();
                return Json::fail(Wholesale::getErrorInfo());
            }
        } else {
            $data['add_time'] = time();

            $res = Wholesale::create($data);
            if ($res) {
                Wholesale::commitTrans();
                return Json::success('添加产品成功!');
            } else {
                Wholesale::rollbackTrans();
                return Json::fail(StoreProductAttr::getErrorInfo());
            }
        }
    }
    public function delete($id)
    {
        if (!$id) return $this->failed('数据不存在');
        $product = Wholesale::get($id);
        if (!$product) return Json::fail('数据不存在!');
        $data['is_del'] = 1;
        if (!Wholesale::edit($data, $id))
            return Json::fail(Wholesale::getErrorInfo('删除失败,请稍候再试!'));
        else
            return Json::successful('删除成功!');
    }

}