<?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\bid;


use app\adminapi\lists\BaseAdminDataLists;
use app\common\model\auth\Admin;
use app\common\model\bid\BidBiddingDecision;
use app\common\model\bid\BidBuyBiddingDocument;
use app\common\model\bid\BidDocumentExamination;
use app\common\lists\ListsSearchInterface;
use app\common\model\bid\BidDocumentExaminationDetail;
use app\common\model\custom\Custom;
use app\common\model\project\Project;


/**
 * 标书审查列表
 * Class BidDocumentExaminationLists
 * @package app\adminapi\listsbid
 */
class BidDocumentExaminationLists extends BaseAdminDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2023/12/02 09:52
     */
    public function setSearch(): array
    {
        return [
            '=' => ['project_id'],
	        '%like%' => ['code']
        ];
    }


    /**
     * @notes 获取标书审查列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2023/12/02 09:52
     */
    public function lists(): array
    {
		$params = $this->request->get(['bid_document_no','project_name']);
		$where = [];
		if(isset($params['bid_document_no']) && $params['bid_document_no'] != ''){
			$bid_doc_ids = BidBuyBiddingDocument::where('bid_document_no','like','%'.$params['bid_document_no'].'%')->column('id');
			$where[] = ['buy_bidding_document_id','in',$bid_doc_ids];
		}
	    if(isset($params['project_name']) && $params['project_name'] != ''){
		    $project_ids = Project::where('name','like','%'.$params['project_name'].'%')->column('id');
		    $where[] = ['project_id','in',$project_ids];
	    }
	    $field = 'id,code,project_id,buy_bidding_document_id';
	    return BidDocumentExamination::where($this->searchWhere)->where($where)
            ->field($field)->limit($this->limitOffset, $this->limitLength)
            ->order(['id' => 'desc'])
            ->select()->each(function($item){
			    $project = Project::field('custom_id,name,project_code')->where('id',$item['project_id'])->findOrEmpty();
			    $custom = Custom::field('name')->where('id',$project['custom_id'])->findOrEmpty();
			    $bid_buy_doc = BidBuyBiddingDocument::field('bid_decision_id,bid_document_no,invite_tenders_company_name,bid_company_name,buyer,buy_date,bid_address')->where('id',$item['buy_bidding_document_id'])->findOrEmpty();
			    $bid_decision = BidBiddingDecision::field('bidding_project_fund_source,bidding_time,bid_type,is_margin,margin_amount,bid_opening_date')->where('id',$bid_buy_doc['bid_decision_id'])->findOrEmpty();
			    $buyer = Admin::field('name')->where('id',$bid_buy_doc['buyer'])->findOrEmpty();
			    $item['project_name'] = $project['name'];
			    $item['project_code'] = $project['project_code'];
			    $item['custom_name'] = $custom['name'];
			    $item['bid_document_no'] = $bid_buy_doc['bid_document_no'];
			    $item['invite_tenders_company_name'] = $bid_buy_doc['invite_tenders_company_name'];
			    $item['bid_company_name'] = $bid_buy_doc['bid_company_name'];
			    $item['bid_address'] = $bid_buy_doc['bid_address'];
			    $item['bidding_project_fund_source'] = $bid_decision->bidding_project_fund_source_text;
			    $item['buyer'] = $buyer['name'];
			    $item['bidding_time'] = $bid_decision['bidding_time'];
			    $item['bid_type'] = $bid_decision->bid_type_text;
			    $item['is_margin'] = $bid_decision->is_margin_text;
			    $item['margin_amount'] = $bid_decision['margin_amount'];
			    $item['bid_opening_date'] = $bid_decision['bid_opening_date'];
				$item['total_amount'] = BidDocumentExaminationDetail::where('bid_document_examination_id',$item['id'])->sum('sale_amount');
		        return $item;
	        })
            ->toArray();
    }


    /**
     * @notes 获取标书审查数量
     * @return int
     * @author likeadmin
     * @date 2023/12/02 09:52
     */
    public function count(): int
    {
	    $params = $this->request->get(['bid_document_no','project_name']);
	    $where = [];
	    if(isset($params['bid_document_no']) && $params['bid_document_no'] != ''){
		    $bid_doc_ids = BidBuyBiddingDocument::where('bid_document_no','like','%'.$params['bid_document_no'].'%')->column('id');
		    $where[] = ['buy_bidding_document_id','in',$bid_doc_ids];
	    }
	    if(isset($params['project_name']) && $params['project_name'] != ''){
		    $project_ids = Project::where('name','like','%'.$params['project_name'].'%')->column('id');
		    $where[] = ['project_id','in',$project_ids];
	    }
        return BidDocumentExamination::field('id')->where($this->searchWhere)->where($where)->count();
    }

}