<?php
namespace app\app\model;

use think\Model;

/**
 * 短信表
 *
 * @author wj
 * @date 2022-07-25
 */
class smscoderecordmodel extends Model
{
    protected $table = "t_smscoderecord";
    public function insertData($data)
    {
        if (!isset($data['create_date']) || empty($data['create_date']) || !is_string($data['create_date'])) {
            $data['create_date'] = date("Y-m-d H:i:s");
        }
        if (isset($data['id'])) {
            unset($data['id']);
        }
        $data = $this->formatData($data);
        if (empty($data)) {
            return false;
        }
        $id = $this->insertGetId($data);
        return $id ? $id : false;
    }

    /**
     * 校验入库数据
     * 20220119
     */
    private function formatData($data)
    {
        $useData = [];
        $fields = $this->getTableFields();
        foreach ($data as $key => $value) {
            if (in_array($key, $fields)) {
                $useData[$key] = $value;
            }
        }
        return $useData;
    }
    public function getInfo($where, $field = "*", $row = true)
    {
        $info = $this->field($field)->where($where);
        if ($row) {
            $info = $info->find();
        } else {
            $info = $info->select();
        }
        return empty($info) ? false : $info;
    }

    public function updateinfo($where, $updateData)
    {
        $row = $this->where($where)->update($updateData);
        return empty($row) ? false : $row;
    }

    public function deleteinfo($where)
    {
        $row = $this->where($where)->delete();
        return empty($row) ? false : $row;
    }

    public function getList($where = [], $field = "*", $page = 1, $size = 10, $order = "id desc", $group = "", $row = false)
    {
        $sqlObj = $this->where($where);
        if ("count" != $field) {
            $sqlObj = $sqlObj->field($field)->order($order)->group($group)->page($page, $size);
            if ($row) {
                $data = $sqlObj->find();
            } else {
                $data = $sqlObj->select();
            }
        } else {
            $data = $sqlObj = $sqlObj->count();
        }
        return $data;
    }
    /**
     * 校验验证码
     *
     * @param  [type] $telno
     * @param  [type] $code
     * @return void
     * @author wj
     * @date 2022-07-25
     */
    public function checkcode($telno, $code)
    {
        $where = ['telno' => $telno];
        $field = ['smscode', 'id'];
        $id = 0;
        $list = $this->where($where)->field($field)->select()->toArray();
        if (empty($list)) {
            return backarr(0, "无对应验证码");
        }
        foreach ($list as $key => $value) {
            if ($code == $value['smscode']) {
                $id = $value['id'];
                break;
            }
        }
        if (empty($id)) {
            return backarr(0, "验证码错误");
        }
        $row = $this->where($where)->delete();
        if (empty($row)) {
            return backarr(0, "验证码删除失败");
        }
        return backarr(1, "校验成功");
    }
}
