Project.php 40.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/18
 * Time: 11:12
 */

namespace app\api\controller;


use app\common\controller\Api;
use fast\Http;
use think\Db;
use think\Validate;

/**
 * 项目模块
 */
class Project extends Api
{
    /**
     * @ApiTitle    (农场管理中农场信息)
     * @ApiSummary  (农场管理中农场信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/project/farm)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
            "id":"id",//农场id
            "name"://农场名称
            "avatar"://头像
            "score":"",//累计公分
        }
        })
     */
    public function farm()
    {
        $user_id = $this->getUserId();
        $farm = Db::name('farm')
            ->alias('a')
            ->join('user r','a.user_id = r.id')
            ->where('a.user_id',$user_id)
            ->field('a.id,a.name,r.avatar,r.score')
            ->find();
        $this->success('success',$farm);
    }

    /**
     * @ApiTitle    (项目列表)
     * @ApiSummary  (项目列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/project/get_all)
     *
     * @ApiParams   (name="farm_id", type="int", required=false, description="农场id")
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
            "id":"id",//项目id
            "project_name"://项目名称
            "image":"image",//项目封面图
            "content"://项目简介
            "look_num":"look_num",//浏览量
            "good_num":"good_num",//点赞量
            "status"://项目状态(0培育中1将收获2已收获)
            "createtime":"createtime",//创建时间
        }
        })
     */
    public function get_all()
    {
//        $domain_name = $this->request->domain();//域名
        $page = $this->request->param('page',1,'intval');
        $pageNum = $this->request->param('pageNum',10,'intval');
        $farm_id = $this->request->param('farm_id');
        if(empty($farm_id)){
            $this->error('缺少必要参数');
        }
        $data = Db::name('project')
            ->where('farm_id',$farm_id)
            ->field('id,project_name,image,content,look_num,good_num,status,createtime')
            ->order('id desc')
            ->page($page,$pageNum)
            ->select();
        foreach ($data as &$v){
            $v['image'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$v['image'];
            $v['createtime'] = date('Y-m-d',$v['createtime']);
        }
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (添加项目)
     * @ApiSummary  (添加项目)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/project/getadd)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="farm_id", type="int", required=false, description="农场id")
     * @ApiParams   (name="image", type="int", required=false, description="产品图片")
     * @ApiParams   (name="project_name", type="string", required=false, description="项目名称")
     * @ApiParams   (name="name", type="int", required=false, description="产品名称")
     * @ApiParams   (name="content", type="string", required=false, description="项目简介")
     * @ApiParams   (name="address", type="string", required=false, description="产品位置")
     * @ApiParams   (name="province", type="string", required=false, description="省份信息")
     * @ApiParams   (name="city", type="string", required=false, description="市区信息")
     * @ApiParams   (name="county", type="string", required=false, description="区县信息")
     * @ApiParams   (name="lng", type="string", required=false, description="经度")
     * @ApiParams   (name="lat", type="string", required=false, description="纬度")
     * @ApiParams   (name="status", type="int", required=false, description="项目状态(0培育中1将收获2已收获)")
     * @ApiParams   (name="estimate", type="string", required=false, description="预估产量")
     * @ApiParams   (name="estimate_time", type="string", required=false, description="预估成熟期")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
        }
        })
     */
    public function getadd()
    {
        $user_id = $this->getUserId();
        $param = $this->request->param();
        $validate = new Validate([
            'farm_id' => 'require',
            'image' => 'require',
            'project_name' => 'require',
            'name'=>'require',
            'content'=>'require',
            'province'=>'require',
            'city' => 'require',
            'county' => 'require',
            'address' => 'require',
            'lng'=>'require',
            'lat'=>'require',
            'estimate'=>'require',
            'estimate_time'=>'require',
            'status'=>'require'
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        $http = 'http://q2ugvq3qf.bkt.clouddn.com';
        $param['image'] = str_replace($http,'',$param['image']);
        $param['createtime'] = time();
        $data = Db::name('project')->insertGetId($param);
        $common = new Common();
        $url = 'pages/ShopDetail/ShopDetail?id='.$data;
        $thumbnail = $common->imgcode($data,$url);
        Db::name('project')->where('id',$data)->update(['thumbnail'=>$thumbnail]);

        //修改农场项目数
        $project_num = Db::name('farm')->where('id',$param['farm_id'])->field('id,project_num')->find();
        $project_num = $project_num['project_num'] + 1;
        Db::name('farm')->where('id',$param['farm_id'])->update(['project_num'=>$project_num]);
        if($data){
            $this->success('添加成功',$data);
        }else{
            $this->error('添加失败');
        }
    }

    /**
     * @ApiTitle    (项目详情)
     * @ApiSummary  (项目详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/project/detail)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="id", type="int", required=false, description="项目id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
            "images"://项目banner
            "project_name"://项目名称
            "status"://项目状态(0培育中1将收获2已收获)
            "content"://项目简介
            "look_num":"look_num",//浏览量
            "good_num":"good_num",//点赞量
            "farm_name"://农场名称
            "farm_content"://农场简介
            "is_guan"://是否关注(1否2是)
            "is_zan"://是否点过赞(1否2是)
            "farm_id"://农场ID
            "nong_name"://农场主名字
            "name"://作物名称
            "farm_address":农场位置
            "estimate"://预估产值
            "estimate_time"://预估成熟期
            "company_name"://企业名称
            "lng"://经度
            "lat"://纬度
            }
        })
     */
    public function detail()
    {
        $token = $this->request->header('token');
        if(empty($token)){
            $id = $this->request->param('id');
            if(empty($id)){
                $this->error('缺少必要参数');
            }
            $data = Db::name('project')
                ->where('id',$id)
                ->find();
            $look_num = $data['look_num']+1;
            Db::name('project')->where('id',$id)->update(['look_num'=>$look_num]);
            $data['image'] = explode(',',$data['image']);
            foreach ($data['image'] as &$val){
                $val = 'http://q2ugvq3qf.bkt.clouddn.com'.$val;
            }
            $data['images'] = explode(',',$data['images']);
            foreach ($data['images'] as &$v){
                $v = 'http://q2ugvq3qf.bkt.clouddn.com'.$v;
            }
            //查出项目对应的农场名称以及农场简介
            $farm = Db::name('farm')
                ->alias('a')
                ->join('user b','a.user_id = b.id')
                ->where('a.id',$data['farm_id'])
                ->field('a.id,a.name,a.content,a.province,a.city,a.address,a.user_id,b.avatar')
                ->find();
            $data['farm_address'] = $farm['address'];
            $data['avatar'] = $farm['avatar'];
            $data['farm_id'] = $farm['id'];
            $data['farm_name'] = $farm['name'];
            $data['farm_content'] = $farm['content'];

            //查看用户对该项目是否点过赞  默认都为未点赞
            $data['is_zan'] = 1;

            //查看用户是否关注过该农场   默认都为未关注
            $data['is_guan'] = 1;

            $user = Db::name('user')->where('id',$farm['user_id'])->field('identity')->find();
            if($user['identity'] == 3){
                $company = Db::name('company')
                    ->where('user_id',$farm['user_id'])
                    ->field('company_name,name')
                    ->find();
                $data['nong_name'] = $company['name'];
                $data['company_name'] = $company['company_name'];
            }else{
                $company = Db::name('personage')
                    ->where('user_id',$farm['user_id'])
                    ->field('id,name')
                    ->find();
                $data['nong_name'] = $company['name'];
                $data['company_name'] = '';
            }
            $this->success('success',$data);
        }
        $user_id = $this->getUserId();
        $id = $this->request->param('id');
        if(empty($id)){
            $this->error('缺少必要参数');
        }
        $data = Db::name('project')
            ->where('id',$id)
            ->find();
        $look_num = $data['look_num']+1;
        Db::name('project')->where('id',$id)->update(['look_num'=>$look_num]);
        $data['image'] = explode(',',$data['image']);
        foreach ($data['image'] as &$val){
            $val = 'http://q2ugvq3qf.bkt.clouddn.com'.$val;
        }
        $data['images'] = explode(',',$data['images']);
        foreach ($data['images'] as &$v){
            $v = 'http://q2ugvq3qf.bkt.clouddn.com'.$v;
        }
        //查出项目对应的农场名称以及农场简介
        $farm = Db::name('farm')
            ->alias('a')
            ->join('user b','a.user_id = b.id')
            ->where('a.id',$data['farm_id'])
            ->field('a.id,a.name,a.content,a.province,a.city,a.address,a.user_id,b.avatar')
            ->find();
        $data['farm_address'] = $farm['address'];
        $data['avatar'] = $farm['avatar'];
        $data['farm_id'] = $farm['id'];
        $data['farm_name'] = $farm['name'];
        $data['farm_content'] = $farm['content'];

        //查看用户对该项目是否点过赞
        $is_zan = Db::name('good')
            ->where('user_id',$user_id)
            ->where('project_id',$id)
            ->find();
        if(empty($is_zan)){
            $data['is_zan'] = 1;
        }else{
            $data['is_zan'] = 2;
        }

        //查看用户是否关注过该农场
        $guan = Db::name('guanzhu')
            ->where('user_id',$user_id)
            ->where('farm_id',$farm['id'])
            ->find();
        if(empty($guan)){
            $data['is_guan'] = 1;
        }else{
            $data['is_guan'] = 2;
        }
        $user = Db::name('user')->where('id',$farm['user_id'])->field('identity')->find();
        if($user['identity'] == 3){
            $company = Db::name('company')
                ->where('user_id',$farm['user_id'])
                ->field('company_name,name')
                ->find();
            $data['nong_name'] = $company['name'];
            $data['company_name'] = $company['company_name'];
        }else{
            $company = Db::name('personage')
                ->where('user_id',$farm['user_id'])
                ->field('id,name')
                ->find();
            $data['nong_name'] = $company['name'];
            $data['company_name'] = '';
        }
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (项目溯源)
     * @ApiSummary  (项目溯源)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/project/pro_record)
     *
     * @ApiParams   (name="id", type="int", required=false, description="项目id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
            "id"://溯源记录ID
            "image"://产品图片或者视频
            "status"://项目状态(0培育中1将收获2已收获)
            "theme"://溯源主题
            "avatar"://用户头像
            "nickname"://农户昵称
            "content"://溯源记录备注
            "week"://星期
            "weather":天气
            "humidity"://湿度
            "temperature"://温度
            "wind"://风力
            "air"://空气质量
            "createtime":创建时间
        }
        })
     */
    public function pro_record()
    {
        $project_id = $this->request->param('id');
        $data = Db::name('record')
            ->alias('a')
            ->join('user b','a.user_id = b.id')
            ->field('a.*,b.avatar,b.nickname')
            ->where('a.project_id',$project_id)
            ->order('a.createtime desc')
            ->select();
        foreach ($data as &$v){
            $v['image'] = explode(',',$v['image']);
            foreach ($v['image'] as &$val){
                $val = 'http://q2ugvq3qf.bkt.clouddn.com'.$val;
            }
            $v['createtime'] = date('Y-m-d',$v['createtime']);
        }

        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (项目点赞)
     * @ApiSummary  (项目点赞)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/project/good)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="project_id", type="int", required=false, description="项目id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
        }
        })
     */
    public function good(){
        $param['user_id'] = $this->getUserId();
        $param['project_id'] = $this->request->param('project_id');
        if(empty($param['project_id'])){
            $this->error('缺少必要参数');
        }
        $farm = Db::name('project')
            ->where('id',$param['project_id'])
            ->field('id,farm_id,good_num')
            ->find();
        $good_num = $farm['good_num']+1;
        $param['farm_id'] = $farm['farm_id'];
        $param['createtime'] = time();

        //查询是否已经点过赞
        $zan = Db::name('good')
            ->where('user_id',$param['user_id'])
            ->where('project_id',$param['project_id'])
            ->find();
        if(!empty($zan)){
            $this->error('操作频繁');
        }
        Db::startTrans();
        $data = Db::name('good')
            ->insertGetId($param);
        $res = Db::name('project')
            ->where('id',$param['project_id'])
            ->update(['good_num'=>$good_num]);
        if($data && $res){
            Db::commit();
            $this->success('成功');
        }else{
            Db::rollback();
            $this->error('失败');
        }
    }

    /**
     * @ApiTitle    (项目取消点赞)
     * @ApiSummary  (项目取消点赞)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/project/nogood)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="project_id", type="int", required=false, description="项目id")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {
    }
    })
     */
    public function nogood(){
        $param['user_id'] = $this->getUserId();
        $param['project_id'] = $this->request->param('project_id');
        if(empty($param['project_id'])){
            $this->error('缺少必要参数');
        }
        $farm = Db::name('project')
            ->where('id',$param['project_id'])
            ->field('id,farm_id,good_num')
            ->find();
        $good_num = $farm['good_num']-1;
        $param['farm_id'] = $farm['farm_id'];
        $where['user_id'] = ['eq',$param['user_id']];
        $where['project_id'] = ['eq',$param['project_id']];
        $where['farm_id'] = ['eq',$param['farm_id']];
        Db::startTrans();
        $data = Db::name('good')
            ->where($where)
            ->delete();
        $res = Db::name('project')
            ->where('id',$param['project_id'])
            ->update(['good_num'=>$good_num]);
        if($data && $res){
            Db::commit();
            $this->success('成功');
        }else{
            Db::rollback();
            $this->error('失败');
        }
    }

    /**
     * @ApiTitle    (农场关注)
     * @ApiSummary  (农场关注)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/project/guanzhu)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="farm_id", type="int", required=false, description="农场id")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {
    }
    })
     */
    public function guanzhu()
    {
        $param['user_id'] = $this->getUserId();

        $identity = Db::name('user')->where('id',$param['user_id'])->field('id,identity')->find();
        if($identity['identity'] == 4 || $identity['identity'] == 1){
            $param['farm_id'] = $this->request->param('farm_id');
            if(empty($param['farm_id'])){
                $this->error('缺少必要参数');
            }
            $is_guan = Db::name('guanzhu')
                ->where('user_id',$param['user_id'])
                ->where('farm_id',$param['farm_id'])
                ->find();
            if(!empty($is_guan)){
                $this->error('操作频繁');
            }
            $param['createtime'] = time();
            //将农场的粉丝数加一
            $fun_num = Db::name('farm')
                ->where('id',$param['farm_id'])
                ->field('id,fun_num')
                ->find();
            $fun_num = $fun_num['fun_num'] + 1;
            Db::startTrans();
            $data = Db::name('guanzhu')
                ->insertGetId($param);
            $res = Db::name('farm')
                ->where('id',$param['farm_id'])
                ->update(['fun_num'=>$fun_num]);
            if($data && $res){
                Db::commit();
                $this->success('成功');
            }else{
                Db::rollback();
                $this->error('失败');
            }
        }
        $param['farm_id'] = $this->request->param('farm_id');
        if(empty($param['farm_id'])){
            $this->error('缺少必要参数');
        }
        $is_guan = Db::name('guanzhu')
            ->where('user_id',$param['user_id'])
            ->where('farm_id',$param['farm_id'])
            ->find();
        if(!empty($is_guan)){
            $this->error('操作频繁');
        }
        $param['createtime'] = time();
        $fun_num = Db::name('farm')
            ->where('id',$param['farm_id'])
            ->field('id,fun_num')
            ->find();
        $fun_num = $fun_num['fun_num'] + 1;

        //将用户的关注数加1
        $info = Db::name('farm')
            ->where('user_id',$param['user_id'])
            ->find();

        $attention_num = $info['attention_num'] + 1;

        Db::startTrans();
        $atte = Db::name('farm')
            ->where('user_id',$param['user_id'])
            ->update(['attention_num'=>$attention_num]);
        $data = Db::name('guanzhu')
            ->insertGetId($param);
        $res = Db::name('farm')
            ->where('id',$param['farm_id'])
            ->update(['fun_num'=>$fun_num]);
        if($data && $res && $atte){
            Db::commit();
            $this->success('成功');
        }else{
            Db::rollback();
            $this->error('失败');
        }
    }

    /**
     * @ApiTitle    (农场取消关注)
     * @ApiSummary  (农场取消关注)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/project/noguan)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="farm_id", type="int", required=false, description="农场id")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {
    }
    })
     */
    public function noguan()
    {
        $param['user_id'] = $this->getUserId();
        $identity = Db::name('user')->where('id',$param['user_id'])->field('id,identity')->find();

        //判断其身份
        if($identity['identity']==1 || $identity['identity']==4){
            //如果身份是游客或者是政府
            $param['farm_id'] = $this->request->param('farm_id');
            if(empty($param['farm_id'])){
                $this->error('缺少必要参数');
            }
            $is_guan = Db::name('guanzhu')
                ->where('user_id',$param['user_id'])
                ->where('farm_id',$param['farm_id'])
                ->find();
            if(empty($is_guan)){
                $this->error('操作频繁');
            }
            $fun_num = Db::name('farm')
                ->where('id',$param['farm_id'])
                ->field('id,fun_num')
                ->find();
            $fun_num = $fun_num['fun_num'] - 1;

            $where['user_id'] = $param['user_id'];
            $where['farm_id'] = $param['farm_id'];

            Db::startTrans();
            $data = Db::name('guanzhu')
                ->where($where)
                ->delete();
            $res = Db::name('farm')
                ->where('id',$param['farm_id'])
                ->update(['fun_num'=>$fun_num]);
            if($data && $res){
                Db::commit();
                $this->success('成功');
            }else{
                Db::rollback();
                $this->error('失败');
            }
        }
        $param['farm_id'] = $this->request->param('farm_id');
        if(empty($param['farm_id'])){
            $this->error('缺少必要参数');
        }
        $is_guan = Db::name('guanzhu')
            ->where('user_id',$param['user_id'])
            ->where('farm_id',$param['farm_id'])
            ->find();
        if(empty($is_guan)){
            $this->error('操作频繁');
        }
        $fun_num = Db::name('farm')
            ->where('id',$param['farm_id'])
            ->field('id,fun_num')
            ->find();
        $fun_num = $fun_num['fun_num'] - 1;

        //将用户的关注数减1
        $info = Db::name('farm')
            ->where('user_id',$param['user_id'])
            ->find();

        $attention_num = $info['attention_num'] - 1;
        $where['user_id'] = $param['user_id'];
        $where['farm_id'] = $param['farm_id'];

        Db::startTrans();
        $atte = Db::name('farm')
            ->where('user_id',$param['user_id'])
            ->update(['attention_num'=>$attention_num]);
        $data = Db::name('guanzhu')
            ->where($where)
            ->delete();
        $res = Db::name('farm')
            ->where('id',$param['farm_id'])
            ->update(['fun_num'=>$fun_num]);
        if($data && $res && $atte){
            Db::commit();
            $this->success('成功');
        }else{
            Db::rollback();
            $this->error('失败');
        }
    }

    /**
     * @ApiTitle    (我的关注)
     * @ApiSummary  (我的关注)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/project/myguan)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
            "id"://关注ID
            "avatar"://用户头像
            "user_id"://用户ID
            "farm_id"://农场id
            "createtime"://创建时间
            "name"://农场名称
            "project_num"://项目数量
            "fun_name"://粉丝数量
        }
        })
     */
    public function myguan()
    {

        $user_id = $this->getUserId();
        $data = Db::name('guanzhu')
            ->where('user_id',$user_id)
            ->select();
        foreach ($data as &$v){
            $farm = Db::name('farm')
                ->alias('a')
                ->join('user b','a.user_id = b.id')
                ->where('a.id',$v['farm_id'])
                ->field('a.name,a.project_num,a.fun_num,b.avatar')
                ->find();
            $v['name'] = $farm['name'];
            $v['project_num'] = $farm['project_num'];
            $v['fun_num'] = $farm['fun_num'];
            $v['avatar'] = $farm['avatar'];
        }
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (我的粉丝)
     * @ApiSummary  (我的粉丝)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/project/myfun)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
        "id"://关注ID
        "user_avatar"://用户头像
        "user_id"://用户ID
        "farm_id"://农场id
        "name"://农场名称
        "project_num"://项目数量
        "fun_name"://粉丝数量
        "is_guan"://1未关注2已关注
        }
        })
     */
    public function myfun()
    {
        $user_id = $this->getUserId();

        //查出我的农场ID
        $farm = Db::name('farm')
            ->where('user_id',$user_id)
            ->field('id,user_id')
            ->find();
        //查出关注我的农场 对应的 用户ID
        $data = Db::name('guanzhu')
            ->where('farm_id',$farm['id'])
            ->field('id,user_id,farm_id')
            ->select();
        foreach ($data as &$v){
            $identity = Db::name('user')->where('id',$v['user_id'])->field('id,identity')->find();


            //如果用户身份是政府或者游客
            if($identity['identity'] == 1 || $identity['identity'] == 4){
                //判断其身份 如果是游客或者是政府   返回头像跟昵称
                $user = Db::name('user')
                    ->where('id',$v['user_id'])
                    ->field('id,avatar,nickname')
                    ->find();
                $v['user_avatar'] = $user['avatar'];
                $v['nickname'] = $user['nickname'];
                $v['is_guan'] = 1;
                $v['name'] = '';
                $v['project_num'] = '';
                $v['fun_num'] = '';
            }else{
                //查出关注我的用户 自己的农场信息
                $user_farm = Db::name('farm')
                    ->alias('a')
                    ->join('user b','a.user_id = b.id')
                    ->field('a.id,a.name,a.project_num,a.fun_num,b.avatar as user_avatar,b.nickname')
                    ->where('a.user_id',$v['user_id'])
                    ->find();
                $v['name'] = $user_farm['name'];
                $v['project_num'] = $user_farm['project_num'];
                $v['fun_num'] = $user_farm['fun_num'];
                $v['user_avatar'] = $user_farm['user_avatar'];
                $v['nickname'] = $user_farm['nickname'];
                $guanzhu = Db::name('guanzhu')
                    ->where('user_id',$user_id)
                    ->where('farm_id',$user_farm['id'])
                    ->find();
                if(empty($guanzhu)){
                    $v['is_guan'] = 1;
                }else{
                    $v['is_guan'] = 2;
                }
            }
        }
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (我的溯源码)
     * @ApiSummary  (我的溯源码)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/project/mycode)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
            "id"://项目ID
            "image"://项目图片
            "thumbnail"://项目溯源码
            "project_name"://项目名称
            "farm_name"://农场名称
        }
        })
     */
    public function mycode()
    {
        $user_id = $this->getUserId();
        $domain_name = $this->request->domain();//域名
        $page = $this->request->param('page',1,'intval');
        $pageNum = $this->request->param('pageNum',10,'intval');
        //查找出我的农场信息  农场id
        $farm = Db::name('farm')
            ->where('user_id',$user_id)
            ->field('id,name')
            ->find();
        //通过农场ID查出我的项目
        $data = Db::name('project')
            ->where('farm_id',$farm['id'])
            ->field('id,project_name,image,thumbnail')
            ->page($page,$pageNum)
            ->select();
        foreach ($data as &$v){
            $v['farm_name'] = $farm['name'];
        }
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (添加溯源记录)
     * @ApiSummary  (添加溯源记录)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/project/record)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="farm_id", type="int", required=false, description="农场id")
     * @ApiParams   (name="project_id", type="int", required=false, description="项目id")
     * @ApiParams   (name="image", type="int", required=false, description="产品图片或者视频")
     * @ApiParams   (name="theme", type="string", required=false, description="溯源主题")
     * @ApiParams   (name="status", type="int", required=false, description="项目状态(0培育中1将收获2已收获)")
     * @ApiParams   (name="content", type="string", required=false, description="溯源记录备注")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {
    }
    })
     */
    public function record()
    {
        $user_id = $this->getUserId();
        $param = $this->request->param();
        $validate = new Validate([
            'farm_id' => 'require',
            'project_id' => 'require',
            'image'=>'require',
            'theme'=>'require',
            'status'=>'require',
            'content'=>'require',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        $http = 'http://q2ugvq3qf.bkt.clouddn.com';
        $param['image'] = str_replace($http,'',$param['image']);
        //查询出当天当地天气
        $data = Db::name('project')
            ->where('id',$param['project_id'])
            ->find();
        $city = $data['city'];
        $url = 'https://api.jisuapi.com/weather/query?appkey=f37f8b7b60cc27d9&city='.$city;
        $result = Http::get($url);
        $jsonarr = json_decode($result,true);
        $param['user_id'] = $user_id;
        $param['createtime'] = time();
        $param['week'] = $jsonarr['result']['week'];
        $param['weather'] = $jsonarr['result']['weather'];
        $param['wind'] = $jsonarr['result']['windpower'];
        $param['humidity'] = $jsonarr['result']['humidity'].'%';
        $param['air'] = $jsonarr['result']['aqi']['quality'];
        $param['temperature'] = $jsonarr['result']['templow'].'~'.$jsonarr['result']['temphigh'].'度';
        $data = Db::name('record')->insertGetId($param);
        Db::name('project')->where('id',$param['project_id'])->update(['status'=>$param['status']]);
        if(!empty($data)){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }

    /**
     * @ApiTitle    (溯源历史)
     * @ApiSummary  (溯源历史)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/project/his_record)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
            "id"://溯源记录ID
            "image"://产品图片或者视频
            "status"://项目状态(0培育中1将收获2已收获)
            "theme"://溯源主题
            "avatar"://用户头像
            "nickname"://农户昵称
            "content"://溯源记录备注
            "week"://星期
            "weather":天气
            "humidity"://湿度
            "temperature"://温度
            "wind"://风力
            "air"://空气质量
            "createtime"://创建时间
            "project_id"://项目ID
            "project_image"://项目图片
            "proect_name"://项目名称
            "look_num"://浏览量
            "good_num"://点赞量
            "project_content"://项目简介
        }
    })
     */
    public function his_record(){
        $user_id = $this->getUserId();
        $data = Db::name('record')
            ->alias('a')
            ->join('project b','a.project_id = b.id')
            ->field('a.*,b.id as project_id,b.image as project_image,b.project_name,b.look_num,b.good_num,b.content as project_content')
            ->where('a.user_id',$user_id)
            ->order('a.createtime desc')
            ->select();
        //按用户的查看时间将数据分段
        foreach ($data as &$v){
            $v['create_time'] = date('Y-m-d',$v['createtime']);
            $v['createtime'] = date('H:i:s',$v['createtime']);
        }
        $times = array_values(array_unique(array_column($data,'create_time')));
        rsort($times);
        //将比赛信息放到对应的时间分段
        $arr = [];
        foreach ($times as $t_k=> $t_v){
            $arr[$t_k]['times'] = $t_v;
            $k = 0;
            foreach ($data as $value){
                $k+=0;
                if($t_v == $value['create_time']){
                    $arr[$t_k]['info'][$k] = $value;
                    $k++;
                }
            }
        }
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (添加项目评论)
     * @ApiSummary  (添加项目评论)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/project/comment)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="farm_id", type="int", required=false, description="农场id")
     * @ApiParams   (name="project_id", type="int", required=false, description="项目id")
     * @ApiParams   (name="conten", type="int", required=false, description="评论内容")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {
    }
    })
     */
    public function comment()
    {
        $user_id = $this->getUserId();
        $param = $this->request->param();
        $validate = new Validate([
            'farm_id' => 'require',
            'project_id' => 'require',
            'content'=>'require',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        $keyword = Db::name('word')
            ->column('name');
        $new_str = str_replace($keyword,"*",$param['content']);
        $count = strpos($new_str,"*");
        if($count != 0){
            $this->error('评论内容含有关键字');
        }
        $param['user_id'] = $user_id;
        $param['createtime'] = time();
        $data = Db::name('comment')
            ->insertGetId($param);
        if(empty($data)){
            $this->error('评论失败');
        }else{
            $this->success('评论成功');
        }
    }

    /**
     * @ApiTitle    (项目评论列表)
     * @ApiSummary  (项目评论列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/project/commentlist)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="farm_id", type="int", required=false, description="农场id")
     * @ApiParams   (name="project_id", type="int", required=false, description="项目id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
            "id"://评论ID
            "avatar"://用户头像
            "nickname"://用户昵称
            "farm_id"://农场id
            "project_id"://项目id
            "user_id"://用户ID
            "content"://评论内容
            "num"://评论个数
            "good_name"://点赞数
            "createtime"://评论时间
            "is_zan"://是否点过赞(1否2是)
        }
        })
     */
    public function commentlist()
    {
        $token = $this->request->header('token');
        if(empty($token)){
            $param = $this->request->param();
            $validate = new Validate([
                'farm_id' => 'require',
                'project_id' => 'require',
            ]);
            if (!$validate->check($param)) {
                $this->error($validate->getError());
            }
            $where['a.farm_id'] = $param['farm_id'];
            $where['a.project_id'] = $param['project_id'];
            $where['a.status'] = 2;
            $data['list'] = Db::name('comment')
                ->alias('a')
                ->join('user b','a.user_id = b.id')
                ->field('a.*,b.avatar,b.nickname,a.createtime')
                ->where($where)
                ->select();
            foreach ($data['list'] as &$v){
                $v['is_zan'] = 1;
                $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
            }
            $data['num'] = Db::name('comment')
                ->alias('a')
                ->join('user b','a.user_id = b.id')
                ->field('a.*,b.avatar,b.nickname')
                ->where($where)
                ->count();
            $this->success('success',$data);
        }
        $user_id = $this->getUserId();
        $param = $this->request->param();
        $validate = new Validate([
            'farm_id' => 'require',
            'project_id' => 'require',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        $where['a.farm_id'] = $param['farm_id'];
        $where['a.project_id'] = $param['project_id'];
        $where['a.status'] = 2;
        $data = Db::name('comment')
            ->alias('a')
            ->join('user b','a.user_id = b.id')
            ->field('a.*,b.avatar,b.nickname,a.createtime')
            ->where($where)
            ->select();
        foreach ($data as &$v){
            $good = Db::name('goodcomment')
                ->where('comment_id',$v['id'])
                ->where('user_id',$user_id)
                ->find();
            if(empty($good)){
                $v['is_zan'] = 1;
            }else{
                $v['is_zan'] = 2;
            }
            $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
        }
        $this->success('success',$data);

    }

    /**
     * @ApiTitle    (评论点赞接口)
     * @ApiSummary  (评论点赞接口)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/project/goodcomment)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="comment_id", type="int", required=false, description="评论id")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {
    }
    })
     */
    public function goodcomment()
    {
        $user_id = $this->getUserId();
        $param['comment_id'] = $this->request->param('comment_id');
        if(empty($param['comment_id'])){
            $this->error('缺少必要参数');
        }
        $param['user_id'] = $user_id;
        $param['createtime'] = time();
        $data = Db::name('goodcomment')
            ->insertGetId($param);
        if(empty($data)){
            $this->error('点赞失败');
        }else{
            $this->success('点赞成功');
        }
    }
}