package com.tofly.feesapi.dzht.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.feesapi.common.core.ResultRespone;
import com.tofly.feesapi.dzht.entity.HtFiles;
import com.tofly.feesapi.dzht.entity.dto.HtGhDto;
import com.tofly.feesapi.dzht.entity.dto.HtXhDto;
import com.tofly.feesapi.dzht.entity.dto.HtXzbgDto;
import com.tofly.feesapi.dzht.entity.vo.HtPreviewParameter;
import com.tofly.feesapi.dzht.service.HtFilesService;
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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

@RestController
@RequestMapping("/htfiles")
@Api(tags = "电子合同文件接口")
public class HtFilesController {

    @Autowired
    private HtFilesService htFilesService;

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone getHtFilesPage(Page page, @ModelAttribute HtFiles htFiles) {
        return ResultRespone.success(htFilesService.page(page, Wrappers.query(htFiles)));
    }

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

    @PostMapping("/wordtopdf")
    @ApiOperation(value = "word转pdf")
    public ResultRespone WordToPdf(@RequestBody HtPreviewParameter htPreviewParameter) {
        return ResultRespone.success(htFilesService.WordToPdf(htPreviewParameter));
    }

    @ApiOperation(value = "新增电子合同文件")
    @PostMapping
    public ResultRespone save(@RequestBody HtFiles htFiles) {
        return ResultRespone.success(htFilesService.save(htFiles));
    }

    @ApiOperation(value = "通过ID删除电子合同文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String")
    })
    @DeleteMapping("/{id}")
    public ResultRespone removeById(@PathVariable String id) {
        return ResultRespone.success(htFilesService.removeById(id));
    }


    @ApiOperation(value = "通过ID批量删除电子合同文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    public ResultRespone removeByIds(String ids) {
        return ResultRespone.success(htFilesService.removeByIds(Arrays.asList(ids.split(","))));
    }

    @PostMapping("/xh/wordtopdf")
    @ApiOperation(value = "销户（word转pdf）")
    public ResultRespone Xh_WordToPdf(@RequestBody HtXhDto dto, HttpServletResponse response, HttpServletRequest request) {
        ResultRespone resultRespone = new ResultRespone();
        try {
            resultRespone.setCode(1);
            resultRespone.setResult(htFilesService.XH_WordToPdf(dto));
        } catch (RuntimeException e) {
            resultRespone.setCode(-1);
            resultRespone.setMessage(e.getMessage());
        }
        return resultRespone;
    }

    @PostMapping("/gh/wordtopdf")
    @ApiOperation(value = "更名过户（word转pdf）")
    public ResultRespone Xh_WordToPdf(@RequestBody HtGhDto dto, HttpServletResponse response, HttpServletRequest request) {
        ResultRespone resultRespone = new ResultRespone();
        try {
            resultRespone.setCode(1);
            resultRespone.setResult(htFilesService.GH_WordToPdf(dto));
        } catch (RuntimeException e) {
            resultRespone.setCode(-1);
            resultRespone.setMessage(e.getMessage());
        }
        return resultRespone;
    }

    @PostMapping("/xzbh/wordtopdf")
    @ApiOperation(value = "用户性质变更（word转pdf）")
    public ResultRespone Xzbg_WordToPdf(@RequestBody HtXzbgDto dto, HttpServletResponse response, HttpServletRequest request) {
        ResultRespone resultRespone = new ResultRespone();
        try {
            resultRespone.setCode(1);
            resultRespone.setResult(htFilesService.XZBG_WordToPdf(dto));
        } catch (RuntimeException e) {
            resultRespone.setCode(-1);
            resultRespone.setMessage(e.getMessage());
        }
        return resultRespone;
    }

}
