package cn.timer.api.controller.insure;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.timer.api.bean.insure.InsureLog;
import cn.timer.api.bean.insure.InsureProduct;
import cn.timer.api.bean.qyzx.QyzxEntInfoM;
import cn.timer.api.config.annotation.CurrentUser;
import cn.timer.api.config.annotation.UserBean;
import cn.timer.api.dao.insure.InsureLogMapper;
import cn.timer.api.dto.insure.InsureDto;
import cn.timer.api.dto.insure.LogDto;
import cn.timer.api.dto.insure.PolicyLogDto;
import cn.timer.api.utils.Page;
import cn.timer.api.utils.Result;
import cn.timer.api.utils.ResultUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author wgd
 * @email 862422848@qq.com
 * @date 2022-03-22 09:55:46
 */
@Api(tags = "8.0保险列表")
@RestController
@Transactional
@RequestMapping(value = "/insureLog", produces = {"application/json"})
public class InsureLogController {
    @Autowired
    private InsureLogMapper insureLogMapper;

    @GetMapping(value = "/logList")
    @ApiOperation(value = "12.日志列表", httpMethod = "GET", notes = "日志列表")
    public Result<Object> logList(@RequestParam("policyId") String policyId) {
        List<InsureLog> list = insureLogMapper.selectListById(policyId);
        if (list.size() > 0) {
            return ResultUtil.data(list);
        }
        return ResultUtil.error("暂无日志");
    }

    @GetMapping(value = "/downUserExcel")
    @ApiOperation(value = "12.人员清单", httpMethod = "GET", notes = "人员清单")
    public void downUserExcel(@RequestParam("logId") String logId, HttpServletRequest request, HttpServletResponse response) {
        InsureLog insureLog = InsureLog.builder().id(Integer.parseInt(logId)).build().selectById();
        if (insureLog == null || StringUtils.isNullOrEmpty(insureLog.getFileUrl())) {
            return;
        }
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        OutputStream sos = null;
        BufferedInputStream bis = null;
        try {
            response.setContentType("application/octet-stream");
            response.setHeader("content-disposition", "attachment; filename=" + new String((dtf2.format(LocalDateTime.now()) + ".xlsx").getBytes("UTF8"), "ISO-8859-1"));
            response.setCharacterEncoding("UTF-8");
            sos = response.getOutputStream();
            String destUrl = "http:" + insureLog.getFileUrl();
            URL url = new URL(destUrl);
            HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();
            //连接指定的网络资源
            httpUrl.connect();
            //获取网络输入流
            bis = new BufferedInputStream(httpUrl.getInputStream());
            int b;
            while ((b = bis.read()) != -1) {
                sos.write(b);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                sos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @PostMapping(value = "/insureLogList")
    @ApiOperation(value = "日志列表--8小时端", httpMethod = "GET", notes = "日志列表")
    public Result<Object> insureLogList(@CurrentUser UserBean userBean, @RequestBody Page page) {
        Map map = Maps.newHashMap();
        List<PolicyLogDto> list = insureLogMapper.selectLogListByOrgCode(userBean.getOrgCode(),page);
        map.put("list", list);
        map.put("total", insureLogMapper.selectLogTotalByOrgCode(userBean.getOrgCode()));
        return ResultUtil.data(map);
    }

    @PostMapping(value = "/actionLogList")
    @ApiOperation(value = "操作列表--运营后台", httpMethod = "GET", notes = "日志列表")
    public Result<Object> actionLogList(@RequestBody LogDto logDto) {
        Map map = Maps.newHashMap();
        List<InsureLog>  list=insureLogMapper.logList(logDto);
        map.put("list", list);
        map.put("totalNum",insureLogMapper.logListTotal(logDto));
        return ResultUtil.data(map);
    }
}