<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------


namespace app\common\logic;


use app\common\enum\PayEnum;
use app\common\enum\RefundEnum;
use app\common\model\auth\Admin;
use app\common\model\equity\EquityConfig;
use app\common\model\external\ExternalPlatform;
use app\common\model\jobs_rules\JobsRules;
use app\common\model\labels\Labels;
use app\common\model\property\PropertyHead;
use app\common\model\recharge\RechargeOrder;
use app\common\model\refund\RefundLog;
use app\common\model\refund\RefundRecord;
use app\common\model\sale\Sale;
use app\common\model\sale\SaleGroup;
use app\common\model\setting\PostageRegion;
use app\common\model\tenant\Tenant;
use app\common\model\shops\ShopGoods;
use app\common\model\training\TrainingCourse;
use app\common\model\training\TrainingQuestions;
use app\common\model\training\TrainingTask;
use app\common\service\pay\AliPayService;
use app\common\service\pay\WeChatPayService;


/**
 * 查询选项类数据
 * Class TableDataLogic
 * @package app\common\logic
 */
class TableDataLogic extends BaseLogic
{


    /**
     * @notes 商品标签列表
     */
    public static function labels()
    {
        return Labels::where('id','>',0)->where('label_type',1)->field('id,label_name as name,id as value,"data_table_labels" as type_value')->select()->toArray();
    }
    public static function allLabels()
    {
        return Labels::where('id','>',0)->field('id,label_name as name,id as value,"data_table_allLabels" as type_value')->select()->toArray();
    }

    /**
     * @notes 销售列表
     */
    public static function sale()
    {
        return Sale::where('id','>',0)->field('id,sale_name as name,id as value,"data_table_sale" as type_value')->select()->toArray();
    }
    /**
     * @notes 销售组列表
     */
    public static function saleGroup()
    {
        return SaleGroup::where('id','>',0)->field('id,sale_name as name,id as value,"data_table_saleGroup" as type_value')->select()->toArray();
    }

    public static function propertyHead()
    {
        return PropertyHead::where('id','>',0)->field('id,village_name as name,id as value,"data_table_propertyHead" as type_value')->select()->toArray();
    }
    /**
     * @notes 工程师标签列表
     */
    public static function masterWorkerLabels()
    {
        // 缓存优化
        $data = cache('labelMasterWorkerLabels');
        if(empty($data)){
            $lists = Labels::where('id','>',0)->where('label_type',2)->field('id,pid,label_name as name,id as value,"data_table_masterWorkerLabels" as type_value')->select()->toArray();
            $data =  linear_to_tree($lists, 'children', 'id', 'pid');
            cache('labelMasterWorkerLabels',$data,3600);
        }
        return $data;
    }
    /**
     * @notes 优惠券标签列表
     */
    public static function couponLabels()
    {
        return Labels::where('id','>',0)->where('label_type',3)->field('id,label_name as name,id as value,"data_table_couponLabels" as type_value')->select()->toArray();
    }

    /**
     * @notes 权益卡列表
     */
    public static function equityConfig()
    {
        return EquityConfig::where('id','>',0)->field('id,equity_name as name,id as value,"data_table_equityConfig" as type_value')->select()->toArray();
    }

    public static function postageRegion(): array
    {
        // 缓存优化
        $data = cache('labelPostageRegion');
        if(empty($data)){
            $lists = PostageRegion::field('id,pid,name as label,id as value,"data_table_postageRegion" as type_value')->select()->toArray();
            $data = linear_to_tree($lists, 'children', 'id', 'pid');
            cache('labelPostageRegion',$data);
        }
        return $data;
    }

    public static function postageRegionCity(): array
    {
        // 缓存优化
        $data = cache('labelPostageRegionCity');
        if(empty($data)){
            $lists = PostageRegion::whereIn('level',[1,2])->field('id,pid,name as label,id as value,"data_table_postageRegionCity" as type_value')->select()->toArray();
            $data = linear_to_tree($lists, 'children', 'id', 'pid');
            cache('labelPostageRegionCity',$data);
        }
        return $data;
    }
    public static function jobsRules(): array
    {
        return JobsRules::field(['id', 'rule_name as label','id as value','"data_table_jobsRules" as type_value'])->select()->toArray();
    }

    public static function provinces()
    {
        return PostageRegion::where('pid','=',0)->field('id,merge_name as name,id as value,"data_table_provinces" as type_value')->select()->toArray();
    }
    public static function citys($province_ids = [])
    {
        if(!empty($province_ids)){
            return PostageRegion::where('pid','in',$province_ids)->where('level','=',2)->field('id,merge_name as name,id as value,"data_table_citys" as type_value')->select()->toArray();
        }
        return PostageRegion::where('level','=',2)->field('id,merge_name as name,id as value,"data_table_citys" as type_value')->select()->toArray();
    }

    public static function admins()
    {
        return Admin::where('disable','=',0)->field('id,name,id as value,"data_table_admins" as type_value')->select()->toArray();
    }

    public static function tenant()
    {
        return Tenant::where('disable','=',0)->field('id,name,id as value,"data_table_tenant" as type_value')->select()->toArray();
    }

    public static function trainingQuestions()
    {
        return TrainingQuestions::field('id,title as name,id as value,question_type,"data_table_trainingQuestions" as type_value')->select()->toArray();
    }
    public static function trainingCourse()
    {
        return TrainingCourse::field('id,course_name as name,id as value,"data_table_trainingCourse" as type_value')->select()->toArray();
    }
    public static function trainingTask()
    {
        return TrainingTask::field('id,task_name as name,id as value,"data_table_trainingTask" as type_value')->select()->toArray();
    }
    public static function shopGoods()
    {
        return ShopGoods::field('id,goods_name as name,id as value,"data_table_shopGoods" as type_value')->select()->toArray();
    }

    public static function externalPlatform()
    {
        return ExternalPlatform::field('id,name,CONVERT(id, CHAR) as value,"data_table_externalPlatform" as type_value')->select()->toArray();
    }
}