package cn.cslg.pas.controller;

import cn.cslg.pas.common.core.base.Constants;
import cn.cslg.pas.common.model.dto.QueryPathStructureNameDTO;
import cn.cslg.pas.common.model.dto.StructureAddNewDTO;
import cn.cslg.pas.common.model.dto.StructureQueryPageDTO;
import cn.cslg.pas.common.model.dto.StructureUpdateDTO;
import cn.cslg.pas.common.model.vo.PathStructureNameVO;
import cn.cslg.pas.common.model.vo.StructureVO;
import cn.cslg.pas.common.utils.Response;
import cn.cslg.pas.exception.XiaoShiException;
import cn.cslg.pas.service.IStructureService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 架构的Controller层
 *
 * @Author chenyu
 * @Date 2023/3/10
 */
@Tag(name = "架构管理模块")
@RequiredArgsConstructor
@Slf4j
@RequestMapping(Constants.API_VERSION_V2 + "/structures")
@RestController
public class StructureController {
    private final IStructureService structureService;

    @Operation(summary = "新增架构")
    @PostMapping("/addNew")
    public String addNew(String jsons, List<MultipartFile> files) {
        if (jsons != null) {
            StructureAddNewDTO structureAddNewDTO = JSONObject.parseObject(jsons, StructureAddNewDTO.class);
            log.info("开始处理【新增架构】的请求，请求参数为：{}, {}", structureAddNewDTO, files);
            try {
                structureService.addNew(structureAddNewDTO, files);
            } catch (XiaoShiException e) {
                return Response.error(e.getMessage());
            }
            return Response.success("新增架构完成");
        }
        return Response.error("请求参数为null");
    }

    @Operation(summary = "修改架构")
    @PostMapping("/update")
    public String update(String jsons, List<MultipartFile> files) {
        if (jsons != null) {
            StructureUpdateDTO structureUpdateDTO = JSONObject.parseObject(jsons, StructureUpdateDTO.class);
            log.info("开始处理【修改架构】的请求，请求参数为：{}, {}", structureUpdateDTO, files);
            try {
                structureService.update(structureUpdateDTO, files);
            } catch (XiaoShiException e) {
                return Response.error(e.getMessage());
            }
            return Response.success("修改架构完成");
        }
        return Response.error("请求参数为null");
    }

    @Operation(summary = "查询架构树")
    @PostMapping("/query")
    public Response query(@RequestBody StructureQueryPageDTO structureQueryPageDTO) {
        log.info("开始处理【查询架构树】的请求，请求参数为：{}", structureQueryPageDTO);
        StructureVO queryResult = structureService.query(structureQueryPageDTO);
        return Response.ok(queryResult);
    }

    @Operation(summary = "查询拼接路径名")
    @PostMapping("/queryPathStructureName")
    public String queryPathStructureName(@RequestBody QueryPathStructureNameDTO queryPathStructureNameDTO) {
        log.info("开始处理【查询所有架构路径和路径架构名称】的业务，参数为：{}", queryPathStructureNameDTO);
        List<PathStructureNameVO> queryResults = structureService.queryPathStructureName(queryPathStructureNameDTO);
        return Response.success(queryResults);
    }

    @Operation(summary = "删除架构")
    @GetMapping("/delete")
    public String delete(Integer id) {
        log.info("开始处理【删除架构】的请求，请求参数为：{}", id);
        try {
            structureService.delete(id);
        } catch (XiaoShiException e) {
            return Response.error(e.getMessage());
        }
        return Response.success("删除架构完成");
    }

}
