<?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\project\Project;
use app\common\model\project\ProjectCostBudget;
use app\common\model\project\ProjectCostBudgetDetail;
use app\common\lists\ListsSearchInterface;
use app\common\model\project\ProjectCostTempSet;


/**
 * 费用预算明细列表
 * Class ProjectCostBudgetDetailLists
 * @package app\adminapi\listsproject
 */
class ProjectCostBudgetDetailLists extends BaseAdminDataLists implements ListsSearchInterface,ListsExcelInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/01/16 14:32
     */
    public function setSearch(): array
    {
        return [
            '=' => ['project_id', 'cost_budget_id'],
        ];
    }


    /**
     * @notes 获取费用预算明细列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/01/16 14:32
     */
    public function lists(): array
    {
        return ProjectCostBudgetDetail::where($this->searchWhere)
            ->field('id,project_id,cost_budget_id,project_cost_temp_id,unit,amount,standard,remark')
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['id' => 'desc'])
            ->select()->each(function($data){
		        $cost_budget = ProjectCostBudget::field('cost_budget_code')->where('id',$data['cost_budget_id'])->findOrEmpty();
		        $project = Project::field('name,project_code')->where('id',$data['project_id'])->findOrEmpty();
		        $project_cost_temp = ProjectCostTempSet::field('first_level_subject,second_level_subject,third_level_subject,is_travel')->where('id',$data['project_cost_temp_id'])->findOrEmpty();
		        $data['cost_budget_code'] = $cost_budget['cost_budget_code'];
		        $data['project_name'] = $project['name'];
		        $data['project_code'] = $project['project_code'];
		        $data['first_level_subject'] = $project_cost_temp['first_level_subject'];
		        $data['second_level_subject'] = $project_cost_temp['second_level_subject'];
		        $data['third_level_subject'] = $project_cost_temp['third_level_subject'];
		        $data['is_travel'] = $project_cost_temp['is_travel'];
		        $data['is_travel_text'] = $project_cost_temp->is_travel_text;
				return $data;
	        })
            ->toArray();
    }


    /**
     * @notes 获取费用预算明细数量
     * @return int
     * @author likeadmin
     * @date 2024/01/16 14:32
     */
    public function count(): int
    {
        return ProjectCostBudgetDetail::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",
			"project_name" => "项目名称",
            "cost_budget_code" => "费用预算单号",
            "first_level_subject" => "一级科目",
            "second_level_subject" => "二级科目",
            "third_level_subject" => "三级科目",
            "is_travel_text" => "是否差旅科目",
            "unit" => "单位",
            "amount" => "预算金额",
            "standard" => "编制标准",
		];
	}

}