<?php

namespace app\admin\controller;

use app\BaseController;
use think\App;
use think\facade\Db;

//规格值
class Specvalue extends BaseController
{
    public $post="";
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->post=$this->request->post();
    }
    public function list(){
        $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']) :"1";
        $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']) :"10";
        $where = [['is_del','=',0]];
        $count = Db::name('spec_value')->where($where)->count();
        $total = ceil($count / $size);
        $page = $page >= $total ? $total : $page;
        $list = Db::name('spec_value')->where($where)->page($page,$size)->order("addtime desc")->select();
        return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
    }

    public function create()
    {
        $spec_id = isset($this->post['spec_id']) && $this->post['spec_id'] !== "" ? intval($this->post['spec_id']) : "";
        if ($spec_id == "") {
            return error_show(1002, "参数spec_id不能为空");
        }
        $spec_value = isset($this->post['spec_value']) && $this->post['spec_value'] !== "" ? trim($this->post['spec_value']) : "";
        if ($spec_value == "") {
            return error_show(1002, "参数spec_value不能为空");
        }

        $id_is_empty = Db::name('spec_value')
            ->field('id')
            ->where([
                'spec_id' => $spec_id,
                'spec_value' => $spec_value,
                'is_del' => 0
            ])->find();

        if ($id_is_empty) return error_show(1005, '数据库已存在该规格值');
        else {
            $data = [
                "spec_id" => $spec_id,
                "spec_value" => $spec_value,
                "is_del" => 0,
                "addtime" => date("Y-m-d H:i:s"),
                "updatetime" => date("Y-m-d H:i:s")
            ];
            $info = Db::name("spec_value")->insert($data);
            if ($info) return error_show(0, "新建成功");
            else   return error_show(1002, "新建失败");
        }
    }
    public function all(){
        $spec_id = isset($this->post['spec_id']) && $this->post['spec_id'] !=="" ? intval($this->post['spec_id']):"";
        if($spec_id == ""){
            return error_show(1002,"参数spec_id不能为空");
        }
        $info = Db::name('spec_value')->where(['spec_id'=>$spec_id,'is_del'=>0])->select();
        if($info==""){
            return error_show(1002,"未找到数据");
        }
        return app_show(0,"获取成功",$info);
    }
}