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

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

import com.ruoyi.dxp.domain.UserResourceItem;
import com.ruoyi.dxp.dto.AuditDto;
import com.ruoyi.dxp.dto.ResourceAuditDto;
import com.ruoyi.dxp.service.IUserResourceItemService;
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.UserResource;
import com.ruoyi.dxp.service.IUserResourceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户订阅资源Controller
 * 
 * @author jy
 * @date 2022-12-07
 */
@RestController
@RequestMapping("/system/audit/resource")
public class UserResourceController extends BaseController
{
    @Autowired
    private IUserResourceService userResourceService;

    @Autowired
    private IUserResourceItemService userResourceItemService;

    /**
     * 查询用户订阅资源列表
     */
    @PreAuthorize("@ss.hasPermi('system/audit:resource:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserResource userResource)
    {
        startPage();
        List<UserResource> list = userResourceService.selectUserResourceList(userResource);
        return getDataTable(list);
    }

    /**
     * 导出用户订阅资源列表
     */
    @PreAuthorize("@ss.hasPermi('system/audit:resource:export')")
    @Log(title = "用户订阅资源", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserResource userResource)
    {
        List<UserResource> list = userResourceService.selectUserResourceList(userResource);
        ExcelUtil<UserResource> util = new ExcelUtil<UserResource>(UserResource.class);
        util.exportExcel(response, list, "用户订阅资源数据");
    }

    /**
     * 获取用户订阅资源详细信息
     */
    @PreAuthorize("@ss.hasPermi('system/audit:resource:list')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userResourceService.selectUserResourceById(id));
    }

    /**
     * 查询用户订购明细
     */
    @PreAuthorize("@ss.hasPermi('system/audit:resource:list')")
    @GetMapping(value = "/useritem")
    public AjaxResult getUserItem(Long id) {
        UserResourceItem req = new UserResourceItem();
        req.setUserResourceId(id);
        List<UserResourceItem> userResourceItems = userResourceItemService.selectUserResourceItemList(req);
        if (userResourceItems != null && userResourceItems.size() > 0) {
            return success(userResourceItems.get(0));
        }

        return success();
    }

    /**
     * 新增用户订阅资源
     */
    @PreAuthorize("@ss.hasPermi('system/audit:resource:add')")
    @Log(title = "用户订阅资源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserResource userResource)
    {
        return toAjax(userResourceService.insertUserResource(userResource));
    }

    /**
     * 修改用户订阅资源
     */
    @PreAuthorize("@ss.hasPermi('system/audit:resource:edit')")
    @Log(title = "用户订阅资源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserResource userResource)
    {
        return toAjax(userResourceService.updateUserResource(userResource));
    }

    /**
     * 删除用户订阅资源
     */
    @PreAuthorize("@ss.hasPermi('system/audit:resource:remove')")
    @Log(title = "用户订阅资源", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids)
    {
        return toAjax(userResourceService.deleteUserResourceById(ids));
    }

    /**
     * 审核
     */
    @PreAuthorize("@ss.hasPermi('system/audit:resource:audit')")
    @Log(title = "用户订阅资源", businessType = BusinessType.AUDIT)
    @PostMapping("/audit")
    public AjaxResult audit(@RequestBody ResourceAuditDto auditDto) {
        return userResourceService.audit(auditDto);
    }

    /**
     * 刷新资源redis缓存
     */
    @PreAuthorize("@ss.hasPermi('system/audit:resource:audit')")
    @PostMapping("/refesh")
    public AjaxResult refeshRedis() {
        userResourceService.writeToRedis();
        return success();
    }
}
