<?php

namespace app\common\service;

use app\common\model\UserIntegral as UserIntegralModel;
use app\common\model\UserParam as UserParamModel;
use app\common\model\User as UserModel;

class IntegralService extends BaseService
{

    const REGISTER      = 0;
    const IMPROVERESUME = 1;
    const CERTIFICATION = 2;
    const ENTRY         = 3;
    const SINGIN        = 4;
    const SHAREJOB      = 5;

    private $_type = [
        ['field' => 'register', 'title' => '新用户注册奖励', 'remark' => '邀请新用户注册奖励积分'],
        ['field' => 'improveresume', 'title' => '首次完善简历奖励', 'remark' => '首次完善简历奖励积分'],
        ['field' => 'certification', 'title' => '首次实名认证通过奖励', 'remark' => '首次实名认证通过奖励积分'],
        ['field' => 'entry', 'title' => '对接成功奖励', 'remark' => '对接成功并入职奖励积分'],
        ['field' => 'signin', 'title' => '签到奖励', 'remark' => '对签到奖励积分'],
        ['field' => 'sharejob', 'title' => '每日分享岗位奖励', 'remark' => '每日分享岗位奖励积分'],
    ];

    public function add($user_id, $type_index, $itype = 1)
    {
        $type     = $this->_type[$type_index];
        $param    = UserParamModel::where(1)->findOrEmpty();
        $intvalue = $param[$type['field']];
        if (empty($intvalue)) {
            return false;
        }
        $integral = new UserIntegralModel();
        $integral->save([
            'userid'      => $user_id,
            'title'       => $type['title'],
            'intvalue'    => $intvalue,
            'intmoney'    => 0.00,
            'onlycontent' => "",
            'remark'      => $type['remark'],
            'itype'       => $itype,
            'createtime'  => time(),
            'yeartime'    => date("Y"),
            'monthtime'   => date("Ym"),
        ]);

        $user           = UserModel::where('id', $user_id)->find();
        $user->integral += $intvalue;
        $user->inttotal += $intvalue;
        $user->save();
    }
}