<?php
/**
 * Created by PhpStorm.
 * User: sicilon_IT
 * Date: 2020/1/27
 * Time: 15:38
 */

namespace app\index\controller;
use app\index\logic\prjmanger;
use app\index\logic\headmanger;
use app\index\logic\workermanger;
use app\index\logic\contactlogic;
use think\Controller;

class Project extends Controller{
    public function queryallprj(){
    $prjlogic=new prjmanger();
    $p_list=$prjlogic->getallprj();

    if ($p_list){
        $res_r['code']=200;
        $res_r['resultData']['records']=$p_list;
    }else{
        $res_r['code']=0;
        $res_r['errmsg']='查询失败';
    }

    return json_encode($res_r,320);
    }

    /*
     * 20230214
     * 根据web管理员身份获取所有的工地项目
     */
    public function queryallprjbycid(){
        $prjinfo=request() -> param();
        $prjlogic=new prjmanger();
        $p_list=$prjlogic->getallprjbycid($prjinfo);

        if ($p_list){
            $res_r['code']=200;
            $res_r['resultData']['records']=$p_list;
        }else{
            $res_r['code']=0;
            $res_r['errmsg']='查询失败';
        }

        return json_encode($res_r,320);
    }


    public function addprj(){
        $prjinfo=request() -> param();
        $prjlogic=new prjmanger();
        $newid=$prjlogic->addnewprj($prjinfo);

        if ($newid>0){
            $res_r['code']=200;
            $res_r['resultData']=$newid;
        }else{
            $res_r['code']=0;
            $res_r['errmsg']='添加失败';
        }

        return json_encode($res_r,320);
    }

    public function getbyid(){
        $qryinfo=request() -> param();

        $prjlogic=new prjmanger();
        $prjinfo=$prjlogic->getbyid($qryinfo['id']);

        if ($prjinfo){
            $res_r['code']=200;
            $res_r['resultData']=$prjinfo;
        }else{
            $res_r['code']=0;
            $res_r['errmsg']='查询失败';
        }

        return json_encode($res_r,320);
    }
 /*
  * 20200205
  * steelxu
  *  web端提交项目工人数据
  */
    public function saveprjworkerlist(){
        $qryinfo=request() -> param();

        $prjlogic=new prjmanger();
        $qryinfo['workerlist']=htmlspecialchars_decode($qryinfo['workerlist']);
        $wokerlist=json_decode($qryinfo['workerlist'],true);
        $prjinfo=$prjlogic->changerelation($qryinfo['pid'],$wokerlist);

        if ($prjinfo){
            $res_r['code']=200;
            $res_r['resultData']=$prjinfo;
        }else{
            $res_r['code']=0;
            $res_r['errmsg']='修改失败';
        }

        return json_encode($res_r,320);
    }

    //项目信息导出execl
    public function outtoexecle(){
        //获取数据
        $prjlogic=new prjmanger();
        $p_list=$prjlogic->getallprj();
        //加载类库
        vendor('PHPExcel.PHPExcel');
        $objPHPExcel = new \PHPExcel();
        //4.激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'ID')
            ->setCellValue('B1', '项目名称')
            ->setCellValue('C1', '项目说明');
        //设置A列水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //设置单元格宽度
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(50);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(100);
        //6.循环刚取出来的数组，将数据逐一添加到excel表格。
        for($i=0;$i<count($p_list);$i++){
            $objPHPExcel->getActiveSheet()->setCellValue('A'.($i+2),$p_list[$i]['id']);//ID
            $objPHPExcel->getActiveSheet()->setCellValue('B'.($i+2),$p_list[$i]['pname']);//名称
            $objPHPExcel->getActiveSheet()->setCellValue('C'.($i+2),$p_list[$i]['pdisstr']);//说明
        }
        //7.设置保存的Excel表格名称
        $filename = '项目信息表'.date('ymd',time()).'.xls';
        //8.设置当前激活的sheet表格名称；
        $objPHPExcel->getActiveSheet()->setTitle('项目信息');
        //9.设置浏览器窗口下载表格
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="'.$filename.'"');
        //生成excel文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //下载文件在浏览器窗口
        $objWriter->save('php://output');
        //原代码没有返回
        $res_r['code']=200;
        $res_r['resultData']=$filename;
        return json_encode($res_r,320);
    }

