<?php

namespace app\admin\controller\user;

use app\admin\controller\BaseController;
use app\admin\server\user\UsersServer;
use PhpOffice\PhpSpreadsheet\Helper\Sample;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;

/**
 * 会员
 * Class Users
 * @package app\admin\controller\user
 */
class Users extends BaseController
{
    /**
     * Notes:获取用户信息
     * @return \support\Response
     * User: yym
     * Date: 2022/7/26
     */
    public function getUserInfo()
    {
        $user_id = $this->request->get('user_id');
        $this->validateCheck('user\UserValidate', ['user_id' => $user_id], 'info');
        return json_success(UsersServer::getUserInfo($user_id), '成功');
    }

    /**
     * Notes:会员列表
     * @return \support\Response
     * User: yym
     * Date: 2022/8/4
     */
    public function getUserList()
    {
        $keywords = (string)$this->request->get('keywords', '');//搜索内容
        $category = (int)$this->request->get('category', '');//会员类型
        $status   = (int)$this->request->get('status', '');//会员状态
        $pid      = (int)$this->request->get('pid', 0);//上级id
        $user_create_time = $this->request->get('user_create_time','');//创建时间
        [$page, $limit] = $this->getPage();
        return json_success(UsersServer::getUserList($keywords, $category, $status, $pid, $page, $limit, $user_create_time), '获取成功');
    }

    /**
     * Notes:营销活动排行
     * @return \support\Response
     * User: ycp
     * Date: 2023/3/1
     */
    public function getRankList()
    {
        $act      = (int)$this->request->get('act', 0);//活动id
        $s_time   = (string)$this->request->get('s_time', '');//开始时间
        $e_time   = (string)$this->request->get('e_time', '');//开始时间
        [$page, $limit] = $this->getPage();
        return json_success(UsersServer::getRankList($act,$s_time,$e_time), '获取成功');
    }

    /**
     * Notes:营销拉新列表
     * @return \support\Response
     * User: ycp
     * Date: 2023/3/1
     */
    public function getActLog()
    {
        $act      = (int)$this->request->get('act', 0);//活动id
        [$page, $limit] = $this->getPage();
        return json_success(UsersServer::getActLog($act,$page,$limit), '获取成功');
    }

    /**
     * Notes:添加会员
     * @return \support\Response
     * User: ZQ
     * Date: 2022/11/21
     */
    public function userInsert()
    {
        $user_account = $this->request->post('user_account');
        $user_login_pwd = $this->request->post('user_login_pwd');
        $user_again_pwd = $this->request->post('user_again_pwd');
        $user_nickname = $this->request->post('user_nickname','');
        $user_headimgurl = $this->request->post('user_headimgurl','');
        $user_sex = $this->request->post('user_sex');
        $user_birthday = $this->request->post('user_birthday');
        $user_age = $this->request->post('user_age','');
        $user_register_address = $this->request->post('user_register_address','');
        $user_phone = $this->request->post('user_phone','');
        $user_status = $this->request->post('user_status');
        $user_real_name = $this->request->post('user_real_name','');
        $user_card_id = $this->request->post('user_card_id','');
        $user_certinfo = $this->request->post('user_certinfo','');
        $user_is_owner = $this->request->post('user_is_owner');
        $user_type = $this->request->post('user_type','');
        $user_spread_uid = $this->request->post('user_spread_uid','');
        $user_source = $this->request->post('user_source');
        $user_register_type = 1;
        $admin_id = $this->request->admin_id;
        $this->validateCheck('user\UserValidate', ['user_account'=>$user_account,'user_login_pwd'=>$user_login_pwd,'user_again_pwd'=>$user_again_pwd,'user_sex'=>$user_sex,'user_birthday'=>$user_birthday,'user_status'=>$user_status,'user_is_owner'=>$user_is_owner,'user_type'=>$user_type,'user_source'=>$user_source], 'create');
        if ($user_type == 1){
            $this->validateCheck('user\UserValidate',['user_real_name'=>$user_real_name,'user_card_id'=>$user_card_id,'user_certinfo'=>$user_certinfo],'userAuth');
        }
        return json_success(UsersServer::userInsert($user_account,$user_login_pwd,$user_again_pwd,$user_nickname,$user_headimgurl,$user_sex,$user_birthday,$user_age,$user_register_address,$user_phone,$user_status,$user_real_name,$user_card_id,$user_certinfo,$user_is_owner,$user_type,$user_spread_uid,$user_source,$admin_id,$user_register_type), '添加成功');

    }

