package com.qmth.sop.business.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.reflect.TypeToken;
import com.qmth.sop.business.bean.report.QualityAnalyseReportBean;
import com.qmth.sop.business.bean.report.QualityAnalyseReportViewBean;
import com.qmth.sop.business.bean.report.QualityAnalyseReportViewExportBean;
import com.qmth.sop.business.entity.TBService;
import com.qmth.sop.business.mapper.QualityAnalyseReportMapper;
import com.qmth.sop.business.service.QualityAnalyseReportService;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.ExceptionResultEnum;
import com.qmth.sop.common.enums.InfluenceDegreeEnum;
import com.qmth.sop.common.enums.QualityProblemReasonEnum;
import com.qmth.sop.common.util.FileUtil;
import com.qmth.sop.common.util.GsonUtil;
import com.qmth.sop.common.util.JacksonUtil;
import com.qmth.sop.common.util.ServletUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description: 质量监控分析报表service impl
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2024/9/18
 */
@Service
public class QualityAnalyseReportServiceImpl extends ServiceImpl<QualityAnalyseReportMapper, TBService>
        implements QualityAnalyseReportService {

    /**
     * 大区质量问题统计
     *
     * @param serviceId
     * @return
     */
    @Override
    public List<QualityAnalyseReportBean> areaQualityReport(Long serviceId) {
        return this.baseMapper.areaQualityReport(serviceId);
    }

    /**
     * 人力商质量问题统计
     *
     * @param serviceId
     * @return
     */
    @Override
    public List<QualityAnalyseReportBean> humanSupplierQualityReport(Long serviceId) {
        return this.baseMapper.humanSupplierQualityReport(serviceId);
    }

    /**
     * 质量问题原因分类统计
     *
     * @param serviceId
     * @return
     */
    @Override
    public List<QualityAnalyseReportBean> reasonQualityReport(Long serviceId) {
        return this.baseMapper.reasonQualityReport(serviceId);
    }

    /**
     * 报表详情列表
     *
     * @param iPage
     * @param serviceId
     * @param areaManagerId
     * @param supplierId
     * @param reason
     * @param customName
     * @param influenceDegree
     * @return
     */
    @Override
    public IPage<QualityAnalyseReportViewBean> viewList(IPage<Map> iPage, Long serviceId, Long areaManagerId,
            Long supplierId, QualityProblemReasonEnum reason, String customName, InfluenceDegreeEnum influenceDegree) {
        return this.baseMapper.viewList(iPage, serviceId, areaManagerId, supplierId,
                Objects.nonNull(reason) ? reason.name() : null, customName,
                Objects.nonNull(influenceDegree) ? influenceDegree.name() : null);
    }

    /**
     * 报表详情导出
     *
     * @param serviceId
     * @param areaManagerId
     * @param supplierId
     * @param reason
     * @param customName
     * @param influenceDegree
     */
    @Override
    public void viewListExport(Long serviceId, Long areaManagerId, Long supplierId, QualityProblemReasonEnum reason,
            String customName, InfluenceDegreeEnum influenceDegree) {
        File fileTemp = null;
        try {
            fileTemp = SystemConstant.getFileTempVar(SystemConstant.XLSX_PREFIX);
            List<QualityAnalyseReportViewBean> qualityAnalyseReportViewBeanList = this.baseMapper.viewListExport(
                    serviceId, areaManagerId, supplierId, Objects.nonNull(reason) ? reason.name() : null, customName,
                    Objects.nonNull(influenceDegree) ? influenceDegree.name() : null);
            List<QualityAnalyseReportViewExportBean> qualityAnalyseReportViewExportBeanList = GsonUtil.fromJson(
                    JacksonUtil.parseJson(qualityAnalyseReportViewBeanList),
                    new TypeToken<List<QualityAnalyseReportViewExportBean>>() {

                    }.getType());
            EasyExcel.write(fileTemp, QualityAnalyseReportViewExportBean.class).sheet("质量监控分析")
                    .doWrite(qualityAnalyseReportViewExportBeanList);
            HttpServletResponse response = ServletUtil.getResponse();
            FileUtil.outputFile(response, fileTemp, "质量监控分析.xlsx");
        } catch (IOException e) {
            throw ExceptionResultEnum.ERROR.exception(e.getMessage());
        } finally {
            if (Objects.nonNull(fileTemp)) {
                fileTemp.delete();
            }
        }
    }
}
