package com.ruoyi.web.controller.dxp.system.share;

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

import com.ruoyi.common.annotation.Anonymous;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dxp.domain.Literature;
import com.ruoyi.dxp.service.ILiteratureService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文献管理Controller
 * 
 * @author jy
 * @date 2022-11-11
 */
@RestController
@RequestMapping("/system/share/literature")
public class LiteratureController extends BaseController
{
    @Autowired
    private ILiteratureService literatureService;

    /**
     * 查询文献管理列表
     */
    @PreAuthorize("@ss.hasPermi('system/share:literature:list')")
    @GetMapping("/list")
    public TableDataInfo list(Literature literature)
    {
        startPage();
        List<Literature> list = literatureService.selectLiteratureList(literature);
        return getDataTable(list);
    }

    /**
     * 导出文献管理列表
     */
    @PreAuthorize("@ss.hasPermi('system/share:literature:export')")
    @Log(title = "文献管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Literature literature)
    {
        List<Literature> list = literatureService.selectLiteratureList(literature);
        ExcelUtil<Literature> util = new ExcelUtil<Literature>(Literature.class);
        util.exportExcel(response, list, "文献管理数据");
    }

    /**
     * 获取文献管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system/share:literature:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(literatureService.selectLiteratureById(id));
    }

    /**
     * 新增文献管理
     */
    @PreAuthorize("@ss.hasPermi('system/share:literature:add')")
    @Log(title = "文献管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Literature literature)
    {
        return toAjax(literatureService.insertLiterature(literature));
    }

    /**
     * 修改文献管理
     */
    @PreAuthorize("@ss.hasPermi('system/share:literature:edit')")
    @Log(title = "文献管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Literature literature)
    {
        return toAjax(literatureService.updateLiterature(literature));
    }

    /**
     * 删除文献管理
     */
    @PreAuthorize("@ss.hasPermi('system/share:literature:remove')")
    @Log(title = "文献管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(literatureService.deleteLiteratureByIds(ids));
    }

    /**
     * 更新文献次数
     * @param id 文献id
     * @param type v 浏览次数，d 下载次数
     * @return
     */
    @Anonymous
    @GetMapping("/addcount")
    public AjaxResult addCount(Long id, String type) {
        if (type == "v") {
            literatureService.addViewCount(id);
        } else if (type == "d") {
            literatureService.addDownloadCount(id);
        } else {
            return error("类型错误");
        }

        return success();
    }
}
