<?php
namespace app\mainapp\controller;

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

use app\common\model\User as UserModel;
use app\common\model\Supply as SupplyModel;
use app\common\model\Param as ParamModel;
use app\common\model\Worker as WorkerModel;
use app\common\model\WorkerLog as WorkerLogModel;
use app\common\model\UserIntegral as UserIntegralModel;

use echowx\WxProgram;

class Supply extends BaseController
{
	
	// 供人总列表
	public function listSupply()
	{
		$ppage = input('ppage/d', 1);
		$psize = input('psize/d', 20);
		$map = array();
		$map[] = ['createtime','<=',time()];
		$map[] = ['status','in','3,4'];
		$searchval = input('searchval/s', "");
		if (!empty($searchval)){
			$map[] =['descity', 'like', '%'.$searchval.'%'];
		}
		$orderby = array('status'=>'asc', 'updatetime'=>'desc', 'id'=>'desc');
		$plist = SupplyModel::with(['worker'])->where($map)->order($orderby)->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 getSupply()
	{
		$supplyid = input('supplyid/d', 0);
		$supply = SupplyModel::with(['worker'])->where('status','in','3,4')->where('id','=',$supplyid)->findOrEmpty(); 
		if ($supply->isEmpty()){
			page_result(1, "供人信息不存在。");
		}
		$supply->updatetime_text = tranTime(strtotime($supply->updatetime));
		$supply->inc('volume', 1)->update();
		$supplylist = SupplyModel::with(['worker'])->where(['status'=>3,'workerid'=>$supply->workerid])->where('createtime','<=',time())->order(['updatetime'=>'desc','createtime'=>'desc','id'=>'desc'])->limit(5)->select();
		foreach($supplylist as $key=>$row){
		    $supplylist[$key]->updatetime_text = tranTime(strtotime($row->updatetime));
		}
		$userid = input('userid/d', 0);
		$workerlog = WorkerLogModel::where(['workerid'=>$supply->workerid,'userid'=>$userid])->whereDay('createtime')->findOrEmpty();
		if ($workerlog->isEmpty()){
			WorkerLogModel::create([ 
				'workerid'		=> $supply->workerid,
				'userid'		=> $userid,
				'ltotal'		=> 1,
				'createtime'	=> date("Y-m-d")
			]);
		}else{
			$workerlog->inc('ltotal', 1)->update(); 
		}
		$iscantele = in_array($userid, $supply->telearr);
		$param = ParamModel::where(1)->findOrEmpty();
		page_result(0, "", array(
			'param'			=> $param,
			'supply'		=> $supply,
			'iscantele'		=> $iscantele,
			'supplylist'	=> $supplylist
			));
	}
	
	
	
	public function teleSupply()
	{
		$supplyid = input('supplyid/d', 0);
		$supply = SupplyModel::findOrEmpty($supplyid);
		if ($supply->isEmpty()){
			page_result(1, "订单信息不存在");
		}
		$userid = input('userid/d', 0);
		if (in_array($userid, $supply->telearr)){
			page_result(0, "", array(
				'iscantele'		=> true
			));
		}
		$user = UserModel::where(1)->findOrEmpty($userid);
		$param = ParamModel::where(1)->findOrEmpty();
		if ($user->integral < $param->telsupply){
			page_result(1, "拨打咨询电话会扣除".$param->telsupply."聘豆，你当前聘豆不足。");
		}
		$intdata = array(
		    'userid'		=> $userid,
		    'title'			=> "电话咨询供人信息扣除",
		    'intvalue'		=> 0 - $param->telsupply,
		    'intmoney'		=> 0.00,
			'onlycontent'	=> "",
		    'remark'		=> "男性".$supply->mnumber."人，女性".$supply->wnumber."人【".$supply->agegroup."】",
		    'itype'			=> 6,
			'status'		=> 2,
		    'createtime'	=> date("Y-m-d H:i:s"),
		    'yeartime'		=> date("Y"),
		    'monthtime'		=> date("Ym")
		);
		UserIntegralModel::create($intdata);
		$integral = intval($user->integral) - intval($param->addsupply);
		$user->save([
			'integral'	=> $integral
		]);
		$telearr = $supply->telearr;
		$telearr[] = $userid;
		$supply->save([
			'telearr' => $telearr
		]);
		page_result(0, "", array(
			'iscantele'		=> true
		));
	}
	
	

}