    //本公司项目信息导出execl
    public function outprojecttoexecle(){
        $qryinfo=request() -> param();
        //获取数据
        $prjlogic=new prjmanger();
       // $p_list=$prjlogic->getallprj();
        $p_list=$prjlogic->getallprjbycid($qryinfo);
        //加载类库
        vendor('PHPExcel.PHPExcel');
        $objPHPExcel = new \PHPExcel();
        //4.激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'ID')
            ->setCellValue('B1', '项目名称')
            ->setCellValue('C1', '项目说明');
        //设置A列水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //设置单元格宽度
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(50);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(100);
        //6.循环刚取出来的数组，将数据逐一添加到excel表格。
        for($i=0;$i<count($p_list);$i++){
            $objPHPExcel->getActiveSheet()->setCellValue('A'.($i+2),$p_list[$i]['id']);//ID
            $objPHPExcel->getActiveSheet()->setCellValue('B'.($i+2),$p_list[$i]['pname']);//名称
            $objPHPExcel->getActiveSheet()->setCellValue('C'.($i+2),$p_list[$i]['pdisstr']);//说明
        }
        //7.设置保存的Excel表格名称
        $filename = '项目信息表'.date('ymd',time()).'.xls';
        //8.设置当前激活的sheet表格名称；
        $objPHPExcel->getActiveSheet()->setTitle('项目信息');
        //9.设置浏览器窗口下载表格
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="'.$filename.'"');
        //生成excel文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //下载文件在浏览器窗口
        $objWriter->save('php://output');
        //原代码没有返回
        $res_r['code']=200;
        $res_r['resultData']=$filename;
        return json_encode($res_r,320);
    }
    /*
     * 创建班组
     */
    public function creategroup(){
        $groupinfo=request() -> param();

        $p_logic=new prjmanger();
        $group=$p_logic->creategroup($groupinfo);

        if ($group){
            $res_r['code']=200;
            $res_r['resultData']['code']=200;
            $res_r['resultData']['group']=$group;
        }else{
            $res_r['code']=0;
            $res_r['resultData']['code']=0;
            $res_r['resultData']['errmsg']='创建失败';
        }

        return json_encode($res_r,320);

    }

    /*
     * steelxu
     * 20200211
     * 指定班组长
     */

    public function setgroupdutyer(){
        $dutyerinfo=request() -> param();

        $p_logic=new headmanger();
        $group=$p_logic->setghead($dutyerinfo);

        if ($group){
            $res_r['code']=200;
            $res_r['resultData']=$group;
        }else{
            $res_r['code']=0;
            $res_r['errmsg']='修改失败';
        }

        return json_encode($res_r,320);

    }

    /*
     * steelxu
     * 20200211
     * 获取项目下所有班组信息
     */

    public function getglist(){
        $qureyinfo=request() -> param();
        $p_logic=new prjmanger();
        $group=$p_logic->getglist($qureyinfo['pid']);

        if ($group){
            $res_r['code']=200;
            $res_r['resultData']=$group;
        }else{
            $res_r['code']=0;
            $res_r['errmsg']='查询失败';
        }

        return json_encode($res_r,320);
    }

     /*
      * 20200212
      * steelxu
      * 获取项目下所有工人
      */
    public function getprjworker(){
        $queryinfo=request() -> param();
        $pagenum=$queryinfo['pageNum'];
        $pagesize=$queryinfo['pageSize'];
        $pid=$queryinfo['pid'];
        $workerlogic=new workermanger();
        $w_list=$workerlogic->getprjworkerlist($pagenum,$pagesize,$pid);

        if ($w_list){
            $res_r['code']=200;
            $res_r['resultData']['list']=$w_list;
        }else{
            $res_r['code']=0;
            $res_r['errmsg']='查询失败';
        }

        return json_encode($res_r,320);
    }

    /*
     * 20200308
     * 提交新合同信息
     */

    public function submitcontact(){
        $queryinfo=request() -> param();
        $l_contact=new contactlogic();

        $rid=$l_contact->savecontact($queryinfo);
        if ($rid>0){
            $res_r['code']=200;
            $res_r['resultData']['code']=200;
            $res_r['resultData']['newid']=$rid;
        }else{
            $res_r['code']=0;
            $res_r['msg']='提交失败';
        }


        return json_encode($res_r,320);

    }

    /*
     * 20200308
     * 获取合同列表
     */
    public function querycontactbypid(){
        $queryinfo=request() -> param();
        $l_contact=new contactlogic();
        $rlist=$l_contact->getclistbypid($queryinfo);

        if ($rlist){
            $res_r['code']=200;
            $res_r['resultData']['list']=$rlist;
        }else{
            $res_r['code']=0;
            $res_r['errmsg']='查询失败';
        }

        return json_encode($res_r,320);

    }

