95 lines
2.7 KiB
PHP
95 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\adminapi\logic\finance;
|
|
|
|
use app\common\logic\BaseLogic;
|
|
use app\common\model\finance\AccountCost;
|
|
use app\common\service\qywx\MediaChannelService;
|
|
|
|
class AccountCostLogic extends BaseLogic
|
|
{
|
|
public static function add(array $params, int $adminId, string $adminName): bool
|
|
{
|
|
try {
|
|
$costDate = (string) $params['cost_date'];
|
|
$mediaChannelCode = trim((string) ($params['media_channel_code'] ?? ''));
|
|
$mediaChannelName = MediaChannelService::getNameByCode($mediaChannelCode);
|
|
|
|
if (AccountCost::where('cost_date', $costDate)->where('media_channel_code', $mediaChannelCode)->count() > 0) {
|
|
self::setError('该日期下所选渠道的账户消耗已存在,请直接编辑');
|
|
|
|
return false;
|
|
}
|
|
|
|
AccountCost::create([
|
|
'cost_date' => $costDate,
|
|
'media_channel_code' => $mediaChannelCode,
|
|
'media_channel_name' => $mediaChannelName,
|
|
'amount' => round((float) $params['amount'], 2),
|
|
'remark' => (string) ($params['remark'] ?? ''),
|
|
'creator_id' => $adminId,
|
|
'creator_name' => $adminName,
|
|
'updater_id' => $adminId,
|
|
'updater_name' => $adminName,
|
|
]);
|
|
|
|
return true;
|
|
} catch (\Throwable $e) {
|
|
self::setError($e->getMessage());
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function edit(array $params, int $adminId, string $adminName): bool
|
|
{
|
|
try {
|
|
$model = AccountCost::find($params['id']);
|
|
if (!$model) {
|
|
self::setError('记录不存在');
|
|
|
|
return false;
|
|
}
|
|
|
|
$model->amount = round((float) $params['amount'], 2);
|
|
$model->remark = (string) ($params['remark'] ?? '');
|
|
$model->updater_id = $adminId;
|
|
$model->updater_name = $adminName;
|
|
$model->save();
|
|
|
|
return true;
|
|
} catch (\Throwable $e) {
|
|
self::setError($e->getMessage());
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function detail(int $id): array
|
|
{
|
|
return AccountCost::findOrEmpty($id)->toArray();
|
|
}
|
|
|
|
public static function delete(int $id): bool
|
|
{
|
|
try {
|
|
$model = AccountCost::find($id);
|
|
if (!$model) {
|
|
self::setError('记录不存在');
|
|
|
|
return false;
|
|
}
|
|
|
|
$model->delete();
|
|
|
|
return true;
|
|
} catch (\Throwable $e) {
|
|
self::setError($e->getMessage());
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|