package com.yueqian.xingyun.settle.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.PageResultUtil;
import com.lframework.starter.mq.core.utils.ExportTaskUtil;
import com.yueqian.xingyun.settle.bo.item.in.GetSettleInItemBo;
import com.yueqian.xingyun.settle.bo.item.in.QuerySettleInItemBo;
import com.yueqian.xingyun.settle.entity.SettleInItem;
import com.yueqian.xingyun.settle.excel.item.in.SettleInItemExportTaskWorker;
import com.yueqian.xingyun.settle.service.SettleInItemService;
import com.yueqian.xingyun.settle.vo.item.in.CreateSettleInItemVo;
import com.yueqian.xingyun.settle.vo.item.in.QuerySettleInItemVo;
import com.yueqian.xingyun.settle.vo.item.in.UpdateSettleInItemVo;
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 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;

/**
 * 收入项目
 *
 * @author zmj
 */
@Tag(name = "收入项目")
@Validated
@RestController
@RequestMapping("/settle/item/in")
public class SettleInItemController extends DefaultBaseController {

  @Autowired
  private SettleInItemService settleInItemService;

  /**
   * 收入项目列表
   */
  @Operation(summary = "收入项目列表")
  @HasPermission({"settle:in-item:query", "settle:in-item:add", "settle:in-item:modify"})
  @GetMapping("/query")
  public InvokeResult<PageResult<QuerySettleInItemBo>> query(@Valid QuerySettleInItemVo vo) {

    PageResult<SettleInItem> pageResult = settleInItemService.query(getPageIndex(vo),
        getPageSize(vo), vo);

    List<SettleInItem> datas = pageResult.getDatas();
    List<QuerySettleInItemBo> results = null;

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

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

  /**
   * 查询收入项目
   */
  @Operation(summary = "查询收入项目")
  @Parameter(name = "id", description = "ID", in = ParameterIn.QUERY, required = true)
  @HasPermission({"settle:in-item:query", "settle:in-item:add", "settle:in-item:modify"})
  @GetMapping
  public InvokeResult<GetSettleInItemBo> get(@NotBlank(message = "ID不能为空！") String id) {

    SettleInItem data = settleInItemService.findById(id);
    if (data == null) {
      throw new DefaultClientException("收入项目不存在！");
    }

    GetSettleInItemBo result = new GetSettleInItemBo(data);

    return InvokeResultBuilder.success(result);
  }

  /**
   * 导出收入项目
   */
  @Operation(summary = "导出收入项目")
  @HasPermission({"settle:in-item:export"})
  @PostMapping("/export")
  public InvokeResult<Void> export(@Valid QuerySettleInItemVo vo) {

    ExportTaskUtil.exportTask("收入项目信息", SettleInItemExportTaskWorker.class, vo);

    return InvokeResultBuilder.success();
  }

  /**
   * 根据ID删除
   */
  @Operation(summary = "根据ID删除")
  @HasPermission({"settle:in-item:delete"})
  @DeleteMapping
  public InvokeResult<Void> deleteById(
      @Parameter(description = "ID", required = true) @NotEmpty(message = "收入项目ID不能为空！") String id) {

    settleInItemService.deleteById(id);

    settleInItemService.cleanCacheByKey(id);

    return InvokeResultBuilder.success();
  }

  /**
   * 新增收入项目
   */
  @Operation(summary = "新增收入项目")
  @HasPermission({"settle:in-item:add"})
  @PostMapping
  public InvokeResult<Void> create(@Valid CreateSettleInItemVo vo) {

    settleInItemService.create(vo);

    return InvokeResultBuilder.success();
  }

  /**
   * 修改收入项目
   */
  @Operation(summary = "修改收入项目")
  @HasPermission({"settle:in-item:modify"})
  @PutMapping
  public InvokeResult<Void> update(@Valid UpdateSettleInItemVo vo) {

    settleInItemService.update(vo);

    settleInItemService.cleanCacheByKey(vo.getId());

    return InvokeResultBuilder.success();
  }
}
