package com.qs.mp.web.controller.api.admin;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qs.mp.admin.domain.ExchangeBanner;
import com.qs.mp.admin.service.IExchangeBannerService;
import com.qs.mp.common.annotation.Log;
import com.qs.mp.common.core.domain.AjaxResult;
import com.qs.mp.common.core.page.TableDataInfo;
import com.qs.mp.common.enums.BusinessType;
import com.qs.mp.common.enums.ErrorCodeEnum;
import com.qs.mp.common.utils.StringUtils;
import com.qs.mp.web.controller.common.BaseApiController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @auther zhongcp
 * @create 2022-04-07 23:45:48
 * @describe 商品banner管理前端控制器
 */
@Api(tags = "商品banner管理API")
@RestController
@RequestMapping("/api/v1/mp/admin/goods/banner/*")
@Component
public class ExchangeBannerMgrController extends BaseApiController {

  @Autowired
  private IExchangeBannerService exchangeBannerService;


  /**
   * 查询商品banner列表, 支持翻页
   *
   * @return
   */
  @PostMapping("/list")
  @PreAuthorize("@ss.hasPermi('business:banner:list')")
  public TableDataInfo list(@RequestBody JSONObject param) {
    startPage();
    List<ExchangeBanner> bannerList = exchangeBannerService.list(
        new LambdaQueryWrapper<ExchangeBanner>().orderByDesc(ExchangeBanner::getSort));
    return getDataTable(bannerList);
  }

  /**
   * 获取商品banner详情信息
   *
   * @param
   * @return
   */
  @PostMapping(value = "/detail")
  @PreAuthorize("@ss.hasPermi('business:banner:query')")
  public AjaxResult info(@RequestBody JSONObject jsonObject) {
    Long bannerId = jsonObject.getLong("bannerId");
    if (null == bannerId || 0 == bannerId) {
      return error(ErrorCodeEnum.ERROR_CODE_1001);
    }
    ExchangeBanner banner = exchangeBannerService.getById(bannerId);
    return AjaxResult.success(banner);
  }


  /**
   * 新增商品banner信息
   *
   * @param
   * @return
   */
  @Log(title = "新增商品banner", businessType = BusinessType.INSERT)
  @ApiOperation(value = "新增商品banner", notes = "后台商品管理新增banner")
  @PostMapping("/create")
  @PreAuthorize("@ss.hasPermi('business:banner:add')")
  public AjaxResult create(@RequestBody ExchangeBanner exchangeBanner) {
    if (StringUtils.isBlank(exchangeBanner.getName()) || StringUtils.isBlank(
        exchangeBanner.getPicUrl())
        || StringUtils.isBlank(exchangeBanner.getType()) || null == exchangeBanner.getLocation()) {
      return error(ErrorCodeEnum.ERROR_CODE_1001);
    }
    // 2.保存
    exchangeBannerService.save(exchangeBanner);
    return AjaxResult.success("保存成功");
  }

  /**
   * 编辑商品banner信息
   *
   * @param
   * @return
   */
  @Log(title = "修改商品banner", businessType = BusinessType.UPDATE)
  @ApiOperation(value = "编辑商品banner信息", notes = "后台商品管理修改商品banner信息")
  @PostMapping("/update")
  @PreAuthorize("@ss.hasPermi('business:banner:edit')")
  public AjaxResult update(@Validated @RequestBody ExchangeBanner exchangeBanner) {
    if (null == exchangeBanner || null == exchangeBanner.getBannerId()
        || 0 == exchangeBanner.getBannerId()) {
      return error(ErrorCodeEnum.ERROR_CODE_1001);
    }
    // 保存数据
    boolean rst = exchangeBannerService.updateById(exchangeBanner);
    return rst ? AjaxResult.success("更新成功") : AjaxResult.error("更新失败");
  }


  /**
   * 删除商品banner
   *
   * @param
   * @return
   */
  @Log(title = "删除商品banner", businessType = BusinessType.DELETE)
  @PostMapping(value = "/remove")
  @PreAuthorize("@ss.hasPermi('business:banner:remove')")
  public AjaxResult remove(@RequestBody ExchangeBanner exchangeBanner) {

    if (null == exchangeBanner || null == exchangeBanner.getBannerId()
        || 0 == exchangeBanner.getBannerId()) {
      return error(ErrorCodeEnum.ERROR_CODE_1001);
    }

    boolean res = exchangeBannerService.removeById(exchangeBanner.getBannerId());
    return res ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
  }

}
