<?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\common\logic\task_template;


use app\common\model\task_template\TaskTemplate;
use app\common\logic\BaseLogic;
use app\common\model\company\CompanyProperty;
use app\common\model\informationg\UserInformationg;
use app\common\model\task_scheduling\TaskScheduling;
use think\facade\Db;


/**
 * 任务模板逻辑
 * Class TaskTemplateLogic
 * @package app\adminapi\logic\task_template
 */
class TaskTemplateLogic extends BaseLogic
{


    /**
     * @notes 添加任务模板
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2023/08/06 17:30
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            $find=TaskTemplate::where('task_scheduling', $params['task_scheduling'])->where('company_id',$params['company_id'])->where('type',$params['type'])->field('id,types,type')->find();
            if($find&&$params['type']==$find['type']){
                self::setError('已经有同一种任务类型了');
                return false;
            }
            if($params['type']==33){
                $count=UserInformationg::where('company_id',$params['company_id'])->where('status',1)->count();
                if($count<300){
                    self::setError('用户档案数量300不足,无法创建任务模板');
                    return false;
                }
            }

            $moeny=TaskTemplate::where('company_id', $params['company_id'])->sum('money');
            if($moeny+$params['money']>200){
                self::setError('任务模板一阶段合计金额不能大于任务调度金额');
                return false;
            }
            $money_two=TaskTemplate::where('company_id', $params['company_id'])->sum('money_two');
            if($money_two+$params['money_two']>200){
                self::setError('任务模板二阶段合计金额不能大于任务调度金额');
                return false;
            }
            $money_three=TaskTemplate::where('company_id', $params['company_id'])->sum('money_three');
            if($money_three+$params['money_three']>200){
                self::setError('任务模板长期合计金额不能大于任务调度金额');
                return false;
            }
            if($params['type']==32){
                $object_id=CompanyProperty::where('company_id',$params['company_id'])->value('object_id');
                if(!$object_id){
                    self::setError('该公司没有三轮车,请先租赁三轮车');
                    return false;
                }
            }
            if($params['types']!=33){
                $params['money_three']=0;
            }
            if($params['type']==35){
                $params['extend']=['shareholder'=>['user_id'=>$params['task_admin']]];
            }
            TaskTemplate::create([
                'title' => $params['title'],
                'admin_id' => $params['admin_id'],
                'company_id' => $params['company_id'],
                'task_scheduling' => $params['task_scheduling']??0,
                'money' => $params['money'],
                'money_two' => $params['money_two'],
                'money_three' => $params['money_three'],
                'type' => $params['type'],
                'types' => $params['types'],
                'status' => $params['status'],
                'content' => $params['content'],
                'extend'=>json_encode($params['extend']),
                'stage_day_one' => $params['stage_day_one']??0,
                'proportion_one' => $params['proportion_one']??0,
                'stage_day_two' => $params['stage_day_two']??0,
                'proportion_two' => $params['proportion_two']??0,
                'recharge' => $params['recharge']??0,
            ]);

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


    /**
     * @notes 编辑任务模板
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2023/08/06 17:30
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            $find=TaskTemplate::where('task_scheduling', $params['task_scheduling'])->where('company_id',$params['company_id'])->where('type',$params['type'])->field('id,types,type,money,money_two,money_three')->find();
            if($find && $find['id']!=$params['id']&&$params['type']==$find['type']){
                self::setError('已经有同一种任务类型了');
                return false;
            }
            if($params['type']==33){
                $count=UserInformationg::where('company_id',$params['company_id'])->where('status',1)->count();
                if($count<300){
                    self::setError('用户档案数量300不足,无法创建任务模板');
                    return false;
                }
            }
            $moeny=TaskTemplate::where('company_id', $params['company_id'])->sum('money');
            if($moeny+$params['money']-$find['money']>200){
                self::setError('任务模板一阶段合计金额不能大于任务调度金额');
                return false;
            }
            $money_two=TaskTemplate::where('company_id', $params['company_id'])->sum('money_two');
            if($money_two+$params['money_two']-$find['money_two']>200){
                self::setError('任务模板二阶段合计金额不能大于任务调度金额');
                return false;
            }
            $money_three=TaskTemplate::where('company_id', $params['company_id'])->sum('money_three');
            if($money_three+$params['money_three']-$find['money_three']>200){
                self::setError('任务模板长期合计金额不能大于任务调度金额');
                return false;
            }
            if($params['type']==32){
                $object_id=CompanyProperty::where('company_id',$params['company_id'])->value('object_id');
                if(!$object_id){
                    self::setError('该公司没有三轮车,请先租赁三轮车');
                    return false;
                }
            }
            if($params['types']!=33){
                $params['money_three']=0;
            }
            TaskTemplate::where('id', $params['id'])->update([
                'title' => $params['title'],
                'admin_id' => $params['admin_id'],
                'money' => $params['money'],
                'money_two' => $params['money_two'],
                'money_three' => $params['money_three'],
                'type' => $params['type'],
                'types' => $params['types'],
                'status' => $params['status'],
                'content' => $params['content'],
                'extend'=>json_encode($params['extend']),
                'stage_day_one' => $params['stage_day_one']??0,
                'proportion_one' => $params['proportion_one']??0,
                'stage_day_two' => $params['stage_day_two']??0,
                'proportion_two' => $params['proportion_two']??0,
                'recharge' => $params['recharge']??0,
            ]);

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


    /**
     * @notes 删除任务模板
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2023/08/06 17:30
     */
    public static function delete(array $params): bool
    {
        return TaskTemplate::destroy($params['id']);
    }


    /**
     * @notes 获取任务模板详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2023/08/06 17:30
     */
    public static function detail($params): array
    {
        return TaskTemplate::findOrEmpty($params['id'])->toArray();
    }
}