<?php
namespace app\mainapp\controller;

use app\mainapp\BaseController;
use think\facade\Session;

use app\common\model\Slide as SlideModel;
use app\common\model\User as UserModel;
use app\common\model\UserIntegral as UserIntegralModel;
use app\common\model\Worker as WorkerModel;
use app\common\model\Agent as AgentModel;
use app\common\model\Comjobs as ComjobsModel;
use app\common\model\Demand as DemandModel;
use app\common\model\Supply as SupplyModel;

use app\common\model\UserParam as UserParamModel;

use app\common\model\Param as ParamModel;
use app\common\model\WorkerForm as WorkerFormModel;

use app\common\model\WorkerShareCate as WorkerShareCateModel;
use app\common\model\WorkerShare as WorkerShareModel;

use think\facade\Config;

use app\common\model\WorkerLog as WorkerLogModel;
use echowx\WxProgram;

class Worker extends BaseController
{
	
	// 获客海报 
	public function pageWshare()
	{
		$catelist = WorkerShareCateModel::where(['status'=>1])->order(['priority'=>'desc','id'=>'desc'])->limit(100)->select()->toArray();
		array_unshift( $catelist, array('id'=>0,'title'=>'全部') );
		page_result(0, "", array(
			'catelist'		=> $catelist  
		));
	}
	
	public function listWshare()
	{	
		$ppage = input('ppage/d', 1);
		$psize = input('psize/d', 20);
		$map = array();
		$cateid = input('cateid/d', 0);
		if ($cateid!=0){
			$map[] = ['cateid', '=', $cateid];
		}
		$plist = WorkerShareModel::where($map)->order(['priority'=>'desc','id'=>'desc'])->page($ppage)->limit($psize)->select();
		page_result(0, "", array(
			'plist' => $plist,
			'pstatus' => $psize > count($plist) ? 'noMore' : 'more'
			));
	}
	
	public function getWorkerImage()
	{
		$workerid = input('workerid/d', 0);
		$shareid = input('shareid/d',0);
		$userid = input('userid/d',0);
		$share = WorkerShareModel::findOrEmpty($shareid);
		$user = UserModel::findOrEmpty($userid);
		$worker = WorkerModel::findOrEmpty($workerid);

		if ($share->isEmpty() || $user->isEmpty() || $worker->isEmpty()){
			page_result(1, "企业、海报或用户信息不存在");
		}
		if (empty($worker->tilpic)) {
            page_result(1, "请上传微店图标");
        }

		$filename = $workerid."_".$userid."_".md5(time()).".jpg";
		$dst_workerpic = root_path("public/attachment/shoppic").$filename;
		$url_workerpic = request()->domain()."/attachment/shoppic/".$filename;
		image_copy_image($share->tilpic, $worker->tilpic, 10,590,70,70,$dst_workerpic);

		$wxprogram = new WxProgram();
		$workerpic = $wxprogram->wxacode_get_unlimited($workerid."&".$userid, "pages/worker/shop", 430, "attachment/shoppic/".md5(time()).".jpg");
		image_copy_image($url_workerpic, $workerpic, 320,555,120,120,$dst_workerpic); 
		
		$font = root_path("public/static/images")."msyh.ttf";
		$ftitle = subtext($worker->ftitle, 7);
		image_copy_text($dst_workerpic, $ftitle, $font, 14, 0, 90, 615, 0x00, 0x00, 0x00);
		
		$texttil = input('texttil/s', "");
		if (!empty($texttil)){
			$texttil = subtext($texttil, 12);
			image_copy_text($dst_workerpic, $texttil, $font, 12, 0, 90, 650, 0x33, 0x33, 0x33);
		}
		image_copy_text($dst_workerpic, "扫码查看好工作", $font, 10, 0, 335, 690, 0x33, 0x33, 0x33);
		page_result(0, "", array(
			'workerpic'	=> $url_workerpic
			));
	}
	
