package org.jeecg.modules.okki.externallink.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.okki.externallink.service.IOkkiExternalLinkService;
import org.jeecg.modules.okki.externallink.entity.OkkiExternalLink;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

 /**
 * @Description: SEO数据外链
 * @Author: jeecg-boot
 * @Date:   2024-05-09
 * @Version: V1.0
 */
@Api(tags="SEO数据外链")
@RestController
@RequestMapping("/externallink/okkiExternalLink")
@Slf4j
public class OkkiExternalLinkController extends JeecgController<OkkiExternalLink, IOkkiExternalLinkService> {
	@Autowired
	private IOkkiExternalLinkService okkiExternalLinkService;
	
	/**
	 * 分页列表查询
	 *
	 * @param okkiExternalLink
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "SEO数据外链-分页列表查询")
	@ApiOperation(value="SEO数据外链-分页列表查询", notes="SEO数据外链-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<OkkiExternalLink>> queryPageList(OkkiExternalLink okkiExternalLink,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<OkkiExternalLink> queryWrapper = QueryGenerator.initQueryWrapper(okkiExternalLink, req.getParameterMap());
		Page<OkkiExternalLink> page = new Page<OkkiExternalLink>(pageNo, pageSize);
		IPage<OkkiExternalLink> pageList = okkiExternalLinkService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param okkiExternalLink
	 * @return
	 */
	@AutoLog(value = "SEO数据外链-添加")
	@ApiOperation(value="SEO数据外链-添加", notes="SEO数据外链-添加")
	@RequiresPermissions("externallink:okki_external_link:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody OkkiExternalLink okkiExternalLink) {
		okkiExternalLinkService.save(okkiExternalLink);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param okkiExternalLink
	 * @return
	 */
	@AutoLog(value = "SEO数据外链-编辑")
	@ApiOperation(value="SEO数据外链-编辑", notes="SEO数据外链-编辑")
	@RequiresPermissions("externallink:okki_external_link:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody OkkiExternalLink okkiExternalLink) {
		okkiExternalLink.setStatus(0);
		okkiExternalLinkService.updateById(okkiExternalLink);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 *
	 *
	 *
	 */
	@AutoLog(value = "SEO数据外链-通过id删除")
	@ApiOperation(value="SEO数据外链-通过id删除", notes="SEO数据外链-通过id删除")
	@RequiresPermissions("externallink:okki_external_link:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		okkiExternalLinkService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "SEO数据外链-批量删除")
	@ApiOperation(value="SEO数据外链-批量删除", notes="SEO数据外链-批量删除")
	@RequiresPermissions("externallink:okki_external_link:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.okkiExternalLinkService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "SEO数据外链-通过id查询")
	@ApiOperation(value="SEO数据外链-通过id查询", notes="SEO数据外链-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<OkkiExternalLink> queryById(@RequestParam(name="id",required=true) String id) {
		OkkiExternalLink okkiExternalLink = okkiExternalLinkService.getById(id);
		if(okkiExternalLink==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(okkiExternalLink);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param okkiExternalLink
    */
    @RequiresPermissions("externallink:okki_external_link:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OkkiExternalLink okkiExternalLink) {
        return super.exportXls(request, okkiExternalLink, OkkiExternalLink.class, "SEO数据外链");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("externallink:okki_external_link:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OkkiExternalLink.class);
    }

	 /**
	  * 变更状态
	  * @param okkiExternalLink
	  * @return
	  */
	 @PostMapping(value = "/status")
	 public Result<String> changeStatus(@RequestBody OkkiExternalLink okkiExternalLink) throws Exception {
		 boolean result = okkiExternalLinkService.changeStatus(okkiExternalLink);
		 if (result) {
			 return Result.OK("操作成功");
		 }else {
			 return Result.error("操作失败");
		 }
	 }
}