    /*
     * 20200309
     * 处理提交的班组合同关系
     */

    public function submitgcrelation(){
        $queryinfo=request() -> param();
        $l_contact=new contactlogic();
        $rid=$l_contact->savegcrelation($queryinfo);
        if ($rid>0){
            $res_r['code']=200;
            $res_r['resultData']['code']=200;
            $res_r['resultData']['newid']=$rid;
        }else{
            $res_r['code']=0;
            $res_r['msg']='提交失败';
        }


        return json_encode($res_r,320);
    }

    /*
     * 20200310
     * 请求项目下所有合同及合同总记分
     */

    public function querycwithscorebypid(){
        $queryinfo=request() -> param();
        $l_contact=new contactlogic();

        $rlist=$l_contact->getctotalbypid($queryinfo);

        if ($rlist){
            $res_r['code']=200;
            $res_r['resultData']['list']=$rlist;
        }else{
            $res_r['code']=0;
            $res_r['errmsg']='查询失败';
        }

        return json_encode($res_r,320);
    }

    /*
     * 20200331
     * 更新班组名称
     */

    public function subgroupname(){
        $groupinfo=request() -> param();
        $p_logic=new prjmanger();
        $rid=$p_logic->updategroupinfo($groupinfo);

        if ($rid){
            $res_r['code']=200;
            $res_r['resultData']=$rid;
        }else{
            $res_r['code']=0;
            $res_r['msg']='更新失败';
        }

        return json_encode($res_r,320);

    }

    /*
 * 20210228
 * steelxu
 *  小程序端提交项目工人数据
 */
    public function subleavejobworklist(){
        $qryinfo=request() -> param();

        $prjlogic=new prjmanger();
        $qryinfo['workerlist']=htmlspecialchars_decode($qryinfo['workerlist']);
        $wokerlist=json_decode($qryinfo['workerlist'],true);
        $prjinfo=$prjlogic->leavejob($wokerlist);

        if ($prjinfo){
            $res_r['code']=200;
            $res_r['resultData']=$prjinfo;
        }else{
            $res_r['code']=0;
            $res_r['errmsg']='修改失败';
        }

        return json_encode($res_r,320);
    }

    /*
* 20210228
* steelxu
*  小程序端端提交项目调动
*/
    public function subtransprjworklist(){
        $qryinfo=request() -> param();

        $prjlogic=new prjmanger();
        $qryinfo['workerlist']=htmlspecialchars_decode($qryinfo['workerlist']);
        $wokerlist=json_decode($qryinfo['workerlist'],true);
        $headid=$qryinfo['headid'];
        $prjinfo=$prjlogic->transferprj($headid,$wokerlist);

        if ($prjinfo){
            $res_r['code']=200;
            $res_r['resultData']=$prjinfo;
        }else{
            $res_r['code']=0;
            $res_r['errmsg']='修改失败';
        }

        return json_encode($res_r,320);
    }

    /*
     * 20210725
     */
    public function queryprjwcount(){

        $p_logic=new prjmanger();
        $rid=$p_logic->getworkercountbypid();

        if ($rid){
            $res_r['code']=200;
            $res_r['resultData']=$rid;
        }else{
            $res_r['code']=0;
            $res_r['msg']='查询失败';
        }

        return json_encode($res_r,320);
    }


    /*
     * 20210807
     */
    public function autoleavejobwithunscore(){
        $l_prj=new prjmanger();
        $l_prj->autoleavejobwithweek();
    }


    /*
     * 20230302
     */
    public function subnewprjlocationforapp(){
        $queryinfo=request()->param();
        $l_p=new prjmanger();
        $rlist=$l_p->subnewprjlocation($queryinfo);

        if (!$rlist){
            $res_r['code']=0;
            $res_r['msg']="项目坐标不重复采集";
            $res_r['data']=$rlist;
        }else{
            $res_r['code']=200;
            $res_r['msg']="提交成功";
            $res_r['data']=$rlist;
        }

        return json_encode($res_r,320);
    }

    /*
     * 20230303
     * 请求某一工地的记录规则
     */
    public function queryprjlocationrecrulebypid(){
        $queryinfo=request()->param();
        $l_p=new prjmanger();
        $rlist=$l_p->getprjlocationinfo($queryinfo);

        if (!$rlist){
            $res_r['code']=0;
            $res_r['msg']="请求失败";
            $res_r['data']=$rlist;
        }else{
            $res_r['code']=200;
            $res_r['msg']="请求成功";
            $res_r['data']=$rlist;
        }

        return json_encode($res_r,320);
    }
}