package com.slodon.b2b2c.model.statistics;

import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.util.ExcelUtil;
import com.slodon.b2b2c.core.util.FileDownloadUtils;
import com.slodon.b2b2c.dao.read.statistics.MemberStoreDayReadMapper;
import com.slodon.b2b2c.dao.write.statistics.MemberStoreDayWriteMapper;
import com.slodon.b2b2c.statistics.example.MemberStoreDayExample;
import com.slodon.b2b2c.statistics.pojo.MemberStoreDay;
import com.slodon.b2b2c.statistics.pojo.base.MemberStoreBase;
import com.slodon.b2b2c.vo.statistics.MemberStoreReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 店铺会员日统计表model
 */
@Component
@Slf4j
public class MemberStoreDayModel {

    @Resource
    private MemberStoreDayReadMapper memberStoreDayReadMapper;
    @Resource
    private MemberStoreDayWriteMapper memberStoreDayWriteMapper;

    /**
     * 新增店铺会员日统计表
     *
     * @param memberStoreDay
     * @return
     */
    public Integer saveMemberStoreDay(MemberStoreDay memberStoreDay) {
        int count = memberStoreDayWriteMapper.insert(memberStoreDay);
        if (count == 0) {
            throw new MallException("添加店铺会员日统计表失败，请重试");
        }
        return count;
    }

