<?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\bank\BankAccount;
use app\common\model\bid\BidBiddingDecision;
use app\common\model\bid\BidSecurityApply;
use app\common\model\bid\BidSecurityRefund;
use app\common\lists\ListsSearchInterface;
use app\common\model\custom\Custom;
use app\common\model\project\Project;
use think\facade\Db;

/**
 * BidSecurityRefund列表
 * Class BidSecurityRefundLists
 * @package app\adminapi\listsbid
 */
class BidSecurityRefundLists extends BaseAdminDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2023/12/18 10:29
     */
    public function setSearch(): array
    {
        return [
            '=' => ['bid_security_apply_id', 'project_id', 'bank_account_id'],
        ];
    }


    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2023/12/18 10:29
     */
    public function lists(): array
    {
        return BidSecurityRefund::where($this->searchWhere)
            ->field('id,project_id,bid_security_apply_id,refund_amount,refund_date,remark,annex,bank_account_id,create_time')
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['id' => 'desc'])
            ->select()->each(function($data){
		        $project = Project::field('custom_id,name,project_code')->where('id',$data['project_id'])->findOrEmpty();
		        $custom = Custom::field('name')->where('id',$project['custom_id'])->findOrEmpty();
		        $bid_security_apply = BidSecurityApply::field('bidding_decision_id')->where('id',$data['bid_security_apply_id'])->findOrEmpty();
		        $bidding_decision = BidBiddingDecision::field('code,bidding_time')->where('id',$bid_security_apply['bidding_decision_id'])->findOrEmpty();
		        $data['bidding_decision_code'] = $bidding_decision['code'];
		        $data['custom_name'] = $custom['name'];
		        $data['project_name'] = $project['name'];
		        $data['project_code'] = $project['project_code'];
		        $data['bidding_time'] = $bidding_decision['bidding_time'];
		        $data['bank_account_info'] = BankAccount::field('account_sn,deposit_bank,account_name,account')->where('id',$data['bank_account_id'])->findOrEmpty();
                return $data;
            })
            ->toArray();
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2023/12/18 10:29
     */
    public function count(): int
    {
        return BidSecurityRefund::where($this->searchWhere)->count();
    }

}