	// 嵌入公众号
	public function pageMpaccount()
	{
		$param = ParamModel::where(1)->find();
		page_result(0, "", array(
			'param'		=> $param,
			'appid'		=> Config::get('wxconfig.appId')   
		));
	}
	
	
	// 申请代理门店
	public function pageForm()
	{
		$param = ParamModel::where(1)->find();
		page_result(0, "", array(
			'param'		=> $param
		));
	}
	
	public function addWorker()
	{
		$comname = input('comname/s', "");
		$address = input('address/s', "");
		$details = input('details/s', "");
		$realname = input('realname/s', "");
		$mobile = input('mobile/s', "");
		$comjobs = input('comjobs/s', "");
		if ( empty($comname) || empty($address) || empty($details) || empty($realname) || empty($mobile) || empty($comjobs) ){
			page_result(1, "名称、地址、简介、姓名、手机号、岗位信息均不能为空。");
		}
		$form = new WorkerFormModel;
		$form->save([
			'workerid'			=> input('workerid/d', 0),
			'comname'			=> $comname,
			'address'			=> $address,
			'details'			=> $details,
			'realname'			=> $realname,
			'mobile'			=> $mobile,
			'comjobs'			=> $comjobs,
			'status'			=> 1,
			'remark'			=> "",
			'createtime'		=> time()
		]);
		page_result(0, "", array());
	}
	
	
	// 微店信息
	public function getShopCode()
	{
		$workerid = input('workerid/d',0);
		$parentid = input('parentid/d',0);
		$wxprogram = new WxProgram();
		$shopcode = $wxprogram->wxacode_get_unlimited($workerid."&".$parentid, "pages/worker/shop", 430, "attachment/shoppic/".md5(time()).".jpg");
		page_result(0, "", array(
			'shopcode'	=> $shopcode
			));
	}
	
	public function pageShop()
	{
		$workerid = input('workerid/d', 0);
		$shopworker = WorkerModel::where(['id'=>$workerid])->findOrEmpty();
		if ( $shopworker->isEmpty() ){
			page_result(1, "用户或公司信息不存在。");
		}
		$countarr['comjobs'] = ComjobsModel::where('workerid', '=', $workerid)->where('status', 'in', '3,4')->count();
		$countarr['demand'] = DemandModel::where('workerid', '=', $workerid)->where('status', 'in', '3,4')->count();
		$countarr['supply'] = SupplyModel::where('workerid', '=', $workerid)->where('status', 'in', '3,4')->count();
		$userid = input('userid/d', 0);
		$workerlog = WorkerLogModel::where(['workerid'=>$workerid,'userid'=>$userid])->whereDay('createtime')->findOrEmpty();
		if ($workerlog->isEmpty()){
			WorkerLogModel::create([ 
				'workerid'		=> $workerid,
				'userid'		=> $userid,
				'ltotal'		=> 1,
				'createtime'	=> date("Y-m-d")
			]);
		}else{
			$workerlog->inc('ltotal', 1)->update(); 
		}
		page_result(0, "", array(
			'shopworker'	=> $shopworker,
			'countarr'		=> $countarr
			));
	}
	
	public function shopComjobs()
	{
		$ppage = input('ppage/d', 1);
		$psize = input('psize/d', 20);
		$map = array();
		$map[] = ['createtime','<=',time()];
		$map[] = ['status','in','3,4'];
		$workerid = input('workerid/d', 0);
		$map[] = ['workerid', '=', $workerid];
		$plist = ComjobsModel::with(['comjobsCate'])->where($map)->order(['status'=>'asc','updatetime'=>'desc', 'id'=>'desc'])->page($ppage)->limit($psize)->select();
		page_result(0, "", array(
			'plist' => $plist,
			'pstatus' => $psize > count($plist) ? 'noMore' : 'more'
			));
	}
	
