package com.tofly.feesapi.sfgl.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.util.StringUtil;
import com.tofly.feesapi.common.Excel.ExcleFileUtil;
import com.tofly.feesapi.common.core.ResultRespone;

import com.tofly.feesapi.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
import com.tofly.feesapi.config.FilePathBean;
import com.tofly.feesapi.file.service.IFileInfoService;
import com.tofly.feesapi.sfgl.entity.YyWyjtz;
import com.tofly.feesapi.sfgl.entity.dto.YyWyjtzDto;
import com.tofly.feesapi.sfgl.entity.dto.YyWyjtzFilter;
import com.tofly.feesapi.sfgl.entity.dto.YyWyjtzGdDto;
import com.tofly.feesapi.sfgl.entity.dto.YyWyjtzUpdate;
import com.tofly.feesapi.sfgl.entity.vo.YyQfmxWyjSelect;
import com.tofly.feesapi.sfgl.entity.vo.YyWyjtzVo;
import com.tofly.feesapi.sfgl.service.YyWyjtzService;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.multipart.MultipartFile;

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

@RestController
@RequestMapping("/yywyjtz")
@Api(tags = "违约金调整记录表接口")
public class YyWyjtzController {

    @Autowired
    private YyWyjtzService yyWyjtzService;
    @Autowired
    private IFileInfoService iFileInfoService;
    @Autowired
    private FilePathBean filePathBean;


    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone getYyWyjtzPage(Page page, @ModelAttribute YyWyjtz yyWyjtz) {
        return ResultRespone.success(yyWyjtzService.page(page, Wrappers.query(yyWyjtz)));
    }

    @GetMapping("/getPage")
    @ApiOperation(value = "自定义分页查询")
    public ResultRespone<YyQfmxWyjSelect> getYyWyjtzPage(PageDto page, @ModelAttribute YyWyjtzFilter dto) {
        return ResultRespone.success(yyWyjtzService.getPage(page, dto));
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出违约金减免记录")
    public void export(HttpServletResponse response, @ModelAttribute YyWyjtzFilter filter) throws IOException {
        // 遍历查询结果进行赋值
        List<YyWyjtzVo> list = yyWyjtzService.getList(filter);
        if (!list.isEmpty()) {
            YyWyjtzVo yyWyjtzVo = new YyWyjtzVo();
            yyWyjtzVo.setYhbh("合计");
            yyWyjtzVo.setTzje(list.stream().map(YyWyjtzVo::getTzje).reduce(BigDecimal.ZERO, BigDecimal::add));
            list.add(yyWyjtzVo);
            ExcleFileUtil.exportExcel(response, list, YyWyjtzVo.class, String.format("%s.xlsx", "违约金减免记录"), filter.getExportFields());
        }
    }

    @ApiOperation(value = "违约金减免")
    @PostMapping("/wyjjm")
    public ResultRespone wyjtzSave(@RequestBody YyWyjtzUpdate dto) {
        return ResultRespone.success(yyWyjtzService.wyjtzSave(dto));
    }

    @ApiOperation(value = "违约金减免全部")
    @PostMapping("/wyjjmall")
    public ResultRespone wyjtzSaveAll(@RequestBody YyWyjtzDto dto) {
        return ResultRespone.success(yyWyjtzService.wyjtzSaveAll(dto));
    }


    @ApiOperation(value = "违约金减免一半")
    @PostMapping("/wyjjmhalf")
    public ResultRespone wyjtzSaveHalf(@RequestBody YyWyjtzDto dto) {
        return ResultRespone.success(yyWyjtzService.wyjtzSaveHalf(dto));
    }


    @ApiOperation(value = "违约金减免全部单条")
    @PostMapping("/wyjjmallone")
    public ResultRespone wyjtzSaveAllOne(@RequestBody YyWyjtzDto dto) {
        return ResultRespone.success(yyWyjtzService.wyjtzSaveAllOne(dto));
    }


    @ApiOperation(value = "违约金减免（工单）")
    @PostMapping(value = "/wyjjm_gd", headers = "content-type=multipart/form-data")
    public ResultRespone wyjtzSave_gd(@Valid YyWyjtzGdDto dto,
                                      @RequestParam(value = "fileList", required = false) List<MultipartFile> fileList) {
        ResultRespone<Object> rest = new ResultRespone<>();
        String gdlsbh = yyWyjtzService.wyjtzSave_gd(dto);
        if (StringUtil.isNotEmpty(gdlsbh)) {
            String path = filePathBean.getHtfile();
            if (fileList.size() > 0) {
                iFileInfoService.saveFileList(fileList, path, gdlsbh, "yy_wyjtz", "违约金调整文件");
            }
            rest.setCode(1);
            rest.setMessage("添加成功");
            rest.setResult(gdlsbh);
        } else {
            rest.setCode(-1);
            rest.setMessage("添加失败");
        }
        return rest;
    }


    @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(yyWyjtzService.getById(id));
    }

    @GetMapping("/getGdPage")
    @ApiOperation(value = "违约金工单分页查询")
    public ResultRespone<PageResult<YyWyjtzVo>> getGdPage(PageDto page, @ModelAttribute YyWyjtzFilter dto) {
        return ResultRespone.success(yyWyjtzService.getGdPage(page, dto));
    }


    @GetMapping("/getGdWyjInfo")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gdlsbh", value = "工单流水编号", required = true, dataType = "String")
    })
    public ResultRespone getGdWyjInfo(String gdlsbh) {
        return ResultRespone.success(yyWyjtzService.getGdWyjInfo(gdlsbh));
    }

}
