package com.yueqian.xingyun.sc.controller.stock.take;

import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import com.lframework.starter.common.utils.CollectionUtil;
import com.lframework.starter.common.utils.StringUtil;
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.PageResultUtil;
import com.lframework.starter.mq.core.utils.ExportTaskUtil;
import com.yueqian.xingyun.sc.bo.stock.take.pre.GetPreTakeStockSheetBo;
import com.yueqian.xingyun.sc.bo.stock.take.pre.PreTakeStockProductBo;
import com.yueqian.xingyun.sc.bo.stock.take.pre.QueryPreTakeStockSheetBo;
import com.yueqian.xingyun.sc.bo.stock.take.pre.QueryPreTakeStockSheetProductBo;
import com.yueqian.xingyun.sc.dto.stock.take.pre.PreTakeStockProductDto;
import com.yueqian.xingyun.sc.dto.stock.take.pre.PreTakeStockSheetFullDto;
import com.yueqian.xingyun.sc.dto.stock.take.pre.QueryPreTakeStockSheetProductDto;
import com.yueqian.xingyun.sc.entity.PreTakeStockSheet;
import com.yueqian.xingyun.sc.entity.TakeStockPlan;
import com.yueqian.xingyun.sc.enums.TakeStockPlanType;
import com.yueqian.xingyun.sc.excel.stock.take.pre.PreTakeStockSheetExportTaskWorker;
import com.yueqian.xingyun.sc.service.stock.take.PreTakeStockSheetService;
import com.yueqian.xingyun.sc.service.stock.take.TakeStockPlanService;
import com.yueqian.xingyun.sc.vo.stock.take.pre.CreatePreTakeStockSheetVo;
import com.yueqian.xingyun.sc.vo.stock.take.pre.QueryPreTakeStockProductVo;
import com.yueqian.xingyun.sc.vo.stock.take.pre.QueryPreTakeStockSheetVo;
import com.yueqian.xingyun.sc.vo.stock.take.pre.UpdatePreTakeStockSheetVo;
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.Parameters;
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 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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 预先盘点单 Controller
 *
 * @author zmj
 */
@Tag(name = "预先盘点单")
@Validated
@RestController
@RequestMapping("/stock/take/pre")
public class PreTakeStockSheetController extends DefaultBaseController {

  @Autowired
  private PreTakeStockSheetService preTakeStockSheetService;

  @Autowired
  private TakeStockPlanService takeStockPlanService;