	public function shopDemand()
	{
		$ppage = input('ppage/d', 1);
		$psize = input('psize/d', 20);
		$map = array();
		$map[] = ['createtime','<=',time()];
		$map[] = ['status','in','3,4'];
		$workerid = input('workerid/d', 0);
		$map[] = ['workerid', '=', $workerid];
		$plist = DemandModel::with(['demandCate'])->where($map)->order(['status'=>'asc','updatetime'=>'desc', 'id'=>'desc'])->page($ppage)->limit($psize)->append(['ftype_text'])->select();
		page_result(0, "", array(
			'plist' => $plist,
			'pstatus' => $psize > count($plist) ? 'noMore' : 'more'
			));
	}
	
	public function shopSupply()
	{
		$ppage = input('ppage/d', 1);
		$psize = input('psize/d', 20);
		$map = array();
		$map[] = ['createtime','<=',time()];
		$map[] = ['status','in','3,4'];
		$workerid = input('workerid/d', 0);
		$map[] = ['workerid', '=', $workerid];
		$plist = SupplyModel::where($map)->order(['status'=>'asc','updatetime'=>'desc', 'id'=>'desc'])->page($ppage)->limit($psize)->select();
		foreach($plist as $key=>$row){
		    $plist[$key]->updatetime_text = tranTime(strtotime($row->updatetime));
		}
		page_result(0, "", array(
			'plist' => $plist,
			'pstatus' => $psize > count($plist) ? 'noMore' : 'more'
			));
	}
	
	
	// 数据信息
	public function pageWorker()
	{ 
		$userid = input('userid/d', 0);
		$workerid = input('workerid/d', 0);
		$countobj = array();
		$param = ParamModel::where(1)->findOrEmpty();
		$countobj[1] = WorkerLogModel::where('workerid', '=', $workerid)->whereDay('createtime', 'yesterday')->sum('ltotal');
		$countobj[1] = $countobj[1] + $param->defviewtol;  
		$countobj[2] = WorkerLogModel::where('workerid', '=', $workerid)->whereDay('createtime')->sum('ltotal');
		$countobj[2] = $countobj[2] + $param->defviewtol;   
		$countobj[3] = WorkerLogModel::where('workerid', '=', $workerid)->group('userid')->count();
		$slide = SlideModel::where('pagetab','=','worker')->order(['priority'=>'desc','id'=>'desc'])->limit(10)->select();
		$userparam = UserParamModel::where(1)->find();
		$workerintegral = UserIntegralModel::where(['userid'=>$userid, 'onlycontent'=>'RegWorker'.$workerid])->findOrEmpty();
		$intregworker = 0;
		if ($workerintegral->isEmpty() && $userparam->intregworker>0){
			$intregworker = $userparam->intregworker;
		}
		$workerinfo = WorkerModel::where('id','=',$workerid)->findOrEmpty();
		page_result(0, "", array(
			'countobj'		=> $countobj,
			'slide'			=> $slide,
			'picregworker'	=> $userparam->picregworker, 
			'intregworker'	=> $intregworker,
			'workerinfo'	=> $workerinfo
		));
	}
	
	public function getWorkerIntegral()
	{
		$userid = input('userid/d', 0);
		$user = UserModel::findOrEmpty($userid);
		$workerid = input('workerid/d', 0);
		$worker = WorkerModel::where(['id'=>$workerid])->findOrEmpty();
		$workerintegral = UserIntegralModel::where(['userid'=>$userid, 'onlycontent'=>'RegWorker'.$workerid])->findOrEmpty();
		if (!$workerintegral->isEmpty()){
			page_result(1, "你注册的该公司信息已领取过奖励聘豆，请不要重复领取。");
		}
		$userparam = UserParamModel::where(1)->find();
		if ($userparam->intregworker<=0){
			page_result(1, "奖励聘豆值不能小于等于0。");
		}
		$data = array(
		    'userid'		=> $userid,
		    'title'			=> $worker->title."劳务公司注册奖励",
		    'intvalue'		=> $userparam->intregworker,
		    'intmoney'		=> input('intmoney/s', 0.00),
			'onlycontent'	=> 'RegWorker'.$workerid,
		    'remark'		=> $worker->title."注册",
		    'itype'			=> 8,
			'status'		=> 2,
		    'createtime'	=> date('Y-m-d H:i:s'),
		    'yeartime'		=> date("Y"),
		    'monthtime'		=> date("Ym")
		);
		UserIntegralModel::create($data);
		$user->save([
			'integral' => intval($user->integral) + intval($userparam->intregworker),
			'inttotal' => intval($user->inttotal) + intval($userparam->intregworker)
		]);
		page_result(0, "", array( ));
	}
	
