<?php
namespace Smi\Model;

use Think\Model;

header("Content-type:text/html;charset=utf-8");
class RegionVillageModel extends Model
{
    /**
     * 保存表单
     */
    public function saveForm()
    {
        $county_id = $_REQUEST['county_id'];
        $name = I("name");
        $type = $_POST['type'];
        $data['county_id'] = $county_id;
        $data['name'] = $name;
        $data['type'] = $type;
        if ($_POST['id']) {
            $result = $this->where("`id`={$_POST['id']}")->save($data);
        } else {
            $result = $this->add($data);
        }
        return $result;
    }

    /**
     * 设置收购商品
     */
    public function purchaseGoods()
    {
        $purchase_goods = implode(",", $_POST['purchase_goods']);
        $data['purchase_goods'] = $purchase_goods;
        if ($_POST['id']) {
            $result = $this->where("`id`={$_POST['id']}")->save($data);
        } else {
            $result = $this->add($data);
        }
        return $result;
    }

    /**
     * 设置轮播图
     */
    public function setCarousel()
    {
        $upload = new \Think\Upload();// 实例化上传类
        // $upload->maxSize = 3145728 ;// 设置附件上传大小
        // 设置附件上传类型
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录 
        $upload->savePath = 'goods_img/'; // 设置附件上传目录
        $upload->autoSub = false; //不要子文件夹
        // 上传文件
        $info = $upload->upload();
        $carouselArray = array();
        foreach ($info as $k => $v) {
            if (substr($k, 0, 8) == "carousel") {
                if ($_FILES[$k]['error'] != 4) {
                    $carouselArray[] = $v["savepath"] . $v["savename"];
                }
            }
        }
        if ($carouselArray[0]) {
            $data['carousel'] = serialize($carouselArray);
        }
        if ($_POST['id']) {
            $result = $this->where("`id`={$_POST['id']}")->save($data);
        } else {
            $result = $this->add($data);
        }
        return $result;
    }
}