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

/**
 * BidSecurityApply列表
 * Class BidSecurityApplyLists
 * @package app\adminapi\listsbid
 */
class BidSecurityApplyLists extends BaseAdminDataLists implements ListsSearchInterface,ListsExcelInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2023/12/16 10:46
     */
    public function setSearch(): array
    {
        return [
            '=' => ['bidding_decision_id', 'project_id', 'pay_type'],
	        '%like%' => ['applier']
        ];
    }


    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2023/12/16 10:46
     */
    public function lists(): array
    {
        return BidSecurityApply::where($this->searchWhere)
            ->field('id,security_apply_code,project_id,bidding_decision_id,applier,refund_date,create_time')
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['id' => 'desc'])
            ->select()->each(function($data){
		        $bidding_decision = BidBiddingDecision::field('code,margin_amount,bidding_time')->where('id',$data['bidding_decision_id'])->findOrEmpty();
		        $project = Project::field('custom_id,name,project_code')->where('id',$data['project_id'])->findOrEmpty();
		        $custom = Custom::field('name')->where('id',$project['custom_id'])->findOrEmpty();
		        $data['bidding_decision_code'] = $bidding_decision['code'];
		        $data['project_name'] = $project['name'];
		        $data['project_code'] = $project['project_code'];
		        $data['custom_name'] = $custom['name'];
		        $data['bidding_time'] = $bidding_decision['bidding_time'];
				//保证金金额
		        $data['margin_amount'] = $bidding_decision['margin_amount'];
				//已退金额
				$data['has_refund_amount'] = BidSecurityRefund::where('bid_security_apply_id',$data['id'])->sum('refund_amount');
				//未退金额
				$data['not_refund_amount'] = $data['margin_amount'] - $data['has_refund_amount'];
                return $data;
            })
            ->toArray();
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2023/12/16 10:46
     */
    public function count(): int
    {
        return BidSecurityApply::where($this->searchWhere)->count();
    }
	
	public function setFileName(): string
	{
		return '投标保证金列表';
	}
	
	/**
	 * @notes 导出字段
	 * @return string[]
	 * @author 段誉
	 * @date 2022/11/24 16:17
	 */
	public function setExcelFields(): array
	{
		return [
			"id" => "id",
			"security_apply_code" => "投标编号",
            "custom_name" => "客户名称",
            "project_name" => "项目名称",
            "bidding_time" => "投标时间",
            "refund_date" => "预计退还时间",
            "margin_amount" => "保证金金额",
            "applier" => "申请人",
            "has_refund_amount" => "已退金额",
            "not_refund_amount" => "未退金额",
            "create_time" => "创建日期",
		];
	}

}