<?php

namespace app\common\logic\finance;

use app\common\enum\user\AccountLogEnum;
use app\common\logic\AccountLogLogic;
use app\common\model\Company;
use app\common\model\company\CompanyAccountLog;
use app\common\model\task\Task;
use app\common\model\task_scheduling_plan\TaskSchedulingPlan;
use app\common\model\task_template\TaskTemplate;
use app\common\model\user\User;
use app\common\model\user\UserAccountLog;
use think\facade\Db;
use think\facade\Log;

class VillageShareProfit
{
    /**
     * 分润
     */
    public function shareProfit($taskInfo, $company, $taskSchedulePlan)
    {
        $proportion = 0;
        //总金额除以2等于不可提现账号金额和收益
        $masterMoney = bcdiv($taskInfo['money'], 2, 2);

        $remark =  '来自任务【' . $taskSchedulePlan['template_info']['title'] . '】,';
        $liaisonMan = (new User())->searchLiaisonMan($company['id']);
        Log::info([$taskSchedulePlan['template_info']['title'].'结算-村联络员用户信息', $liaisonMan]);

        // 用户收益变动
        $arr = [$liaisonMan['id'], AccountLogEnum::UM_INC_TASK, AccountLogEnum::INC, $masterMoney, $taskSchedulePlan['sn'],   $remark.'任务结算获得收益' . $masterMoney . '元', ['company_id' => $company['id'], 'proportion' => $proportion], 1];
        $this->master($arr);
        // 用户余额变动
        $arr_two = [$liaisonMan['id'], AccountLogEnum::UM_INC_TASKUSER, AccountLogEnum::INC, $masterMoney, $taskSchedulePlan['sn'], $remark . '获得账户余额' . $masterMoney . '元', ['company_id' => $company['id'], 'proportion' => $proportion], 1];
        $this->Account($arr_two);

        // 公司收益
        $deposit_count = bcadd($company['deposit'], $masterMoney, 2);
        // 公司收益变动记录
        $this->AccountLog($company['id'], $deposit_count, $masterMoney, 1, CompanyAccountLog::TASK_INC_INCOME);
        $company_money_count = bcadd($company['company_money'], $masterMoney, 2);
        //公司余额变动记录
        $this->AccountLog($company['id'], $company_money_count, $masterMoney, 1, CompanyAccountLog::TASK_INC_COMPANY_MONEY);
        // 变更公司收益和余额
        Company::where('id', $company['id'])->update(['deposit' => Db::raw('deposit+' . $masterMoney), 'company_money' => Db::raw('company_money+' . $masterMoney)]);
    }
    public function AccountLog($companyId, $left_amount, $changeAmount, $change_object = 1, $change_type = 1, $action = 1)
    {
        $company_log = [
            'sn' => generate_sn(UserAccountLog::class, 'sn', 20),
            'company_id' => $companyId,
            'change_object' => $change_object, //变动对象
            'change_type' => $change_type, //变动类型
            'action' => $action, //1-增加 2-减少
            'left_amount' => $left_amount, //变动后数量
            'change_amount' => $changeAmount, //变动数量
            'status' => 1,
        ];
        CompanyAccountLog::create($company_log);
    }
    /**负责人的分润
     * @param $data
     */
    private function master($data)
    {
        User::where('id', $data[0])->update(['deposit'=>Db::raw('deposit+' . $data[3]),'user_money'=>Db::raw('user_money+' . $data[3])]);
        return AccountLogLogic::add($data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6], $data[7]);
    }

    /**成员分润
     * @param $data
     */
    private function member($data)
    {
        User::where('id', $data[0])->update(['deposit'=>Db::raw('deposit+' . $data[3]),'user_money'=>Db::raw('user_money+' . $data[3])]);
        return AccountLogLogic::add($data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6], $data[7]);
    }

    private function Account($data)
    {
        return AccountLogLogic::add($data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6], $data[7]);
    }

    public function dealVillageTaskSettlement1(Task $taskInfo, Company $townCompany,  $taskSchedulePlan)
    {
        try {
            Db::startTrans();
            $this->shareProfit($taskInfo, $townCompany, $taskSchedulePlan);
            // 更改结算状态
            (new TaskSchedulingPlan())->settlement($taskSchedulePlan['id']);
            // 更改任务状态
            Task::where(['id' => $taskSchedulePlan['task_id']])->update(['status' => 3,'money' => $taskInfo['money']]);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            Log::error($taskSchedulePlan['template_info']['title'].'-任务结算失败:' . $e->getMessage());
            return false;
        }
    }

    public function dealVillageTaskSettlement2($taskInfo, $townCompany, $taskSchedulePlan, $leftTransactionPool)
    {
        try {
            Db::startTrans();
            $this->shareProfit($taskInfo, $townCompany, $taskSchedulePlan);
            // 更改结算状态
            (new TaskSchedulingPlan())->settlement($taskSchedulePlan['id']);
            // 更改任务状态
            Task::where(['id' => $taskSchedulePlan['task_id']])->update(['status' => 3,'money' => $taskInfo['money']]);
            // 更新镇交易池
            (new TaskTemplate())->updateTransactionPool($taskSchedulePlan['template_info']['id'], $leftTransactionPool);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            Log::error($taskSchedulePlan['template_info']['title'].'-任务结算失败:' . $e->getMessage());
            return false;
        }
    }

    public function dealVillageTaskSettlement3($taskInfo, $townCompany, $taskSchedulePlan)
    {
        try {
            Db::startTrans();
            $this->shareProfit($taskInfo, $townCompany, $taskSchedulePlan);
            // 更改结算状态
            (new TaskSchedulingPlan())->settlement($taskSchedulePlan['id']);
            // 更改任务状态
            Task::where(['id' => $taskSchedulePlan['task_id']])->update(['status' => 3,'money' => $taskInfo['money']]);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            Log::error($taskSchedulePlan['template_info']['title'].'-任务结算失败:' . $e->getMessage());
            return false;
        }
    }

    public function dealVillageTaskSettlement4(Task $taskInfo, Company $townCompany,  $taskSchedulePlan)
    {
        try {
            Db::startTrans();
            $this->shareProfit($taskInfo, $townCompany, $taskSchedulePlan);
            // 更改结算状态
            (new TaskSchedulingPlan())->settlement($taskSchedulePlan['id']);
            // 更改任务状态
            Task::where(['id' => $taskSchedulePlan['task_id']])->update(['status' => 3,'money' => $taskInfo['money']]);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            Log::error($taskSchedulePlan['template_info']['title'].'-任务结算失败:' . $e->getMessage());
            return false;
        }
    }

    public function dealVillageTaskSettlement5(Task $taskInfo, Company $townCompany,  $taskSchedulePlan)
    {
        try {
            Db::startTrans();
            $this->shareProfit($taskInfo, $townCompany, $taskSchedulePlan);
            // 更改结算状态
            (new TaskSchedulingPlan())->settlement($taskSchedulePlan['id']);
            // 更改任务状态
            Task::where(['id' => $taskSchedulePlan['task_id']])->update(['status' => 3,'money' => $taskInfo['money']]);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            Log::error($taskSchedulePlan['template_info']['title'].'-任务结算失败:' . $e->getMessage());
            return false;
        }
    }

    public function dealVillageTaskSettlement6(Task $taskInfo, Company $townCompany,  $taskSchedulePlan)
    {
        try {
            Db::startTrans();
            $this->shareProfit($taskInfo, $townCompany, $taskSchedulePlan);
            // 更改结算状态
            (new TaskSchedulingPlan())->settlement($taskSchedulePlan['id']);
            // 更改任务状态
            Task::where(['id' => $taskSchedulePlan['task_id']])->update(['status' => 3,'money' => $taskInfo['money']]);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            Log::error($taskSchedulePlan['template_info']['title'].'-任务结算失败:' . $e->getMessage());
            return false;
        }
    }

    public function dealVillageTaskSettlement7(Task $taskInfo, Company $townCompany,  $taskSchedulePlan, $leftTransactionPool)
    {
        try {
            Db::startTrans();
            $this->shareProfit($taskInfo, $townCompany, $taskSchedulePlan);
            // 更改结算状态
            (new TaskSchedulingPlan())->settlement($taskSchedulePlan['id']);
            // 更改任务状态
            Task::where(['id' => $taskSchedulePlan['task_id']])->update(['status' => 3,'money' => $taskInfo['money']]);
            // 更新交易池
            (new TaskTemplate())->updateTransactionPool($taskSchedulePlan['template_info']['id'], $leftTransactionPool);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            Log::error($taskSchedulePlan['template_info']['title'].'-任务结算失败:' . $e->getMessage());
            return false;
        }
    }

    public function dealVillageTaskSettlement8(Task $taskInfo, Company $townCompany,  $taskSchedulePlan)
    {
        try {
            Db::startTrans();
            $this->shareProfit($taskInfo, $townCompany, $taskSchedulePlan);
            // 更改结算状态
            (new TaskSchedulingPlan())->settlement($taskSchedulePlan['id']);
            // 更改任务状态
            Task::where(['id' => $taskSchedulePlan['task_id']])->update(['status' => 3,'money' => $taskInfo['money']]);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            Log::error($taskSchedulePlan['template_info']['title'].'-任务结算失败:' . $e->getMessage());
            return false;
        }
    }

}