<?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\controller\custom;


use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\custom\CustomerDemandLists;
use app\adminapi\logic\custom\CustomerDemandLogic;
use app\adminapi\validate\custom\CustomerDemandValidate;
use app\common\model\custom\Custom;
use app\common\model\custom\CustomerDemand;
use app\common\model\project\Project;


/**
 * 客户需求控制器
 * Class CustomerDemandController
 * @package app\adminapi\controller\custom
 */
class CustomerDemandController extends BaseAdminController
{


    /**
     * @notes 获取客户需求列表
     * @return \think\response\Json
     * @author likeadmin
     * @date 2023/11/24 21:18
     */
    public function lists()
    {
        return $this->dataLists(new CustomerDemandLists());
    }


    /**
     * @notes 添加客户需求
     * @return \think\response\Json
     * @author likeadmin
     * @date 2023/11/24 21:18
     */
    public function add()
    {
        $params = (new CustomerDemandValidate())->post()->goCheck('add');
        $result = CustomerDemandLogic::add($params);
        if (true === $result) {
            return $this->success('添加成功', [], 1, 1);
        }
        return $this->fail(CustomerDemandLogic::getError());
    }


    /**
     * @notes 编辑客户需求
     * @return \think\response\Json
     * @author likeadmin
     * @date 2023/11/24 21:18
     */
    public function edit()
    {
        $params = (new CustomerDemandValidate())->post()->goCheck('edit');
        $result = CustomerDemandLogic::edit($params);
        if (true === $result) {
            return $this->success('编辑成功', [], 1, 1);
        }
        return $this->fail(CustomerDemandLogic::getError());
    }


    /**
     * @notes 删除客户需求
     * @return \think\response\Json
     * @author likeadmin
     * @date 2023/11/24 21:18
     */
    public function delete()
    {
        $params = (new CustomerDemandValidate())->post()->goCheck('delete');
        CustomerDemandLogic::delete($params);
        return $this->success('删除成功', [], 1, 1);
    }


    /**
     * @notes 获取客户需求详情
     * @return \think\response\Json
     * @author likeadmin
     * @date 2023/11/24 21:18
     */
    public function detail()
    {
        $params = (new CustomerDemandValidate())->goCheck('detail');
        $result = CustomerDemandLogic::detail($params);
        return $this->data($result);
    }
	
	//根据项目id获取客户需求列表
	public function getListByProjectId(): \think\response\Json
	{
		$params = $this->request->get(['project_id','page_size','page_no']);
		if(empty($params['project_id'])){
			return $this->fail('缺少必要参数');
		}
		$pageSize = !empty($params['page_size']) ? $params['page_size'] : 15;
		$pageNo = !empty($params['page_size']) ? $params['page_no'] : 1;
		$data = CustomerDemand::where('project_id',$params['project_id'])
			->field(['id', 'project_id', 'theme', 'supplier', 'supplier_contacts', 'importance', 'recording_time', 'demand_content', 'annex'])
			->order(['id' => 'desc'])->page($pageNo,$pageSize)
			->select()->each(function($item){
				$item['importance_text'] = $item->importance_text;
				$item['recording_time'] = date('Y-m-d H:i:s',$item['recording_time']);
				$project = Project::field('name,project_code,custom_id')->where('id',$item['project_id'])->findOrEmpty();
				$custom = Custom::field('name')->where('id',$project['custom_id'])->findOrEmpty();
				$item['project_name'] = $project['name'];
				$item['project_code'] = $project['project_code'];
				$item['custom_name'] = $custom['name'];
				return $item;
			})->toArray();
		$count = CustomerDemand::field('id')->where('project_id',$params['project_id'])->count();
		$result = [
			'count' => $count,
			'page_no' => $pageNo,
			'page_size' => $pageSize,
			'lists' => $data
		];
		return $this->success('请求成功',$result);
	}

}