package com.xinelu.manage.controller.info;

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

import com.xinelu.manage.service.infocategory.IInfoCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xinelu.common.annotation.Log;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.enums.BusinessType;
import com.xinelu.manage.domain.info.Info;
import com.xinelu.manage.service.info.IInfoService;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 资讯信息Controller
 *
 * @author xinelu
 * @date 2024-04-11
 */
@Slf4j
@RestController
@RequestMapping("/manage/info")
public class InfoController extends BaseController {
    @Resource
    private IInfoService infoService;
    @Resource
    private IInfoCategoryService infoCategoryService;

    /**
     * 查询资讯信息列表
     */
    @PreAuthorize("@ss.hasPermi('manage:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(Info info) {
        startPage();
        List<Info> list = infoService.selectInfoList(info);
        return getDataTable(list);
    }

    /**
     * 导出资讯信息列表
     */
    @PreAuthorize("@ss.hasPermi('manage:info:export')")
    @Log(title = "资讯信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Info info) {
        List<Info> list = infoService.selectInfoList(info);
        ExcelUtil<Info> util = new ExcelUtil<>(Info.class);
        util.exportExcel(response, list, "资讯信息数据");
    }

    /**
     * 获取资讯信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(infoService.selectInfoById(id));
    }

    /**
     * 新增资讯信息
     */
    @PreAuthorize("@ss.hasPermi('manage:info:add')")
    @Log(title = "资讯信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Info info) {
        return toAjax(infoService.insertInfo(info));
    }

    /**
     * 修改资讯信息
     */
    @PreAuthorize("@ss.hasPermi('manage:info:edit')")
    @Log(title = "资讯信息", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Info info) {
        return infoService.updateInfo(info);
    }

    /**
     * 删除资讯信息
     */
    @PreAuthorize("@ss.hasPermi('manage:info:remove')")
    @Log(title = "资讯信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return infoService.deleteInfoByIds(ids);
    }


    @Log(title = "图片文件上传", businessType = BusinessType.OTHER)
    @PostMapping("/picUpload")
    public AjaxResult picUpload(@RequestBody MultipartFile file) {

        return infoService.picUpload(file);
    }
}
