package com.inspect.metadata.version.controller;

import com.inspect.metadata.version.domain.VersionManagement;
import com.inspect.metadata.version.service.IVersionManagementService;
import com.inspect.base.core.utils.poi.ExcelUtil;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.base.core.web.page.TableDataInfo;
import com.inspect.common.log.annotation.Log;
import com.inspect.common.log.enums.BizType;
import com.inspect.common.security.annotation.RequiresPermissions;

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

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping({"/version"})
public class VersionManagementController extends BaseController {
    @Resource
    private IVersionManagementService versionManagementService;

    @RequiresPermissions({"basedata:version:list"})
    @GetMapping({"/list"})
    public TableDataInfo list(VersionManagement versionManagement) {
        this.startPage();
        List<VersionManagement> list = this.versionManagementService.selectVersionManagementList(versionManagement);
        return this.getDataTable(list);
    }

    @RequiresPermissions({"basedata:version:export"})
    @Log(title = "版本管理", businessType = BizType.EXPORT)
    @PostMapping({"/export"})
    public void export(HttpServletResponse response, VersionManagement versionManagement) {
        List<VersionManagement> list = this.versionManagementService.selectVersionManagementList(versionManagement);
        ExcelUtil<VersionManagement> util = new ExcelUtil<>(VersionManagement.class);
        util.exportExcel(response, list, "版本管理数据");
    }

    @RequiresPermissions({"basedata:version:query"})
    @GetMapping({"/{versionId}"})
    public AjaxResult getInfo(@PathVariable("versionId") Long versionId) {
        return AjaxResult.success(this.versionManagementService.selectVersionManagementByVersionId(versionId));
    }

    @RequiresPermissions({"basedata:version:add"})
    @Log(title = "版本管理", businessType = BizType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VersionManagement versionManagement) {
        return this.toAjax(this.versionManagementService.insertVersionManagement(versionManagement));
    }

    @RequiresPermissions({"basedata:version:edit"})
    @Log(title = "版本管理", businessType = BizType.UPDATE)
    @PostMapping({"/edit"})
    public AjaxResult edit(@RequestBody VersionManagement versionManagement) {
        return this.toAjax(this.versionManagementService.updateVersionManagement(versionManagement));
    }

    @RequiresPermissions({"basedata:version:remove"})
    @Log(title = "版本管理", businessType = BizType.DELETE)
    @PostMapping({"/remove/{versionIds}"})
    public AjaxResult remove(@PathVariable Long[] versionIds) {
        return this.toAjax(this.versionManagementService.deleteVersionManagementByVersionIds(versionIds));
    }

    @PostMapping({"/uploadPicMap"})
    public AjaxResult uploadPicMap(@RequestPart("file") MultipartFile file) throws Exception {
        String path = this.versionManagementService.uploadPicMap(file.getInputStream(), file.getOriginalFilename());
        return AjaxResult.success(path);
    }

    @GetMapping({"/downloadPicMap"})
    public AjaxResult downloadPicMap(String versionId) {
        VersionManagement versionManagement = this.versionManagementService.selectVersionManagementByVersionId(Long.valueOf(versionId));
        Map map = new HashMap<>();
        map.put("path", versionManagement.getFilePath());
        return AjaxResult.success(map);
    }

    @RequiresPermissions({"basedata:version:list"})
    @GetMapping({"/getNewVersionInfo"})
    public AjaxResult list() {
        List<VersionManagement> list = this.versionManagementService.getNewVersionInfo();
        return AjaxResult.success(list);
    }
}