    /**
     * Notes:会员认证审核
     * @return \support\Response
     * User: yym
     * Date: 2022/8/4
     */
    public function authUser()
    {
        $user_id  = $this->request->post('user_id');//会员编号
        $status   = $this->request->post('status');//审核状态 0=待审核 1=审核通过 2=审核驳回 3=主动撤销
        $content  = $this->request->post('content', '');//审核意见
        $category = $this->request->post('category');//分类 实名认证、会员升级
        $admin_id = $this->request->admin_id;
        $this->validateCheck('user\UserValidate', ['user_id' => $user_id, 'status' => $status, 'content' => $content, 'category' => $category], 'auth');
        UsersServer::authUser($admin_id, $user_id, $status, $content, $category);
        return json_success([], '操作成功');
    }

    /**
     * Notes:获取会员体检信息
     * @return \support\Response
     * @throws \Exception
     * User: yym
     * Date: 2022/9/21
     */
    public function getUserReport()
    {
        $user_id = $this->request->get('user_id');//会员编号
        $this->validateCheck('user\UserValidate', ['user_id' => $user_id], 'report');
        return json_success(UsersServer::getUserReport($user_id), '获取成功');
    }

    /**
     * Notes:获取会员设备列表
     * @return \support\Response
     * @throws \Exception
     * User: ZQ
     * Date: 2022/9/22
     */
    public function getUserEquipment()
    {
        [$page, $limit] = $this->getPage();
        $equipment_user_id = $this->request->get('user_id');
        $equipment_class = $this->request->get('equipment_class');
        $equipment_name = $this->request->get('equipment_name');
        $equipment_tripartite_sn = $this->request->get('equipment_tripartite_sn');
        $equipment_create_time = $this->request->get('equipment_create_time');
        return json_success(UsersServer::getUserEquipment($page,$limit,$equipment_user_id,$equipment_name,$equipment_class,$equipment_tripartite_sn,$equipment_create_time), '获取成功');
    }

    /**
     * Notes:获取会员设备详情
     * @return \support\Response
     * @throws \Exception
     * User: ZQ
     * Date: 2022/9/22
     */
    public function userEquipmentInfo()
    {
        $equipment_id = $this->request->get('equipment_id');
        $this->validateCheck('intelligence\UserValidate', ['equipment_id' => $equipment_id], 'info');
        return json_success(UsersServer::userEquipmentInfo($equipment_id), '获取成功');
    }

    /**
     * Notes:获取会员下的智能产品设备列表
     * @return \support\Response
     * @throws \Exception
     * User: ZQ
     * Date: 2022/11/15
     */
    public function equipmentList()
    {
        $equipment_user_id = $this->request->get('equipment_user_id');
        return json_success(UsersServer::equipmentList($equipment_user_id), '获取成功');
    }

