<?php
/** 店铺营销
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author lss <lss@qq.com>
 */

namespace Shop\Model;
use Think\Model;
class ShopsAccountModel extends Model{
	/**
     * 更新配送信息
     * @param array $postdata 表单提交数据
     * @param int $type 提交的类型:0修改,1添加
     * @Author xiaol <xiaol@qq.com>
     */
    public function update_shipping_info($postdata,$type=0){
        $where=$data=array();
		$modelname='ShopsDelivery';
        $moreshipstr = md5(implode('', $postdata['start_price']) . implode('', $postdata['scale']) . implode('', $postdata['delivery_price']));
        $where['shop_id']=$postdata['shop_id'];
        if (($postdata['moreshipstr'] != $moreshipstr) || $type) {
            M($modelname)->where($where)->delete();
            // foreach ($postdata['start_price'] as $k => $v) {
            foreach ($postdata['scale'] as $k => $v) {
                if (!$v) continue;
                $data[] = array('shop_id' => $postdata['shop_id'], 'start_price' => $postdata['start_price'][$k], 'scale' => $v, 'delivery_price' => $postdata['delivery_price'][$k]);
            }
            if ($data) $add = M($modelname)->addAll($data);
        }
        unset($data, $postdata);
    }

    /**
     * 更新包装费信息
     * @param array $postdata 表单提交数据
     * @param int $type 提交的类型:0修改,1添加
     * @Author xiaol <xiaol@qq.com>
     */
    public function update_bagging_info($postdata,$type=0){
        $where = $data = array();
		$modelname = 'ShopsPackage';
        $data['shop_id'] = $postdata['shop_id'];
        $data['package_price'] = $postdata['package_price'];
        $data['package_start_price'] = $postdata['package_start_price'];
        $data['to_time'] = $postdata['to_time'];
        $where['shop_id'] = $postdata['shop_id'];
        $count = M($modelname)->where($where)->count();
        if($count>0) M($modelname)->where($where)->save($data);
        else M($modelname)->add($data);
    }
	
	/**
     * 更新包装费信息(修改)
     * @param array $postdata 表单提交数据
     * @param int $type 提交的类型:0修改,1添加
     * @Author xiaol <xiaol@qq.com>
     */
    public function change_update_bagging_info($postdata,$type=0){
        $where = $data = array();
		$modelname = 'ShopsPackage';
        $data['shop_id'] = $postdata['shop_id'];
        $data['package_price'] = $postdata['package_price'];
        $data['package_start_price'] = $postdata['package_start_price'];
		if($postdata['time_type'] == 2){
			$postdata['to_time'] = $postdata['to_time']*60;
		}
        $data['to_time'] = $postdata['to_time'];
        $data['time_int'] = $postdata['time_int'];
        $data['long_time'] = $postdata['long_time'];
        $where['shop_id'] = $postdata['shop_id'];
        $count = M($modelname)->where($where)->count();
        if($count>0) M($modelname)->where($where)->save($data);
        else M($modelname)->add($data);
    }
}
	
?>