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


use app\common\model\property\PropertyHead;
use app\common\model\property\PropertySurplusLog;
use app\common\logic\BaseLogic;
use think\Exception;
use think\facade\Db;


/**
 * PropertySurplusLog逻辑
 * Class PropertySurplusLogLogic
 * @package app\adminapi\logic
 */
class PropertySurplusLogLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/09/20 16:31
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            PropertySurplusLog::create([
                'in_out' => $params['in_out'],
                'property_commission_id' => $params['property_commission_id'],
                'amount' => $params['amount'],
                'status' => $params['status'],
                'remark' => $params['remark'],
                'property_head_id' => $params['property_head_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/09/20 16:31
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            $status = PropertySurplusLog::where('id', $params['id'])->value('status');
            if($status != 0){
                throw new Exception('状态不允许操作');
            }

            if(intval($params['status']) === 1){
                $res = self::EntryCash($params);
            }
            if(intval($params['status']) === 2){
                $res = self::CancelCash($params);
            }
            if(!$res){
                throw new Exception(self::getError());
            }


            /*PropertySurplusLog::where('id', $params['id'])->update([
                'in_out' => $params['in_out'],
                'property_commission_id' => $params['property_commission_id'],
                'amount' => $params['amount'],
                'status' => $params['status'],
                'remark' => $params['remark'],
                'property_head_id' => $params['property_head_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/09/20 16:31
     */
    public static function delete(array $params): bool
    {
        return PropertySurplusLog::destroy($params['id']);
    }


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

    /**
     * 提现入账
     * @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;
        }
    }

}