    /**
     * Notes:添加会员智能产品设备
     * @return \support\Response
     * User: ZQ
     * Date: 2022/11/14
     */
    public function userEquipmentInsert()
    {
        $equipment_intelligence_id  = $this->request->post('equipment_intelligence_id');
        $equipment_user_id   = $this->request->post('equipment_user_id');
        $equipment_name  = $this->request->post('equipment_name', '');
        $equipment_only = $this->request->post('equipment_only');
        $equipment_type = $this->request->post('equipment_type');
        $equipment_class = $this->request->post('equipment_class');
        $equipment_status = $this->request->post('equipment_status');
        $equipment_tripartite_sn = $this->request->post('equipment_tripartite_sn');
        $equipment_property_id = $this->request->post('equipment_property_id', '');
        $admin_id = $this->request->admin_id;
        //查询智能设备唯一编号equipment_only是否重复
        $check = UsersServer::equipmentCheck($equipment_only);
        if (!empty($check)){
            throw new \Exception('智能设备唯一编号已存在！');
        }
        $result = UsersServer::insertEquipment($admin_id, $equipment_intelligence_id, $equipment_user_id, $equipment_name, $equipment_only,$equipment_type,$equipment_class,$equipment_tripartite_sn,$equipment_status,$equipment_property_id);
        return json_success($result, '添加成功');
    }

    /**
     * Notes:删除会员智能产品设备
     * @return \support\Response
     * User: ZQ
     * Date: 2022/11/14
     */
    public function userEquipmentDelete()
    {
        $equipment_id = $this->request->get('equipment_id');
        $admin_id = $this->request->admin_id;
        $this->validateCheck('user\UserValidate', ['equipment_id' => $equipment_id], 'equipmentDel');
        return json_success(UsersServer::equipmentDel($equipment_id,$admin_id), '删除成功');
    }

    /**
     * Notes:获取会员消息列表
     * @return \support\Response
     * @throws \Exception
     * User: ZQ
     * Date: 2022/9/22
     */
    public function userEquipmentLog()
    {
        [$page, $limit] = $this->getPage();
        $log_user_id = $this->request->get('log_user_id');
        $log_equipment_id = $this->request->get('log_equipment_id');
        return json_success(UsersServer::getEquipmentLog($page,$limit,$log_user_id,$log_equipment_id), '获取成功');
    }

    /**
     * Notes:获取会员消息详情
     * @return \support\Response
     * @throws \Exception
     * User: ZQ
     * Date: 2022/9/22
     */
    public function equipmentLogInfo()
    {
        $log_id = $this->request->get('log_id');
        $this->validateCheck('intelligence\LogValidate', ['log_id' => $log_id], 'info');
        return json_success(UsersServer::equipmentLogInfo($log_id), '获取成功');
    }

    /**
     * Notes:赠送会员权益包
     * @return \support\Response
     * User: yym
     * Date: 2022/11/21
     */
    public function givePackage()
    {
        $user_id    = $this->request->post('user_id');//会员编号
        $package_id = $this->request->post('package_id');//套餐包编号
        $type       = $this->request->post('type');//
        $admin_id   = $this->request->admin_id;
        $this->validateCheck('package\PackageValidate', ['package_id' => $package_id, 'user_id' => $user_id, 'type' => $type], 'give');
        UsersServer::givePackage($user_id, $package_id, $type, $admin_id);
        return json_success([], '赠送成功');
    }
    /**
     * Notes:获取会员下的亲属关系列表
     * @return \support\Response
     * @throws \Exception
     * User: ZQ
     * Date: 2022/9/22
     */
    public function relativesList()
    {
        $relatives_user_id = $this->request->get('relatives_user_id');
        return json_success(UsersServer::relativesList($relatives_user_id), '获取成功');
    }

    /**
     * Notes:获取会员亲属关系列表
     * @return \support\Response
     * @throws \Exception
     * User: ZQ
     * Date: 2022/9/22
     */
    public function relativesToList()
    {
        [$page, $limit] = $this->getPage();
        $keywords = (string)$this->request->get('keywords', '');//搜索内容
        return json_success(UsersServer::relativesToList($page,$limit,$keywords), '获取成功');
    }

