package com.vctgo.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vctgo.common.core.exception.ServiceException;
import com.vctgo.common.core.utils.poi.ExcelUtil;
import com.vctgo.common.core.web.controller.BaseController;
import com.vctgo.common.core.web.domain.AjaxResult;
import com.vctgo.common.log.annotation.Log;
import com.vctgo.common.log.enums.BusinessType;
import com.vctgo.common.security.annotation.Logical;
import com.vctgo.common.security.annotation.RequiresPermissions;
import com.vctgo.system.api.domain.SysTenant;
import com.vctgo.system.api.domain.dto.SysGardenManageDto;
import com.vctgo.system.domain.SysGardenManage;
import com.vctgo.system.service.ISysGardenManageService;
import com.vctgo.system.service.ISysTenantService;
import com.vctgo.system.util.ConstantUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 幼儿园集团管理Controller
 *
 * @author wanglonghua
 * @date 2023-06-02
 */
@RestController
@RequestMapping("/garden")
public class SysGardenManageController extends BaseController {
    @Autowired
    private ISysGardenManageService sysGardenManageService;

    @Autowired
    private ISysTenantService sysTenantService;

    /**
     * 查询幼儿园集团管理列表
     */
    @RequiresPermissions("system:garden:page")
    @GetMapping("/page")
    public AjaxResult pageList(SysGardenManage sysGardenManage) {
        IPage<SysGardenManage> list = sysGardenManageService.selectSysGardenManagePage(sysGardenManage);
        return AjaxResult.success(list);
    }

    /**
     * 查询幼儿园集团管理列表
     */
    @GetMapping("/list/all")
    public AjaxResult listAll(SysGardenManage sysGardenManage) {
        List<SysGardenManage> list = sysGardenManageService.selectSysGardenManageList(sysGardenManage);
        return AjaxResult.success(list);
    }

    /**
     * 导出幼儿园集团管理列表
     */
    @RequiresPermissions("system:garden:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysGardenManage sysGardenManage) {
        List<SysGardenManage> list = sysGardenManageService.selectSysGardenManageList(sysGardenManage);
        ExcelUtil<SysGardenManage> util = new ExcelUtil<SysGardenManage>(SysGardenManage.class);
        util.exportExcel(response, list, "幼儿园集团管理数据");
    }

    /**
     * 获取幼儿园集团管理详细信息
     */
    @RequiresPermissions("system:garden:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        SysGardenManage sysGardenManage = sysGardenManageService.selectSysGardenManageById(id);
        return AjaxResult.success(sysGardenManage);
    }

    /**
     * 获取幼儿园集团管理详细信息
     */
    @GetMapping(value = "/gardenManage/{id}")
    public AjaxResult getGardenManageInfo(@PathVariable("id") Long id) {
        SysGardenManage sysGardenManage = sysGardenManageService.selectSysGardenManageById(id);
        return AjaxResult.success(sysGardenManage);
    }

    @RequiresPermissions(value = {"system:garden:check", "system:garden:add"}, logical = Logical.OR)
    @GetMapping(value = {"/list/{name}/", "/list/{name}/{id}"})
    public AjaxResult repeatGardenNameList(@PathVariable("name") String name,
                                           @PathVariable(value = "id", required = false) String id) {
        Map<String, Object> params = new HashMap<>();
        params.put("name", name);
        if (StringUtils.isNotEmpty(id) && StringUtils.isNumeric(id)) {
            params.put("id", Long.parseLong(id));
        }
        List<SysGardenManage> gardenManageList = sysGardenManageService.selectGardenListByParams(params);
        return AjaxResult.success(gardenManageList);
    }

    /**
     * 判断一个集团园是否已开通集团租户
     */
    @GetMapping("/detail/{gardenId}")
    public AjaxResult gardenTenantDetail(@PathVariable Long gardenId) {
        SysTenant sysTenant = new SysTenant();
        sysTenant.setGardenId(gardenId);
        sysTenant.setTenantType(ConstantUtils.TENANT_TYPE_1);
        sysTenant.setDelFlag(ConstantUtils.IS_DELETED_0);
        return AjaxResult.success(sysTenantService.selectSysTenantList(sysTenant));
    }

    /**
     * 新增幼儿园集团管理
     */
    @RequiresPermissions("system:garden:add")
    @PostMapping
    public AjaxResult add(@RequestBody SysGardenManage sysGardenManage) {
        return toAjax(sysGardenManageService.insertSysGardenManage(sysGardenManage));
    }

    /**
     * 修改幼儿园集团管理
     */
    @RequiresPermissions("system:garden:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody SysGardenManage sysGardenManage) {
        return toAjax(sysGardenManageService.updateSysGardenManage(sysGardenManage));
    }

    /**
     * 逻辑删除幼儿园集团管理
     */
    @RequiresPermissions("system:garden:abandon")
    @PutMapping("/abandon/{ids}")
    public AjaxResult logicalDelete(@PathVariable Long[] ids) {
        List<SysTenant> sysGardenTenantList = new ArrayList<>();
        if (ids.length == 1) {
            SysTenant gardenSysTenant = new SysTenant();
            gardenSysTenant.setGardenId(ids[0]);
            gardenSysTenant.setTenantType(ConstantUtils.TENANT_TYPE_1);
            gardenSysTenant.setDelFlag(ConstantUtils.IS_DELETED_0);
            sysGardenTenantList = sysTenantService.selectSysTenantList(gardenSysTenant);
            if (CollectionUtils.isNotEmpty(sysGardenTenantList)) {
                throw new ServiceException("所选集团存在集团租户，不允许删除");
            }
        } else if (ids.length > 1) {
            List<Long> existTenantGarden = new ArrayList<>();
            for (Long id : ids) {
                SysTenant gardenSysTenant = new SysTenant();
                gardenSysTenant.setGardenId(id);
                gardenSysTenant.setTenantType(ConstantUtils.TENANT_TYPE_1);
                gardenSysTenant.setDelFlag(ConstantUtils.IS_DELETED_0);
                sysGardenTenantList = sysTenantService.selectSysTenantList(gardenSysTenant);
                if (CollectionUtils.isNotEmpty(sysGardenTenantList)) {
                    existTenantGarden.add(id);
                }
            }
            if (CollectionUtils.isNotEmpty(existTenantGarden)) {
                throw new ServiceException("所选集团中有" + existTenantGarden.size() + "个集团存在集团租户，不允许删除");
            }
        }

        return toAjax(sysGardenManageService.logicalDeleteByIds(ids));
    }

    /**
     * 删除幼儿园集团管理
     */
    @RequiresPermissions("system:garden:remove")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysGardenManageService.deleteSysGardenManageByIds(ids));
    }


    /**
     * 修改幼儿园集团管理
     */
    @GetMapping(value = "/gardenManage")
    public AjaxResult getGardenManageIdList(@SpringQueryMap SysGardenManageDto sysGardenManageDto) {
        List<Long> gardenManageIdList = sysGardenManageService.selectGardenManageIdList(sysGardenManageDto);
        return AjaxResult.success(gardenManageIdList);
    }
}
