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


use app\common\enum\worker\WorkerAccountLogEnum;
use app\common\logic\WorkerAccountLogLogic;
use app\common\model\finance\MasterWorkerCaseOutLog;
use app\common\model\financial\FinancialPaymentRecords;
use app\common\model\financial\MasterSettlementDetails;
use app\common\model\master_worker\EngineerSettlement;
use app\common\logic\BaseLogic;
use app\common\model\master_worker\MasterWorker;
use think\facade\Db;


/**
 * EngineerSettlement逻辑
 * Class EngineerSettlementLogic
 * @package app\adminapi\logic
 */
class EngineerBillLogic extends BaseLogic
{

    public static function submitForm($params)
    {
        Db::startTrans();
        try {
            if(!empty($params['left_amount']) && !empty($params['user_money']) && !((float)$params['left_amount'] > (float)$params['user_money'])){
                $batch_number = 'bn' . date('YmdHis') . rand(1000, 9999);
                $params['deadline_time'] = $deadline_time = strtotime($params['deadline_time'])+86400;

                FinancialPaymentRecords::create([
                    'export_conditions' => $params,
                    'batch_number' => $batch_number,
                    'upload_status' => 1,
                    'payment_status' => 1,
                ]);
                MasterSettlementDetails::where('master_worker_id',$params['id'])
                                        ->where('change_time', '<', $deadline_time)
                                        ->where('status', '=', 1)
                                        ->update([
                                            'status' => 2,
                                            'batch_number' => $batch_number,
                                        ]);

                $insertXlsData = [];
                $insertXlsData[0] = [
                    'master_worker_id' => $params['id'],
                    'worker_number' => $params['worker_number'],
                    'engineer_name' => $params['account_holder'],

                    'bank_account' => $params['bank_account'],
                    'bank_name' => $params['bank_name'],
                    'opening_branch' => $params['opening_branch'],

                    'original_balance' => $params['user_money'],
                    'total_settlement_amount' => $params['left_amount'],
                    'payment_status' => 0,
                    'is_export' => 1,

                    'batch_number'=> $batch_number,
                    'create_time'=> time(),
                    'update_time'=> time(),
                    'admin_id'=> 0,
                    'is_deduction'=> 0,
                ];
                // 生成 可发 数据表格
                Db::name('engineer_settlement')->insertAll($insertXlsData);

                $analogData = [];
                $analogData[0] = [
                    'master_worker_id' => $params['id'],
                    'worker_number' => $params['worker_number'],
                    'engineer_name' => $params['account_holder'],
                    'bank_account' => $params['bank_account'],
                    'bank_name' => $params['bank_name'],
                    'opening_branch' => $params['opening_branch'],
                    'original_balance' => $params['user_money'],
                    'total_settlement_amount' => $params['left_amount'],
                    'batch_number'=> $batch_number,
                    'deduction_amount' => $params['deduction_amount'],
                    'deduction_describe' => $params['deduction_describe'],
                    'final_settlement_amount' => $params['final_settlement_amount'],
                    'settlement_time' => $params['settlement_time'],
                    'settlement_details_remarks' => $params['settlement_details_remarks'],
                ];
                $resData = EngineerSettlementLogic::engineerSettlementValidate($analogData);
                if(is_array($resData)){
                    $result = EngineerSettlementLogic::add($resData,$params['adminId']);
                    if ($result === false){
                        throw new \Exception(EngineerSettlementLogic::getError());
                    }
                }
            }else{
                throw new \Exception('打款失败');
            }

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


}