    /**
     * Notes:删除会员亲属关系
     * @return \support\Response
     * @throws \Exception
     * User: ZQ
     * Date: 2022/9/22
     */
    public function relativesDel()
    {
        $relatives_id = $this->request->get('relatives_id');
        $admin_id = $this->request->admin_id;
        $this->validateCheck('user\UserValidate', ['relatives_id' => $relatives_id], 'relatives');
        return json_success(UsersServer::relativesDel($relatives_id,$admin_id), '删除成功');
    }

    /**
     * Notes:获取会员固定资产列表
     * @return \support\Response
     * @throws \Exception
     * User: ZQ
     * Date: 2022/11/23
     */
    public function propertyList()
    {
        $property_user_id = $this->request->get('property_user_id');//会员id
        $this->validateCheck('user\UserValidate', ['user_id' => $property_user_id], 'info');
        return json_success(UsersServer::propertyList($property_user_id), '获取成功');
    }

    /**
     * Notes:会员导入功能
     * @return \support\Response
     * @throws \Exception
     * User: ZQ
     * Date: 2022/12/1
     */
    public function userExcelImport()
    {
        $excel_address = $this->request->get('excel_address');//文件地址;
        //$excel_address = iconv("utf-8", "gb2312", $excel_address);
        $admin_id = $this->request->admin_id;
        require_once 'vendor/phpoffice/phpexcel/Classes/PHPExcel.php';
        require_once 'vendor/phpoffice/phpexcel/Classes/PHPExcel/IOFactory.php';
        $objPHPExcel = \PHPExcel_IOFactory::load($excel_address);
        // 逐行读取 sheet 内的内容（常用）
        foreach ($objPHPExcel->getworksheetiterator() as $sheet){   //循环sheet
            foreach ($sheet->getrowiterator() as $row){     //循环row
                if ($row->getrowindex() < 2){  // 默认从第二行开始
                    continue;
                }
                foreach ($row->getcelliterator() as $cell){     //循环cell
                    $data[] = $cell->getvalue();

                }
                if(!empty($data)){
                    $user_account = $data[0];
                    $user_login_pwd = $data[1];
                    $user_again_pwd = $data[1];
                    $user_nickname = $data[2];
                    $user_headimgurl = '';
                    if ($data[3] == '男'){
                        $user_sex = 0;
                    }else{
                        $user_sex = 1;
                    }
                    $user_birthday = $data[4];
                    $user_age = $data[5];
                    $user_register_address = $data[6];
                    $user_phone = $data[7];
                    $user_status = 0;
                    $user_real_name = $data[8];
                    $user_card_id = $data[9];
                    $user_certinfo = ['http://op.wanyuewellness.cn/idcardUp.jpg','http://op.wanyuewellness.cn/idcardDown.jpg'];
                    /*if ($data[10] == "否"){
                        $user_is_owner = 0;
                    }elseif ($data[10] == "准业主"){
                        $user_is_owner = 1;
                    }elseif ($data[10] == "业主"){
                        $user_is_owner = 2;
                    }*/
                    $user_is_owner = 0;
                    if (!empty($user_real_name) && !empty($user_card_id)){
                        $user_type = 1;
                    }else{
                        $user_type = 0;
                    }
                    $user_spread_uid = '';
                    $user_source = $data[10];
                    $user_register_type = 2;

                    //var_dump($data);
                    unset($data);
                    UsersServer::userInsert($user_account,$user_login_pwd,$user_again_pwd,$user_nickname,$user_headimgurl,$user_sex,$user_birthday,$user_age,$user_register_address,$user_phone,$user_status,$user_real_name,$user_card_id,$user_certinfo,$user_is_owner,$user_type,$user_spread_uid,$user_source,$admin_id,$user_register_type);
                }else{
                    throw new \Exception('读取失败');
                }
            }

        }

        return json_success('', '导入成功');
    }

