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.Goods;
import com.qs.mp.admin.domain.GoodsCategory;
import com.qs.mp.admin.domain.GoodsTag;
import com.qs.mp.admin.domain.GoodsTagRel;
import com.qs.mp.admin.service.IGoodsCategoryService;
import com.qs.mp.admin.service.IGoodsService;
import com.qs.mp.admin.service.IGoodsTagRelService;
import com.qs.mp.admin.service.IGoodsTagService;
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.web.controller.common.BaseApiController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
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 商品标签管理前端控制器
 */
@Api("商品标签管理API")
@RestController
@RequestMapping("/api/v1/mp/admin/goods/tag/*")
@Component
public class GoodsTagMgrController extends BaseApiController {

  @Autowired
  private IGoodsTagService goodsTagService;

  @Autowired
  private IGoodsTagRelService goodsTagRelService;


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

  /**
   * 查询商品标签列表
   *
   * @return
   */
  @PostMapping("/items")
  public AjaxResult items(@RequestBody JSONObject param) {
    List<GoodsTag> tagList = goodsTagService.list(new LambdaQueryWrapper<GoodsTag>().orderByDesc(GoodsTag::getSort));
    return AjaxResult.success(tagList);
  }

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


  /**
   * 新增商品标签信息
   *
   * @param
   * @return
   */
  @Log(title = "新增商品标签", businessType = BusinessType.INSERT)
  @ApiOperation(value = "新增商品标签", notes = "后台商品管理新增标签")
  @PostMapping("/create")
  @PreAuthorize("@ss.hasPermi('business:tag:add')")
  public AjaxResult create(@Validated @RequestBody GoodsTag goodsTag) {
    // 1、校验名称是否重复
    int cnt = goodsTagService.count(new LambdaQueryWrapper<GoodsTag>()
        .eq(GoodsTag::getName, goodsTag.getName()));
    if (cnt > 0) {
      return AjaxResult.error("标签名称已存在！");
    }
    // 2.保存
    goodsTagService.save(goodsTag);
    return AjaxResult.success("保存成功");
  }

  /**
   * 编辑商品标签信息
   *
   * @param
   * @return
   */
  @Log(title = "修改商品标签", businessType = BusinessType.UPDATE)
  @ApiOperation(value = "编辑商品标签信息", notes = "后台商品管理修改商品标签信息")
  @PostMapping("/update")
  @PreAuthorize("@ss.hasPermi('business:tag:edit')")
  public AjaxResult update(@Validated @RequestBody GoodsTag goodsTag) {
    if (null == goodsTag || null == goodsTag.getTagId()
        || 0 == goodsTag.getTagId()) {
      return error(ErrorCodeEnum.ERROR_CODE_1001);
    }
    int cnt = goodsTagService.count(new LambdaQueryWrapper<GoodsTag>()
        .eq(GoodsTag::getName, goodsTag.getName()).notIn(GoodsTag::getTagId,
            Arrays.asList(goodsTag.getTagId())));
    if (cnt > 0) {
      return AjaxResult.error("标签名称已存在！");
    }
    // 保存数据
    boolean rst = goodsTagService.updateById(goodsTag);
    return rst ? AjaxResult.success("更新成功") : AjaxResult.error("更新失败");
  }


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

    if (null == goodsTag || null == goodsTag.getTagId()
        || 0 == goodsTag.getTagId()) {
      return error(ErrorCodeEnum.ERROR_CODE_1001);
    }
    goodsTagRelService.remove(new LambdaQueryWrapper<GoodsTagRel>()
        .eq(GoodsTagRel::getTagId, goodsTag.getTagId()));

    boolean res = goodsTagService.removeById(goodsTag.getTagId());
    return res ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
  }

}