	public function listWorker()
	{
		$ppage = input('ppage/d', 1);
		$psize = input('psize/d', 20);
		$map = array();
		$map[] = ['createtime','<=',time()];
		$map[] = ['status','in','4,5'];
		$searchval = input('searchval/s', "");
		if (!empty($searchval)){
			$map[] =['title|ftitle', 'like', '%'.$searchval.'%'];
		}
		$wtype = input('wtype/s', "");
		if (!empty($wtype)){
			$map[] = ['wtype', 'IN', $wtype];
		}
		$orderby = array('status'=>'asc', 'createtime'=>'desc', 'id'=>'desc');
		$plist = WorkerModel::where($map)->order($orderby)->page($ppage)->limit($psize)->append(['wtype_text'])->select();  
		page_result(0, "", array(
			'plist' => $plist,
			'pstatus' => $psize > count($plist) ? 'noMore' : 'more'
			));
	}
	
    //根据会员id获得会员信息
    public function getUserinfoByUserid()
    {
        $userid = input('userid/d', 0);
        $userinfo=UserModel::where(["id"=>$userid])->findOrEmpty()->toArray();
        if($userinfo){
            page_result(0, "", array(
                'userinfo' => $userinfo
            ));
        }else{
            page_result(1, "会员信息不存在", "");
        }
    }
	
	public function updateWorker()
	{
		$id = input('id/d', 0);
		$userid = input('userid/d', 0);
		$worker = WorkerModel::where(['userid'=>$userid])->findOrEmpty($id);
		if ( $worker->isEmpty() ){
			page_result(1, "公司信息不存在。");
		}
		if ( $worker->wtype==2 ){
			page_result(1, "已是派遣公司类型，无须再升级。");
		}
		$picone = input('picone/s', "");
		$pictwo = input('pictwo/s', "");
		$picthr = input('picthr/s', "");
		if ( empty($picone) || empty($pictwo) || empty($picthr) ){
			page_result(1, "相关证件不能为空。");
		}
		$worker->save([
			'picone'		=> $picone,
			'pictwo'		=> $pictwo,
			'picthr'		=> $picthr,
			'status'		=> 4  
		]);
		page_result(0, "", array(
			'worker'	=> $worker
			));
	}
	
	public function editWorker()
	{
		$id = input('id/d', 0);
		$userid = input('userid/d', 0);
		$worker = WorkerModel::where(['userid'=>$userid])->findOrEmpty($id);
		if ( $worker->isEmpty() ){
			page_result(1, "公司信息不存在。");
		}
		$wtype = input('wtype/d', 1);
		$realname = input('realname/s', "");
		$mobile = input('mobile/s', "");
		$province = input('province/s', "");
		$address = input('address/s', "");
		if ( empty($realname) || empty($mobile) || empty($province) || empty($address) ){
			page_result(1, "公司名称，微店名称，姓名手机号，地址信息不能为空。");
		}
		$data = [
			'userid'		=> $userid,
			'tilpic'  		=> input('tilpic/s', ""),
			'realname'  	=> input('realname/s', ""),
			'mobile'    	=> input('mobile/s', ""),
			'weixin'    	=> input('weixin/s', ""),
			'province'		=> input('province/s', ""),
			'city'			=> input('city/s', ""),
			'district'		=> input('district/s', ""),
			'address'		=> input('address/s', ""),
			'details'		=> input('details/s', "")
		];
		$worker->save($data);
		page_result(0, "", array(
			'worker'	=> $worker
			));
	}
	
}