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

namespace App\Model;
use Think\Model;
class ShopsAccountModel extends Model{
    // 获取收银员列表
    public function cashier_list($paramdata){
        $map=array();
        $map['status']=1;
        $map['is_delete']=0;
        $map['login_name']=array('neq','');
        $map['pid|id']=$paramdata['sid'];

        $pagesize=empty($pagesize)?C('WEB_ARTICLE_PAGE'):$pagesize;
        $list=page_list('ShopsAccount',$map,'id asc','id,shop_id,name,mobile,pid',$pagesize,'',1);
        if($list['list']) {
            $map_info=array();
            $map_info['addtime']=NOW_TIME;
            if($list['list']){
                foreach($list['list'] as $k=>$v){
                    $map_info['shop_id']=$v['shop_id'];
                    $income=M('OrderInfo')->where($map_info)->sum('order_amount');
                    if($income){
                        $list['list'][$k]['income']= format_price($income,2);
                    }else{
                        $list['list'][$k]['income']= 0;
                    }

                }
            }
        }
        return $list['list'];

    }
    // 添加收银员
    public function add_cashier($postdata){
        $return = $sa_data = array();
        $return['status'] = 1;
        if(empty($postdata)){
            $return['status'] = 2;
            $return['msg'] = '参数不能为空';
            return $return;
        }
        $model = M("ShopsAccount");
		
		$sa_data['login_name'] = $postdata['login_name'];
        $sa_data['mobile'] = $postdata['mobile'];

        if(empty($postdata['password'])){$return['status'] = 2;$return['msg']='您没有输入密码';return $return;}
        if ($postdata['password'] != $postdata['repassword']){$return['status'] = 3;$return['msg']='两次输入的密码不相同';return $return;}
        $sa_data['salt'] = $postdata['salt'] = randStr();
        $sa_data['password'] = $postdata['password'] = creatpwd($postdata['password'], 'HANCHAO'.$postdata['salt']);

        $check_post = $this->check_post($postdata, 0);
        if($check_post['status'] == 0){
            $return['status'] = 3;
            $return['msg'] = $check_post['msg'];
            return $return;
        }
        $postdata = D('Shops')->format_post($postdata);
        $sa_data['shop_id'] = intval($postdata['shop_id']);
        $sa_data['name'] = $postdata['name'];
        $sa_data['qq'] = $postdata['qq'];
        $sa_data['weixin'] = $postdata['weixin'];
        $sa_data['is_main'] = 0;
        $sa_data['pid'] = $postdata['sid'];;
        $sa_data['updatetime'] = NOW_TIME;
		if(isset($postdata['registration_id'])){
			$sa_data['registration_id'] = $postdata['registration_id'];
		}
		$sa_data['registration_type'] = $postdata['registration_type'];
        $result= M('ShopsAccount')->add($sa_data);

        $return['data'] = $result;
        if ($result) {
            $return['status'] = 1;
            $return['msg'] = '添加收银员成功';
        } else {
            $return['status'] = 3;
            $return['msg'] = '添加收银员失败';
        }
        return $return;
    }
    /**
     * 检查提交的数据是否合法
     * @param array $postdata
     * @param $id Shopsaccount表的id
     */
    private function check_post($postdata = array(), $id = 0){
        $return = array();
        $return['status'] = 0;
        $return['msg'] = '';
        if (empty($postdata))$return['msg'] = "非法数据";
        if ($postdata['name'] == '')$return['msg'] = "名称不得为空";
        if ($postdata['mobile'] == '')$return['msg'] = "手机号不得为空";
        $res = D('Shops')->check_phone($postdata['mobile'], $id);
        if($res['status'] == 3){
            $return['status'] = 0;
            $return['msg'] = '该手机已经存在';
            return $return;
        }
		$login_name = intval($postdata['login_name']);
		if(!empty($login_name) && !preg_match('/^\d{6}$/', $login_name)){
			$return['status'] = 0;
            $return['msg'] = '该工号格式不正确';
            return $return;
		}
		$res_login_name = D('Shops')->check_login_name($login_name, $id);
        if($res_login_name['status'] == 3){
            $return['status'] = 0;
            $return['msg'] = '该工号已经存在';
            return $return;
        }
        if(empty($return['msg'])){
            $return['status'] = 1;
        }
        return $return;
    }
    public function edit_cashier($postdata){
        $return = $sa_data = array();
        $return['status'] = 1;
        if(empty($postdata)){
            $return['status'] = 2;
            $return['msg'] = '参数不能为空';
            return $return;
        }
        $model = M("ShopsAccount");
		
		$sa_data['login_name'] = $postdata['login_name'];
        $sa_data['mobile'] = $postdata['mobile'];

        if (!empty($postdata['password']) && $postdata['password'] != $postdata['repassword']){
			$return['status'] = 3;$return['msg']='两次输入的密码不相同';return $return;
		}
        if(!empty($postdata['password'])){
            $sa_data['salt'] = $postdata['salt'] = randStr();
            $sa_data['password'] = $postdata['password'] = creatpwd($postdata['password'], 'HANCHAO'.$postdata['salt']);
        }

        $check_post = $this->check_post($postdata, $postdata['id']);
        if($check_post['status'] == 0){
            $return['status'] = 3;
            $return['msg'] = $check_post['msg'];
            return $return;
        }
        $postdata = D('Shops')->format_post($postdata);
        $sa_data['shop_id'] = intval($postdata['shop_id']);
        $sa_data['name'] = $postdata['name'];
        $sa_data['qq'] = $postdata['qq'];
        $sa_data['weixin'] = $postdata['weixin'];
        $sa_data['is_main'] = 0;
        $sa_data['pid'] = $postdata['sid'];;
        $sa_data['updatetime'] = NOW_TIME;
        $map=array();
        $map['id']=$postdata['id'];
        $result= M('ShopsAccount')->where($map)->save($sa_data);

        if ($result || $result==0) {
            $return['status'] = 1;
            $return['msg'] = '编辑收银员成功';
        } else {
            $return['status'] = 3;
            $return['msg'] = '编辑收银员失败';
        }
        return $return;
    }
    public function cashier_del($id){
        $return=array('status'=>3,'msg'=>'');
        if(empty($id)){
            $return['msg'] = '数据';
            return $return;
        }
        $data=$map=array();
        $map['id']=intval($id);
        $data['is_delete']=1;
        $ret=M('ShopsAccount')->where($map)->save($data);
        if($ret){
            $return['status']=1;
        }else{
            $return['msg']='删除失败';
        }
        return $return;
    }
	public function edit_shop_login($postdata){
		$return = $sa_data = $shop_data = array();
        $return['status'] = 1;
        if(empty($postdata)){
            $return['status'] = 2;
            $return['msg'] = '参数不能为空';
            return $return;
        }
        $model = M("ShopsAccount");
		
        if (!empty($postdata['password']) && $postdata['password'] != $postdata['repassword']){
			$return['status'] = 3;$return['msg']='两次输入的密码不相同';return $return;
		}
        if(!empty($postdata['password'])){
			$return['is_pwd'] = 1;
            $sa_data['salt'] = $shop_data['salt'] = randStr();
            $sa_data['password'] = $shop_data['password'] = $return['token'] = creatpwd($postdata['password'], 'HANCHAO'.$shop_data['salt']);
        }

        $check_post = $this->check_post($postdata, $postdata['id']);
        if($check_post['status'] == 0){
            $return['status'] = 3;
            $return['msg'] = $check_post['msg'];
            return $return;
        }
        $postdata = D('Shops')->format_post($postdata);
        $shop_data['name'] = $sa_data['name'] = $postdata['name'];
		$shop_data['login_name'] = $sa_data['login_name'] = $postdata['login_name'];
        $shop_data['contacts_phone'] = $shop_data['phone'] = $sa_data['mobile'] = $postdata['mobile'];
        $shop_data['qq'] = $sa_data['qq'] = $postdata['qq'];
        $shop_data['weixin'] = $sa_data['weixin'] = $postdata['weixin'];
        $shop_data['updatetime'] = $sa_data['updatetime'] = NOW_TIME;
        $map = array();
        $map['id'] = $postdata['sid'];
        $map['shop_id'] = $postdata['shop_id'];
        $result= $model->where($map)->save($sa_data);
		
		$map = array();
        $map['id'] = $postdata['shop_id'];
        $ret= M('Shops')->where($map)->save($shop_data);

        if ($result || $result==0 || $ret || $ret==0) {
            $return['status'] = 1;
            $return['msg'] = '编辑管理员成功';
        } else {
            $return['status'] = 3;
            $return['msg'] = '编辑管理员失败';
        }
        return $return;
	}
}
?>