package com.yinet.web.controller.touchscreen;

import com.yinet.common.config.Global;
import com.yinet.common.core.controller.BaseController;
import com.yinet.common.core.domain.AjaxResult;
import com.yinet.common.core.page.TableDataInfo;
import com.yinet.common.utils.file.FileUploadUtils;
import com.yinet.system.domain.TouchScreenNotice;
import com.yinet.system.service.TouchScreenNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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;

/**
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/touchscreen/notice")
public class NoticeAdminController extends BaseController {
    static String prefix = "touchscreen/admin/notice";
    @Autowired
    private TouchScreenNoticeService touchScreenNoticeService;
    /**
     * 公示公告
     *
     * @return
     */
    @GetMapping()
    public String GetInfoList() {
        return prefix + "/list";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list() {
        startPage();
        List<TouchScreenNotice> touchScreenNotices = touchScreenNoticeService.selectNoticeList();
        return getDataTable(touchScreenNotices);
    }

    /**
     * 新增公示公告
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增公示公告
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated TouchScreenNotice notice) {
        return toAjax(touchScreenNoticeService.insertNotice(notice));
    }

    /**
     * 编辑公示公告
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        mmap.addAttribute("noticeNode", touchScreenNoticeService.selectNoticeById(id));
        return prefix + "/edit";
    }

    /**
     * 编辑公示公告
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated TouchScreenNotice notice) {
        return toAjax(touchScreenNoticeService.updateNotice(notice));
    }

    /**
     * 删除公示公告
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult deletnotice(String ids) {
        return toAjax(touchScreenNoticeService.deletNotice(ids));
    }

    @PostMapping("/uploadImg")
    @ResponseBody
    public String fileUpload(@RequestParam("preview") MultipartFile file) {
        String preview = "";
        try {
            preview = FileUploadUtils.upload(Global.getNoticeImgPath(), file);
        } catch (IOException e) {
            return "上传失败";
        }
        System.out.println("上传路径：" + preview);
        return "{\"path\": \"" + preview + "\"}";
    }
}
