<?php

namespace app\api\controller;

use app\api\BaseController;
use think\facade\Db;

/**
 * 数据统计接口.
 */
class Census extends BaseController
{

//    获取一些统计数据
    public function get_list(){

        $where  = $mmm = $www = $where2 = [];
        $post = get_params();

//        //根据个人村id进行查询
//        if (JWT_UID) {
//            $find = Db::table('fa_szxc_information_useraddress')->where('user_id', JWT_UID)->find();
//            if ($find) {
//                if ($find['auth_range']==1){
//                    $mmm[] = $where[] = ['b.village_id', '=', $find['village_id']];
//                }elseif ($find['auth_range']==2){
//                    $mmm[] = $where[] = ['b.street_id', '=', $find['street_id']];
//                }elseif ($find['auth_range']==3){
//                    $mmm[] = $where[] = ['b.area_id', '=', $find['area_id']];
//                }
//            }
//        }
        if ($post) {
            if (isset($post['area_id']) && !empty($post['area_id'])) {
                $mmm['b.area_id'] = $www['d.area_id']  = $where['area_id'] = $post['area_id'];
                $where2['a.area_id'] = $post['area_id'];
            }
            if (isset($post['street_id']) && !empty($post['street_id'])) {
                $mmm['b.street_id'] = $www['d.street_id']  = $where['street_id'] = $post['street_id'];
                $where2['a.street_id'] = $post['street_id'];
            }
            if (isset($post['village_id']) && !empty($post['village_id'])) {
                $mmm['b.village_id'] = $www['d.village_id'] = $where['village_id'] = $post['village_id'];
                $where2['a.village_id'] = $post['village_id'];
            }
        }

        $map[] = ['a.status','=',1];
        $map[] = ['a.is_del','=',0];
        //本镇已录入的商户
        $num_4 = Db::connect('shop')->table('eb_merchant')->alias('a')->join('eb_merchant_address b','a.mer_id = b.mer_id')->where($mmm)->where($map)->count();

        //企业农产品销量
        $num_2 = Db::connect('shop')->table('eb_product_order_log')->where($where)->where('status',1)->where('mer_type_id','in','2,3')->count();

        //加工企业和品牌企业数量
        $map[] = ['a.type_id','in','2,3'];
        $num_1 = Db::connect('shop')->table('eb_merchant')->alias('a')->join('eb_merchant_address b','a.mer_id = b.mer_id')->where($mmm)->where($map)->count();

        //企业通过我们平台采购的农产品(差用户标签条件)
        $num_3 = Db::connect('shop')->table('eb_user')->alias('a')->join('eb_nk_user b','a.uid = b.user_id')
            ->join(['eb_product_order_log'=>'c'],'a.uid = c.uid')
            ->join(['nk_lihaink_cn.fa_szxc_information_useraddress'=>'d'],'b.n_user_id = d.user_id')
            ->where($www)->where('a.status',1)->where('c.status',1)->count();

        //镇农产品需求量分析(事业单位、企业、居民)
        //农产品市场行情分析、显示
        //农产品市内需求量
        //已婚 未婚 离异 可根据年龄统计各年龄层人员,学历统计
        //土地面积
        //园地 耕地 林地 牧草地 养殖 坑塘 农田水利设施用地
        //种植面积 养殖面积 荒地面积
        //镇农产品结构展示
        //运输距离显示
//        // 大于60岁人数
//        $old_num = Db::table('fa_szxc_information_usermsg')->where($where)->whereAge('>=', 60)->count();
////        儿童人数
//        $children_num = Db::table('fa_szxc_information_usermsg')->where($where)->whereAge('<', 15)->count();
////        未婚男
//        $unmarried_man_num = Db::table('fa_szxc_information_usermsg')->where($where)->where([['gender', '=', 1], ['marital_status', '=', 169]])->count();
////        未婚女
//        $unmarried_woman_num = Db::table('fa_szxc_information_usermsg')->where($where)->where([['gender', '=', 2], ['marital_status', '=', 169]])->count();
////        少数民族
//        $nation_s_num = Db::table('fa_szxc_information_usermsg')->where($where)->whereBetween('nation', [13, 68])->count();
////        汉族
//        $nation_h_num = Db::table('fa_szxc_information_usermsg')->where($where)->where('nation', 12)->count();
//        //残疾人数
//        $whether_disabled_num = Db::table('fa_szxc_information_usermsg')
//            ->alias('m')
//            ->where($where)
//            ->join(['fa_szxc_information_insurance'=>'i'],'m.user_id=i.user_id and i.whether_disabled!=112')->count();
////        参保人数
//        $insurance_type_num = Db::table('fa_szxc_information_usermsg')
//            ->alias('m')
//            ->where($where)
//            ->join(['fa_szxc_information_insurance'=>'i'],'m.user_id=i.user_id and i.insurance_type!=219')->count();

        //土地总面积
        $land_area_num = Db::table('fa_szxc_information_useraddress')
            ->alias('u')
            ->where($where)
            ->join(['fa_szxc_information_planting'=>'p'], 'u.user_id = p.user_id')->sum('p.land_area');
        //农田水利
        $sum_218 = Db::table('fa_szxc_information_useraddress')
            ->alias('u')
            ->where($where)
            ->join(['fa_szxc_information_planting'=>'p'], 'u.user_id = p.user_id and p.nature_of_land=218')->sum('p.land_area');
        //坑塘
        $sum_217 = Db::table('fa_szxc_information_useraddress')
            ->alias('u')
            ->where($where)
            ->join(['fa_szxc_information_planting'=>'p'], 'u.user_id = p.user_id and p.nature_of_land=217')->sum('p.land_area');
        //养殖
        $sum_yz = Db::table('fa_szxc_information_useraddress')
            ->alias('u')
            ->where($where)
            ->join(['fa_szxc_information_planting'=>'p'], 'u.user_id = p.user_id and p.nature_of_land=216')->sum('p.land_area');
        //养殖面积
        $sum_216 = Db::table('fa_szxc_information_useraddress')
            ->alias('u')
            ->where($where)
            ->join(['fa_szxc_information_planting'=>'p'], 'u.user_id = p.user_id')->sum('p.breed_area');
        //牧草地
        $sum_215 = Db::table('fa_szxc_information_useraddress')
            ->alias('u')
            ->where($where)
            ->join(['fa_szxc_information_planting'=>'p'], 'u.user_id = p.user_id and p.nature_of_land=215')->sum('p.land_area');
        //林地
        $sum_214 = Db::table('fa_szxc_information_useraddress')
            ->alias('u')
            ->where($where)
            ->join(['fa_szxc_information_planting'=>'p'], 'u.user_id = p.user_id and p.nature_of_land=214')->sum('p.land_area');
        //耕地
        $sum_70 = Db::table('fa_szxc_information_useraddress')
            ->alias('u')
            ->where($where)
            ->join(['fa_szxc_information_planting'=>'p'], 'u.user_id = p.user_id and p.nature_of_land=70')->sum('p.land_area');
        //园地
        $sum_69 = Db::table('fa_szxc_information_useraddress')
            ->alias('u')
            ->where($where)
            ->join(['fa_szxc_information_planting'=>'p'], 'u.user_id = p.user_id and p.nature_of_land=69')->sum('p.land_area');

//        荒地面积
        $waste_land_area = Db::table('fa_szxc_information_useraddress')
            ->alias('u')
            ->where($where)
            ->join(['fa_szxc_information_planting'=>'p'], 'u.user_id = p.user_id')->sum('p.waste_land_area');
        //农产品种类
        $cms_agriculture_products = Db::table('cms_agriculture_products')->where('status',1)->count();
//        //蔬菜基地数量
//        $cms_manufacturer = Db::table('cms_manufacturer')->where('status',1)->count();
//        //农贸市场数量
//        $cms_farm_product_market = Db::table('cms_farm_product_market')->where('status',1)->count();
        //种植面积
        $zz_mianji = $land_area_num - $sum_216 - $waste_land_area;
        //总人口
        $num = Db::table('fa_szxc_information_usermsg')->where($where)->count();
        //成熟周期  [{"name": "白菜", "pic": "http:agaagag.png", "zhouqi": "5周"}]
        $zhouqi = Db::table('cms_agriculture_products_traceability')
                ->withAttr('content', function ($value, $data) {
                    if ($data['content'] != '') {
                        $cont = json_decode($data['content'],1);
                        foreach ($cont as $k => $v){
                            $cont[$k]['cjl'] = '昨日成交量提示15%';
                        }
                        return $cont;
                    }else{
                        return '';
                    }

                })
                ->where('status',1)->field('content')->select();

//        订单商品总数
        $need_num = Db::connect('shop')->table('eb_product_order_log')->alias('a')->join('eb_store_order b','a.order_id = b.order_id')->where($where2)->where('a.status',1)->sum('b.total_num');
        $return['index'] = [
                ['name' => '总人口','value'=>$num.'人'],
                ['name' => '农产品种类','value'=>$cms_agriculture_products.'种'],
                ['name' => '品牌企业','value'=>$num_1.'家'],
                ['name' => '种植面积','value'=>$zz_mianji],
                ['name' => '养殖面积','value'=>$sum_216],
                ['name' => '荒地面积','value'=>$waste_land_area],
            ];
        $return['xingzhi'] = [
            ['name' => '农田水利','value'=>$sum_218],
            ['name' => '坑塘','value'=>$sum_217],
            ['name' => '养殖','value'=>$sum_yz],
            ['name' => '牧草地','value'=>$sum_215],
            ['name' => '林地','value'=>$sum_214],
            ['name' => '耕地','value'=>$sum_70],
            ['name' => '园地','value'=>$sum_69],
        ];
        $return['qita'] = [
            ['name' => '土地总面积','value'=>$land_area_num],
            ['name' => '农产品销量','value'=>$num_2.'单'],
            ['name' => '农产采购量','value'=>$num_3.'单'],
            ['name' => '已录入商户','value'=>$num_4.'个'],
            ['name' => '市需求量','value'=>$need_num.'个'],
//            ['name' => '大于60岁人数','value'=>$old_num],
//            ['name' => '儿童人数','value'=>$children_num],
//            ['name' => '未婚男','value'=>$unmarried_man_num],
//            ['name' => '未婚女','value'=>$unmarried_woman_num],
//            ['name' => '少数民族','value'=>$nation_s_num],
//            ['name' => '汉族','value'=>$nation_h_num],
//            ['name' => '残疾人数','value'=>$whether_disabled_num],
//            ['name' => '参保人数','value'=>$insurance_type_num],
//            ['name' => '蔬菜基地数量','value'=>$cms_manufacturer.'个'],
//            ['name' => '农贸市场数量','value'=>$cms_farm_product_market.'个'],
        ];
        $return['zhouqi'] = [
            ['name' => '农产品周期','value'=>$zhouqi],
        ];
        $return['juli'] = [
            ['name' => '运输距离','value'=>'20km'],
        ];
        $this->apiSuccess('获取成功', $return);
    }


}