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


use app\adminapi\lists\BaseAdminDataLists;
use app\common\model\auth\Admin;
use app\common\model\material\MaterialWarehouse;
use app\common\lists\ListsSearchInterface;
use app\common\model\project\Project;


/**
 * 仓库管理列表
 * Class MaterialWarehouseLists
 * @package app\adminapi\listsmaterial
 */
class MaterialWarehouseLists extends BaseAdminDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/01/04 14:23
     */
    public function setSearch(): array
    {
        return [
            '=' => ['is_mrp'],
            '%like%' => ['code', 'name'],
        ];
    }


    /**
     * @notes 获取仓库管理列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/01/04 14:23
     */
    public function lists(): array
    {
		$params = $this->request->get(['project_name']);
		$where = [];
		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 MaterialWarehouse::where($this->searchWhere)->where($where)
            ->field(['id', 'org_id', 'dept_id', 'project_id', 'code', 'name', 'is_mrp', 'head_user', 'address', 'telephone', 'remark', 'annex'])
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['id' => 'desc'])
            ->select()->each(function($item){
		        $project = Project::field('name,project_code')->where('id',$item['project_id'])->findOrEmpty();
		        $admin = Admin::field('name')->where('id',$item['head_user'])->findOrEmpty();
		        $item['project_name'] = $project['name'];
		        $item['project_code'] = $project['project_code'];
		        $item['head_user'] = $admin['name'];
		        $item['is_mrp'] = $item->is_mrp_text;
				unset($item['org_id'],$item['dept_id'],$item['project_id']);
				return $item;
	        })
            ->toArray();
    }


    /**
     * @notes 获取仓库管理数量
     * @return int
     * @author likeadmin
     * @date 2024/01/04 14:23
     */
    public function count(): int
    {
	    $params = $this->request->get(['project_name']);
	    $where = [];
	    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 MaterialWarehouse::where($this->searchWhere)->where($where)->count();
    }

}