<?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\model\master_worker\MasterWorker;
use app\common\model\master_worker\MasterWorkerTeam;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * MasterWorkerTeam逻辑
 * Class MasterWorkerTeamLogic
 * @package app\adminapi\logic
 */
class MasterWorkerTeamLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/11/20 17:42
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            $masterWorker = \app\common\model\master_worker\MasterWorker::where('id', $params['master_worker_id'])->find();
            if($masterWorker['team_id']){
                throw new \Exception('该师傅已加入团队，请先退出团队');
            }
            $masterWorkerTeam = MasterWorkerTeam::create([
                'team_name' => $params['team_name'],
                'master_worker_id' => $params['master_worker_id'],
            ]);
            MasterWorker::where('id', $params['master_worker_id'])->update(
                [
                    'team_id' => $masterWorkerTeam->id,
                    'team_role' => 1,
                ]
            );
            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/11/20 17:42
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            $masterWorkerTeam = MasterWorkerTeam::where('id', $params['id'])->find();
            $masterWorker = \app\common\model\master_worker\MasterWorker::where('id', $params['master_worker_id'])->find();
            if(($masterWorkerTeam['master_worker_id'] != $params['master_worker_id']) && $masterWorker['team_id']){
                throw new \Exception('该师傅已加入团队，请先退出团队');
            }

            \app\common\model\master_worker\MasterWorker::where('id', $masterWorkerTeam['master_worker_id'])->update(
                [
                    'team_id' => 0,
                    'team_role' => 0,
                ]
            );
            MasterWorkerTeam::where('id', $params['id'])->update([
                'team_name' => $params['team_name'],
                'master_worker_id' => $params['master_worker_id'],
            ]);
            \app\common\model\master_worker\MasterWorker::where('id', $params['master_worker_id'])->update(
                [
                    'team_id' => $params['id'],
                    'team_role' => 1,
                ]
            );
            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/11/20 17:42
     */
    public static function delete(array $params): bool
    {
        $masterWorkerTeam = MasterWorkerTeam::where('id', $params['id'])->find();
        \app\common\model\master_worker\MasterWorker::where('id', $masterWorkerTeam['master_worker_id'])->update(
            [
                'team_id' => 0,
                'team_role' => 0,
            ]
        );
        return MasterWorkerTeam::destroy($params['id']);
    }


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