<?php

/**
 * 开发者相关
 *
 * @author Yang
 */
class CaijiAction extends PublicAction {

    //初始化检测
    public function _initialize() {
        parent::_initialize();


    }






	//增加软件数据库保存
    public function index() {



		//ICON图标处理
		$icon = $_POST['bpi_icon'];
		$icon = str_replace('soft_icon/','',$icon);
		//$icon = str_replace('soft_img/','',$icon);
		$icon = str_replace('<img src=\"','',$icon);
		$icon = str_replace('\" />','',$icon);

		//文件下载地址处理：
		$downurl = $_POST['bpi_url'];
		$downurl = str_replace('soft/','',$downurl);
		$downurl = str_replace('<a class=\"d1\" href=\"','',$downurl);
		$downurl = str_replace('></a>','',$downurl);
		$downurl = trim($downurl);




		//软件截图处理
		$soft_picture = $_POST['bpi_picture'];
		$soft_picturedb = str_replace('<img src=\"soft_img/','',$soft_picture);
		$soft_picturedb = str_replace('\" />',',',$soft_picturedb);
		$soft_picturedb = substr($soft_picturedb,0,strlen($soft_picturedb)-1);
		$md5_file = MD5($downurl);
		$soft = M('software');



/*bpi_name=[标签:标题]&bpi_type=[标签:类别]&bpi_language=[标签:软件语言]&bpi_version=[标签:版本号]&bpi_mobile_name=[标签:作者]&bpi_size=[标签:文件大小]&bpi_system=[标签:适用固件]&bpi_time=[标签:上架时间]&bpi_explain=[标签:软件介绍]&bpi_update_explain=[标签:更新说明]&bpi_picture=[标签:软件截图]&bpi_url=[标签:下载地址]&bpi_icon=[标签:ICON]*/


		Vendor('qr.phpqrcode'); //引入二维码类库
        $map['name'] = $_POST['bpi_name'];
        $map['mobile_name'] = $_POST['bpi_mobile_name'];
        $map['charge'] = '0';
        $map['version'] = $_POST['bpi_version'];
        $map['language'] = $_POST['bpi_language'];
        $map['explain'] = $_POST['bpi_explain'];
		$map['update_explain'] = $_POST['bpi_update_explain'];
        $map['icon'] = $icon;
        $map['url'] = $downurl;
        $map['md5_file'] = $md5_file;
        $map['time'] = strtotime($_POST['bpi_time']);
		$map['size'] = $_POST['bpi_size'];
        $map['status'] = 1;
        $map['recommend'] = 0;
		$map['upload_uid'] = 0;
		$map['picture'] = $soft_picturedb;
		$map['suitsdk'] = $_POST['bpi_system'];
		//$map['keyword'] = $_POST['bpi_keyword'];

		$menu = M('menu');
		$menu_db = $menu->field('`id`,`pid`')->where("name='" . $_POST['bpi_type'] . "'")->find();

        $arg['menu_id'] = $menu_db['pid'];
        $arg['menu_child_id'] = $menu_db['id'];




        $soft_belong = M('soft_belong');
        $soft_version = M('soft_version');

        $time = date('Ymd');
        $map['qr_url'] = '/Public/Upload/qr/' . $time . '/';
        $id = $soft->add($map);

        if (!$id) {
            $this->ajaxReturn('', '', 0);
        } else {
            //增加软件版本列表
            $cond['soft_id'] = $id;
            $cond['soft_version'] = $map['version'];
            $cond['update_time'] = time();
            $soft_version->add($cond);
            F('List/menu_list', NULL); //清空列表缓存
            //绘制二维码保存
            if (!is_dir('./Public/Upload/qr/')) {
                mkdir('./Public/Upload/qr/');
            }
            if (!is_dir('./Public/Upload/qr/' . $time)) {
                mkdir('./Public/Upload/qr/' . $time);
            }
            QRcode::png($id, '.' . $map['qr_url'] . $id . '.png', $level = QR_ECLEVEL_L, $size = 3, $margin = 2);
            if (isset($_POST['assign_check']) && $_POST['assign_check'] == 'on') { //没有分配列表
                $this->ajaxReturn('', '', 1);
            } else {
                $arg['soft_id'] = $id;
                if (!$soft_belong->add($arg)) {
                    $this->ajaxReturn('', '上传软件成功，但分配失败。稍后请手动分配', 0);
                } else {
                    echo "1";
                }
            }
        }
    }


	//软件更新临时保存
    public function test() {

	 for($i = 0; $i < 3000; $i++) {
		$idd='123456789';
		$menu_child_id='456789';//游戏类别处理
		//$menu_child_id='0123';//

		$iddb = $this->rands('1',$strtolower=1,$idd,'1');
		$mcid = $this->rands('1',$strtolower=1,$menu_child_id,'8');



		$rsdb['id'] = $iddb;
		$soft = M('software');
        $soft_msg = $soft->field()->where($rsdb)->find();

        //$map['id'] = $soft_msg['id'];
        $map['name'] = $soft_msg['name'];
        $map['mobile_name'] = $soft_msg['mobile_name'];
        $map['charge'] = $soft_msg['charge'];
        $map['version'] = $soft_msg['version'];
        $map['size'] = $soft_msg['size'];
        $map['language'] = $soft_msg['language'];
        $map['time'] = $soft_msg['time'];
        $map['explain'] = $soft_msg['explain'];
        $map['update_explain'] = $soft_msg['update_explain'];
        $map['keyword'] = $soft_msg['keyword'];
        $map['icon'] = $soft_msg['icon'];
        $map['picture'] = $soft_msg['picture'];
        $map['url'] = $soft_msg['url'];
        $map['passageway'] = $soft_msg['passageway'];
        $map['suitsdk'] = $soft_msg['suitsdk'];
        $map['status'] = $soft_msg['status'];
        $map['recommend'] = $soft_msg['recommend'];
        $map['recommend_order'] = $soft_msg['recommend_order'];
        $map['jumpurl'] = $soft_msg['jumpurl'];
        $map['roll'] = $soft_msg['roll'];
        $map['roll_img'] = $soft_msg['roll_img'];
        $map['roll_order'] = $soft_msg['roll_order'];
        $map['apk_show'] = $soft_msg['apk_show'];
        $map['upload_uid'] = $soft_msg['upload_uid'];
        $map['md5_file'] = $soft_msg['md5_file'];
        $map['qr_url'] = $soft_msg['qr_url'];

		$id = $soft->add($map);

		$soft_belong = M('soft_belong');
        $soft_version = M('soft_version');

        $arg['soft_id'] = $id;
        $arg['soft_version'] = $map['version'];
        $arg['update_time'] = time();
        $soft_version->add($arg);

		$belong['soft_id'] = $id;
		$belong['menu_id'] = '3';
        $belong['menu_child_id'] = $mcid;
		$soft_belong->add($belong);
	 }


    }

    public function rands($length,$strtolower=1,$chars,$hash) {
        $max = strlen($chars) - 1;
        mt_srand((double)microtime() * 1000000);
        for($i = 0; $i < $length; $i++) {
        	$hash .= $chars[mt_rand(0, $max)];
        }
        if($strtolower==1){
        	$hash=strtolower($hash);
        }
        return $hash;
	}



}

?>
