<?php

namespace web\ambap;

class me {

    public static function json_user_bank_change() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $bankno = $post->get('bankno');
        $bankname = $post->get('bankname');
        $bankaccount = $post->get('bankaccount');
        $bankcode = $post->get('bankcode');
        $updata = array();
        $updata['bankno'] = $bankno;
        $updata['bankname'] = $bankname;
        $updata['bankaccount'] = $bankaccount;
        $updata['bankcode'] = $bankcode;
        $csql = new \ciy\sql('ap_usr_ext');
        $csql->where('id', $rsuser['id']);
        $extrow = $db->getone($csql);
        if (is_array($extrow)) {
            if ($db->update($csql, $updata) === false)
                return errjson('ext更新失败:' . $db->error);
        } else {
            $updata['id'] = $rsuser['id'];
            if ($db->insert($csql, $updata) === false)
                return errjson('ext新增失败:' . $db->error);
        }
        return succjson();
    }
    public static function json_cashout_lst_list() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $query = $post->get('query');
        $csql = new \ciy\sql('ap_cash_out');
        $csql->where('vuser', $rsuser['id']);
        $liid = $post->getint('liid');
        if ($liid > 0)
            $csql->where('cashstatus', $liid);
        $csql->order(objstr($query, 'order', 'id desc'));
        $pageno = $post->getint('pageno', 1);
        $pagecount = $post->getint('pagecount', 10);
        $csql->limit($pageno, $pagecount);
        $rows = $db->get($csql);
        $ret = array('pageno' => $pageno, 'pagecount' => $pagecount, 'list' => $rows);
        return succjson($ret);
    }
    public static function json_cashout_cash_inv() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->getint('id');
        $invcode = $post->get('invcode');
        $invfile = $post->get('invfile');
        if (empty($invcode))
            return errjson('请填写发票编号');
        if (empty($invfile))
            return errjson('请上传发票');
        $csql = new \ciy\sql('ap_cash_out');
        $csql->where('vuser', $rsuser['id']);
        $csql->where('id', $id);
        $cashoutrow = $db->getone($csql);
        if (!is_array($cashoutrow))
            return errjson('提现不存在');
        if ($cashoutrow['cashstatus'] != 20)
            return errjson('提现不是已通过状态');
        try {
            $db->begin();
            $updata = array();
            $updata['cashstatus'] = 30;
            $updata['invcode'] = $invcode;
            $updata['invfile'] = $invfile;
            $updata['cashtimes'] = tostamp();
            $csql = new \ciy\sql('ap_cash_out');
            $csql->where('id', $id);
            if ($db->update($csql, $updata) === false)
                throw new \Exception('更新失败:' . $db->error);
            $updata['id'] = $id;
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            return errjson($ex->getMessage());
        }
        $ret['data'] = $updata;
        return succjson($ret);
    }
    public static function json_cashout_cash_wxrecvpay() {
        global $db;
        global $_token;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->getint('id');
        $csql = new \ciy\sql('ap_cash_out');
        $csql->where('id', $id);
        $cashrow = $db->getone($csql);
        if (!is_array($cashrow))
            return errjson('提现不存在');
        if ($cashrow['cashstatus'] == 21) {
            if ($cashrow['cashtimes'] > tostamp() - 86400)
                return errjson('提现打款进程被打断。24小时后重新操作，请务必点击收款');
        } else if ($cashrow['cashstatus'] != 20)
            return errjson('提现不是已通过状态');
        if ($cashrow['vuser'] != $rsuser['id'])
            return errjson('提现用户不匹配');
        $updata = array();
        $updata['cashstatus'] = 21;
        $updata['cashtimes'] = tostamp();
        $csql = new \ciy\sql('ap_cash_out');
        $csql->where('id', $id);
        if ($db->update($csql, $updata) === false)
            return errjson('更新失败');

        $cashmoney = $cashrow['cashmoney'];
        if ($cashrow['taxmoney'] > 0)
            $cashmoney -= $cashrow['taxmoney'];
        $param = array();
        $param['out_bill_no'] = 'TB' . date('ymd') . '8' . $id;
        $param['transfer_scene_id'] = '1005';
        $param['openid'] = $rsuser['wxminaid'];
        $param['notify_url'] = $_token['maindomain'] . 'wxpay.transfer';
        $param['transfer_remark'] = '佣金奖励';
        $param['transfer_amount'] = toint($cashmoney);
        $param['transfer_scene_report_infos'] = array();
        $param['transfer_scene_report_infos'][] = array('info_type' => '岗位类型', 'info_content' => '采集员');
        $param['transfer_scene_report_infos'][] = array('info_type' => '报酬说明', 'info_content' => '用户主动申请提现');
        $weixinapi = new \web\api\weixin(1);
        $wxret = $weixinapi->transfer($param);
        if (is_string($wxret))
            return errjson($wxret);
        $ret['transfer'] = $wxret;
        return succjson($ret);
    }
    private static function caltax($money) {
        if ($money <= 80000)
            return 0;
        if ($money <= 400000)
            $taxable_income = $money - 80000;
        else
            $taxable_income = $money * 0.8;
        if ($taxable_income <= 2000000)
            return toint($taxable_income * 0.2);
        else if ($taxable_income <= 5000000)
            return toint($taxable_income * 0.3 - 200000);
        else
            return toint($taxable_income * 0.4 - 700000);
    }
    public static function json_cashout_cash_submit() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $cashtype = $post->getint('cashtype');
        $cash = $post->getint('cash');
        if ($rsuser['mycashmoney'] < $cash)
            return errjson('余额不足');
        $csql = new \ciy\sql('ap_cash_out');
        $csql->where('vuser', $rsuser['id']);
        $csql->where('cashstatus<90');
        $chkrow = $db->getone($csql);
        if (is_array($chkrow))
            return errjson('有未处理提现');
        $taxperson = true;
        if ($cashtype == 1) {
            $csql = new \ciy\sql('ap_usr_ext');
            $csql->where('id', $rsuser['id']);
            $extrow = $db->getone($csql);
            if (!is_array($extrow))
                return errjson('用户信息不存在');
            if (empty($extrow['truename']))
                return errjson('请先完善个人信息');
            if ($extrow['truename'] != $extrow['bankaccount'])
                $taxperson = false;
        }
        $taxmoney = -1;
        if ($taxperson) {
            $monthstart = strtotime(date('Y-m-01'));
            $csql = new \ciy\sql('ap_cash_out');
            $csql->where('vuser', $rsuser['id']);
            $csql->where('cashstatus=100');
            $csql->where('addtimes>=', $monthstart);
            $cashoutrows = $db->get($csql);
            $totalcash = 0;
            $totaltax = 0;
            foreach ($cashoutrows as $cashoutrow) {
                $totalcash += $cashoutrow['cashmoney'];
                $totaltax += $cashoutrow['taxmoney'];
            }
            $taxmoney = self::caltax($totalcash + $cash) - $totaltax;
        }
        try {
            $db->begin();
            \web\cdao::usercashoe($db, 11, $rsuser['id'], -$cash, '提现');
            $updata = array();
            $updata['cashstatus'] = 10;
            $updata['cashmoney'] = $cash;
            $updata['taxmoney'] = $taxmoney;
            $updata['cashtype'] = $cashtype;
            $updata['vuser'] = $rsuser['id'];
            $updata['addtimes'] = tostamp();
            if ($cashtype == 1) {
                $updata['bankaccount'] = $extrow['bankaccount'];
                $updata['bankno'] = $extrow['bankno'];
                $updata['bankname'] = $extrow['bankname'];
                $updata['bankcode'] = $extrow['bankcode'];
            }
            $csql = new \ciy\sql('ap_cash_out');
            if ($db->insert($csql, $updata) === false)
                throw new \Exception('创建失败:' . $db->error);
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            return errjson($ex->getMessage());
        }
        return succjson();
    }
    public static function json_cashout_cash_init() {
        global $db;
        $rsuser = verifyfast();
        $csql = new \ciy\sql('ap_user');
        $csql->where('id', $rsuser['id']);
        $csql->column('myinvmoney,mycashmoney,mybondmoney');
        $ret['user'] = $db->getone($csql);
        $csql = new \ciy\sql('ap_cash_out');
        $csql->where('vuser', $rsuser['id']);
        $csql->where('cashstatus<90');
        $ret['cashs'] = $db->get($csql);
        $ret['tip'] = '[MD]
每天可提交1次，以元为单位申请。
非对公提现，平台代扣缴劳务个税。
多次提现以本月1日开始累计计算个税。
由于微信支付限制，申请提现平台审核通过后，需手动点击收款操作。
';
        return succjson($ret);
    }
    public static function json_user_info_change() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $name = $post->get('name');
        $icon = $post->get('icon');
        $mobile = $post->get('mobile');
        $wxno = $post->get('wxno');
        $email = $post->get('email');
        if (empty($name))
            return errjson('姓名不能为空');
        if (!ismobile($mobile))
            return errjson('手机号码格式不正确');
        if (empty($email)) {
            if (ismail($email))
                return errjson('邮箱格式不正确');
        }
        $updata = array();
        $updata['name'] = $name;
        $updata['icon'] = $icon;
        $updata['mobile'] = $mobile;
        $csql = new \ciy\sql('ap_user');
        $csql->where('id', $rsuser['id']);
        if ($db->update($csql, $updata) === false)
            return errjson('更新失败:' . $db->error);
        $updata = array();
        $updata['wxno'] = $wxno;
        $updata['email'] = $email;
        $csql = new \ciy\sql('ap_usr_ext');
        $csql->where('id', $rsuser['id']);
        $extrow = $db->getone($csql);
        if (is_array($extrow)) {
            if ($db->update($csql, $updata) === false)
                return errjson('ext更新失败:' . $db->error);
        } else {
            $updata['id'] = $rsuser['id'];
            if ($db->insert($csql, $updata) === false)
                return errjson('ext新增失败:' . $db->error);
        }
        return succjson();
    }
    public static function json_cashie_lst_list() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $query = $post->get('query');
        $csql = new \ciy\sql('ap_cash_ie');
        $csql->where('vuser', $rsuser['id']);
        $liid = $post->getint('liid');
        if ($liid == 1)
            $csql->where('iemoney>0');
        if ($liid == 2)
            $csql->where('iemoney<0');
        $csql->order(objstr($query, 'order', 'id desc'));
        $pageno = $post->getint('pageno', 1);
        $pagecount = $post->getint('pagecount', 10);
        $csql->limit($pageno, $pagecount);
        $rows = $db->get($csql);
        $ret = array('pageno' => $pageno, 'pagecount' => $pagecount, 'list' => $rows);
        return succjson($ret);
    }
    public static function json_cashin_lst_list() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $query = $post->get('query');
        $csql = new \ciy\sql('ap_cash_in');
        $csql->where('paytimes>0');
        $csql->where('vuser', $rsuser['id']);
        $csql->order(objstr($query, 'order', 'id desc'));
        $pageno = $post->getint('pageno', 1);
        $pagecount = $post->getint('pagecount', 10);
        $csql->limit($pageno, $pagecount);
        $rows = $db->get($csql);
        $ret = array('pageno' => $pageno, 'pagecount' => $pagecount, 'list' => $rows);
        return succjson($ret);
    }
    public static function json_cash_charge_chk() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->getint('orderid');
        for ($i = 0; $i < 10; $i++) {
            $csql = new \ciy\sql('ap_cash_in');
            $csql->where('id', $id);
            $buyrow = $db->getone($csql);
            if (!is_array($buyrow))
                return errjson('订单不存在');
            if ($buyrow['paytimes'] > 0)
                return succjson();
            sleep(1);
        }
        return errjson('充值不成功,请联系客服');
    }
    public static function json_cash_charge_pay() {
        global $db;
        global $_token;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $money = $post->getint('money');
        $updata = array();
        $updata['name'] = '';
        $updata['inmoney'] = $money;
        $updata['vuser'] = $rsuser['id'];
        $updata['addtimes'] = tostamp();
        $updata['paytimes'] = 0;
        $csql = new \ciy\sql('ap_cash_in');
        if ($db->insert($csql, $updata) === false)
            return errjson('订单创建失败:' . $db->error);
        $orderid = $db->insert_id();
        $param = array();
        $param['description'] = '充值';
        $param['out_trade_no'] = 'IN' . date('ymd') . '2' . $orderid;
        $param['notify_url'] = $_token['maindomain'] . 'wxpay.charge';
        $param['amount'] = array();
        $param['amount']['total'] = (int)$money;
        $param['payer'] = array();
        $param['payer']['openid'] = $rsuser['wxminaid'];
        $weixinapi = new \web\api\weixin(1);
        $retpay = $weixinapi->pay($param);
        if (is_string($retpay))
            return errjson($retpay);
        $retpay['orderid'] = $orderid;
        return succjson($retpay);
    }
    public static function json_cash_charge_init() {
        global $db;
        $csql = new \ciy\sql('ap_cash_bundle');
        $ret['charges'] = $db->get($csql);
        return succjson($ret);
    }
    public static function json_pnt_buylst_list() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $query = $post->get('query');
        $csql = new \ciy\sql('ap_pnt_buy');
        $csql->where('vuser', $rsuser['id']);
        $csql->where('paytimes>0');
        $csql->order(objstr($query, 'order', 'id desc'));
        $pageno = $post->getint('pageno', 1);
        $pagecount = $post->getint('pagecount', 10);
        $csql->limit($pageno, $pagecount);
        $rows = $db->get($csql);
        $ret = array('pageno' => $pageno, 'pagecount' => $pagecount, 'list' => $rows);
        return succjson($ret);
    }
    public static function json_pnt_buynow_chk() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->getint('orderid');
        for ($i = 0; $i < 10; $i++) {
            $csql = new \ciy\sql('ap_pnt_buy');
            $csql->where('id', $id);
            $buyrow = $db->getone($csql);
            if (!is_array($buyrow))
                return errjson('订单不存在');
            if ($buyrow['paytimes'] > 0)
                return succjson();
            sleep(1);
        }
        return errjson('支付不成功,请联系客服');
    }

    public static function json_pnt_buynow_pay() {
        global $db;
        global $_token;
        $rsuser = verifyfast();
        //建立订单，支付
        $post = new \ciy\post();
        $id = $post->getint('id');
        $csql = new \ciy\sql('ap_pnt_bundle');
        $csql->where('id', $id);
        $bundlerow = $db->getone($csql);
        if (!is_array($bundlerow))
            return errjson('购买项目不存在');
        $payprice = $bundlerow['buymoney'];
        $updata = array();
        $updata['pnt'] = $bundlerow['buypnt'];
        $updata['buypntmoney'] = $payprice;
        $updata['vuser'] = $rsuser['id'];
        $updata['addtimes'] = tostamp();

        $csql = new \ciy\sql('ap_pnt_buy');
        if ($db->insert($csql, $updata) === false)
            return errjson('订单创建失败:' . $db->error);
        $orderid = $db->insert_id();
        $param = array();
        $param['description'] = '购买金币';
        $param['out_trade_no'] = 'PT' . date('ymd') . '9' . $orderid;
        $param['notify_url'] = $_token['maindomain'] . 'wxpay.pay_pnt';
        $param['amount'] = array();
        $param['amount']['total'] = (int)$payprice;
        $param['payer'] = array();
        $param['payer']['openid'] = $rsuser['wxminaid'];
        $weixinapi = new \web\api\weixin(1);
        $retpay = $weixinapi->pay($param);
        if (is_string($retpay))
            return errjson($retpay);
        $retpay['orderid'] = $orderid;
        return succjson($retpay);
    }
    public static function json_pnt_buynow_init() {
        global $db;
        $csql = new \ciy\sql('ap_pnt_bundle');
        $ret['buys'] = $db->get($csql);
        return succjson($ret);
    }
    public static function json_pnt_lst_list() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $query = $post->get('query');
        $csql = new \ciy\sql('ap_pnt_record');
        $csql->where('vuser', $rsuser['id']);
        $csql->order(objstr($query, 'order', 'id desc'));
        $pageno = $post->getint('pageno', 1);
        $pagecount = $post->getint('pagecount', 10);
        $csql->limit($pageno, $pagecount);
        $rows = $db->get($csql);
        $ret = array('pageno' => $pageno, 'pagecount' => $pagecount, 'list' => $rows);
        return succjson($ret);
    }
    public static function json_safe_ccub_init() {
        global $db;
        $rsuser = verifyfast();
        $csql = new \ciy\sql('dao_ccub_apply');
        $csql->where('vuser', $rsuser['id']);
        $csql->order('id desc');
        $ret['data'] = $db->getone($csql);
        if (is_array($ret['data']))
            $ret['data']['cciy'] = $ret['data']['cciy'] . '';
        $ret['tip'] = '[MD]
打造一人一码的唯一标识。
申请标识后可参与众产投票。
#主体国籍
#多国身份
在非主体国拥有居留、永居、入籍等身份。
与普通公民有同种社会标识的情况。
';
        return succjson($ret);
    }
    public static function json_safe_ccub_submit() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $country = $post->get('country');
        if (empty($country))
            return errjson('主体国籍不能为空');
        $dualcitizen = $post->getint('dualcitizen');
        $csql = new \ciy\sql('ap_usr_ext');
        $csql->where('id', $rsuser['id']);
        $usrextrow = $db->getone($csql);
        if (!is_array($usrextrow))
            return errjson('用户不存在');
        $updata = array();
        $updata['auditstatus'] = 20;
        $updata['vuser'] = $rsuser['id'];
        $updata['name'] = $usrextrow['truename'];
        $updata['idid'] = $usrextrow['idid'];
        $updata['country'] = $country;
        $updata['dualcitizen'] = $dualcitizen;
        $updata['addtimes'] = tostamp();
        $csql = new \ciy\sql('dao_ccub_apply');
        if ($db->insert($csql, $updata) === false)
            return errjson('新增失败:' . $db->error);
        return succjson();
    }
    public static function json_user_show_more() {
        global $db;
        $ret['data'] = null;
        return succjson($ret);
    }
    public static function json_safe_real_init() {
        global $db;
        $rsuser = verifyfast();
        $csql = new \ciy\sql('ap_usr_real_apply');
        $csql->where('vuser', $rsuser['id']);
        $csql->order('id desc');
        $ret['data'] = $db->getone($csql);
        $ret['inputtype'] = 1;
        return succjson($ret);
    }
    public static function json_safe_real_submit() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $name = $post->get('name');
        if (empty($name))
            return errjson('姓名不能为空.');
        $idid = idcard($post->get('idid'));
        if (is_string($idid))
            return errjson($idid);
        $idcard1 = $post->get('idcard1');
        $idcard2 = $post->get('idcard2');
        $updata = array();
        $updata['auditstatus'] = 20;
        $updata['vuser'] = $rsuser['id'];
        $updata['name'] = $name;
        $updata['idid'] = $idid['code'];
        $updata['idcard1'] = $idcard1;
        $updata['idcard2'] = $idcard2;
        $updata['addtimes'] = tostamp();
        $csql = new \ciy\sql('ap_usr_real_apply');
        if ($db->insert($csql, $updata) === false)
            return errjson('新增失败:' . $db->error);
        return succjson();
    }
    public static function json_invoicetitle_del() {
        global $db;
        $rsuser = verifyfast();
        if ($rsuser === null)
            return errjson('您还未登陆', 2);
        $post = new \ciy\post();
        $id = $post->getint('id');
        $csql = new \ciy\sql('ap_invoicetitle');
        $csql->where('vuser', $rsuser['id']);
        $csql->where('id', $id);
        if ($db->delete($csql) === false)
            return errjson('删除失败:' . $db->error);
        return succjson();
    }
    public static function json_invoicetitle_edit() {
        global $db;
        $rsuser = verifyfast();
        if ($rsuser === null)
            return errjson('您还未登陆', 2);
        $post = new \ciy\post();
        $id = $post->getint('id');
        if ($id > 0) {
            $csql = new \ciy\sql('ap_invoicetitle');
            $csql->where('vuser', $rsuser['id']);
            $csql->where('id', $id);
            $shiprow = $db->getone($csql);
            if (!is_array($shiprow))
                return errjson('抬头不存在' . $id);
        }
        $name = $post->get('name');
        $addr = $post->get('addr');
        $phone = $post->get('phone');
        $taxnumber = $post->get('taxnumber');
        $bankname = $post->get('bankname');
        $bankaccount = $post->get('bankaccount');
        try {
            $db->begin();
            $updata = array();
            $updata['name'] = $name;
            $updata['addr'] = $addr;
            $updata['taxnumber'] = $taxnumber;
            $updata['phone'] = $phone;
            $updata['bankname'] = $bankname;
            $updata['bankaccount'] = $bankaccount;
            $updata['uptimes'] = tostamp();
            $csql = new \ciy\sql('ap_invoicetitle');
            if ($id > 0) {
                $csql->where('id', $id);
                if ($db->update($csql, $updata) === false)
                    throw new \Exception('更新失败:' . $db->error);
            } else {
                $updata['vuser'] = $rsuser['id'];
                if ($db->insert($csql, $updata) === false)
                    throw new \Exception('新增失败:' . $db->error);
                $id = $db->insert_id();
            }
            $updata['id'] = $id;
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            return errjson($ex->getMessage());
        }
        $ret['data'] = $updata;
        return succjson($ret);
    }
    public static function json_invoicetitle_list() {
        global $db;
        $rsuser = verifyfast();
        $csql = new \ciy\sql('ap_invoicetitle');
        $csql->where('vuser', $rsuser['id']);
        $csql->order('id desc');
        $ret['list'] = $db->get($csql);
        return succjson($ret);
    }
    public static function json_invoice_lst_list() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $query = $post->get('query');
        $csql = new \ciy\sql('ap_invoicing');
        $csql->where('vuser', $rsuser['id']);
        $liid = $post->getint('liid');
        if ($liid > 0)
            $csql->where('auditstatus', $liid);
        $csql->order(objstr($query, 'order', 'id desc'));
        $pageno = $post->getint('pageno', 1);
        $pagecount = $post->getint('pagecount', 10);
        $csql->limit($pageno, $pagecount);
        $rows = $db->get($csql);
        $ret = array('pageno' => $pageno, 'pagecount' => $pagecount, 'list' => $rows);
        $ret['ap_invoicedetail'] = getrelation($db, $rows, 'ap_invoicedetail', 'invoicedetailid');
        return succjson($ret);
    }
    public static function json_invoicing_submit() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $invoicetitleid = $post->getint('invoicetitleid');
        $invoicedetailid = $post->getint('invoicedetailid');
        $money = $post->getint('money');
        if ($money < 100)
            return errjson('需填开票金额');
        if ($rsuser['myinvmoney'] < $money)
            return errjson('可开票金额不足');
        $csql = new \ciy\sql('ap_invoicetitle');
        $csql->where('id', $invoicetitleid);
        $invoicerow = $db->getone($csql);
        if (!is_array($invoicerow))
            return errjson('开票抬头不存在');
        try {
            $db->begin();
            $updata = array();
            $updata['myinvmoney'] = array('myinvmoney-' . $money);
            $csql = new \ciy\sql('ap_user');
            $csql->where('id', $rsuser['id']);
            if ($db->update($csql, $updata) === false)
                throw new \Exception('更新账户失败:' . $db->error);
            $updata = array();
            $updata['invoicemoney'] = $money;
            $updata['invoicetitleid'] = $invoicetitleid;
            $updata['invoicedetailid'] = $invoicedetailid;
            $updata['vuser'] = $rsuser['id'];
            $updata['addtimes'] = tostamp();
            $updata['auditstatus'] = 20;
            $updata['name'] = $invoicerow['name'];
            $updata['taxnumber'] = $invoicerow['taxnumber'];
            $updata['addr'] = $invoicerow['addr'];
            $updata['phone'] = $invoicerow['phone'];
            $updata['bankname'] = $invoicerow['bankname'];
            $updata['bankaccount'] = $invoicerow['bankaccount'];
            $csql = new \ciy\sql('ap_invoicing');
            if ($db->insert($csql, $updata) === false)
                throw new \Exception('创建失败:' . $db->error);
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            return errjson($ex->getMessage());
        }
        return succjson();
    }
    public static function json_invoicing_init() {
        global $db;
        $rsuser = verifyfast();
        $csql = new \ciy\sql('ap_user');
        $csql->where('id', $rsuser['id']);
        $csql->column('myinvmoney');
        $ret['user'] = $db->getone($csql);
        $csql = new \ciy\sql('ap_invoicedetail');
        $csql->column('id,name');
        $ret['details'] = $db->get($csql);
        $ret['tip'] = '[MD]
可联系客服签订相关合同。
';
        return succjson($ret);
    }
    public static function json_user_shipaddr_list() {
        global $db;
        $rsuser = verifyfast();
        $csql = new \ciy\sql('ap_usr_shipaddr');
        $csql->where('vuser', $rsuser['id']);
        $csql->order('id desc');
        $ret['list'] = $db->get($csql);
        return succjson($ret);
    }
    public static function json_user_shipaddr_edit() {
        global $db;
        $rsuser = verifyfast();
        if ($rsuser === null)
            return errjson('您还未登陆', 2);
        $post = new \ciy\post();
        $id = $post->getint('id');
        if ($id > 0) {
            $csql = new \ciy\sql('ap_usr_shipaddr');
            $csql->where('vuser', $rsuser['id']);
            $csql->where('id', $id);
            $shiprow = $db->getone($csql);
            if (!is_array($shiprow))
                return errjson('地址不存在' . $id);
        }
        $name = $post->get('name');
        $addr = $post->get('addr');
        $phone = $post->get('phone');
        $areacode = $post->getint('areacode');
        try {
            $db->begin();
            $updata = array();
            $updata['name'] = $name;
            $updata['addr'] = $addr;
            $updata['areacode'] = $areacode;
            $updata['phone'] = $phone;
            $updata['uptimes'] = tostamp();
            $csql = new \ciy\sql('ap_usr_shipaddr');
            if ($id > 0) {
                $csql->where('id', $id);
                if ($db->update($csql, $updata) === false)
                    throw new \Exception('更新失败:' . $db->error);
            } else {
                $updata['vuser'] = $rsuser['id'];
                if ($db->insert($csql, $updata) === false)
                    throw new \Exception('新增失败:' . $db->error);
                $id = $db->insert_id();
            }
            $updata['id'] = $id;
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            return errjson($ex->getMessage());
        }
        $ret['data'] = $updata;
        return succjson($ret);
    }
    public static function json_user_shipaddr_del() {
        global $db;
        $rsuser = verifyfast();
        if ($rsuser === null)
            return errjson('您还未登陆', 2);
        $post = new \ciy\post();
        $id = $post->getint('id');
        $csql = new \ciy\sql('ap_usr_shipaddr');
        $csql->where('vuser', $rsuser['id']);
        $csql->where('id', $id);
        if ($db->delete($csql) === false)
            return errjson('删除失败:' . $db->error);
        return succjson();
    }
    public static function json_share_qrcode_get() {
        $rsuser = verifyfast();
        $filename = '/xcxqrcode/' . $rsuser['id'] . '.jpg';
        $ret['qrcode'] = $filename;
        $filename = PATH_WEB . 'ud' . $filename;
        if (file_exists($filename))
            return succjson($ret);
        $param = array();
        $param['scene'] = 'upid=' . $rsuser['id'] . '&carduser=' . $rsuser['id'];
        $param['page'] = 'pages/main/index';
        $param['check_path'] = true;
        $param['env_version'] = 'release';
        $param['width'] = 1280;
        $weixinapi = new \web\api\weixin(1);
        $wxret = $weixinapi->call('https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={accesstoken}', $param, true);
        if (is_string($wxret))
            return errjson($wxret);
        dirmake(dirname($filename));
        $fp = fopen($filename, 'w');
        fwrite($fp, $wxret['bin']);
        fclose($fp);
        return succjson($ret);
    }
    public static function json_safe_password_change() {
        global $db;
        global $_token;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $oldpass = $post->get('oldpass');
        $newpass = $post->get('newpass');
        $csql = new \ciy\sql('ap_user');
        $csql->where('id', $rsuser['id']);
        $rsuser = $db->getone($csql);
        if (!empty($rsuser['password']) && $rsuser['password'] != md5($oldpass . $_token['salt']))
            return errjson('旧密码错误.');
        $updata = array();
        $updata['password'] = md5($newpass . $_token['salt']);
        $csql = new \ciy\sql('ap_user');
        $csql->where('id', $rsuser['id']);
        if ($db->update($csql, $updata) === false)
            return errjson('密码更新失败:' . $db->error);
        return succjson();
    }
    public static function json_suggest_get() {
        global $db;
        $rsuser = verifyuser();
        $ret['list'] = array();
        if ($rsuser == null)
            return succjson($ret);
        $csql = new \ciy\sql('ap_usr_suggest');
        $csql->where('vuser', $rsuser['id']);
        $csql->order('id desc');
        $csql->limit(1, 10);
        $ret['list'] = $db->get($csql);
        return succjson($ret);
    }
    public static function json_suggest_submit() {
        global $db;
        $rsuser = verifyuser();
        $post = new \ciy\post();
        $content = $post->get('content');
        $imgs = $post->get('imgs');
        $contract = $post->get('contract');
        $updata = array();
        $updata['content'] = $content;
        $updata['imgs'] = $imgs;
        $updata['contract'] = $contract;
        if ($rsuser === null)
            $updata['vuser'] = 0;
        else
            $updata['vuser'] = $rsuser['id'];
        $updata['addtimes'] = tostamp();
        $csql = new \ciy\sql('ap_usr_suggest');
        if ($db->insert($csql, $updata) === false)
            return errjson('更新失败:' . $db->error);
        $updata['id'] = $db->insert_id();
        $ret['data'] = $updata;
        return succjson($ret);
    }
    public static function json_problem_chat_send() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $content = $post->get('content');
        $id = $post->getint('id');
        $updata = array();
        $updata['problemid'] = $id;
        $updata['content'] = $content;
        $updata['sendtype'] = 1;
        $updata['addtimes'] = tostamp();
        $csql = new \ciy\sql('ap_usr_problechat');
        if ($db->insert($csql, $updata) === false)
            return errjson('更新失败:' . $db->error);
        $updata['id'] = $db->insert_id();
        $ret['data'] = $updata;
        $updata = array();
        $updata['replytimes'] = 0;
        $updata['closetimes'] = 0;
        $csql = new \ciy\sql('ap_usr_problem');
        $csql->where('id', $id);
        if ($db->update($csql, $updata) === false)
            return errjson('更新失败:' . $db->error);
        return succjson($ret);
    }
    public static function json_problem_chat_get() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->getint('id');
        $csql = new \ciy\sql('ap_usr_problechat');
        $csql->where('problemid', $id);
        $mid = $post->getint('mid');
        $pagecount = $post->getint('pagecount', 20);
        if ($post->get('op') == 'max') {
            $csql->where('id>', $mid);
            $csql->order('id');
        } else {
            $csql->where('id<', $mid);
            $csql->order('id desc');
            $csql->limit(1, $pagecount);
        }
        $rows = $db->get($csql);
        $ret['list'] = $rows;
        $ret['pagecount'] = $pagecount;
        if ($post->getbool('once')) {
            $ret['once'] = true;
            $csql = new \ciy\sql('ap_usr_problem');
            $csql->where('id', $id);
            $ret['problem'] = $db->getone($csql);
        }
        return succjson($ret);
    }
    public static function json_problem_pub_close() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->getint('id');
        $updata = array();
        $updata['closetimes'] = tostamp();
        $csql = new \ciy\sql('ap_usr_problem');
        $csql->where('id', $id);
        if ($db->update($csql, $updata) === false)
            return errjson('更新失败:' . $db->error);
        return succjson();
    }
    public static function json_problem_pub_add() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $content = $post->get('content');
        $imgs = $post->get('imgs');
        $contract = $post->get('contract');
        $updata = array();
        $updata['content'] = $content;
        $updata['imgs'] = $imgs;
        $updata['contract'] = $contract;
        $updata['vuser'] = $rsuser['id'];
        $updata['addtimes'] = tostamp();
        $csql = new \ciy\sql('ap_usr_problem');
        if ($db->insert($csql, $updata) === false)
            return errjson('更新失败:' . $db->error);
        $updata['id'] = $db->insert_id();
        $ret['data'] = $updata;
        return succjson($ret);
    }
    public static function json_problem_pub_list() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $csql = new \ciy\sql('ap_usr_problem');
        $liid = $post->getint('liid');
        if ($liid == 3)
            $csql->where('closetimes=0')->where('replytimes>0');
        else if ($liid == 9)
            $csql->where('closetimes>0');
        else
            $csql->where('closetimes=0')->where('replytimes=0');
        $csql->where('vuser', $rsuser['id']);
        $csql->order('id desc');
        $pageno = $post->getint('pageno', 1);
        $pagecount = $post->getint('pagecount', 10);
        $csql->limit($pageno, $pagecount);
        $rows = $db->get($csql);
        $ret = array('pageno' => $pageno, 'pagecount' => $pagecount, 'list' => $rows);
        return succjson($ret);
    }
    public static function json_me_userbuy_chk() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->getint('orderid');
        for ($i = 0; $i < 10; $i++) {
            $csql = new \ciy\sql('ap_usr_buy_user');
            $csql->where('id', $id);
            $buyrow = $db->getone($csql);
            if (!is_array($buyrow))
                return errjson('订单不存在');
            if ($buyrow['paytimes'] > 0) {
                $csql = new \ciy\sql('ap_user');
                $csql->where('id', $rsuser['id']);
                $userrow = $db->getone($csql);
                $ret['accounttimes'] = $userrow['accounttimes'];
                $ret['userlevel'] = $userrow['userlevel'];
                return succjson($ret);
            }
            sleep(1);
        }
        return errjson('支付信息未同步，请下拉刷新');
    }

    public static function json_me_userbuy_create() {
        global $db;
        global $_token;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->getint('id');
        $csql = new \ciy\sql('ap_usr_buy_bundle');
        $csql->where('id', $id);
        $bundlerow = $db->getone($csql);
        if (!is_array($bundlerow))
            return errjson('购买项目不存在');
        $money = $bundlerow['usermoney'];
        $updata = array();
        $updata['bundleid'] = $id;
        $updata['userlevel'] = $bundlerow['userlevel'];
        $updata['days'] = $bundlerow['days'];
        $updata['buydaymoney'] = $money;
        $updata['vuser'] = $rsuser['id'];
        $updata['addtimes'] = tostamp();
        $csql = new \ciy\sql('ap_usr_buy_user');
        if ($db->insert($csql, $updata) === false)
            return errjson('订单创建失败:' . $db->error);
        $orderid = $db->insert_id();
        $param = array();
        $param['description'] = '购买会员';
        $param['out_trade_no'] = 'UB' . date('ymd') . '9' . $orderid;
        $param['notify_url'] = $_token['maindomain'] . 'wxpay.pay_user';
        $param['amount'] = array();
        $param['amount']['total'] = (int)$money;
        $param['payer'] = array();
        $param['payer']['openid'] = $rsuser['wxminaid'];
        $weixinapi = new \web\api\weixin(1);
        $retpay = $weixinapi->pay($param);
        if (is_string($retpay))
            return errjson($retpay);
        $retpay['orderid'] = $orderid;
        return succjson($retpay);
    }
    public static function json_me_balance_get() {
        global $db;
        $rsuser = verifyfast();
        $csql = new \ciy\sql('ap_user');
        $csql->where('id', $rsuser['id']);
        $ret['user'] = $db->getone($csql);
        unset($ret['user']['password']);
        unset($ret['user']['sid']);
        unset($ret['user']['wxminaid']);
        unset($ret['user']['wxminakey']);
        unset($ret['user']['wxunionid']);
        unset($ret['user']['memo']);
        return succjson($ret);
    }
}
