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


use app\common\model\financial\FinancialPaymentRecords;
use app\common\logic\BaseLogic;
use app\common\model\financial\MasterSettlementDetails;
use app\common\model\master_worker\EngineerSettlement;
use think\facade\Db;


/**
 * FinancialPaymentRecords逻辑
 * Class FinancialPaymentRecordsLogic
 * @package app\adminapi\logic
 */
class FinancialPaymentRecordsLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/12/02 11:55
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            FinancialPaymentRecords::create([
                'export_conditions' => $params['export_conditions'],
                'batch_number' => $params['batch_number'],
                'upload_status' => $params['upload_status'],
                'payment_status' => $params['payment_status'],
            ]);

            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/12/02 11:55
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            FinancialPaymentRecords::where('id', $params['id'])->update([
                'export_conditions' => $params['export_conditions'],
                'batch_number' => $params['batch_number'],
                'upload_status' => $params['upload_status'],
                'payment_status' => $params['payment_status'],
            ]);

            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/12/02 11:55
     */
    public static function delete(array $params): bool
    {
        Db::startTrans();
        try {
            $recordInfo = FinancialPaymentRecords::findOrEmpty($params['id'])->toArray();
            if($recordInfo){
                if((int)$recordInfo['upload_status'] !== 1){
                    throw new \Exception('非待上传，不能取消');
                }
                MasterSettlementDetails::where('batch_number',$recordInfo['batch_number'])->where('status','<',3)->update(['status'=>1,'batch_number'=>'']);
                EngineerSettlement::destroy(['batch_number'=>$recordInfo['batch_number'],'is_deduction'=>['neq',1]]);
                FinancialPaymentRecords::update(['upload_status'=>2],['id'=>$params['id']]);
            }else{
                throw new \Exception('记录不存在');
            }
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/12/02 11:55
     */
    public static function detail($params): array
    {
        return FinancialPaymentRecords::findOrEmpty($params['id'])->toArray();
    }
}