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.fee.GetSettleFeeSheetBo;
import com.yueqian.xingyun.settle.bo.fee.QuerySettleFeeSheetBo;
import com.yueqian.xingyun.settle.dto.fee.SettleFeeSheetFullDto;
import com.yueqian.xingyun.settle.entity.SettleFeeSheet;
import com.yueqian.xingyun.settle.excel.fee.SettleFeeSheetExportTaskWorker;
import com.yueqian.xingyun.settle.service.SettleFeeSheetService;
import com.yueqian.xingyun.settle.vo.fee.ApprovePassSettleFeeSheetVo;
import com.yueqian.xingyun.settle.vo.fee.ApproveRefuseSettleFeeSheetVo;
import com.yueqian.xingyun.settle.vo.fee.CreateSettleFeeSheetVo;
import com.yueqian.xingyun.settle.vo.fee.QuerySettleFeeSheetVo;
import com.yueqian.xingyun.settle.vo.fee.UpdateSettleFeeSheetVo;
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/feesheet")
public class SettleFeeSheetController extends DefaultBaseController {

  @Autowired
  private SettleFeeSheetService settleFeeSheetService;

  /**
   * 供应商费用单列表
   */
  @Operation(summary = "供应商费用单列表")
  @HasPermission({"settle:fee-sheet:query"})
  @GetMapping("/query")
  public InvokeResult<PageResult<QuerySettleFeeSheetBo>> query(@Valid QuerySettleFeeSheetVo vo) {

    PageResult<SettleFeeSheet> pageResult = settleFeeSheetService.query(getPageIndex(vo),
        getPageSize(vo), vo);

    List<SettleFeeSheet> datas = pageResult.getDatas();
    List<QuerySettleFeeSheetBo> results = null;

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

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

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

    ExportTaskUtil.exportTask("供应商费用单信息", SettleFeeSheetExportTaskWorker.class, vo);

    return InvokeResultBuilder.success();
  }

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

    SettleFeeSheetFullDto data = settleFeeSheetService.getDetail(id);

    GetSettleFeeSheetBo result = new GetSettleFeeSheetBo(data);

    return InvokeResultBuilder.success(result);
  }

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

    vo.validate();

    String id = settleFeeSheetService.create(vo);

    return InvokeResultBuilder.success(id);
  }

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

    vo.validate();

    settleFeeSheetService.update(vo);

    return InvokeResultBuilder.success();
  }

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

    settleFeeSheetService.approvePass(vo);

    return InvokeResultBuilder.success();
  }

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

    vo.validate();

    settleFeeSheetService.directApprovePass(vo);

    return InvokeResultBuilder.success();
  }

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

    settleFeeSheetService.approveRefuse(vo);

    return InvokeResultBuilder.success();
  }

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

    settleFeeSheetService.deleteById(id);

    return InvokeResultBuilder.success();
  }
}