    /**
     * Notes:添加医生医嘱
     * @return \support\Response
     * @throws \Exception
     * User: ZQ
     * Date: 2022/12/8
     */
    public function userDoctorLog()
    {
        $log_user_id = $this->request->post('log_user_id');
        $log_type = $this->request->post('log_type');
        $log_equipment_id = $this->request->post('log_equipment_id');
        $log_doctor_id = $this->request->post('log_doctor_id');
        $log_subject_id = $this->request->post('log_subject_id');
        $log_message = $this->request->post('log_message','');
        $log_resolvent = $this->request->post('log_resolvent','');
        $admin_id = $this->request->admin_id;
        $this->validateCheck('user\UserValidate', ['log_user_id' => $log_user_id, 'log_type' => $log_type, 'log_doctor_id' => $log_doctor_id, 'log_subject_id' => $log_subject_id], 'userDocter');
        $data = UsersServer::doctorInsert($log_user_id, $log_type, $log_equipment_id, $log_doctor_id, $log_subject_id, $log_message, $log_resolvent, $admin_id);
        return json_success($data, '添加成功');
    }

    /**
     * Notes:会员医生医嘱列表
     * @return \support\Response
     * @throws \Exception
     * User: ZQ
     * Date: 2022/12/8
     */
    public function userDoctorList()
    {
        $log_user_id = $this->request->get('log_user_id');
        [$page, $limit] = $this->getPage();
        $this->validateCheck('user\UserValidate', ['log_user_id' => $log_user_id], 'DocterList');
        return json_success(UsersServer::doctorList($log_user_id, $page, $limit), '成功');
    }

    /**
     * Notes:删除会员医生医嘱
     * @return \support\Response
     * @throws \Exception
     * User: ZQ
     * Date: 2022/12/8
     */
    public function userDoctorDel()
    {
        $log_user_id = $this->request->get('log_user_id');
        $log_id = $this->request->get('log_id');
        $admin_id = $this->request->admin_id;
        $this->validateCheck('user\UserValidate', ['log_user_id' => $log_user_id, 'log_id' => $log_id], 'DocterDel');
        return json_success(UsersServer::doctorDel($log_user_id, $log_id, $admin_id), '删除成功');
    }

    /**
     * Notes:修改会员健康基础数据
     * @return \support\Response
     * @throws \Exception
     * User: ZQ
     * Date: 2022/12/8
     */
    public function userHealthyDataUpdate()
    {
        $data_user_id = $this->request->post('data_user_id');
        $data_Inspection_suggestions = $this->request->post('data_Inspection_suggestions','');
        $data_bone_density = $this->request->post('data_bone_density','');
        $data_tcm_testing = $this->request->post('data_tcm_testing','');
        $admin_id = $this->request->admin_id;
        $this->validateCheck('user\UserValidate', ['data_user_id' => $data_user_id], 'healthyDel');
        return json_success(UsersServer::healthyUpdate($data_user_id, $data_Inspection_suggestions, $data_bone_density, $data_tcm_testing, $admin_id), '修改成功');
    }

    /**
     * Notes:删除会员健康基础数据
     * @return \support\Response
     * @throws \Exception
     * User: ZQ
     * Date: 2022/12/8
     */
    public function userHealthyDataDel()
    {
        $data_user_id = $this->request->get('data_user_id');
        $admin_id = $this->request->admin_id;
        $this->validateCheck('user\UserValidate', ['data_user_id' => $data_user_id], 'healthyDel');
        return json_success(UsersServer::healthyDel($data_user_id, $admin_id), '删除成功');
    }

    /**
     * Notes:查询会员健康基础数据
     * @return \support\Response
     * @throws \Exception
     * User: ZQ
     * Date: 2022/12/8
     */
    public function userHealthyData()
    {
        $data_user_id = $this->request->get('data_user_id');
        $this->validateCheck('user\UserValidate', ['data_user_id' => $data_user_id], 'healthyDel');
        return json_success(UsersServer::healthyInfo($data_user_id), '成功');
    }

}