  /**
   * 查询列表
   */
  @Operation(summary = "查询列表")
  @HasPermission({"stock:take:pre:query"})
  @GetMapping("/query")
  public InvokeResult<PageResult<QueryPreTakeStockSheetBo>> query(
      @Valid QueryPreTakeStockSheetVo vo) {

    PageResult<PreTakeStockSheet> pageResult = preTakeStockSheetService.query(getPageIndex(vo),
        getPageSize(vo),
        vo);

    List<PreTakeStockSheet> datas = pageResult.getDatas();
    List<QueryPreTakeStockSheetBo> results = null;

    if (!CollectionUtil.isEmpty(datas)) {
      results = datas.stream().map(QueryPreTakeStockSheetBo::new).collect(Collectors.toList());
    }

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

  /**
   * 导出列表
   */
  @Operation(summary = "导出列表")
  @HasPermission({"stock:take:pre:export"})
  @PostMapping("/export")
  public InvokeResult<Void> export(@Valid QueryPreTakeStockSheetVo vo) {

    ExportTaskUtil.exportTask("预先盘点单信息", PreTakeStockSheetExportTaskWorker.class, vo);

    return InvokeResultBuilder.success();
  }

  /**
   * 根据ID查询
   */
  @Operation(summary = "根据ID查询")
  @Parameter(name = "id", description = "ID", in = ParameterIn.QUERY, required = true)
  @HasPermission({"stock:take:pre:query"})
  @GetMapping
  public InvokeResult<GetPreTakeStockSheetBo> getDetail(
      @NotBlank(message = "id不能为空！") String id) {

    PreTakeStockSheetFullDto data = preTakeStockSheetService.getDetail(id);
    if (data == null) {
      throw new DefaultClientException("预先盘点单不存在！");
    }

    GetPreTakeStockSheetBo result = new GetPreTakeStockSheetBo(data);

    return InvokeResultBuilder.success(result);
  }

  /**
   * 根据预先盘点单、盘点任务查询商品信息
   */
  @Operation(summary = "根据预先盘点单、盘点任务查询商品信息")
  @Parameters({
      @Parameter(name = "id", description = "ID", in = ParameterIn.QUERY, required = true),
      @Parameter(name = "planId", description = "盘点任务ID", in = ParameterIn.QUERY, required = true)})
  @HasPermission({"stock:take:sheet:add", "stock:take:sheet:modify"})
  @GetMapping("/products")
  public InvokeResult<List<QueryPreTakeStockSheetProductBo>> getProducts(
      @NotBlank(message = "ID不能为空！") String id,
      @NotBlank(message = "盘点任务ID不能为空！") String planId) {

    TakeStockPlan takeStockPlan = takeStockPlanService.getById(planId);
    if (takeStockPlan.getTakeType() == TakeStockPlanType.SIMPLE) {
      planId = null;
    }

    List<QueryPreTakeStockSheetProductDto> datas = preTakeStockSheetService.getProducts(id, planId);
    List<QueryPreTakeStockSheetProductBo> results = CollectionUtil.emptyList();
    if (!CollectionUtil.isEmpty(datas)) {
      results = datas.stream()
          .map(t -> new QueryPreTakeStockSheetProductBo(t, takeStockPlan.getScId()))
          .collect(Collectors.toList());
    }

    return InvokeResultBuilder.success(results);
  }

  /**
   * 新增
   */
  @Operation(summary = "新增")
  @HasPermission({"stock:take:pre:add"})
  @PostMapping
  public InvokeResult<Void> create(@Valid @RequestBody CreatePreTakeStockSheetVo vo) {

    vo.validate();

    preTakeStockSheetService.create(vo);

    return InvokeResultBuilder.success();
  }

  /**
   * 修改
   */
  @Operation(summary = "修改")
  @HasPermission({"stock:take:pre:modify"})
  @PutMapping
  public InvokeResult<Void> update(@Valid @RequestBody UpdatePreTakeStockSheetVo vo) {

    vo.validate();

    preTakeStockSheetService.update(vo);

    return InvokeResultBuilder.success();
  }

  /**
   * 根据ID删除
   */
  @Operation(summary = "根据ID删除")
  @Parameter(name = "id", description = "ID", in = ParameterIn.QUERY, required = true)
  @HasPermission({"stock:take:pre:delete"})
  @DeleteMapping
  public InvokeResult<Void> deleteById(@NotBlank(message = "ID不能为空！") String id) {

    preTakeStockSheetService.deleteById(id);

    return InvokeResultBuilder.success();
  }

  /**
   * 根据关键字查询商品列表
   */
  @Operation(summary = "根据关键字查询预先盘点单商品列表")
  @Parameter(name = "condition", description = "关键字", in = ParameterIn.QUERY, required = true)
  @HasPermission({"stock:take:pre:add", "stock:take:pre:modify"})
  @GetMapping("/product/search")
  public InvokeResult<List<PreTakeStockProductBo>> searchProducts(String condition) {

    if (StringUtil.isBlank(condition)) {
      return InvokeResultBuilder.success(CollectionUtil.emptyList());
    }
    PageResult<PreTakeStockProductDto> pageResult = preTakeStockSheetService.queryPreTakeStockByCondition(
        getPageIndex(), getPageSize(), condition);
    List<PreTakeStockProductBo> results = CollectionUtil.emptyList();
    List<PreTakeStockProductDto> datas = pageResult.getDatas();
    if (!CollectionUtil.isEmpty(datas)) {
      results = datas.stream().map(PreTakeStockProductBo::new).collect(Collectors.toList());
    }

    return InvokeResultBuilder.success(results);
  }

  /**
   * 查询商品列表
   */
  @Operation(summary = "查询预先盘点单商品列表")
  @HasPermission({"stock:take:pre:add", "stock:take:pre:modify"})
  @GetMapping("/product/list")
  public InvokeResult<PageResult<PreTakeStockProductBo>> queryProductList(
      @Valid QueryPreTakeStockProductVo vo) {

    PageResult<PreTakeStockProductDto> pageResult = preTakeStockSheetService.queryPreTakeStockList(
        getPageIndex(vo), getPageSize(vo), vo);
    List<PreTakeStockProductBo> results = null;
    List<PreTakeStockProductDto> datas = pageResult.getDatas();

    if (!CollectionUtil.isEmpty(datas)) {
      results = datas.stream().map(PreTakeStockProductBo::new).collect(Collectors.toList());
    }

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