<?php
namespace app\mainapp\controller;

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

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

use app\common\validate\Supply as SupplyValidate;
use think\exception\ValidateException;

class Wsupply extends BaseController
{
		
	public function statusSupply()
	{
		$userid = input('userid/d', 0);
		$workerid = input('workerid/d', 0);
		$user = UserModel::where(1)->findOrEmpty($userid);
		$worker = WorkerModel::where(['userid'=>$userid])->findOrEmpty($workerid);
		if ( $user->isEmpty() || $worker->isEmpty() ){
			page_result(1, "用户或公司信息不存在。");
		}
		$supplyid = input('supplyid/d', 0);
		$supply = SupplyModel::where(1)->findOrEmpty($supplyid);
		if ( $supply->isEmpty() ){
			page_result(1, "招聘订单信息不存在。");
		}
		$status = input('status/d', 3);
		$supply->save([
			'status'	=> $status
		]);
		page_result(0, "", array());
	}
	
	public function updateSupply()
	{
		$userid = input('userid/d', 0);
		$workerid = input('workerid/d', 0);
		$user = UserModel::where(1)->findOrEmpty($userid);
		$worker = WorkerModel::where(['userid'=>$userid])->findOrEmpty($workerid);
		if ( $user->isEmpty() || $worker->isEmpty() ){
			page_result(1, "用户或公司信息不存在。");
		}
		$param = ParamModel::where(1)->findOrEmpty();
		if ($user->integral < $param->topsupply){
			page_result(1, "每次置顶招聘订单信息需要扣除".$param->topsupply."积分，你当前积分不足。");
		}
		$supplyid = input('supplyid/d', 0);
		$supply = SupplyModel::where(1)->findOrEmpty($supplyid);
		if ( $supply->isEmpty() ){
			page_result(1, "招聘信息不存在。");
		}
		$supply->save(['updatetime'=>time()]);
		$intdata = array(
		    'userid'		=> $userid,
		    'title'			=> "置顶招聘订单信息扣除",
		    'intvalue'		=> 0 - $param->topsupply,
		    'intmoney'		=> 0.00,
			'onlycontent'	=> "",
		    'remark'		=> input('title/s', ""),
		    '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->topsupply);
		$user->save([
			'integral'	=> $integral
		]);
		page_result(0, "", array());
	}
	
	public function listSupply()
	{
		$status = input('status/d', 1);
		$workerid = input('workerid/d', 0);
		$ppage = input('ppage/d', 1);
		$psize = input('psize/d', 20);
		$map = array();
		$map[] = ['workerid','=',$workerid];
		if (!empty($status)){
			$map[] = ['status','=',$status];
		}
		$orderby = array('updatetime'=>'desc', 'createtime'=>'desc', 'id'=>'desc');
		$plist = SupplyModel::with(['worker'])->where($map)->order($orderby)->page($ppage)->limit($psize)->append(['ftype_text'])->select();
		foreach($plist as $key=>$row){
		    $plist[$key]->updatetime_text = tranTime(strtotime($row->updatetime));
		}
		$param = ParamModel::where(1)->findOrEmpty();
		page_result(0, "", array(
			'param' => $param,
			'plist' => $plist,
			'pstatus' => $psize > count($plist) ? 'noMore' : 'more'
			));
	}
	
	public function getSupply()
	{
		$workerid = input('workerid/d', 0);
		$supplyid = input('supplyid/d', 0);
		$supply = SupplyModel::where('workerid','=',$workerid)->where('id','=',$supplyid)->findOrEmpty();
		if ($supply->isEmpty()){
			$supply = "NULL";
		}
		page_result(0, "", array(
			'supply'		=> $supply
			));
	}
	
	
	public function editSupply() 
	{
		$userid = input('userid/d', 0);
		$workerid = input('workerid/d', 0);
		$user = UserModel::where(1)->findOrEmpty($userid);
		$worker = WorkerModel::where(['userid'=>$userid])->findOrEmpty($workerid);
		if ( $user->isEmpty() || $worker->isEmpty() ){
			page_result(1, "用户或公司信息不存在。");
		}
		$param = ParamModel::where(1)->findOrEmpty();
		$id = input('id/d', 0); 
		$wtype = input('wtype/d', 1);
		$zwagall = $wtype==1 ? input('zwagall/s', "") : '';
		$data = [
			'workerid'			=> input('workerid/d', 0),
			'mnumber'			=> input('mnumber/d', 0),
			'wnumber'			=> input('wnumber/d', 0),
			'agegroup'			=> input('agegroup/s', ""),
			'province'			=> input('province/s', ""),
			'city'				=> input('city/s', ""),
			'district'			=> input('district/s', ""),
			'descity'			=> input('descity/s', ""),
			'candate'			=> input('candate/s', ""),
			'telephone'			=> input('telephone/s', ""),
			'remark'			=> input('remark/s', "")
		];
		try {
		    validate(SupplyValidate::class)->check($data);
		} catch (ValidateException $e) {
			page_result(1, $e->getError());
		}
		if ($id==0) {
			$data = array_merge( $data, [
				'status'			=> 1,
				'priority'			=> 0,
				'updatetime'		=> date("Y-m-d H:i:s"),
				'createtime'		=> date("Y-m-d H:i:s"),
				'volume'			=> 0,
				'telearr'			=> array()
			]);
			if ($user->integral < $param->addsupply){
				page_result(1, "每次发布供人信息需要扣除".$param->addsupply."积分，你当前积分不足。");
			}
			$supply = SupplyModel::create($data);
			$intdata = array(
			    'userid'		=> $userid,
			    'title'			=> "发布供人信息扣除",
			    'intvalue'		=> 0 - $param->addsupply,
			    'intmoney'		=> 0.00,
				'onlycontent'	=> "",
			    'remark'		=> "男".input('mnumber/d', 0)."人，女".input('wnumber/d', 0)."人，".input('agegroup/s', ""), 
			    '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
			]);
		}else{
			$supply = SupplyModel::where('id','=',$id)->findOrEmpty();
			$supply->save($data);
		}
		page_result(0, "", array(
			'supply'		=> $supply
			));
	}
	
}