<?php

namespace app\api\controller;
use think\facade\Db;
use think\facade\Lang;
/**
 * ============================================================================
 * 
 * ============================================================================
 * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司，并保留所有权利。
 * 网站地址: https://www.valimart.net/
 * ----------------------------------------------------------------------------
 * 
 * ============================================================================
 * 领取红包控制器
 */
class Memberbonus extends MobileMember {

    public function initialize() {
        parent::initialize();
        Lang::load(base_path() . 'home/lang/'.config('lang.default_lang').'/memberbonus.lang.php');
    }
    /**
     * @api {POST} api/Memberbonus/get_receive_list 红包记录
     * @apiVersion 1.0.0
     * @apiGroup Memberbonus
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     */
    public function get_receive_list() {
        $bonus_model=model('bonus');
        $bonuslog_list=$bonus_model->getBonusreceiveList(array('member_id'=>$this->member_info['member_id']),$this->pagesize);
        $result = array_merge(array('log_list' => $bonuslog_list), mobile_page($bonus_model->page_info));
        ds_json_encode(10000, '',$result);
    }
    /**
     * @api {POST} api/Memberbonus/receive 活动红包领取
     * @apiVersion 1.0.0
     * @apiGroup Memberbonus
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} bonus_id 活动ID
     * 
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     */
    public function receive() {
        $bonus_id = intval(input('param.bonus_id'));
        if ($bonus_id < 0) {
            ds_json_encode(10001, lang('param_error'));
        }
        $bonus_model = model('bonus');
        $condition = array();
        $condition[] = array('bonus_id','=',$bonus_id);
        $bonus = $bonus_model->getOneBonus($condition); //获取当前红包的领取金额
        if ($bonus['bonus_begintime'] > TIMESTAMP) {
            ds_json_encode(10001, lang('bonus_not_begin'));
        }
        if ($bonus['bonus_type'] != 1) {
            ds_json_encode(10001, lang('bonus_type_error'));
        }
        if ($bonus['bonus_state'] == 2 || TIMESTAMP > $bonus['bonus_endtime']) {
            ds_json_encode(10001, lang('bonus_expire'));
        }
        if ($bonus['bonus_state'] == 3) {
            ds_json_encode(10001, lang('bonus_invalid'));
        }

        //判断当前用户是否领取过
        $condition = array();
        $condition[] = array('bonus_id','=',$bonus_id);
        $condition[] = array('member_id','=',$this->member_info['member_id']);
        $bonusreceive = $bonus_model->getOneBonusreceive($condition);
        if ($bonusreceive) {
            ds_json_encode(10001, sprintf(lang('bonusreceive_info'),date('Y-m-d H:i:s', $bonusreceive['bonusreceive_time']),$bonusreceive['bonusreceive_price']));
        }
        //获取未领取单个红包
        $condition = array();
        $condition[] = array('bonus_id','=',$bonus_id);
        $condition[] = array('member_id','=',0);
        $bonusreceive = $bonus_model->getOneBonusreceive($condition);
        if (empty($bonusreceive)) {
            ds_json_encode(10001, lang('bonus_send_over'));
        }

        try {
            Db::startTrans();

            $res=$bonus_model->receiveBonus($this->member_info,$bonus,$bonusreceive,lang('receive_bonus'));
            if(!$res['code']){
                throw new \think\Exception($res['msg'], 10006);
            }
            Db::commit();
            ds_json_encode(10000, sprintf(lang('get_bonus_info'),$bonusreceive['bonusreceive_price']));
        } catch (Exception $e) {
            Db::rollback();
            ds_json_encode(10001, $e->getMessage());
        }
    }

}
