143 lines
5.1 KiB
PHP
143 lines
5.1 KiB
PHP
<?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\material;
|
||
|
||
|
||
use app\common\model\auth\Admin;
|
||
use app\common\model\material\Material;
|
||
use app\common\logic\BaseLogic;
|
||
use app\common\model\material\MaterialClassify;
|
||
use think\facade\Db;
|
||
|
||
|
||
/**
|
||
* 自购材料逻辑
|
||
* Class MaterialLogic
|
||
* @package app\adminapi\logic\material
|
||
*/
|
||
class MaterialLogic extends BaseLogic
|
||
{
|
||
|
||
|
||
/**
|
||
* @notes 添加自购材料
|
||
* @param array $params
|
||
* @return bool
|
||
* @author likeadmin
|
||
* @date 2024/01/04 10:59
|
||
*/
|
||
public static function add(array $params,$admin_id): bool
|
||
{
|
||
Db::startTrans();
|
||
try {
|
||
Material::create([
|
||
'first_level' => $params['first_level'],
|
||
'second_level' => $params['second_level'] ?? 0,
|
||
'three_level' => $params['three_level'] ?? 0,
|
||
'code' => $params['code'],
|
||
'brand' => $params['brand'] ?? '',
|
||
'name' => $params['name'],
|
||
'specs' => $params['specs'],
|
||
'unit' => $params['unit'],
|
||
'parameter_description' => $params['parameter_description'] ?? '',
|
||
'inventory' => $params['inventory'] ?? 0,
|
||
'sales_price' => $params['sales_price'] ?? 0,
|
||
'cost_price' => $params['cost_price'] ?? 0,
|
||
'annex' => $params['annex']? json_encode($params['annex']) : null,
|
||
'add_user' => $admin_id,
|
||
'update_user' => $admin_id,
|
||
]);
|
||
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/04 10:59
|
||
*/
|
||
public static function edit(array $params,$admin_id): bool
|
||
{
|
||
Db::startTrans();
|
||
try {
|
||
Material::where('id', $params['id'])->update([
|
||
'first_level' => $params['first_level'],
|
||
'second_level' => $params['second_level'] ?? 0,
|
||
'three_level' => $params['three_level'] ?? 0,
|
||
'code' => $params['code'],
|
||
'brand' => $params['brand'] ?? '',
|
||
'name' => $params['name'],
|
||
'specs' => $params['specs'],
|
||
'unit' => $params['unit'],
|
||
'parameter_description' => $params['parameter_description'] ?? '',
|
||
'inventory' => $params['inventory'] ?? 0,
|
||
'sales_price' => $params['sales_price'] ?? 0,
|
||
'cost_price' => $params['cost_price'] ?? 0,
|
||
'annex' => $params['annex']? json_encode($params['annex']) : null,
|
||
'update_user' => $admin_id,
|
||
'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/04 10:59
|
||
*/
|
||
public static function delete(array $params): bool
|
||
{
|
||
return Material::destroy($params['id']);
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 获取自购材料详情
|
||
* @param $params
|
||
* @return array
|
||
* @author likeadmin
|
||
* @date 2024/01/04 10:59
|
||
*/
|
||
public static function detail($params): array
|
||
{
|
||
$field = 'id,first_level,second_level,three_level,code,brand,name,specs,unit,parameter_description,inventory,sales_price,cost_price,annex,add_user,update_user,create_time,update_time';
|
||
$data = Material::field($field)->findOrEmpty($params['id'])->toArray();
|
||
$classify = MaterialClassify::where('id','in',[$data['first_level'],$data['second_level'],$data['three_level']])->column('name','id');
|
||
$admin = Admin::where('id','in',[$data['add_user'],$data['update_user']])->column('name','id');
|
||
$data['first_level_name'] = $classify[$data['first_level']];
|
||
$data['second_level_name'] = !empty($classify[$data['second_level']]) ? $classify[$data['second_level']] : '';
|
||
$data['three_level_name'] = !empty($classify[$data['three_level']]) ? $classify[$data['three_level']] : '';
|
||
$data['add_user_name'] = $admin[$data['add_user']];
|
||
$data['update_user_name'] = $admin[$data['update_user']];
|
||
return $data;
|
||
}
|
||
} |