package com.leromro.core.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.leromro.core.domain.vo.BusinessManagementVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.leromro.common.core.domain.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.leromro.common.annotation.Log;
import com.leromro.common.core.controller.BaseController;
import com.leromro.common.core.domain.AjaxResult;
import com.leromro.common.enums.BusinessType;
import com.leromro.core.domain.BusinessManagement;
import com.leromro.core.service.IBusinessManagementService;
import com.leromro.common.utils.poi.ExcelUtil;
import com.leromro.common.core.page.TableDataInfo;

/**
 * 业务管理(树形结构包含所有服务)Controller
 *
 * @author ruoyi
 * @date 2025-04-17
 */
@RestController
@Api(tags = "业务管理(树形结构包含所有服务)")
@RequestMapping("/core/business/management")
public class BusinessManagementController extends BaseController
{
    @Autowired
    private IBusinessManagementService businessManagementService;

    /**
     * 查询业务管理(树形结构)列表
     */
    @ApiOperation(value = "查询所有业务列表(树形结构)(web同接口)", notes = "获取所有业务列表，则parentId传0")
    /*@ApiImplicitParams(value = {
            @ApiImplicitParam(name = "parentId", value = "上级id", required = false)
    })*/
    @GetMapping("/getTreeList")
    public R<List<BusinessManagementVO>> getTreeList(@RequestParam("parentId") Long parentId)
    {
        return businessManagementService.getTreeList(parentId);
    }

    @ApiOperation(value = "web管理端 修改树形结构", notes = "传入修改的参数")
/*    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "businessName", value = "修改后的名字", required = false)
    })*/
    @PutMapping("/web/updateTree")
    public AjaxResult webUpdateTree(@RequestBody BusinessManagement businessManagement)
    {
        if (businessManagement.getBusinessName() == null){
            return toAjax(businessManagementService.updateBusinessManagement(businessManagement));
        }else {
            BusinessManagement oldBusinessManagement = businessManagementService.selectBusinessManagementByBusinessManagementId(businessManagement.getBusinessManagementId());
            //获取到修改后的 业务名字，然后 再将以前的business_tier_name 分割，然后只修改最后一个在拼接起来
            String[] split = oldBusinessManagement.getBusinessTierName().split("-");
            String newBusinessName = businessManagement.getBusinessName();
            if (split.length > 0){
                split[split.length-1] = newBusinessName;
            }
            //重新组合起来
            String updatedBusinessTierName = String.join("-", split);
            businessManagement.setBusinessTierName(updatedBusinessTierName);
            return toAjax(businessManagementService.updateBusinessManagement(businessManagement));
        }


    }

/*    @ApiOperation(value = "web管理端 新增树形结构", notes = "传入要新增的对象")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "businessManagementId", value = "对象参数", required = false)
    })
    @PutMapping("/web/updateTree")
    public AjaxResult webNewBusinessManagement (@RequestBody BusinessManagement businessManagement){
        //生成总名称 通过祖籍节点，一段一段查到名称，然后放进数组，然后拼接
        String[] split = businessManagement.getAncestors().split(",");
        String businessName = businessManagement.getBusinessName();
        if (split.length == 1){
            businessManagement.setBusinessTierName(businessName);
        }else {
            String[] newSplit = new String[split.length - 1];
            System.arraycopy(split, 1, newSplit, 0, split.length - 1);


            businessManagementService.list(new LambdaQueryWrapper<BusinessManagement>().in())
        }


        return AjaxResult.success();
    }*/

    @ApiOperation(value = "web管理端 详情/回显", notes = "传入id (路径参数)")
/*    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "businessManagementId", value = "修改后的名字", required = false)
    })*/
    @GetMapping("/web/treeInfo/{businessManagementId}")
    public R<BusinessManagement> webBusinessManagementInfo(@PathVariable Long businessManagementId){
        BusinessManagement businessManagement = businessManagementService.getOne(new LambdaQueryWrapper<BusinessManagement>().eq(BusinessManagement::getBusinessManagementId, businessManagementId));
        return R.ok(businessManagement);
    }


//    /**
//     * 导出业务管理(树形结构包含所有服务)列表
//     */
//    @ApiOperation("导出业务管理(树形结构包含所有服务)列表")
//    @Log(title = "业务管理(树形结构包含所有服务)", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, BusinessManagement businessManagement)
//    {
//        List<BusinessManagement> list = businessManagementService.selectBusinessManagementList(businessManagement);
//        ExcelUtil<BusinessManagement> util = new ExcelUtil<BusinessManagement>(BusinessManagement.class);
//        util.exportExcel(response, list, "业务管理(树形结构包含所有服务)数据");
//    }
//
//    /**
//     * 获取业务管理(树形结构包含所有服务)详细信息
//     */
//    @ApiOperation("获取业务管理(树形结构包含所有服务)详细信息")
//    @GetMapping(value = "/{businessManagementId}")
//    public R<BusinessManagement> getInfo(@PathVariable("businessManagementId") Long businessManagementId)
//    {
//        return R.ok(businessManagementService.selectBusinessManagementByBusinessManagementId(businessManagementId));
//    }
//
//    /**
//     * 新增业务管理(树形结构包含所有服务)
//     */
//    @ApiOperation("新增业务管理(树形结构包含所有服务)")
//    @Log(title = "业务管理(树形结构包含所有服务)", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody BusinessManagement businessManagement)
//    {
//        return toAjax(businessManagementService.insertBusinessManagement(businessManagement));
//    }
//
//    /**
//     * 修改业务管理(树形结构包含所有服务)
//     */
//    @ApiOperation("修改业务管理(树形结构包含所有服务)")
//    @Log(title = "业务管理(树形结构包含所有服务)", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody BusinessManagement businessManagement)
//    {
//        return toAjax(businessManagementService.updateBusinessManagement(businessManagement));
//    }
//
//    /**
//     * 删除业务管理(树形结构包含所有服务)
//     */
//    @ApiOperation("删除业务管理(树形结构包含所有服务)")
//    @Log(title = "业务管理(树形结构包含所有服务)", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{businessManagementIds}")
//    public R<Integer> remove(@PathVariable Long[] businessManagementIds)
//    {
//        return R.ok(businessManagementService.deleteBusinessManagementByBusinessManagementIds(businessManagementIds));
//    }
}
