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

namespace App\Model;
use Think\Model;
class ShopLoginModel extends Model{
    protected $tableName = 'shops';
    /**
     * 修改主站账号
     */
    public function resetacc($postdata){
        $return=array('status'=>3);
        $uid=$postdata['root_id'];
        $admin_mod = D('Admin');
        if(empty($postdata['username'])){$return['msg']='请输入账户名称';return $return;}
        $adminsalt = $admin_mod->where("id='" .$uid . "'")->field('salt,username')->find();
        if($postdata['username']==$adminsalt['username']){$return['msg']='您没有做任何修改!';return $return;}
        $savedata=array();
        $savedata['id']=$uid;
        $savedata['username'] = $postdata['username'];
        $result = $admin_mod->save($savedata);
        if (false !== $result) {
            $this->updatepwd($postdata['username'],1);
            $return['status']=1;
            $return['msg']=L('DO_OK');
        } else {
            $return['msg']=L('DO_ERROR');
        }
        return $return;
    }

    /**
     * 修改主站密码
     */
    public function resetpwd($postdata){
        $return=array('status'=>3);
        $uid=$postdata['root_id'];
        $admin_mod = D('Admin');
        if(empty($postdata['password']) || empty($postdata['oldpassword'])){$return['msg']='您没有输入密码';return $return;}
        $regular= new \Common\Vendor\Regular();
        if(!$regular->passWord(6,18,$postdata['password'])){$return['msg']="密码格式为6-18位字符";return $return;}
        if ($postdata['password'] != $postdata['repassword']){$return['msg']='两次输入的密码不相同';return $return;}
        $adminsalt = $admin_mod->where("id='" .$uid . "'")->field('salt,password')->find();
        $oldpassword=creatpwd($postdata['oldpassword'], $adminsalt['salt']);
        if($oldpassword!=$adminsalt['password']){$return['msg']='旧密码错误!';return $return;}
        $savedata['id']=$uid;
        $savedata['password'] = creatpwd($postdata['password'], $adminsalt['salt']);
        $result = $admin_mod->save($savedata);
        if (false !== $result) {
            $this->updatepwd($postdata['password']);
            $return['status']=1;
            $return['new_password']=$savedata['password'];
            $return['msg']=L('DO_OK');
        } else {
            $return['msg']=L('DO_ERROR');
        }
        return $return;
    }


    /**
     * 修改子站账号
     * @param $postdata
     * @param string $mod('Shops':门店,'ShopsAccount':收银员)
     */
    public function set_acc($postdata){
        $return=array('status'=>3);
        $uid=$postdata['sid'];
        if(empty($postdata['username'])){$return['msg']='请输入账户名称';return $return;}
        $salt = M('ShopsAccount')->where("id='" .$uid . "'")->field('pid,shop_id,salt,login_name')->find();
        if($postdata['username']==$salt['login_name']){$return['msg']='您没有做任何修改!';return $return;}
        $map=$savedata=array();$map['id']=$uid;
        $savedata['login_name'] = $postdata['username'];
        $result = M('ShopsAccount')->where($map)->save($savedata);
        if (false !== $result) {
            if($salt['pid']==0){
                $map=array();$map['id']=$salt['shop_id'];
                M('Shops')->where($map)->save($savedata);
            }
            $return['status']=1;
            $return['msg']=L('DO_OK');
        } else {
            $return['msg']=L('DO_ERROR');
        }
        return $return;
    }

    /**
     * 修改子站密码
     * @param $postdata
     * @param string $mod('Shops':门店,'ShopsAccount':收银员)
     */
    public function set_pwd($postdata){
        $return=array('status'=>3);
        $uid=$postdata['sid'];
        if(empty($postdata['password']) || empty($postdata['oldpassword'])){$return['msg']='您没有输入密码';return $return;}
        $regular= new \Common\Vendor\Regular();
        if(!$regular->passWord(6,18,$postdata['password'])){$return['msg']="密码格式为6-18位字符";return $return;}
        if ($postdata['password'] != $postdata['repassword']){$return['msg']='两次输入的密码不相同';return $return;}
        $salt = M('ShopsAccount')->where("id='" .$uid . "'")->field('pid,shop_id,salt,password')->find();
        $oldpassword=creatpwd($postdata['oldpassword'], 'HANCHAO'.$salt['salt']);
        if($oldpassword!=$salt['password']){$return['msg']='旧密码错误!';return $return;}
        $map=$savedata=array();$map['id']=$uid;
        $savedata['password'] = creatpwd($postdata['password'], 'HANCHAO'.$salt['salt']);
        $result = M('ShopsAccount')->where($map)->save($savedata);
        if (false !== $result) {
            if($salt['pid']==0){
                $map=array();$map['id']=$salt['shop_id'];
                M('Shops')->where($map)->save($savedata);
            }
            $return['status']=1;
            $return['new_password']=$savedata['password'];
            $return['msg']=L('DO_OK');
        } else {
            $return['msg']=L('DO_ERROR');
        }
        return $return;
    }

    /**
     * 获取门店某些字段
     * @param $data
     */
    public function get_shops_info($data){
        $return=array('status'=>3);
        $map=array();$map['id']=$data['current_shop_id'];
        $field=$data['field'];//note开启关闭、tone提示音
        $result = M('Shops')->where($map)->field($field)->find();
        if (!empty($result)) {
            $return['status']=1;
            $return['data']=$result;
        } else {
            $return['msg']=L('DO_ERROR');
        }
        return $return;
    }

    /**
     * 修改门店某个字段
     */
    public function set_shops_item($data){
        $return=array('status'=>3);
        $map=array();$map['id']=$data['current_shop_id'];
        $field=$data['field'];//note开启关闭、tone提示音
        $value=$data['value'];
        $savedata=array($field=>$value);
        if($field=='is_open_tone' && $value==1){$savedata['note_tone']=1;}
        $result = M('Shops')->where($map)->save($savedata);
        if (false !== $result) {
            $return['status']=1;
            $return['msg']=L('DO_OK');
        } else {
            $return['msg']=L('DO_ERROR');
        }
        return $return;
    }
    /**
     * 退款
     */
    public function order_tui(){
        $return['status'] = 1;
        $return['msg'] = '退款成功';
        return $return;
    }
}
?>