package org.jeecg.modules.adweb.product.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.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
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.adweb.product.entity.AdwebProduct;
import org.jeecg.modules.adweb.product.service.IAdwebProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;

/**
 * @Description: wp站点产品
 * @Author: jeecg-boot
 * @Date: 2024-12-06
 * @Version: V1.0
 */
@Tag(name = "wp站点产品")
@RestController
@RequestMapping("/adweb/adwebProduct")
@Slf4j
public class AdwebProductController extends JeecgController<AdwebProduct, IAdwebProductService> {
    @Autowired private IAdwebProductService adwebProductService;

    /**
     * 分页列表查询
     *
     * @param adwebProduct
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    // @AutoLog(value = "wp站点产品-分页列表查询")
    @Operation(summary = "wp站点产品-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<AdwebProduct>> queryPageList(
            AdwebProduct adwebProduct,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        QueryWrapper<AdwebProduct> queryWrapper =
                QueryGenerator.initQueryWrapper(adwebProduct, req.getParameterMap());
        Page<AdwebProduct> page = new Page<AdwebProduct>(pageNo, pageSize);
        IPage<AdwebProduct> pageList = adwebProductService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param adwebProduct
     * @return
     */
    @AutoLog(value = "wp站点产品-添加")
    @Operation(summary = "wp站点产品-添加")
    @PreAuthorize("@jps.requiresPermissions('adweb:adweb_product:add')")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody AdwebProduct adwebProduct) {
        adwebProductService.save(adwebProduct);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param adwebProduct
     * @return
     */
    @AutoLog(value = "wp站点产品-编辑")
    @Operation(summary = "wp站点产品-编辑")
    @PreAuthorize("@jps.requiresPermissions('adweb:adweb_product:edit')")
    @RequestMapping(
            value = "/edit",
            method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody AdwebProduct adwebProduct) {
        adwebProductService.updateById(adwebProduct);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "wp站点产品-通过id删除")
    @Operation(summary = "wp站点产品-通过id删除")
    @PreAuthorize("@jps.requiresPermissions('adweb:adweb_product:delete')")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        adwebProductService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "wp站点产品-批量删除")
    @Operation(summary = "wp站点产品-批量删除")
    @PreAuthorize("@jps.requiresPermissions('adweb:adweb_product:deleteBatch')")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.adwebProductService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    // @AutoLog(value = "wp站点产品-通过id查询")
    @Operation(summary = "wp站点产品-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<AdwebProduct> queryById(@RequestParam(name = "id", required = true) String id) {
        AdwebProduct adwebProduct = adwebProductService.getById(id);
        if (adwebProduct == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(adwebProduct);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param adwebProduct
     */
    @PreAuthorize("@jps.requiresPermissions('adweb:adweb_product:exportXls')")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AdwebProduct adwebProduct) {
        return super.exportXls(request, adwebProduct, AdwebProduct.class, "wp站点产品");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @PreAuthorize("@jps.requiresPermissions('adweb:adweb_product:importExcel')")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AdwebProduct.class);
    }
}
