<?php
declare (strict_types = 1);

namespace app\model\api;

use app\model\api\ServiceType as ServiceTypeModel;
use app\model\api\ServiceLabel as ServiceLabelModel;
use app\model\api\User as UserModel;

use think\Model;

/**
 * @mixin \think\Model
 */
class InfoAudit extends Model
{
    
    /**
     * 获取列表
     * @param type $post
     * @param type $is_admin
     */
    public function getList($post,$is_admin=0){
        $post["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
        $post["page"]     = $post["page"]<=0     ? 1  : (int)$post["page"];
        $where=[];
        if(is_numeric($post['status'])) {
            $where[]=["au.status","=",(int)$post['status']];
        }
        if(is_numeric($post['uid']) && $post['uid']>0) {
            $where[]=["au.uid","=",(int)$post['uid']];
        }else if(is_mobile($post['user_mobile'])){
            $udata = (new UserModel)->where("mobile",$post['user_mobile'])->find();
            if(!empty($udata)){
                $where[]=["au.uid","=",(int)$udata['uid']];
            }
        }
        if(is_mobile($post['mobile'])) {
            $where[]=["au.mobile","=",$post['mobile']];
        }
        $startTime = "";
        $endTime = "";
        if(!empty($post['time'][0]) && !empty($post['time'][1])) {
           $startTime =  strtotime($post['time'][0]);
           $endTime = strtotime($post['time'][1]);
           $where[]=["au.time","between","{$startTime},{$endTime}"];
        }
        if(!empty($post['keyword'])){
            $where[]=["au.name","=",$post['keyword']];
        }
        $pageCount = $this->alias("au")->where($where)->count();
        $data = null;
        if($pageCount>0){
            $userField = $is_admin==1 ? ",u.mobile as user_mobile,u.nickname as user_nickname,u.avatar as user_avatar,a.name as admin_name" : "";
            $data = (new InfoAudit)
                ->alias("au")
                ->field("au.*,wt.title as user_work_type_title,s.title as service_time_type_title{$userField}")
                ->leftJoin("user u"              , "u.uid  = au.uid")//用户信息
                ->leftJoin("user_work_type wt"   , "wt.id  = au.user_work_type_id")//职称
                ->leftJoin("service_time_type s" , "s.code = au.service_type")//服务时长类型
                ->leftJoin("admin a"             , "a.id   = au.admin_id")//管理员
                ->where($where)
                ->order("au.id","desc")
                ->page((int)$post["page"], $post["pageSize"])
                ->select()
                ->toArray();
        }
        $data = empty($data) ? [] : $data;
        $serviceTypeModel = new ServiceTypeModel();
        foreach($data as $k=>$v){
            $data[$k]["birthday"]           = empty($data[$k]["birthday"])  ? "" : date("Y-m-d",$data[$k]["birthday"]);//生日
            $data[$k]["time"]               = date("Y-m-d H:i:s",$data[$k]["time"]);//添加时间
            $data[$k]["service_audit_imgs"] = getImageAr($data[$k]["service_audit_imgs"]);//我的证书
            $data[$k]["service_intro_imgs"] = getImageAr($data[$k]["service_intro_imgs"]);//我的介绍图片
            $data[$k]["service_imgs"]       = getImageAr($data[$k]["service_imgs"]);//我的服务展示照片
            //服务区域
            $data[$k]["service_area"]       = getImageAr($data[$k]["service_area"]);
            $data[$k]["service_area_all"]   = [];
            foreach($data[$k]["service_area"] as $k2=>$v2){
                $data[$k]["service_area_all"][]=getAreaItemAr($v2);
            }
            //服务内容
            $data[$k]["service_project"]    = getImageAr($data[$k]["service_project"]);//我的服务内容
            $data[$k]["service_project_ar"] = [];
            if(!empty($data[$k]["service_project"])){
                $data[$k]["service_project_ar"] = (new ServiceTypeModel())->where("id","in",$data[$k]["service_project"])->select();
            }
            //介绍标签
            $data[$k]["service_label"]       = getImageAr($data[$k]["service_label"]);
            $data[$k]["service_label_ar"]    = (new ServiceLabelModel)->getColumnList("id", "title", $data[$k]["service_label"]);
            
            $data[$k]["admin_time"] = empty($data[$k]["admin_time"])?"":date("Y-m-d H:i:s",$data[$k]["admin_time"]);
        }
        return ["list" => $data, "pageSize" => $post["pageSize"],"page"=>$post["page"],"totalCount"=>$pageCount];
    }
    /**
     * 
     * @param type $id
     * @param type $type
     */
    public function getItem($post,$is_admin=0){
        if(empty($post)){
            return [];
        }
        $where=[];
        if(isset($post["status"]) && in_array((string)$post["status"], ["0","1","-1"])){
            $where[]=["au.status","=",(int)$post["status"]];
        }
        if(!empty($post["id"])){
            $where[]=["au.id","=",$post["id"]];
        }
        if(!empty($post["uid"])){
            $where[]=["au.uid","=",$post["uid"]];
        }
        //后台
        $userField = $is_admin==1 ? ",u.mobile as user_mobile,u.nickname as user_nickname,u.avatar as user_avatar,a.name as admin_name" : "";
        $data = $this
                ->alias("au")
                ->field("au.*,wt.title as user_work_type_title,s.title as service_time_type_title{$userField}")
                ->leftJoin("user u"              ,"u.uid = au.uid")//用户信息
                ->leftJoin("user_work_type wt"   , "wt.id = au.user_work_type_id")//职称
                ->leftJoin("service_time_type s" , "s.code = au.service_type")//服务时长类型
                ->leftJoin("admin a"             , "a.id = au.admin_id")//管理员
                ->where($where)
                ->order("au.id","desc")
                ->find();
        if(empty($data)){
            return [];
        }
        $data = $data->toArray();
        $serviceTypeModel = new ServiceTypeModel();
        $data["age"]                = empty($data["birthday"])  ? "" : date("Y") - date("Y",$data["birthday"]);
        $data["birthday"]           = empty($data["birthday"])  ? "" : date("Y-m-d",$data["birthday"]);//生日
        
        $data["time"]               = date("Y-m-d H:i:s",$data["time"]);//添加时间
        $data["service_audit_imgs"] = getImageAr($data["service_audit_imgs"]);//我的证书
        $data["service_intro_imgs"] = getImageAr($data["service_intro_imgs"]);//我的介绍图片
        $data["service_imgs"]       = getImageAr($data["service_imgs"]);//我的服务展示照片
        //服务区域
        $data["service_area"]       = getImageAr($data["service_area"]);
        $data["service_area_all"]   = [];
        foreach($data["service_area"] as $k2=>$v2){
            $data["service_area_all"][]=getAreaItemAr($v2);
        }
        //服务内容
        $data["service_project"]    = getImageAr($data["service_project"]);//我的服务内容
        $data["service_project_ar"] = [];
        if(!empty($data["service_project"])){
            $data["service_project_ar"] = (new ServiceTypeModel())->where("id","in",$data["service_project"])->select();
        }
        $data["service_label"]       = getImageAr($data["service_label"]);
        $data["service_label_ar"]    = (new ServiceLabelModel)->getColumnList("id", "title", $data["service_label"]);
        
        if($is_admin==0){
            unset($data["admin_id"]);
            unset($data["admin_mono"]);
            unset($data["admin_time"]);
            unset($data["uid"]);
            unset($data["service_price"]);
            unset($data["service_area"]);
            unset($data["service_project"]);
        }else{
            $data["admin_time"] = empty($data["admin_time"])?"":date("Y-m-d H:i:s",$data["admin_time"]);
        }
        return $data;
        
    }
    
    
}
