<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台(PHP版)
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用,可去除界面版权logo
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
// | github下载:https://github.com/likeshop-github/likeadmin
// | 访问官网:https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\lists\build;


use app\adminapi\lists\BaseAdminDataLists;
use app\common\model\build\BuildDivision;
use app\common\model\build\BuildPlan;
use app\common\model\build\BuildProcessSettings;
use app\common\model\build\BuildReport;
use app\common\model\build\BuildReportDetail;
use app\common\lists\ListsSearchInterface;
use app\common\model\project\Project;
use app\common\model\project\ProjectPersonnel;


/**
 * 人工明细列表
 * Class BuildReportDetailLists
 * @package app\adminapi\listsbuild
 */
class BuildReportDetailLists extends BaseAdminDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2023/12/22 16:10
     */
    public function setSearch(): array
    {
        return [];
    }


    /**
     * @notes 获取人工明细列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2023/12/22 16:10
     */
    public function lists(): array
    {
	    $params = $this->request->get(['project_id','plan_id']);
	    $where = [];
	    if(isset($params['project_id']) && $params['project_id'] != 0){
		    $plan_ids = BuildPlan::where('project_id',$params['project_id'])->column('id');
			$report_ids = BuildReport::where('plan_id','in',$plan_ids)->column('id');
		    $where[] = ['report_id','in',$report_ids];
	    }
	    if(isset($params['plan_id']) && $params['plan_id'] != 0){
		    $report_ids = BuildReport::where('plan_id',$params['plan_id'])->column('id');
		    $where[] = ['report_id','in',$report_ids];
	    }
        return BuildReportDetail::where($this->searchWhere)->where($where)
            ->field(['id', 'report_id', 'person_id', 'work_num', 'remark'])
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['id' => 'desc'])
            ->select()->each(function($item){
				$report = BuildReport::field('plan_id,report_code,create_time')->where('id',$item['report_id'])->findOrEmpty();
				$plan = BuildPlan::field('project_id,process_id,zy_code,price')->where('id',$report['plan_id'])->findOrEmpty();
				$project = Project::field('name')->where('id',$plan['project_id'])->findOrEmpty();
				$process = BuildProcessSettings::field('division_id,process_step_no,process_step')->where('id',$plan['process_id'])->findOrEmpty();
				$division = BuildDivision::field('subentry_engineering')->where('id',$process['division_id'])->findOrEmpty();
				$person = ProjectPersonnel::field('name,idcard,work_type')->where('id',$item['person_id'])->findOrEmpty();
		        $item['report_code'] = $report['report_code'];
				$item['zy_code'] = $plan['zy_code'];
				$item['project_name'] = $project['name'];
				$item['subentry_engineering'] = $division['subentry_engineering'];
		        $item['process_step_no'] = $process['process_step_no'];
		        $item['process_step'] = $process['process_step'];
				$item['user_name'] = $person['name'];
				$item['user_idcard'] = $person['idcard'];
				$item['user_work_type'] = $person->work_type_text;
				$item['price'] = $plan['price'];
		        $item['amount'] = $plan['price']*$item['work_num'];
		        $item['report_date'] = $report['create_time'];
	        })
            ->toArray();
    }


    /**
     * @notes 获取人工明细数量
     * @return int
     * @author likeadmin
     * @date 2023/12/22 16:10
     */
    public function count(): int
    {
	    $params = $this->request->get(['project_id','plan_id']);
	    $where = [];
	    if(isset($params['project_id']) && $params['project_id'] != 0){
		    $plan_ids = BuildPlan::where('project_id',$params['project_id'])->column('id');
		    $report_ids = BuildReport::where('plan_id','in',$plan_ids)->column('id');
		    $where[] = ['report_id','in',$report_ids];
	    }
	    if(isset($params['plan_id']) && $params['plan_id'] != 0){
		    $report_ids = BuildReport::where('plan_id',$params['plan_id'])->column('id');
		    $where[] = ['report_id','in',$report_ids];
	    }
        return BuildReportDetail::where($this->searchWhere)->where($where)->count();
    }

}