| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 | <?phpnamespace App\Http\Controllers\Admin\Flow;use App\Exports\BasicExport;use App\Http\Controllers\Controller;use App\Http\Models\FlowInfo;use App\Http\Models\FlowInfoTpl;use App\Http\Models\FlowStage;use App\Http\Models\FlowStageTpl;use App\Http\Models\Role;use App\Http\Models\Site;use App\Http\Models\SiteProcess;use App\Http\Models\User;use Illuminate\Http\Request;use Illuminate\Support\Facades\DB;/** * 项目管理 详情下 新版流程 * Class IndexController * @package App\Http\Controllers\Admin\Flow */class IndexController extends Controller{    public function index($siteId)    {        $stageList = FlowStage::query()->where(['site_id' => $siteId])            ->with('infoList')->orderBy('sort')->get();        foreach ($stageList as $item) {            if ($item->expected_date) {                $item->expected_date = date('Y-m-d', strtotime($item->expected_date));            }            if ($item->complete_date) {                $item->complete_date = date('Y-m-d', strtotime($item->complete_date));            }            $item->extension = explode(',', $item->extension);        }        $roleScope = array_keys(FlowInfoTpl::RoleScope);        unset($roleScope[1]); //删除客户        $userList = User::query()->select(['id', 'nickname', 'role_id'])->whereIn('role_id', $roleScope)            ->get()->toArray();        $site = Site::query()->select(['cn_title'])->find($siteId);        $userList[] = ['id' => -1, 'role_id' => -1, 'nickname' => $site->cn_title ?? '站点名称'];        $deploy = SiteProcess::query()->where([['process_id', '=', 7], ['active', '=', 2], ['site_id', '=', $siteId]])->whereNotNull('deploy')->value('deploy');        return view('admin.flow.index', [            'siteId' => $siteId,            'stageList' => $stageList,            'userList' => $userList,            'authUser' => auth()->user(),            'domain' => $deploy['domain'] ?? '',        ]);    }    //流程保存    public function flowSave(Request $request, $siteId)    {        $dataList = $request->input('dataList') ?? [];        $where = ['site_id' => $siteId];        $stepIds = array_column($dataList, 'step_id');        $stageIds = FlowStage::query()->where($where)->pluck('id')->toArray();        $delIds = array_diff($stageIds, $stepIds);        if ($delIds) {            FlowStage::query()->where($where)->whereIn('id', $delIds)->delete();        }        $stageTplList = FlowInfoTpl::query()->get();        $mapInfoIds = FlowInfo::query()->select(['id', 'stage_id'])->where($where)->get()->groupBy('stage_id')->toArray();        $reqInfoList = [];        foreach ($dataList as $item) {            foreach ($item['children'] as $v) {                $reqInfoList[] = [                    'except_range_date' => $v['except_range_date'],                    'info_id' => $v['info_id'] ?? 0,                    'step_id' => $item['step_id'],                    'step_title' => $item['step_title'],                ];            }        }        $scopeInfoList = FlowInfo::query()->selectRaw('id,except_range_date')->get();        $modifyInfo = [            'info_id' => 0,            'except_range_date' => null,            'step_title' => '',        ];        foreach ($reqInfoList as $item) {            foreach ($scopeInfoList as $v) {                if ($item['info_id'] == $v->id && !empty($item['except_range_date']) && ($item['except_range_date'] != $v->except_range_date)) {                    $modifyInfo = [                        'info_id' => $item['info_id'],                        'except_range_date' => $item['except_range_date'],                        'step_title' => $item['step_title'],                    ];                    break;                }            }        }        $isUpdate = false;        $nextDate = null;        $stageExcept = [            'collect' => null,            'schema' => null,            'homepage' => null,            'insidePage' => null,            'wholeOffer' => null,            'testSite' => null,            'keyword' => null,            'seo' => null,            'online' => null        ];        foreach ($dataList as $step) {            $stepChildren = $step['children'] ?? [];            if (!empty($step['extension'])) {                $extension = implode(',', $step['extension']);            } else {                $extension = '';            }            if (empty($step['step_id'])) {                $stage = FlowStage::query()->create([                    'title' => $step['step_title'],                    'expected_date' => $step['expected_date'],                    'complete_date' => $step['complete_date'],                    'extension' => $extension,                    'site_id' => $siteId,                    'sort' => $this->getStageSort($stageTplList, $step['step_title'])                ]);                foreach ($stepChildren as $item) {                    FlowInfo::query()->create([                        'site_id' => $siteId,                        'stage_id' => $stage->id,                        'except_range_date' => $item['except_range_date'],                        'detail_list' => ($item['children'] ?? []),                    ]);                }            } else {                $infoIds = array_column($mapInfoIds[$step['step_id']] ?? [], 'id');                $dataInfoIds = array_column($stepChildren, 'info_id');                $infoDelIds = array_diff($infoIds, $dataInfoIds);                if ($infoIds) {                    FlowInfo::query()->where($where)->whereIn('id', $infoDelIds)->delete();                }                $updateData = [                    'title' => $step['step_title'],                    'expected_date' => $step['expected_date'],                    'complete_date' => $step['complete_date'],                    'extension' => $extension,                ];                FlowStage::query()->where(['id' => $step['step_id']])->update($updateData);                foreach ($stepChildren as $item) {                    if (empty($item['info_id'])) {                        FlowInfo::query()->create([                            'site_id' => $siteId,                            'stage_id' => $step['step_id'],                            'except_range_date' => $item['except_range_date'],                            'detail_list' => ($item['children'] ?? []),                        ]);                    } else {                        $exceptDate = $item['except_range_date'];//                        $exceptDate = null;                        if ($step['step_title'] == "项目资料搜集") {                            $stageExcept['collect'] = $item['except_range_date'];                        }                        if ($step['step_title'] == "网站架构" && $stageExcept['collect']) {                            if ($isUpdate) {                                $exceptDate = $stageExcept['schema'] = date('Y-m-d', strtotime('+1 week', strtotime($stageExcept['collect'])));                            } else {                                $stageExcept['schema'] = $item['except_range_date'];                            }                        }                        if ($step['step_title'] == "首页设计" && $stageExcept['schema']) {                            if ($isUpdate) {                                $exceptDate = $stageExcept['homepage'] = date('Y-m-d', strtotime('+1 week', strtotime($stageExcept['schema'])));                            } else {                                $stageExcept['homepage'] = $item['except_range_date'];                            }                        }                        if ($step['step_title'] == "内页设计" && $stageExcept['homepage']) {                            if ($isUpdate) {                                $exceptDate = $stageExcept['insidePage'] = date('Y-m-d', strtotime('+1 week', strtotime($stageExcept['homepage'])));                            } else {                                $stageExcept['insidePage'] = $item['except_range_date'];                            }//                            dd($exceptDate);                        }                        if ($step['step_title'] == "整站资料提供" && $stageExcept['schema']) {                            if ($isUpdate) {                                $exceptDate = $stageExcept['wholeOffer'] = date('Y-m-d', strtotime('+4 week', strtotime($stageExcept['schema'])));                            } else {                                $stageExcept['wholeOffer'] = $item['except_range_date'];                            }                        }                        if ($step['step_title'] == "测试站" && $stageExcept['insidePage']) {                            if ($isUpdate) {                                $exceptDate = $stageExcept['testSite'] = date('Y-m-d', strtotime('+4 week', strtotime($stageExcept['insidePage'])));                            } else {                                $stageExcept['testSite'] = $item['except_range_date'];                            }                        }                        if ($step['step_title'] == "关键词" && $stageExcept['insidePage']) {                            if ($isUpdate) {                                $exceptDate = $stageExcept['keyword'] = date('Y-m-d', strtotime('+4 week', strtotime($stageExcept['insidePage'])));                            } else {                                $stageExcept['keyword'] = $item['except_range_date'];                            }                        }                        if ($step['step_title'] == "SEO完善" && $stageExcept['testSite']) {                            if ($isUpdate) {                                $exceptDate = $stageExcept['seo'] = date('Y-m-d', strtotime('+1 week', strtotime($stageExcept['testSite'])));                            } else {                                $stageExcept['seo'] = $item['except_range_date'];                            }                        }                        if ($step['step_title'] == "上线" && $stageExcept['seo']) {                            if ($isUpdate) {                                $exceptDate = $stageExcept['online'] = date('Y-m-d', strtotime('+1 week', strtotime($stageExcept['seo'])));                            } else {                                $stageExcept['online'] = $item['except_range_date'];                            }                        }//                        if ($isUpdate) {//                            $exceptDate= $nextDate = $this->computeDate($step['step_title'], $nextDate);//                        }                        if ($item['info_id'] == $modifyInfo['info_id']) {//                            $exceptDate = $nextDate = $modifyInfo['except_range_date'];//一定有值                            $isUpdate = true;                        }                        FlowInfo::query()->where(['id' => $item['info_id']])->update([                            'except_range_date' => $exceptDate,                            'detail_list' => json_encode($item['children'] ?? []),                        ]);                    }                }            }        }        return response()->json(['message' => '操作成功']);    }    //流程清空    public function flowClear($siteId)    {        FlowStage::query()->where(['site_id' => $siteId])->delete();        FlowInfo::query()->where(['site_id' => $siteId])->delete();    }    //初始化流程    public function initFlow($siteId)    {        $site = Site::query()->select(['created_at'])->find($siteId);        if (!$site) {            return response()->json(['message' => '站点信息不存在'], 400);        }        $site = $site->toArray();        $stageTplList = FlowStageTpl::query()->with('infoTplList')->get();        if (FlowStage::query()->where(['site_id' => $siteId])->exists()) {            return response()->json(['message' => '流程已初始化'], 400);        }        $userIds = DB::table('user_has_sites')->where(['site_id' => $siteId])->pluck('user_id')->toArray();        $userList = User::query()->select(['id', 'role_id'])->whereIn('id', $userIds)->get()->toArray();        $datetime = date('Y-m-d H:i:s');        $stageExcept = [            'collect' => null,            'schema' => null,            'homepage' => null,            'insidePage' => null,            'wholeOffer' => null,            'testSite' => null,            'keyword' => null,            'seo' => null,            'online' => null        ];        foreach ($stageTplList as $item) {            $stage = FlowStage::query()->create([                'sort' => $item->sort,                'title' => $item->title,                'site_id' => $siteId,            ]);            $insertData = [];            foreach ($item->infoTplList as $info) {                $detailList = $info->detail_list;                foreach ($detailList as &$detail) {                    $duty_man = $detail['duty_man'] ?? [];                    $detail['duty_user'] = $this->getUserIdsByRoleIds($userList, $duty_man);                }                $exceptDate = null;                if ($item->title == "项目资料搜集") {                    $exceptDate = $stageExcept['collect'] = date('Y-m-d', strtotime($site['created_at']));                }                if ($item->title == "网站架构" && $stageExcept['collect']) {                    $exceptDate = $stageExcept['schema'] = date('Y-m-d', strtotime('+1 week', strtotime($stageExcept['collect'])));                }                if ($item->title == "首页设计" && $stageExcept['schema']) {                    $exceptDate = $stageExcept['homepage'] = date('Y-m-d', strtotime('+1 week', strtotime($stageExcept['schema'])));                }                if ($item->title == "内页设计" && $stageExcept['homepage']) {                    $exceptDate = $stageExcept['insidePage'] = date('Y-m-d', strtotime('+1 week', strtotime($stageExcept['homepage'])));                }                if ($item->title == "整站资料提供" && $stageExcept['schema']) {                    $exceptDate = $stageExcept['wholeOffer'] = date('Y-m-d', strtotime('+4 week', strtotime($stageExcept['schema'])));                }                if ($item->title == "测试站" && $stageExcept['insidePage']) {                    $exceptDate = $stageExcept['testSite'] = date('Y-m-d', strtotime('+4 week', strtotime($stageExcept['insidePage'])));                }                if ($item->title == "关键词" && $stageExcept['insidePage']) {                    $exceptDate = $stageExcept['keyword'] = date('Y-m-d', strtotime('+4 week', strtotime($stageExcept['insidePage'])));                }                if ($item->title == "SEO完善" && $stageExcept['testSite']) {                    $exceptDate = $stageExcept['seo'] = date('Y-m-d', strtotime('+1 week', strtotime($stageExcept['testSite'])));                }                if ($item->title == "上线" && $stageExcept['seo']) {                    $exceptDate = $stageExcept['online'] = date('Y-m-d', strtotime('+1 week', strtotime($stageExcept['seo'])));                }                $insertData[] = [                    'site_id' => $siteId,                    'stage_id' => $stage->id,                    'detail_list' => json_encode(array_values($detailList)),                    'except_range_date' => $exceptDate,                    'created_at' => $datetime,                    'updated_at' => $datetime,                ];            }            FlowInfo::query()->insert($insertData);        }        return response()->json(['message' => '操作成功']);    }    public function getStageSort($stageTpl, $stageTitle)    {        foreach ($stageTpl as $item) {            if ($item->title === $stageTitle) {                return $item->sort;            }        }        return 0;    }    private function computeDate($stepTitle, $prevDate)    {        $preTime = strtotime($prevDate);        if ($stepTitle == "项目资料搜集") {            return $prevDate;        }        if ($stepTitle == "网站架构") {            return date('Y-m-d', strtotime('+1 week', $preTime));        }        if ($stepTitle == "首页设计") {            return date('Y-m-d', strtotime('+1 week', $preTime));        }        if ($stepTitle == "内页设计") {            return date('Y-m-d', strtotime('+1 week', $preTime));        }        if ($stepTitle == "整站资料提供") {            return date('Y-m-d', strtotime('+2 week', $preTime));        }        if ($stepTitle == "测试站") {            return date('Y-m-d', strtotime('+4 week', $preTime));        }        if ($stepTitle == "关键词") {            return date('Y-m-d', strtotime('+0 week', $preTime));        }        if ($stepTitle == "SEO完善") {            return date('Y-m-d', strtotime('+1 week', $preTime));        }        if ($stepTitle == "上线") {            return date('Y-m-d', strtotime('+1 week', $preTime));        }        return null;    }    protected function getUserIdsByRoleIds(&$scopes, $roleIds)    {        $userIds = [];        foreach ($scopes as $scope) {            if (in_array($scope['role_id'], $roleIds)) {                $userIds[] = $scope['id'];            }        }        if (in_array(-1, $roleIds)) {            $userIds[] = -1;        }        return $userIds;    }}
 |