package com.tofly.feesapi.wxgl.wechatofficalacctmgt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.feesapi.common.core.ResultRespone;

import com.tofly.feesapi.file.fileconfiguration.FileStorageService;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.WxGhsq;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.dto.WxCommonDto;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.vo.WxGhsqVo;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.service.WxGhsqService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;

@RestController
@RequestMapping("/api/wechatofficalacctmgt/wxghsq")
@Api(tags = "微信-更名过户申请接口")
public class WxGhsqController {

    @Autowired
    private WxGhsqService wxGhsqService;
    @Autowired
    private FileStorageService fileStorageService;


    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone<Page<WxGhsqVo>> getWxGhsqPage(Page page, @ModelAttribute WxCommonDto dto) {
        ResultRespone<Page<WxGhsqVo>> resultRespone = new ResultRespone<>();
        try {
            Page<WxGhsqVo> wxghsqPage = wxGhsqService.getWxGhsqPage(page, dto);
            resultRespone.setCode(1);
            resultRespone.setResult(wxghsqPage);
        } catch (RuntimeException e) {
            resultRespone.setCode(-1);
            resultRespone.setMessage(e.getMessage());
        }
        return resultRespone;
    }


    @PostMapping("/upload")
    @ApiOperation(value = "附件上传")
    public ResultRespone uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            // 设置文件上传的路径
            String remoteFilePath = "/ftppath";
            String fileName = file.getOriginalFilename();
            String remoteFileName = remoteFilePath + "/" + fileName;
            // 上传文件到FTP服务器
            fileStorageService.upload(file, remoteFileName);
            return ResultRespone.success("上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultRespone.failed("上传失败");
        }

    }


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


    @ApiOperation(value = "新增微信-更名过户申请")
    @PostMapping
    public ResultRespone save(@RequestBody WxGhsq wxGhsq) {
        return ResultRespone.success(wxGhsqService.save(wxGhsq));
    }


    @ApiOperation(value = "修改微信-更名过户申请")
    @PutMapping
    public ResultRespone updateById(@RequestBody WxGhsq wxGhsq) {
        return ResultRespone.success(wxGhsqService.updateById(wxGhsq));
    }


    @ApiOperation(value = "通过ID删除微信-更名过户申请")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String")
    })
    @DeleteMapping("/{id}")
    public ResultRespone removeById(@PathVariable String id) {
        return ResultRespone.success(wxGhsqService.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(wxGhsqService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
