package com.tao.controller;

import com.tao.pojo.AjaxResult;
import com.tao.pojo.AppElement;
import com.tao.service.AppElementService;
import com.tao.service.AppPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName AppElementController
 * @Description TODO
 * @Author zhangyuantao
 * @Date 2022/12/10 16:34
 * @Version 1.0
 */
@Controller
@RequestMapping("/element")
public class AppElementController {

    @Autowired
    AppElementService appElementService;

    @Autowired
    AppPageService appPageService;

    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file) throws Exception {
        try {
            String name = file.getName();
            System.out.println(name);

            AjaxResult ajax = AjaxResult.success();
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/sync")
    @ResponseBody
    public AjaxResult syncElement(@RequestParam(name = "rootPath", required = true) String rootPath) {
        if (rootPath != null && rootPath != "" && rootPath.contains("page")) {
            appPageService.deleteAll();
            return appElementService.syncElements(rootPath);
        } else {
            return AjaxResult.error("目录校验错误，请重新确认！");
        }
    }

    @GetMapping("/data")
    @ResponseBody
    public AjaxResult getElements(String selectType, AppElement appElement) {
        if ("fuzzy".equals(selectType)) {
            if (appElement.getElementType() != null && appElement.getElementType() == 3) {
                appElement.setElementType(1l);
                List<AppElement> appElements = appElementService.selectAppElementListFuzzy(appElement);
                appElement.setElementType(2l);
                return AjaxResult.success("成功查询", appElements.addAll(appElementService.selectAppElementListFuzzy(appElement)));
            }
            return AjaxResult.success("成功查询", appElementService.selectAppElementListFuzzy(appElement));
        } else {
            // 前端传elemenType为3，则需要返回个人的数据，即type为1或2的数据
            if (appElement.getElementType() != null && appElement.getElementType() == 3) {
                List<AppElement> appElements = appElementService.selectAppElementListByType(1l);
                appElements.addAll(appElementService.selectAppElementListByType(2l));
                return AjaxResult.success("成功查询", appElements);
            }
            return AjaxResult.success("成功查询", appElementService.selectAppElementList(appElement));
        }
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addElement(AppElement appElement) {
        appElementService.insertAppElement(appElement);
        return AjaxResult.success("添加成功");
    }

    @GetMapping("/verifyName")
    @ResponseBody
    public AjaxResult findElementByName(String elementName) {
        AppElement appElement = appElementService.selectAppElementByName(elementName);
        if (appElement != null) {
            return AjaxResult.error("已存在同名元素");
        } else {
            return AjaxResult.success("未重复");
        }
    }

}
