package com.bootdo.data.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.common.annotation.Log;
import com.bootdo.data.domain.DataUploadDO;
import com.bootdo.data.service.DataUploadService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * @Description: 重要数据
 * @Author: wangp
 * @Date: 2022/2/27 14:49
 */
@Controller
@RequestMapping("/data/dataUpload")
public class DataUploadController {
    @Autowired
    private DataUploadService dataUploadService;

    @Log("请求重要数据")
    @GetMapping()
    @RequiresPermissions("data:dataUpload:dataUpload")
    String DataUpload() {
        return "data/dataUpload/dataUpload";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("data:dataUpload:dataUpload")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<DataUploadDO> dataUploadList = dataUploadService.list(query);
        int total = dataUploadService.count(query);
        PageUtils pageUtils = new PageUtils(dataUploadList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("data:dataUpload:add")
    String add() {
        return "data/dataUpload/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("data:dataUpload:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        DataUploadDO dataUpload = dataUploadService.get(id);
        model.addAttribute("dataUpload", dataUpload);
        return "data/dataUpload/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("data:dataUpload:add")
    public R save(DataUploadDO dataUpload) {
        if (dataUploadService.save(dataUpload) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("data:dataUpload:edit")
    public R update(DataUploadDO dataUpload) {
        dataUploadService.update(dataUpload);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("data:dataUpload:remove")
    public R remove(Long id) {
        if (dataUploadService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("data:dataUpload:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] ids) {
        dataUploadService.batchRemove(ids);
        return R.ok();
    }

}
