package com.tofly.feesapi.sfgl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.feesapi.common.Excel.ExcleFileUtil;
import com.tofly.feesapi.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
import com.tofly.feesapi.common.util.GuidGenerator;
import com.tofly.feesapi.sfgl.entity.YyQfmxDzmx;
import com.tofly.feesapi.sfgl.entity.dto.YyQfmxDzmxFilter;
import com.tofly.feesapi.sfgl.entity.vo.YyQfmxDzmxSelect;
import com.tofly.feesapi.sfgl.service.YyQfmxDzmxService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/yyqfmxdzmx")
@Api(tags="用户呆账明细接口")
public class YyQfmxDzmxController {

    @Autowired
    private YyQfmxDzmxService yyQfmxDzmxService;

    @GetMapping("/page" )
    @ApiOperation(value = "呆账分页查询")
    public ResultRespone<PageResult<YyQfmxDzmxSelect>> getYyQfmxDzmxPage(PageDto pageDto, @ModelAttribute YyQfmxDzmxFilter filter) {
      return ResultRespone.success(yyQfmxDzmxService.getYyQfmxDzmxPage(pageDto,filter));
    }

    @ApiOperationSupport(order = 5)
    @GetMapping("/export")
    @ApiOperation(value = "导出呆账明细", tags = {"报表管理"})
    public void export(HttpServletResponse response, @ModelAttribute YyQfmxDzmxFilter filter) throws IOException {
        List<YyQfmxDzmxSelect> list = yyQfmxDzmxService.getList(filter);
        if (!list.isEmpty()) {
            YyQfmxDzmxSelect yyQfmxDzmxSelect = new YyQfmxDzmxSelect();
            yyQfmxDzmxSelect.setYhbh("合计");
            yyQfmxDzmxSelect.setSl(list.stream().map(YyQfmxDzmxSelect::getSl).reduce(BigDecimal.ZERO, BigDecimal::add));
            yyQfmxDzmxSelect.setJe(list.stream().map(YyQfmxDzmxSelect::getJe).reduce(BigDecimal.ZERO, BigDecimal::add));
            list.add(yyQfmxDzmxSelect);
            String fileName = GuidGenerator.getGuid();
            ExcleFileUtil.exportExcel(response, list, YyQfmxDzmxSelect.class, String.format("%s.xlsx", fileName), filter.getExportFields());
        }
    }

    @GetMapping("/{id}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String")
    })
    public ResultRespone getById(@PathVariable("id" ) String id) {
      return ResultRespone.success(yyQfmxDzmxService.getById(id));
    }

    @ApiOperation(value = "新增用户呆账明细")
    @PostMapping
    public ResultRespone save(@RequestBody YyQfmxDzmx yyQfmxDzmx) {
      return ResultRespone.success(yyQfmxDzmxService.save(yyQfmxDzmx));
    }

    @ApiOperation(value = "修改用户呆账明细")
    @PutMapping
    public ResultRespone updateById(@RequestBody YyQfmxDzmx yyQfmxDzmx) {
      return ResultRespone.success(yyQfmxDzmxService.updateById(yyQfmxDzmx));
    }
}
