<?php
namespace app\mainapp\controller;

use app\common\service\IncomeService;
use app\mainapp\BaseController;

use \app\common\model\Resident as ResidentModel;
use \app\common\model\ComjobsReport as ComjobsReportModel;
use \app\common\model\Broker as BrokerModel;
use \app\common\model\ResidentLog as ResidentLogModel;


class Resident extends BaseController
{

    public function comjobsReport()
    {
        $ppage    = input('ppage/d', 1);
        $psize    = input('psize/d', 20);
        $map      = [];
        $residentid = input('residentid/d', 0);
        $resident = ResidentModel::find($residentid);
        if (empty($resident)) {
            page_result(1,'没有权限查看');
        }
        $brokerIds = BrokerModel::where('workerid',$resident['workerid'])->column('id');
        if (empty($brokerIds)) {
            page_result(0, "", [
                'plist'   => [],
                'pstatus' => 'noMore',
            ]);
        }
        $map[] = ['brokerid','in',$brokerIds];
        $plist = ComjobsReportModel::with(['comjobs', 'broker'])->where($map)->order(['createtime' => 'desc', 'id' => 'desc'])->page($ppage)->limit($psize)->append(['status_text'])->select();
        page_result(0, "", [
            'plist'   => $plist,
            'pstatus' => $psize > count($plist) ? 'noMore' : 'more',
        ]);
    }

    public function comjobsSetReportStatus()
    {
        $residentid = input('residentid/d', 0);
        $id       = input('id/d', 0);
        $report   = ComjobsReportModel::with(['comjobs', 'broker'])->where(['id' => $id])->append(['status_text'])->findOrEmpty();
        if ($report->isEmpty()) {
            page_result(1, "报备信息不存在。");
        }

        //悬赏金
        $status    = input('status/d', 1);
        if ($status['status'] <= $report['status']) {
            page_result(1, "状态只能往后选。");
        }
        if ($status == 3) {
            $incomeService = new IncomeService();
            $incomeService->add($report['brokerid'], $report['comjobs']['retmoney'], '悬赏金', '悬赏招聘推荐入职成功后返佣金额');
        }

        //更改状态
        $retremark = $status == 4 ? input('retremark/s', "") : "";
        $report->save([
            'status'    => $status,
            'retremark' => $retremark,
        ]);

        //增加操作记录
        $status_text = [1=>'待审核', 2=>'待面试', 3=>'已入职', 4=>'无效报备'];
        ResidentLogModel::create([
            'workerid' => $report['workerid'],
            'residentid' => $residentid,
            'content' => "修改状态为“{$status_text[$status]}”，报备id为{$id}",
            'createtime' => time(),
        ]);
        page_result(0, "", [
            'report' => $report,
        ]);
    }

    public function listDemand()
    {
        $status   = input('status/d', 1);
        $ppage    = input('ppage/d', 1);
        $psize    = input('psize/d', 20);
        $map      = [];
        $residentid = input('residentid/d', 0);
        $resident = ResidentModel::find($residentid);
        if (empty($resident)) {
            page_result(1,'没有权限查看');
        }
        $brokerIds = BrokerModel::where('workerid',$resident['workerid'])->column('id');
        if (empty($brokerIds)) {
            page_result(0, "", [
                'plist'   => [],
                'pstatus' => 'noMore',
            ]);
        }
        $map[] = ['brokerid','in',$brokerIds];
        if (!empty($status)) {
            $map[] = ['status', '=', $status];
        }

        $orderby = ['updatetime' => 'desc', 'createtime' => 'desc', 'id' => 'desc'];
        $plist   = DemandModel::with(['demandCate'])->where($map)->order($orderby)->page($ppage)->limit($psize)->append(['ftype_text'])->select();
        $param   = ParamModel::where(1)->findOrEmpty();
        page_result(0, "", [
            'param'   => $param,
            'plist'   => $plist,
            'pstatus' => $psize > count($plist) ? 'noMore' : 'more',
        ]);
    }
}