package com.zjrs.zwnw.psam.blo.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.mohrss.leaf.core.framework.domain.blo.impl.BLOImpl;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.persistens.PageUtil;
import com.zjrs.zwnw.psam.blo.SjtjBLO;
import com.zjrs.zwnw.psam.dto.PsamKglDTO;
import com.zjrs.zwnw.psam.dto.PsamTjDTO;
import com.zjrs.zwnw.psam.dto.PsamXfycDTO;
import com.zjrs.zwnw.psam.entity.RsPsamFfxxbMapper;
import com.zjrs.zwnw.psam.entity.RsPsamXfmxbMapper;
import com.zjrs.zwnw.psam.entity.RsPsamYwxxSqbMapper;
import com.zjrs.zwnw.psam.po.RsPsamXfmxbPO;
import com.zjrs.zwnw.sxgl.blo.impl.JcsxqdkBLOimpl;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 *
 * @author liangxiudong
 * @since 2021-04-07
 */
@Service
public class SjtjBLOImpl extends BLOImpl implements SjtjBLO {
    @Resource
    private RsPsamFfxxbMapper rsPsamFfxxbMapper;
    @Resource
    private RsPsamYwxxSqbMapper rsPsamYwxxSqbMapper;
    @Resource
    private RsPsamXfmxbMapper rsPsamXfmxbMapper;

    @Override
    public List<PsamTjDTO> getKzttjList() {
        return rsPsamFfxxbMapper.getKzttjList();
    }

    @Override
    public void exportKzttjList(HttpServletResponse res) {
        InputStream inputStream = null;
        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "psam" + File.separator + "exportKzttj.xlsx");
            String fileName = "exportKzttj.xlsx";
            res.setContentType("application/vnd.ms-excel;charset=utf-8");
            res.setHeader("Content-disposition", "attachment;filename=" + fileName);
            res.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public PageResult getShsltjList(PsamKglDTO dto, int page, int rows) {
        Page<PsamTjDTO> page1 = new Page<>(page, rows);
        Page<PsamTjDTO> pageResult = (Page) rsPsamFfxxbMapper.getShsltjList(page1, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void exportShsltjList(PsamKglDTO dto, HttpServletResponse res) {
        InputStream inputStream = null;
        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "psam" + File.separator + "exportShsltjList.xlsx");

            String fileName = "exportShsltjList.xlsx";
            res.setContentType("application/vnd.ms-excel;charset=utf-8");
            res.setHeader("Content-disposition", "attachment;filename=" + fileName);
            res.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public PageResult getJgtjList(PsamKglDTO dto, int page, int rows) {
        Page<PsamTjDTO> page1 = new Page<>(page, rows);
        Page<PsamTjDTO> pageResult = (Page) rsPsamFfxxbMapper.getJgtjList(page1, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public PageResult getJgtjDetailList(PsamKglDTO dto, int page, int rows) {
        Page<Map<String,Object>> page1 = new Page<>(page, rows);
        Page<Map<String,Object>> pageResult = (Page) rsPsamFfxxbMapper.getJgtjDetailList(page1, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void exportJgtjList(PsamKglDTO dto, HttpServletResponse res) {
        InputStream inputStream = null;
        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "psam" + File.separator + "exportJgtjList.xlsx");

            String fileName = "exportJgtjList.xlsx";
            res.setContentType("application/vnd.ms-excel;charset=utf-8");
            res.setHeader("Content-disposition", "attachment;filename=" + fileName);
            res.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public PageResult getShtjList(PsamKglDTO dto, int page, int rows) {
        Page<PsamTjDTO> page1 = new Page<>(page, rows);
        Page<PsamTjDTO> pageResult = (Page) rsPsamFfxxbMapper.getShtjList(page1, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void exportShtjList(PsamKglDTO dto, HttpServletResponse res) {
        InputStream inputStream = null;
        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "psam" + File.separator + "exportShtjList.xlsx");

            String fileName = "exportShtjList.xlsx";
            res.setContentType("application/vnd.ms-excel;charset=utf-8");
            res.setHeader("Content-disposition", "attachment;filename=" + fileName);
            res.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public PageResult getShtjDetailList(PsamKglDTO dto, int page, int rows) {
        Page<PsamTjDTO> page1 = new Page<>(page, rows);
        Page<PsamTjDTO> pageResult = (Page) rsPsamFfxxbMapper.getShtjDetailList(page1, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public PageResult getRqListBySqrq(PsamKglDTO dto, int page, int rows) {
        Page<PsamTjDTO> page1 = new Page<>(page, rows);
        Page<PsamTjDTO> pageResult = (Page) rsPsamYwxxSqbMapper.getRqListBySqrq(page1, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public PageResult getRqListByFfrq(PsamKglDTO dto, int page, int rows) {
        Page<PsamTjDTO> page1 = new Page<>(page, rows);
        Page<PsamTjDTO> pageResult = (Page) rsPsamYwxxSqbMapper.getRqListByFfrq(page1, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void exportRqList(PsamKglDTO dto, HttpServletResponse res) {
        InputStream inputStream = null;
        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "psam" + File.separator + "exportRqtj.xlsx");

            String fileName = "exportRqtj.xlsx";
            res.setContentType("application/vnd.ms-excel;charset=utf-8");
            res.setHeader("Content-disposition", "attachment;filename=" + fileName);
            res.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public PageResult getXfyckList(PsamXfycDTO dto, int page, int rows) {
        Page<Map<String, Object>> page1 = new Page<>(page, rows);
        Page<Map<String, Object>> pageResult = (Page) rsPsamXfmxbMapper.getXfyckList(page1, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        List<Map<String, Object>> list= (List<Map<String, Object>>) result.getRows();
        for(int i=0;i<list.size();i++){
            Map<String, Object> map=list.get(i);
            QueryWrapper<RsPsamXfmxbPO> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("psambh",map.get("psambh"));
            queryWrapper.orderByDesc("jysj");
            RsPsamXfmxbPO rsPsamXfmxbPO=this.rsPsamXfmxbMapper.selectList(queryWrapper).get(0);
            map.put("lng",rsPsamXfmxbPO.getAzlng());
            list.set(i,map);
        }
        result.setRows(list);
        return result;
    }

    @Override
    public PageResult getXfmxList(PsamKglDTO dto, int page, int rows) {
        Page<RsPsamXfmxbPO> page1 = new Page<>(page, rows);
        QueryWrapper<RsPsamXfmxbPO> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("psambh",dto.getPsambh());
        Page<RsPsamXfmxbPO> pageResult = (Page) rsPsamXfmxbMapper.selectPage(page1,queryWrapper);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }
}