    /**
     * 根据statsId删除店铺会员日统计表
     *
     * @param statsId statsId
     * @return
     */
    public Integer deleteMemberStoreDay(Long statsId) {
        if (StringUtils.isEmpty(statsId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = memberStoreDayWriteMapper.deleteByPrimaryKey(statsId);
        if (count == 0) {
            log.error("根据statsId：" + statsId + "删除店铺会员日统计表失败");
            throw new MallException("删除店铺会员日统计表失败,请重试");
        }
        return count;
    }

    /**
     * 根据statsId更新店铺会员日统计表
     *
     * @param memberStoreDay
     * @return
     */
    public Integer updateMemberStoreDay(MemberStoreDay memberStoreDay) {
        if (StringUtils.isEmpty(memberStoreDay.getStatsId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = memberStoreDayWriteMapper.updateByPrimaryKeySelective(memberStoreDay);
        if (count == 0) {
            log.error("根据statsId：" + memberStoreDay.getStatsId() + "更新店铺会员日统计表失败");
            throw new MallException("更新店铺会员日统计表失败,请重试");
        }
        return count;
    }

    /**
     * 根据statsId获取店铺会员日统计表详情
     *
     * @param statsId statsId
     * @return
     */
    public MemberStoreDay getMemberStoreDayByStatsId(Long statsId) {
        return memberStoreDayReadMapper.getByPrimaryKey(statsId);
    }

    /**
     * 根据条件获取店铺会员日统计表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<MemberStoreDay> getMemberStoreDayList(MemberStoreDayExample example, PagerInfo pager) {
        List<MemberStoreDay> memberStoreDayList;
        if (pager != null) {
            pager.setRowsCount(memberStoreDayReadMapper.countByExample(example));
            memberStoreDayList = memberStoreDayReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            memberStoreDayList = memberStoreDayReadMapper.listByExample(example);
        }
        return memberStoreDayList;
    }

    /**
     * 根据条件获取店铺会员统计
     *
     * @param example 查询条件信息
     * @return
     */
    public List<MemberStoreBase> getMemberStoreList(MemberStoreDayExample example, PagerInfo pager) {
        pager.setRowsCount(memberStoreDayReadMapper.getMemberStoreCount(example));
        return memberStoreDayReadMapper.getMemberStoreList(example, pager.getStart(), pager.getPageSize());
    }

    /**
     * 会员报表导出
     *
     * @param request
     * @param response
     * @param example
     * @return
     */
    public boolean memberReportExport(HttpServletRequest request, HttpServletResponse response, MemberStoreDayExample example) {
        List<MemberStoreBase> memberStoreList = memberStoreDayReadMapper.getMemberStoreList(example, null, null);
        List<MemberStoreReportVO> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(memberStoreList)) {
            memberStoreList.forEach(memberStoreBase -> {
                list.add(new MemberStoreReportVO(memberStoreBase));
            });
        }
        // 用于存放生成的excel文件名称
        List<File> fileNames = new ArrayList<>();
        // 每次导出的excel的文件名
        String fileNameS = "";
        // 将excel导出的文件位置
        String filePath = "/upload/";
        File files = new File(filePath);
        // 如果文件夹不存在则创建
        if (!files.exists() && !files.isDirectory()) {
            files.mkdirs();
        }

        //excel标题
        String titles = "会员名,下单数,下单金额,支付订单数,支付金额,退单数,退款金额";
        String[] titleArray = titles.split(",");

        // 创建Excel文件,每个excel限制2000条数据,超过则打包zip
        int count = list.size();
        int max = 2000;
        int num = count % max;
        int num1;
        if (num == 0) {
            num1 = count / max;
        } else {
            num1 = count / max + 1;
        }

        try {
            if (list.size() <= max) {
                // 导出的excel的文件名
                fileNameS = "会员报表-" + ExcelUtil.getSystemDate() + "-" + list.size() + ".xls";

                //下载单个excle
                byte b[] = ExcelUtil.export("会员报表", titleArray, list);

                File f = new File(filePath + fileNameS);
                FileUtils.writeByteArrayToFile(f, b, true);

                // 下载
                FileDownloadUtils.setExcelHeadInfo(response, request, fileNameS);

                ServletOutputStream out = response.getOutputStream();

                String path = "/upload/" + fileNameS; //Excel在服务器上的路径

                File file = new File(path);

                FileInputStream fis = new FileInputStream(file);
                ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(4096);

                byte[] cache = new byte[4096];
                for (int offset = fis.read(cache); offset != -1; offset = fis.read(cache)) {
                    byteOutputStream.write(cache, 0, offset);
                }

                byte[] bt = byteOutputStream.toByteArray();

                out.write(bt);
                out.flush();
                out.close();
                fis.close();
                if (file.exists()) {
                    file.delete();
                }

            } else {
                for (int i = 1; i <= num1; i++) {
                    fileNameS = "会员报表-" + ExcelUtil.getSystemDate() + "-" + i + ".xls";
                    List<MemberStoreReportVO> exportDatas = new ArrayList<>();
                    if (i == num1) {
                        for (int j = max * (i - 1); j < list.size(); j++) {
                            exportDatas.add(list.get(j));
                        }
                    } else {
                        for (int j = max * (i - 1); j < 2000 * i; j++) {
                            exportDatas.add(list.get(j));
                        }
                    }

                    //下载单个excle
                    byte b[] = ExcelUtil.export("会员报表", titleArray, exportDatas);

                    File f = new File(filePath + fileNameS);
                    FileUtils.writeByteArrayToFile(f, b, true);

                    fileNames.add(new File(filePath + fileNameS));
                }

                String zipName = "会员报表-" + ExcelUtil.getSystemDate() + ".zip";
                // 导出压缩文件的全路径
                String zipFilePath = filePath + zipName;
                System.out.println(zipFilePath);
                // 导出zip
                File zip = new File(zipFilePath);

                // 下载
                FileDownloadUtils.setZipDownLoadHeadInfo(response, request, zipName);

                // 将excel文件生成压缩文件
                File srcFile[] = new File[fileNames.size()];
                for (int j = 0, n1 = fileNames.size(); j < n1; j++) {
                    srcFile[j] = new File(String.valueOf(fileNames.get(j)));
                }
                FileDownloadUtils.ZipFiles(srcFile, zip);

                OutputStream outputStream = response.getOutputStream();
                InputStream inputStream = new FileInputStream(zipFilePath);
                byte[] buffer = new byte[8192];
                int i = -1;
                while ((i = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, i);
                }
                outputStream.flush();
                outputStream.close();
                inputStream.close();

                FileDownloadUtils.deleteDir(new File(filePath));
            }

        } catch (Exception e) {
            try {
                if (!response.isCommitted()) {
                    response.setContentType("text/html;charset=utf-8");
                    response.setHeader("Content-Disposition", "");
                    String html = FileDownloadUtils.getErrorHtml("下载失败");
                    response.getOutputStream().write(html.getBytes(StandardCharsets.UTF_8));
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        return true;
    }
    //endregion 会员报表导出
}