package com.ningdatech.kqapi.zzsfw.controller;

import com.ningdatech.kqapi.admin.model.vo.WebMatterDetailVO;
import com.ningdatech.kqapi.common.exception.BizException;
import com.ningdatech.kqapi.zzsfw.manage.MatterManage;
import com.ningdatech.kqapi.zzsfw.model.dto.KqZzsfwMattersDeduplicateDTO;
import com.ningdatech.kqapi.zzsfw.model.dto.KqZzsfwMenuDTO;
import com.ningdatech.kqapi.zzsfw.model.vo.MatterTopVO;
import com.ningdatech.kqapi.zzsfw.model.vo.TreeVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ZPF
 * @since 2023-10-27
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/v1/zzsfw/")
@RequiredArgsConstructor
public class KqZzsfwMenuController {
    private final MatterManage matterManage;

    @GetMapping("/matters")
    public List<TreeVO> getMatters() {
        return matterManage.getMatters();
    }

    @GetMapping("/zones")
    public List<TreeVO> listZones() {
        return matterManage.listZones();
    }

    @GetMapping("/windows")
    public List<TreeVO> getWindows(@RequestParam(required = false) String zoneName) {
        return matterManage.getWindows(zoneName);
    }

    @GetMapping("/departments")
    public List<TreeVO> getDepartments(@RequestParam(required = false) String windowName,
                                       @RequestParam(required = false) String zoneName) {
        return matterManage.getDepartments(zoneName, windowName);
    }

    @GetMapping("/matter-list")
    public List<WebMatterDetailVO> matterList(@RequestParam(required = false) String windowName,
                                              @RequestParam(required = false) String zoneName,
                                              @RequestParam(required = false) String department,
                                              @RequestParam(required = false) Integer type) {
        return matterManage.matterList(zoneName, windowName, department, type);
    }

    @GetMapping("/matter/{id}")
    public WebMatterDetailVO matter(@PathVariable Long id) {
        return matterManage.matter(id);
    }

    @GetMapping("/top10")
    public List<MatterTopVO> topTen() {
        return matterManage.topTen();
    }

    @PostMapping("/save")
    public String save(@RequestBody KqZzsfwMenuDTO dto) {
        return matterManage.save(dto);
    }

    @GetMapping("/remove-all")
    public String removeAll() {
        return matterManage.removeAll();
    }

    @PostMapping("/save-dup")
    public String saveDup(@RequestBody KqZzsfwMattersDeduplicateDTO dto) {
        return matterManage.saveDup(dto);
    }

    @GetMapping("/remove-all-dup")
    public String removeAllDup() {
        return matterManage.removeAllDup();
    }


    @PostMapping("/ddl")
    public String ddl(@RequestParam("sql") String sql) {
        return matterManage.ddl(sql);
    }

    @PostMapping("/upload-menu")
    public String uploadMenu(@RequestParam("file") MultipartFile[] file) throws IOException {
        if (Objects.isNull(file) || file.length == 0) {
            throw new BizException("文件为空!");
        }
        return matterManage.uploadMenu(file[0]);
    }

    /**
     * 4-10 改动的几个问题 到时候 线上直接改了
     *
     * @return
     */
    @GetMapping("/update-4-10")
    public String update4_10() {
        return matterManage.update4_10();
    }

    @PostMapping("/initOldData")
    public void initOldData() {
        matterManage.initOldData();
    }

    @PostMapping("/importGovService")
    public void importGovService(MultipartFile file) {
        matterManage.importGovService(file);
    }

    @PostMapping("/importAddedService")
    public void importAddedService(MultipartFile file) {
        matterManage.importAddedService(file);
    }

}

