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


use app\adminapi\lists\BaseAdminDataLists;
use app\common\lists\ListsExcelInterface;
use app\common\model\material\MaterialPurchaseRequestDetail;
use app\common\model\project\Project;
use app\common\model\project\ProjectMaterialBudget;
use app\common\lists\ListsSearchInterface;
use app\common\model\project\ProjectMaterialBudgetDetail;


/**
 * 材料预算列表
 * Class ProjectMaterialBudgetLists
 * @package app\adminapi\listsproject
 */
class ProjectMaterialBudgetLists extends BaseAdminDataLists implements ListsSearchInterface,ListsExcelInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/01/08 16:30
     */
    public function setSearch(): array
    {
        return [
            '=' => ['project_id', 'material_id', 'budget_type'],

        ];
    }


    /**
     * @notes 获取材料预算列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/01/08 16:30
     */
    public function lists(): array
    {
        return ProjectMaterialBudget::where($this->searchWhere)
            ->field('id,project_id,material_budget_code,remark,annex')
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['id' => 'desc'])
            ->select()->each(function($data){
		        $project = Project::field('name,project_code')->where('id',$data['project_id'])->findOrEmpty();
		        $data['project_name'] = $project['name'];
		        $data['project_code'] = $project['project_code'];
				//预算总数量
				$data['total_num'] = ProjectMaterialBudgetDetail::where('material_budget_id',$data['id'])->sum('num');
				//预算总金额
				$data['total_amount'] = ProjectMaterialBudgetDetail::where('material_budget_id',$data['id'])->sum('amount');
				//申购总数量
		        $ProjectMaterialBudgetDetailIds = ProjectMaterialBudgetDetail::where('material_budget_id',$data['id'])->column('id');
				$data['total_apply_num'] = MaterialPurchaseRequestDetail::where('project_material_budget_detail_id','in',$ProjectMaterialBudgetDetailIds)->sum('num');
				//剩余预算总数量
				$data['total_residual_num'] = $data['total_num'] - $data['total_apply_num'];
				return $data;
	        })
            ->toArray();
    }


    /**
     * @notes 获取材料预算数量
     * @return int
     * @author likeadmin
     * @date 2024/01/08 16:30
     */
    public function count(): int
    {
        return ProjectMaterialBudget::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",
			"material_budget_code" => "预算单号",
            "project_name" => "项目名称",
            "project_code" => "项目编码",
            "total_num" => "预算总数量",
            "total_apply_num" => "申请总数量",
            "total_residual_num" => "剩余预算数量",
            "total_amount" => "预算金额",
            "remark" => "备注",
		];
	}

}