package com.yueqian.xingyun.settle.controller;

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.PageResultUtil;
import com.lframework.starter.mq.core.utils.ExportTaskUtil;
import com.yueqian.xingyun.settle.bo.sheet.GetSettleSheetBo;
import com.yueqian.xingyun.settle.bo.sheet.QuerySettleSheetBo;
import com.yueqian.xingyun.settle.bo.sheet.SettleBizItemBo;
import com.yueqian.xingyun.settle.dto.sheet.SettleBizItemDto;
import com.yueqian.xingyun.settle.dto.sheet.SettleSheetFullDto;
import com.yueqian.xingyun.settle.entity.SettleSheet;
import com.yueqian.xingyun.settle.excel.sheet.SettleSheetExportTaskWorker;
import com.yueqian.xingyun.settle.service.SettleSheetService;
import com.yueqian.xingyun.settle.vo.sheet.ApprovePassSettleSheetVo;
import com.yueqian.xingyun.settle.vo.sheet.ApproveRefuseSettleSheetVo;
import com.yueqian.xingyun.settle.vo.sheet.CreateSettleSheetVo;
import com.yueqian.xingyun.settle.vo.sheet.QuerySettleSheetVo;
import com.yueqian.xingyun.settle.vo.sheet.QueryUnSettleBizItemVo;
import com.yueqian.xingyun.settle.vo.sheet.UpdateSettleSheetVo;
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 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.PatchMapping;
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;

/**
 * 供应商结算单
 *
 * @author zmj
 */
@Tag(name = "供应商结算单")
@Validated
@RestController
@RequestMapping("/settle/sheet")
public class SettleSheetController extends DefaultBaseController {

  @Autowired
  private SettleSheetService settleSheetService;

  /**
   * 供应商结算单列表
   */
  @Operation(summary = "供应商结算单列表")
  @HasPermission({"settle:sheet:query"})
  @GetMapping("/query")
  public InvokeResult<PageResult<QuerySettleSheetBo>> query(@Valid QuerySettleSheetVo vo) {

    PageResult<SettleSheet> pageResult = settleSheetService.query(getPageIndex(vo), getPageSize(vo),
        vo);

    List<SettleSheet> datas = pageResult.getDatas();
    List<QuerySettleSheetBo> results = null;

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

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

  /**
   * 导出
   */
  @Operation(summary = "导出")
  @HasPermission({"settle:sheet:export"})
  @PostMapping("/export")
  public InvokeResult<Void> export(@Valid QuerySettleSheetVo vo) {

    ExportTaskUtil.exportTask("供应商结算单信息", SettleSheetExportTaskWorker.class, vo);

    return InvokeResultBuilder.success();
  }

  /**
   * 根据ID查询
   */
  @Operation(summary = "根据ID查询")
  @Parameter(name = "id", description = "ID", in = ParameterIn.QUERY, required = true)
  @HasPermission({"settle:sheet:query"})
  @GetMapping
  public InvokeResult<GetSettleSheetBo> findById(
      @NotBlank(message = "供应商结算单ID不能为空！") String id) {

    SettleSheetFullDto data = settleSheetService.getDetail(id);

    GetSettleSheetBo result = new GetSettleSheetBo(data);

    return InvokeResultBuilder.success(result);
  }

  /**
   * 创建供应商结算单
   */
  @Operation(summary = "创建供应商结算单")
  @HasPermission({"settle:sheet:add"})
  @PostMapping
  public InvokeResult<String> create(@RequestBody @Valid CreateSettleSheetVo vo) {

    vo.validate();

    String id = settleSheetService.create(vo);

    return InvokeResultBuilder.success(id);
  }

  /**
   * 修改供应商结算单
   */
  @Operation(summary = "修改供应商结算单")
  @HasPermission({"settle:sheet:modify"})
  @PutMapping
  public InvokeResult<Void> update(@RequestBody @Valid UpdateSettleSheetVo vo) {

    vo.validate();

    settleSheetService.update(vo);

    return InvokeResultBuilder.success();
  }

  /**
   * 审核通过供应商结算单
   */
  @Operation(summary = "审核通过供应商结算单")
  @HasPermission({"settle:sheet:approve"})
  @PatchMapping("/approve/pass")
  public InvokeResult<Void> approvePass(@RequestBody @Valid ApprovePassSettleSheetVo vo) {

    settleSheetService.approvePass(vo);

    return InvokeResultBuilder.success();
  }

  /**
   * 直接审核通过供应商结算单
   */
  @Operation(summary = "直接审核通过供应商结算单")
  @HasPermission({"settle:sheet:approve"})
  @PostMapping("/approve/pass/direct")
  public InvokeResult<Void> directApprovePass(@RequestBody @Valid CreateSettleSheetVo vo) {

    vo.validate();

    settleSheetService.directApprovePass(vo);

    return InvokeResultBuilder.success();
  }

  /**
   * 审核拒绝供应商结算单
   */
  @Operation(summary = "审核拒绝供应商结算单")
  @HasPermission({"settle:sheet:approve"})
  @PatchMapping("/approve/refuse")
  public InvokeResult<Void> approveRefuse(@RequestBody @Valid ApproveRefuseSettleSheetVo vo) {

    settleSheetService.approveRefuse(vo);

    return InvokeResultBuilder.success();
  }

  /**
   * 删除供应商结算单
   */
  @Operation(summary = "删除供应商结算单")
  @Parameter(name = "id", description = "ID", in = ParameterIn.QUERY, required = true)
  @HasPermission({"settle:sheet:delete"})
  @DeleteMapping
  public InvokeResult<Void> deleteById(@NotBlank(message = "供应商结算单ID不能为空！") String id) {

    settleSheetService.deleteById(id);

    return InvokeResultBuilder.success();
  }

  /**
   * 查询未结算的业务单据
   */
  @Operation(summary = "查询未结算的业务单据")
  @HasPermission({"settle:sheet:add", "settle:sheet:modify"})
  @GetMapping("/unsettle-items")
  public InvokeResult<List<SettleBizItemBo>> getUnCheckItems(@Valid QueryUnSettleBizItemVo vo) {

    List<SettleBizItemDto> results = settleSheetService.getUnSettleBizItems(vo);
    List<SettleBizItemBo> datas = CollectionUtil.emptyList();
    if (!CollectionUtil.isEmpty(results)) {
      datas = results.stream().map(SettleBizItemBo::new).collect(Collectors.toList());
    }

    return InvokeResultBuilder.success(datas);
  }
}
