<?php
namespace app\adminapi\logic\property;

use app\common\enum\GoodsEnum;
use app\common\enum\PayEnum;
use app\common\enum\WorkEnum;
use app\common\logic\BaseLogic;
use app\common\model\coupon\UserCoupon;
use app\common\model\dict\DictData;
use app\common\model\goods\Goods;
use app\common\model\master_worker\MasterWorker;
use app\common\model\orders\RechargeOrder;
use app\common\model\property\PropertyCommission;
use app\common\model\property\PropertyHead;
use app\common\model\property\PropertyOrder;
use app\common\model\property\PropertySurplusLog;
use app\common\model\property\PropertyUser;
use app\common\model\recharge\OrderGoods;
use app\common\model\works\ServiceWork;
use app\workerapi\logic\ServiceWorkLogLogic;
use think\Exception;
use think\facade\Db;


/**
 *  物业分成收益逻辑
 * Class PropertyCommissionLogic
 * @package app\api\logic
 */
class PropertyCommissionLogic extends BaseLogic
{
    /**
     * 提现入账申请通过
     * @param $params
     * @return false|true
     */
    public static function EntryCash($params)
    {
        Db::startTrans();
        try {

            $propertySurplusLogInfo = PropertySurplusLog::where([
                "id" => $params['id'],
                'in_out' => 2,
                'status' => 0
            ])->findOrEmpty();
            if($propertySurplusLogInfo->isEmpty()){
                throw new Exception('申请中记录不存在');
            }
            $propertySurplusLogInfo = $propertySurplusLogInfo->toArray();
            $propertyHeadId = $propertySurplusLogInfo['property_head_id'];
            $amount = $propertySurplusLogInfo['amount'];
            // 更新 出账记录-已入账
            PropertySurplusLog::where(['id' => $params['id']])->update(['status' => 1]);
            // 更新 物业负责人已提收益
            PropertyHead::where(['id' => $propertyHeadId])->update(['extract_profit_amount' => Db::raw('extract_profit_amount+'.$amount)]);

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

    /**
     * 提现取消
     * @param $params
     * @return false|true
     */
    public static function CancelCash($params)
    {
        Db::startTrans();
        try {
            $propertySurplusLogInfo = PropertySurplusLog::where([
                "id" => $params['id'],
                'in_out' => 2,
                'status' => 0
            ])->findOrEmpty();
            if($propertySurplusLogInfo->isEmpty()){
                throw new Exception('申请中记录不存在');
            }
            $propertySurplusLogInfo = $propertySurplusLogInfo->toArray();
            $propertyHeadId = $propertySurplusLogInfo['property_head_id'];
            $amount = $propertySurplusLogInfo['amount'];

            // 更新 出账记录-取消
            PropertySurplusLog::where(['id' => $params['id']])->update(['status' => 2]);
            // 更新 物业负责人剩余收益 金额恢复
            PropertyHead::where(['id' => $propertyHeadId])->update(['surplus_profit_amount' => Db::raw('surplus_profit_amount+'.$amount)]);

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

    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/09/20 16:17
     */
    public static function detail($params): array
    {
        return PropertyCommission::findOrEmpty($params['id'])->toArray();
    }
}