package com.yueqian.xingyun.basedata.controller;

import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import com.lframework.starter.common.utils.CollectionUtil;
import com.lframework.starter.web.core.annotations.security.HasPermission;
import com.lframework.starter.web.core.controller.DefaultBaseController;
import com.lframework.starter.web.core.components.resp.InvokeResult;
import com.lframework.starter.web.core.components.resp.InvokeResultBuilder;
import com.lframework.starter.web.core.components.resp.PageResult;
import com.lframework.starter.web.core.utils.ExcelUtil;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.yueqian.xingyun.basedata.bo.product.brand.GetProductBrandBo;
import com.yueqian.xingyun.basedata.bo.product.brand.QueryProductBrandBo;
import com.yueqian.xingyun.basedata.entity.ProductBrand;
import com.yueqian.xingyun.basedata.excel.product.brand.ProductBrandImportListener;
import com.yueqian.xingyun.basedata.excel.product.brand.ProductBrandImportModel;
import com.yueqian.xingyun.basedata.service.product.ProductBrandService;
import com.yueqian.xingyun.basedata.vo.product.brand.CreateProductBrandVo;
import com.yueqian.xingyun.basedata.vo.product.brand.QueryProductBrandVo;
import com.yueqian.xingyun.basedata.vo.product.brand.UpdateProductBrandVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.Operation;
import java.util.List;
import java.util.stream.Collectors;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
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.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 品牌管理
 *
 * @author zmj
 */
@Tag(name = "品牌管理")
@Validated
@RestController
@RequestMapping("/basedata/product/brand")
public class ProductBrandController extends DefaultBaseController {

  @Autowired
  private ProductBrandService productBrandService;

  /**
   * 品牌列表
   */
  @Operation(summary = "品牌列表")
  @HasPermission({"base-data:product:brand:query", "base-data:product:brand:add",
      "base-data:product:brand:modify"})
  @GetMapping("/query")
  public InvokeResult<PageResult<QueryProductBrandBo>> query(@Valid QueryProductBrandVo vo) {

    PageResult<ProductBrand> pageResult = productBrandService.query(getPageIndex(vo),
        getPageSize(vo), vo);

    List<ProductBrand> datas = pageResult.getDatas();
    List<QueryProductBrandBo> results = null;

    if (!CollectionUtil.isEmpty(datas)) {

      results = datas.stream().map(QueryProductBrandBo::new).collect(Collectors.toList());
    }

    return InvokeResultBuilder.success(PageResultUtil.rebuild(pageResult, results));
  }


  /**
   * 查询品牌
   */
  @Operation(summary = "查询品牌")
  @Parameter(name = "id", description = "ID", in = ParameterIn.QUERY, required = true)
  @HasPermission({"base-data:product:brand:query", "base-data:product:brand:add",
      "base-data:product:brand:modify"})
  @GetMapping
  public InvokeResult<GetProductBrandBo> get(@NotBlank(message = "ID不能为空！") String id) {

    ProductBrand data = productBrandService.findById(id);
    if (data == null) {
      throw new DefaultClientException("品牌不存在！");
    }

    GetProductBrandBo result = new GetProductBrandBo(data);

    return InvokeResultBuilder.success(result);
  }

  /**
   * 根据ID删除
   */
  @Operation(summary = "根据ID删除")
  @HasPermission({"base-data:product:brand:delete"})
  @DeleteMapping
  public InvokeResult<Void> deleteById(
      @Parameter(description = "ID", required = true) @NotEmpty(message = "品牌ID不能为空！") String id) {

    productBrandService.deleteById(id);

    productBrandService.cleanCacheByKey(id);

    return InvokeResultBuilder.success();
  }

  /**
   * 新增品牌
   */
  @Operation(summary = "新增品牌")
  @HasPermission({"base-data:product:brand:add"})
  @PostMapping
  public InvokeResult<Void> create(@Valid CreateProductBrandVo vo) {

    productBrandService.create(vo);

    return InvokeResultBuilder.success();
  }

  /**
   * 修改品牌
   */
  @Operation(summary = "修改品牌")
  @HasPermission({"base-data:product:brand:modify"})
  @PutMapping
  public InvokeResult<Void> update(@Valid UpdateProductBrandVo vo) {

    productBrandService.update(vo);

    productBrandService.cleanCacheByKey(vo.getId());

    return InvokeResultBuilder.success();
  }

  @Operation(summary = "下载导入模板")
  @HasPermission({"base-data:product:brand:import"})
  @GetMapping("/import/template")
  public void downloadImportTemplate() {
    ExcelUtil.exportXls("品牌导入模板", ProductBrandImportModel.class);
  }

  @Operation(summary = "导入")
  @HasPermission({"base-data:product:brand:import"})
  @PostMapping("/import")
  public InvokeResult<Void> importExcel(@NotBlank(message = "ID不能为空") String id,
      @NotNull(message = "请上传文件") MultipartFile file) {

    ProductBrandImportListener listener = new ProductBrandImportListener();
    listener.setTaskId(id);
    ExcelUtil.read(file, ProductBrandImportModel.class, listener).sheet().doRead();

    return InvokeResultBuilder.success();
  }
}
