<commit_before><?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Consumption extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        check_access_right('user', $this->session);
    }
    public function index()
    {
        $this->load->helper('url');
        $this->load->helper('form');
        $consumption_form_url = site_url(['consumption', 'input']);
        $consumer_validate_url = site_url(['validator', 'check_consumer_mobile']);
        $csrf = array(
            'name' => $this->security->get_csrf_token_name(),
            'hash' => $this->security->get_csrf_hash()
        );
        $csrf_cookie_name = $this->security->get_csrf_cookie_name();
        $view_data = [];
        $view_data['sms_url'] = site_url('sms/sent_sms_for_consumption');
        $view_data['consumption_form_url'] = $consumption_form_url;
        $view_data['consumer_validate_url'] = $consumer_validate_url;
        $view_data['csrf'] = $csrf;
        $view_data['csrf_cookie_name'] = $csrf_cookie_name;
        $this->load->view('layout/default_header');
        $this->load->view('consumption/index', $view_data);
        $this->load->view('layout/default_footer');
    }

    public function input()
    {
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');
        $config = array(
            /*array(
                'field' => 'consumer_name',
                'label' => '消费者会员名',
                'rules' => 'required|callback__check_consumer_name',
                'errors' => [
                                'required' => '消费者会员名不能为空',
                                'callback__check_consumer_name' => '找不到消费者会员'
                            ]
            ),*/
            array(
                'field' => 'mobile',
                'label' => '消费会员手机号码',
                'rules' => 'required|callback__check_consumer_mobile',
                'errors' => [
                    'required' => '消费会员手机号码不能为空',
                    'callback__check_consumer_name' => '找不到消费会员手机号吗'
                ]
            ),
            /*array(
                'field' => 'verify_code',
                'label' => '手机验证码',
                'rules' => 'required|callback__check_vcode|trim',
                'errors' => [
                    'required' => '%s 必填',
                    'callback__check_vcode' => '%s 无效',
                ],
            ),*/
            /*array(
                'field' => 'volume',
                'label' => '消费金额',
                'rules' => 'required|callback__is_money',
                'errors' => array(
                    'required' => '%s必填',
                    'callback__is_money' => '%s无效',
                ),
            ),*/
            [
                'field' => 'exchange_type',
                'label' => '交易媒介',
                'rules' => 'required|trim|is_natural|less_than[2]',
            ],
            array(
                'field' => 'title',
                'label' => '消费名称',
                'rules' => 'required',
                'errors' => [
                    'required' => '%s 必填',
                ],
            ),
        );

        $this->form_validation->set_rules($config);
        if($this->input->post('exchange_type') === '1')
            $this->form_validation->set_rules('score', '消费积分', 'required|trim|is_natural_no_zero|callback__is_score_enough');
        else
            $this->form_validation->set_rules('volume', '消费金额', 'required|trim|callback__is_money');
        if ($this->form_validation->run() == FALSE) {
            $this->load->helper('url');
            $consumption_form_url = site_url(['consumption', 'input']);
            $consumer_validate_url = site_url(['validator', 'check_consumer_mobile']);
            $csrf = array(
                'name' => $this->security->get_csrf_token_name(),
                'hash' => $this->security->get_csrf_hash()
            );
            $csrf_cookie_name = $this->security->get_csrf_cookie_name();
            $view_data = [];
            $view_data['sms_url'] = site_url('sms/sent_sms_for_consumption');
            $view_data['consumption_form_url'] = $consumption_form_url;
            $view_data['consumer_validate_url'] = $consumer_validate_url;
            $view_data['csrf'] = $csrf;
            $view_data['csrf_cookie_name'] = $csrf_cookie_name;
            $this->load->view('layout/default_header');
            $this->load->view($this->input->post('render_url'), $view_data);
            $this->load->view('layout/default_footer');
        } else {
            $this->load->model('consumption_model', 'Consumption_model');
            if($this->input->post('exchange_type') === '1'){
                $result = $this->Consumption_model->addScoreConsumptionLog($this->input->post());
                $arr = [
                    "title" => 'M网',
                    "volume" => $this->input->post('score')."分",
                    "store" => $this->session->userdata('display_name'),
                    "total" => $this->db->query("
                        select score from ".DB_PREFIX."user where mobile = '".$this->input->post('mobile') ."' limit 1
                        ")->result()[0]->score."分",
                ];
            } else {
                $result = $this->Consumption_model->addConsumptionLog($this->input->post());
                $arr = [
                    "title" => 'M网',
                    "volume" => $this->input->post('volume')."元",
                    "reward" => $this->input->post('volume')."分",
                    "store" => $this->session->userdata('display_name'),
                    "total" => $this->db->query("
                        select score from ".DB_PREFIX."user where mobile = '".$this->input->post('mobile') ."' limit 1
                        ")->result()[0]->score."分",
                ];
            }
            if($result === true) {
                $this->session->set_flashdata('flash_data', [ 'message' => '消费纪录录入成功', 'type' => 'success' ]);
                if($this->input->post('exchange_type') === '1')
                    sms_quick_send(2, $this->input->post('mobile'), $arr);
                else
                    sms_quick_send(3, $this->input->post('mobile'), $arr);
            }
            if($result === false)
                $this->session->set_flashdata('flash_data', [ 'message' => '消费纪录录入失败，请资讯平台管理员', 'type' => 'error' ]);
            redirect($this->input->post('render_url'));
        }
    }

    public function _check_consumer_name()
    {
        $this->load->database();
        $this->db->select('id');
        $query = $this->db->get_where('user', ['user_name' => $this->input->post('consumer_name')]);
        if($query->num_rows() > 0)
            return true;
        else
            return false;
    }

    public function _check_consumer_mobile()
    {
        $this->load->database();
        $this->db->select('id');<commit_msg>sms helper<commit_after><?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Consumption extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        check_access_right('user', $this->session);
    }
    public function index()
    {
        $this->load->helper('url');
        $this->load->helper('form');
        $consumption_form_url = site_url(['consumption', 'input']);
        $consumer_validate_url = site_url(['validator', 'check_consumer_mobile']);
        $csrf = array(
            'name' => $this->security->get_csrf_token_name(),
            'hash' => $this->security->get_csrf_hash()
        );
        $csrf_cookie_name = $this->security->get_csrf_cookie_name();
        $view_data = [];
        $view_data['sms_url'] = site_url('sms/sent_sms_for_consumption');
        $view_data['consumption_form_url'] = $consumption_form_url;
        $view_data['consumer_validate_url'] = $consumer_validate_url;
        $view_data['csrf'] = $csrf;
        $view_data['csrf_cookie_name'] = $csrf_cookie_name;
        $this->load->view('layout/default_header');
        $this->load->view('consumption/index', $view_data);
        $this->load->view('layout/default_footer');
    }

    public function input()
    {
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');
        $config = array(
            /*array(
                'field' => 'consumer_name',
                'label' => '消费者会员名',
                'rules' => 'required|callback__check_consumer_name',
                'errors' => [
                                'required' => '消费者会员名不能为空',
                                'callback__check_consumer_name' => '找不到消费者会员'
                            ]
            ),*/
            array(
                'field' => 'mobile',
                'label' => '消费会员手机号码',
                'rules' => 'required|callback__check_consumer_mobile',
                'errors' => [
                    'required' => '消费会员手机号码不能为空',
                    'callback__check_consumer_name' => '找不到消费会员手机号吗'
                ]
            ),
            /*array(
                'field' => 'verify_code',
                'label' => '手机验证码',
                'rules' => 'required|callback__check_vcode|trim',
                'errors' => [
                    'required' => '%s 必填',
                    'callback__check_vcode' => '%s 无效',
                ],
            ),*/
            /*array(
                'field' => 'volume',
                'label' => '消费金额',
                'rules' => 'required|callback__is_money',
                'errors' => array(
                    'required' => '%s必填',
                    'callback__is_money' => '%s无效',
                ),
            ),*/
            [
                'field' => 'exchange_type',
                'label' => '交易媒介',
                'rules' => 'required|trim|is_natural|less_than[2]',
            ],
            array(
                'field' => 'title',
                'label' => '消费名称',
                'rules' => 'required',
                'errors' => [
                    'required' => '%s 必填',
                ],
            ),
        );

        $this->form_validation->set_rules($config);
        if($this->input->post('exchange_type') === '1')
            $this->form_validation->set_rules('score', '消费积分', 'required|trim|is_natural_no_zero|callback__is_score_enough');
        else
            $this->form_validation->set_rules('volume', '消费金额', 'required|trim|callback__is_money');
        if ($this->form_validation->run() == FALSE) {
            $this->load->helper('url');
            $consumption_form_url = site_url(['consumption', 'input']);
            $consumer_validate_url = site_url(['validator', 'check_consumer_mobile']);
            $csrf = array(
                'name' => $this->security->get_csrf_token_name(),
                'hash' => $this->security->get_csrf_hash()
            );
            $csrf_cookie_name = $this->security->get_csrf_cookie_name();
            $view_data = [];
            $view_data['sms_url'] = site_url('sms/sent_sms_for_consumption');
            $view_data['consumption_form_url'] = $consumption_form_url;
            $view_data['consumer_validate_url'] = $consumer_validate_url;
            $view_data['csrf'] = $csrf;
            $view_data['csrf_cookie_name'] = $csrf_cookie_name;
            $this->load->view('layout/default_header');
            $this->load->view($this->input->post('render_url'), $view_data);
            $this->load->view('layout/default_footer');
        } else {
            $this->load->model('consumption_model', 'Consumption_model');
            if($this->input->post('exchange_type') === '1'){
                $result = $this->Consumption_model->addScoreConsumptionLog($this->input->post());
                $arr = [
                    "title" => 'M网',
                    "volume" => $this->input->post('score')."分",
                    "store" => $this->session->userdata('display_name'),
                    "total" => $this->db->query("
                        select score from ".DB_PREFIX."user where mobile = '".$this->input->post('mobile') ."' limit 1
                        ")->result()[0]->score."分",
                ];
            } else {
                $result = $this->Consumption_model->addConsumptionLog($this->input->post());
                $arr = [
                    "title" => 'M网',
                    "volume" => $this->input->post('volume')."元",
                    "reward" => $this->input->post('volume')."分",
                    "store" => $this->session->userdata('display_name'),
                    "total" => $this->db->query("
                        select score from ".DB_PREFIX."user where mobile = '".$this->input->post('mobile') ."' limit 1
                        ")->result()[0]->score."分",
                ];
            }
            $this->load->helper('sms');
            if($result === true) {
                $this->session->set_flashdata('flash_data', [ 'message' => '消费纪录录入成功', 'type' => 'success' ]);
                if($this->input->post('exchange_type') === '1')
                    sms_quick_send(2, $this->input->post('mobile'), $arr);
                else
                    sms_quick_send(3, $this->input->post('mobile'), $arr);
            }
            if($result === false)
                $this->session->set_flashdata('flash_data', [ 'message' => '消费纪录录入失败，请资讯平台管理员', 'type' => 'error' ]);
            redirect($this->input->post('render_url'));
        }
    }

    public function _check_consumer_name()
    {
        $this->load->database();
        $this->db->select('id');
        $query = $this->db->get_where('user', ['user_name' => $this->input->post('consumer_name')]);
        if($query->num_rows() > 0)
            return true;
        else
            return false;
    }

    public function _check_consumer_mobile()
    {
        $this->load->database();
        $this->db->select('id');
{'fraction_non_alphanumeric': 0.17303162252837462, 'fraction_numerical': 0.0015704190163466343, 'mean_word_length': 1.7497546614327772, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 319, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '658202', 'n_tokens_mistral': 4243, 'n_tokens_neox': 4192, 'n_words': 533}