Merge pull request '新增出差申请模块' (#183) from zhangwei into dev

Reviewed-on: #183
This commit is contained in:
weiz 2024-01-17 10:43:18 +08:00
commit 7d79a836d8
6 changed files with 546 additions and 0 deletions

View File

@ -0,0 +1,108 @@
<?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\project;
use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\project\ProjectTripApplyLists;
use app\adminapi\logic\project\ProjectTripApplyLogic;
use app\adminapi\validate\project\ProjectTripApplyValidate;
/**
* 出差申请控制器
* Class ProjectTripApplyController
* @package app\adminapi\controller\project
*/
class ProjectTripApplyController extends BaseAdminController
{
/**
* @notes 获取出差申请列表
* @return \think\response\Json
* @author likeadmin
* @date 2024/01/17 09:22
*/
public function lists()
{
return $this->dataLists(new ProjectTripApplyLists());
}
/**
* @notes 添加出差申请
* @return \think\response\Json
* @author likeadmin
* @date 2024/01/17 09:22
*/
public function add()
{
$params = (new ProjectTripApplyValidate())->post()->goCheck('add');
$result = ProjectTripApplyLogic::add($params);
if (true === $result) {
return $this->success('添加成功', [], 1, 1);
}
return $this->fail(ProjectTripApplyLogic::getError());
}
/**
* @notes 编辑出差申请
* @return \think\response\Json
* @author likeadmin
* @date 2024/01/17 09:22
*/
public function edit()
{
$params = (new ProjectTripApplyValidate())->post()->goCheck('edit');
$result = ProjectTripApplyLogic::edit($params);
if (true === $result) {
return $this->success('编辑成功', [], 1, 1);
}
return $this->fail(ProjectTripApplyLogic::getError());
}
/**
* @notes 删除出差申请
* @return \think\response\Json
* @author likeadmin
* @date 2024/01/17 09:22
*/
public function delete()
{
$params = (new ProjectTripApplyValidate())->post()->goCheck('delete');
ProjectTripApplyLogic::delete($params);
return $this->success('删除成功', [], 1, 1);
}
/**
* @notes 获取出差申请详情
* @return \think\response\Json
* @author likeadmin
* @date 2024/01/17 09:22
*/
public function detail()
{
$params = (new ProjectTripApplyValidate())->goCheck('detail');
$result = ProjectTripApplyLogic::detail($params);
return $this->data($result);
}
}

View File

@ -0,0 +1,94 @@
<?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\model\auth\Admin;
use app\common\model\project\Project;
use app\common\model\project\ProjectManagerAppointment;
use app\common\model\project\ProjectTripApply;
use app\common\lists\ListsSearchInterface;
/**
* 出差申请列表
* Class ProjectTripApplyLists
* @package app\adminapi\listsproject
*/
class ProjectTripApplyLists extends BaseAdminDataLists implements ListsSearchInterface
{
/**
* @notes 设置搜索条件
* @return \string[][]
* @author likeadmin
* @date 2024/01/17 09:22
*/
public function setSearch(): array
{
return [
'=' => ['project_id', 'traffic'],
'%like%' => ['trip_apply_code', 'origin_address', 'target_address'],
];
}
/**
* @notes 获取出差申请列表
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author likeadmin
* @date 2024/01/17 09:22
*/
public function lists(): array
{
return ProjectTripApply::where($this->searchWhere)
->field(['id', 'project_id', 'trip_apply_code', 'origin_address', 'target_address', 'traffic', 'start_date', 'end_date', 'reason', 'days', 'remark'])
->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc'])
->select()->each(function($data){
$project = Project::field('name,project_code')->where('id',$data['project_id'])->findOrEmpty();
$manager = ProjectManagerAppointment::field('project_manager')->where('project_id',$data['project_id'])->findOrEmpty();
$data['traffic_text'] = $data->traffic_text;
$data['project_name'] = $project['name'];
$data['project_code'] = $project['project_code'];
if($manager->isEmpty()){
$data['project_manager'] = '';
}else{
$admin = Admin::field('name')->where('id',$manager['project_manager'])->findOrEmpty();
$data['project_manager'] = $admin['name'];
}
return $data;
})
->toArray();
}
/**
* @notes 获取出差申请数量
* @return int
* @author likeadmin
* @date 2024/01/17 09:22
*/
public function count(): int
{
return ProjectTripApply::where($this->searchWhere)->count();
}
}

View File

@ -0,0 +1,140 @@
<?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\logic\project;
use app\common\model\auth\Admin;
use app\common\model\project\Project;
use app\common\model\project\ProjectManagerAppointment;
use app\common\model\project\ProjectTripApply;
use app\common\logic\BaseLogic;
use think\facade\Db;
/**
* 出差申请逻辑
* Class ProjectTripApplyLogic
* @package app\adminapi\logic\project
*/
class ProjectTripApplyLogic extends BaseLogic
{
/**
* @notes 添加出差申请
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/01/17 09:22
*/
public static function add(array $params): bool
{
Db::startTrans();
try {
ProjectTripApply::create([
'project_id' => $params['project_id'],
'trip_apply_code' => data_unique_code('项目出差'),
'origin_address' => $params['origin_address'],
'target_address' => $params['target_address'],
'traffic' => $params['traffic'],
'start_date' => strtotime($params['start_date']),
'end_date' => strtotime($params['end_date']),
'reason' => $params['reason'] ?? '',
'days' => daysBetweenDates($params['start_date'],$params['end_date']),
'remark' => $params['remark'] ?? '',
'annex' => $params['annex'] ? json_encode($params['annex']) : null,
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 编辑出差申请
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/01/17 09:22
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
ProjectTripApply::where('id', $params['id'])->update([
'project_id' => $params['project_id'],
'origin_address' => $params['origin_address'],
'target_address' => $params['target_address'],
'traffic' => $params['traffic'],
'start_date' => strtotime($params['start_date']),
'end_date' => strtotime($params['end_date']),
'reason' => $params['reason'] ?? '',
'days' => daysBetweenDates($params['start_date'],$params['end_date']),
'remark' => $params['remark'] ?? '',
'annex' => $params['annex'] ? json_encode($params['annex']) : null,
'update_time' => time()
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 删除出差申请
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/01/17 09:22
*/
public static function delete(array $params): bool
{
return ProjectTripApply::destroy($params['id']);
}
/**
* @notes 获取出差申请详情
* @param $params
* @return array
* @author likeadmin
* @date 2024/01/17 09:22
*/
public static function detail($params): array
{
$data = ProjectTripApply::field('id,project_id,trip_apply_code,origin_address,target_address,traffic,start_date,end_date,reason,days,remark,annex')
->findOrEmpty($params['id']);
$project = Project::field('name,project_code')->where('id',$data['project_id'])->findOrEmpty();
$manager = ProjectManagerAppointment::field('project_manager')->where('project_id',$data['project_id'])->findOrEmpty();
$data['traffic_text'] = $data->traffic_text;
$data['project_name'] = $project['name'];
$data['project_code'] = $project['project_code'];
if($manager->isEmpty()){
$data['project_manager'] = '';
}else{
$admin = Admin::field('name')->where('id',$manager['project_manager'])->findOrEmpty();
$data['project_manager'] = $admin['name'];
}
return $data->toArray();
}
}

View File

@ -0,0 +1,144 @@
<?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\validate\project;
use app\common\model\dict\DictData;
use app\common\model\project\Project;
use app\common\validate\BaseValidate;
/**
* 出差申请验证器
* Class ProjectTripApplyValidate
* @package app\adminapi\validate\project
*/
class ProjectTripApplyValidate extends BaseValidate
{
/**
* 设置校验规则
* @var string[]
*/
protected $rule = [
'id' => 'require',
'project_id' => 'require|checkProject',
'origin_address' => 'require',
'target_address' => 'require',
'traffic' => 'require|checkTraffic',
'start_date' => 'require|dateFormat:Y-m-d',
'end_date' => 'require|dateFormat:Y-m-d|checkEndDate',
'annex' => 'checkAnnex'
];
/**
* 参数描述
* @var string[]
*/
protected $field = [
'id' => 'id',
'project_id' => '项目id',
'origin_address' => '出差起始地',
'target_address' => '出差目的地',
'traffic' => '交通工具',
'start_date' => '出差时间',
'end_date' => '结束时间',
];
/**
* @notes 添加场景
* @return ProjectTripApplyValidate
* @author likeadmin
* @date 2024/01/17 09:22
*/
public function sceneAdd()
{
return $this->remove('id',true);
}
/**
* @notes 编辑场景
* @return ProjectTripApplyValidate
* @author likeadmin
* @date 2024/01/17 09:22
*/
public function sceneEdit()
{}
/**
* @notes 删除场景
* @return ProjectTripApplyValidate
* @author likeadmin
* @date 2024/01/17 09:22
*/
public function sceneDelete()
{
return $this->only(['id']);
}
/**
* @notes 详情场景
* @return ProjectTripApplyValidate
* @author likeadmin
* @date 2024/01/17 09:22
*/
public function sceneDetail()
{
return $this->only(['id']);
}
public function checkProject($value): bool|string
{
$data = Project::where('id',$value)->findOrEmpty();
if($data->isEmpty()){
return '项目信息不存在';
}
return true;
}
public function checkTraffic($value): bool|string
{
$dict = DictData::where('type_value','traffic')->column('value');
if(!in_array($value,$dict)){
return '交通工具无效';
}
return true;
}
public function checkEndDate($value,$rule,$data): bool|string
{
$end_time = strtotime($value);
$start_time = strtotime($data['start_date']);
if($end_time <= $start_time){
return '结束时间必须大于开始时间';
}
return true;
}
public function checkAnnex($value): bool|string
{
if(!empty($value) && $value != ''){
if(!is_array($value)){
return '附件格式错误';
}
}
return true;
}
}

View File

@ -343,3 +343,11 @@ function data_unique_code($str): string
//质量检查单-20231215-0001
return $str.'-'.date('Ymd',time()).'-'.mt_rand(100000, 999999);
}
function daysBetweenDates($start_date,$end_date): bool|int
{
$start = new DateTime($start_date);
$end = new DateTime($end_date);
$interval = $start->diff($end);
return $interval->days;
}

View File

@ -0,0 +1,52 @@
<?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\common\model\project;
use app\common\model\BaseModel;
use app\common\model\dict\DictData;
use think\model\concern\SoftDelete;
/**
* 出差申请模型
* Class ProjectTripApply
* @package app\common\model\project
*/
class ProjectTripApply extends BaseModel
{
use SoftDelete;
protected $name = 'project_trip_apply';
protected $deleteTime = 'delete_time';
public function getAnnexAttr($value){
return !empty($value) ? json_decode($value,true) : '';
}
public function getStartDateAttr($value): string
{
return !empty($value) ? date('Y-m-d',$value) : '';
}
public function getEndDateAttr($value): string
{
return !empty($value) ? date('Y-m-d',$value) : '';
}
public function getTrafficTextAttr($value,$data){
$dict = DictData::where('type_value','traffic')->column('name','value');
return !empty($data['traffic']) ? $dict[$data['traffic']] : '';
}
}