package com.dxhy.domestic.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.constant.CommonConstants;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.util.StringHelper;
import com.dxhy.common.utils.R;
import com.dxhy.domestic.service.customsrefund.DomesticCustomsRefundService;
import com.dxhy.vo.custom.BaseCustomsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * @author ddx
 */
@RestController
@RequestMapping("/customsRefund")
@Slf4j
public class DomesticRefundController extends AbstractController {

    @Resource
    private DomesticCustomsRefundService domesticCustomsRefundService;

    /**
     * 退税认证-海关缴款书手工认证列表
     */
    @PostMapping("/queryManualConfirmList")
    @SysLog("退税认证海关缴款书手工认证列表")
    @ResponseBody
    public Object queryManualConfirmList(Page<Object> page, @RequestBody BaseCustomsVO baseCustomsVO) {
        try {
            baseCustomsVO.setUserInfo(getUserInfo());
            baseCustomsVO.setQsFlag(getUserInfo().getQsStatus());
            baseCustomsVO.setBusiness(getUserInfo().getBusiness());
            return ResponseEntity
                .ok(R.ok().put("data", domesticCustomsRefundService.queryManualConfirmList(page, baseCustomsVO)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 退税认证-海关缴款书手工认证
     */
    @PostMapping("/manualConfirm")
    public ResponseEntity<?> manualConfirm(@RequestBody BaseCustomsVO baseCustomsVO) {
        try {
            baseCustomsVO.setUserInfo(getUserInfo());
            String result = domesticCustomsRefundService.manualConfirm(baseCustomsVO, getUserInfo());
            return ResponseEntity.ok(R.ok().put("data", result));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(e.getMessage()));
        }
    }

    /**
     * 文件下载
     *
     */
    @RequestMapping("/downLoad")
    @ResponseBody
    public ResponseEntity<?> downLoadFtpFileMould(String fileName, HttpServletResponse response) {
        try {
            File downLoadFile = new File(this.getClass().getResource("/").getPath() + "template/" + fileName);
            // 获取文件的长度
            long fileLength = downLoadFile.length();
            // 设置文件输出类型
            response.setContentType("application/octet-stream");
            response.setHeader("Content-disposition",
                "attachment; filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), "ISO8859-1"));
            // 设置输出长度
            response.setHeader("Content-Length", String.valueOf(fileLength));
            // 获取输入流
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(downLoadFile));
            // 输出流
            BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
            bos.flush();
            // 关闭流
            bis.close();
            bos.close();
            return null;
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 退税认证-海关缴款书导入认证-
     */
    @PostMapping("/importConfirmList")
    public ResponseEntity<?> importConfirmList(@RequestBody(required = false) BaseCustomsVO baseCustomsVO) {
        try {
            baseCustomsVO.setUserInfo(getUserInfo());
            if (StringHelper.isBlank(baseCustomsVO.getDataList())) {
                return ResponseEntity.ok(R.error("请上传excel文件!"));
            }
            return ResponseEntity.ok(Objects
                .requireNonNull(R.ok().put("data", domesticCustomsRefundService.importConfirmList(baseCustomsVO))));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 退税认证-海关缴款书认证查询列表
     */
    @PostMapping("/queryConfirmList")
    @SysLog("退税认证海关缴款书认证查询列表")
    @ResponseBody
    public Object queryConfirmList(Page<Object> page, @RequestBody BaseCustomsVO baseCustomsVO) {
        try {
            baseCustomsVO.setUserInfo(getUserInfo());
            baseCustomsVO.setBusiness(getUserInfo().getBusiness());
            return ResponseEntity
                .ok(R.ok().put("data", domesticCustomsRefundService.queryConfirmList(page, baseCustomsVO)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 退税认证-退税统计
     */
    @PostMapping("/queryRefundCountList")
    @SysLog("退税统计列表")
    @ResponseBody
    public Object queryRefundCountList(Page<Object> page, @RequestBody BaseCustomsVO baseCustomsVO) {
        try {
            baseCustomsVO.setUserInfo(getUserInfo());
            return ResponseEntity
                .ok(R.ok().put("data", domesticCustomsRefundService.queryRefundCountList(page, baseCustomsVO)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 退税认证-退税统计明细列表
     */
    @PostMapping("/queryRefundCountDetailList")
    @SysLog("退税统计明细列表")
    @ResponseBody
    public Object queryRefundCountDetailList(Page<Object> page, @RequestBody BaseCustomsVO baseCustomsVO) {
        try {
            baseCustomsVO.setUserInfo(getUserInfo());
            return ResponseEntity
                .ok(R.ok().put("data", domesticCustomsRefundService.queryRefundCountDetailPage(page, baseCustomsVO)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }


    /**
    * @Description 退税确认弹框界面
    * @param page
    * @param baseCustomsVO
    * @Return java.lang.Object
    * @Author wangruwei
    * @Date 2024/3/15 11:17
    **/
    @PostMapping("/queryRefundAlertPage")
    @SysLog("退税确认弹框界面")
    @ResponseBody
    public Object queryRefundAlertPage(Page<Object> page, @RequestBody BaseCustomsVO baseCustomsVO) {
        try {
            baseCustomsVO.setUserInfo(getUserInfo());
            return ResponseEntity
                    .ok(R.ok().put("data", domesticCustomsRefundService.queryRefundAlertPage(page, baseCustomsVO)));
        } catch (Exception e) {
            log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
    * @Description 退税确认弹框点击申请
    * @param baseCustomsVO
    * @Return java.lang.Object
    * @Author wangruwei
    * @Date 2024/3/15 15:44
    **/
    @PostMapping("/refundAlertApply")
    @SysLog("退税确认弹框点击申请")
    @ResponseBody
    public Object refundAlertApply(@RequestBody BaseCustomsVO baseCustomsVO) {
        try {
            baseCustomsVO.setUserInfo(getUserInfo());
            return domesticCustomsRefundService.refundAlertApply(baseCustomsVO);
        } catch (Exception e) {
            log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }


}
