package com.ruoyi.web.controller.dxp.customer;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.dxp.constant.DxpConstants;
import com.ruoyi.dxp.domain.Literature;
import com.ruoyi.dxp.domain.UserResource;
import com.ruoyi.dxp.service.ILiteratureService;
import com.ruoyi.dxp.service.IUserResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/customer/literature")
public class LiteratureCustomerController extends BaseController {
    @Autowired
    private ILiteratureService literatureService;

    @Autowired
    private IUserResourceService userResourceService;

    /**
     * 文献列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Literature literature)
    {
        startPage();
        literature.setPublishType("open,condition");
        List<Literature> list = literatureService.selectLiteratureList(literature);

        // 用户查询不返回地址
        if (list != null && list.size() > 0) {
            for (Literature item : list) {
                item.setFileUrl("");
            }
        }
        return getDataTable(list);
    }

    /**
     * 增加下载次数
     */
    @GetMapping("/addcount")
    public AjaxResult addDownloadCount(Long id) {
        literatureService.addDownloadCount(id);
        return success();
    }

    /**
     * 获取我已通过审核的文献
     */
    @GetMapping("/geturl")
    public AjaxResult getFileUrl(Long id) {
        Literature literature = literatureService.selectLiteratureById(id);
        if (literature == null) {
            return error("文献不存在");
        }
        if (literature.getPublishType().equals("open")) {
            return success(literature);
        }
        if (literature.getPublishType().equals("condition")) {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            UserResource req = new UserResource();
            req.setResourceId(id);
            req.setUserId(loginUser.getUserId());
            List<UserResource> userResources = userResourceService.selectUserResourceList(req);
            if (userResources != null && userResources.size() > 0) {
                if (userResources.get(0).getStatus().equals(DxpConstants.STATUS_VALID)) {
                    return success(literature);
                } else if (userResources.get(0).getStatus().equals(DxpConstants.STATUS_WAIT_APPROVED)) {
                    return AjaxResult.success("wait");
                } else if (userResources.get(0).getStatus().equals(DxpConstants.STATUS_REJECT)) {
                    return AjaxResult.success("您提交的申请被驳回，驳回原因：" + userResources.get(0).getAuditRemark() + "，请重新提交申请");
                }
            }
        }

        return success();
    }
}
