package com.ruoyi.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.system.domain.SysLinks;
import com.ruoyi.system.domain.query.SysLinksQuery;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import com.ruoyi.system.service.ISysLinksService;

/**
 * <pre>
 * - 链接前端API接口
 * </pre>
 *
 * @author J.L.Zhou<1 2 0 2 0 0 4 2> - <151 1104 7708>
 * @version 1.0
 * @since 2023-11-06 10:33
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/links")
@Slf4j
@SaIgnore
public class SysLinksController {

  private final ISysLinksService linksService;

  //	缓存名称格式说明:cacheNames#ttl#maxIdleTime#maxSize
//	@Cacheable(cacheNames = "cache:sys:links#60s#10m#20", key = "#id", condition = "#id != null")
  @SaCheckPermission("sys:links:query")
  @GetMapping("/{id}")
  public Object id(@NotNull(message = "链接编号不能为空") @PathVariable Long id) {
    return linksService.getById(id);
  }

  //	@SaCheckPermission("sys:links:page")
  @GetMapping("/page")
  public Object page(Page<SysLinks> page, SysLinks links, SysLinksQuery query) {
    if (log.isTraceEnabled()) {
      log.trace("链接分页条件\n\tpage:{}\n\tlinks:{}\n\tquery:{}", JsonUtils.toJsonString(page),
        JsonUtils.toJsonString(links), JsonUtils.toJsonString(query));
    }

    return linksService.page(page, query.where(linksService.where(links)));
  }

  /**
   * 获取移动端图库
   * @return
   */
  @GetMapping("/getAppImgs")
  @SaIgnore
  public Object getAppImgs() {
    return linksService.list(linksService.where().lambda().eq(SysLinks::getTypeId, 1).eq(SysLinks::getStatus, "0"));
  }

  @SaCheckPermission("sys:links:add")
  @Log(title = "链接", businessType = BusinessType.INSERT)
  @RepeatSubmit
  @PostMapping
//  @Lock4j//必要时,使用分布式锁
  public Boolean add(@Validated(AddGroup.class) @RequestBody SysLinks links) {
    if (log.isTraceEnabled()) {
      log.trace("链接添加参数:{}", JsonUtils.toJsonString(links));
    }
    return linksService.save(links);
  }

  @SaCheckPermission("sys:links:update")
  @Log(title = "链接", businessType = BusinessType.UPDATE)
  @RepeatSubmit
  @PutMapping
//  @Lock4j//必要时,使用分布式锁
//	@CacheEvict(cacheNames = "cache:sys:links",key = "#links.id")//修改时删除缓存中对应的项
  public Boolean update(@Validated(EditGroup.class) @RequestBody SysLinks links) {
    if (log.isTraceEnabled()) {
      log.trace("链接修改参数:{}", JsonUtils.toJsonString(links));
    }
    return linksService.updateById(links);
  }

  @SaCheckPermission("sys:links:delete")
  @Log(title = "链接", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
//	@CacheEvict(cacheNames = "cache:sys:links",allEntries = true)//删除时,清空所有缓存项
  public Boolean remove(@NotEmpty(message = "编号不能为空") @PathVariable Long[] ids) {
    return linksService.removeBatchByIds(Arrays.asList(ids));
  }
}
