package cn.cecep.talroad.service.impl.analysis;

import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.config.AuditWordField;
import cn.cecep.talroad.domain.BEnterpriseIrNotifierproNotice;
import cn.cecep.talroad.domain.analysis.SRaActionReportsAuditResult;
import cn.cecep.talroad.mapper.analysis.SRaActionReportsAuditResultMapper;
import cn.cecep.talroad.service.IBEnterpriseIrNotifierproNoticeService;
import cn.cecep.talroad.service.analysis.ISRaActionReportsAuditResultService;
import cn.cecep.talroad.util.ExportWordUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 执行报告规范性审核结果Service业务层处理
 *
 * @author szhpt
 * @date 2023-03-28*/
@Service
public class SRaActionReportsAuditResultServiceImpl extends ServiceImpl<SRaActionReportsAuditResultMapper, SRaActionReportsAuditResult> implements ISRaActionReportsAuditResultService
{
    @Autowired
    private SRaActionReportsAuditResultMapper sRaActionReportsAuditResultMapper;

    @Autowired
    private IBEnterpriseIrNotifierproNoticeService enterpriseIrNotifierproNoticeService;

    /**
     * 查询执行报告规范性审核结果列表
     *
     * @param sRaActionReportsAuditResult 执行报告规范性审核结果
     * @return 执行报告规范性审核结果
     */
    @Override
    public List<SRaActionReportsAuditResult> selectSRaActionReportsAuditResultList(SRaActionReportsAuditResult sRaActionReportsAuditResult)
    {
        return sRaActionReportsAuditResultMapper.selectSRaActionReportsAuditResultList(sRaActionReportsAuditResult);
    }

    @Override
    public IPage<SRaActionReportsAuditResult> resultList(Page<SRaActionReportsAuditResult> page, QueryWrapper<SRaActionReportsAuditResult> orderByDesc) {
        return sRaActionReportsAuditResultMapper.resultList(page,orderByDesc);
    }

    @Override
    public void getResultFactoryDetailWord(String id, HttpServletResponse response) throws IOException, TemplateException, IllegalAccessException {
        Page<SRaActionReportsAuditResult> page = new Page(0, 1);
        page = sRaActionReportsAuditResultMapper.resultList(page,new QueryWrapper<SRaActionReportsAuditResult>().eq("results.id",id));
        if (page.getRecords().size() == 0) {
            response.sendError(500,"没有找到该企业");
            return;
        }
        SRaActionReportsAuditResult sRaActionReportsAuditResult = page.getRecords().get(0);
        Map map = new HashMap();
        map.put("factoryName",sRaActionReportsAuditResult.getFactoryName());
        map.put("licenseCode",sRaActionReportsAuditResult.getLicenseCode());
        map.put("auditDeptName",StringUtils.isBlank(sRaActionReportsAuditResult.getAuditDeptName())?"":sRaActionReportsAuditResult.getAuditDeptName());
        map.put("auditPersonName",StringUtils.isBlank(sRaActionReportsAuditResult.getAuditPersonName())?"":sRaActionReportsAuditResult.getAuditPersonName());
        map.put("auditTime",sRaActionReportsAuditResult.getAuditTime() == null?"":sRaActionReportsAuditResult.getAuditTime());
        map.put("manTypeFocus","");map.put("manTypeSimplify","");
        if(StringUtils.isNotBlank(sRaActionReportsAuditResult.getManType())){
            if(sRaActionReportsAuditResult.getManType().equalsIgnoreCase("FOCUS")){
                map.put("manTypeFocus","✓");
            }else if(sRaActionReportsAuditResult.getManType().equalsIgnoreCase("SIMPLIFY")){
                map.put("manTypeSimplify","✓");
            }
        }
        //检查14个是否的字段 目前按空就是否，因为无法确定字段里存储的是什么鬼东西 产品也不写
        //反射获取注解处理需要判断是否的字段
        Class<? extends SRaActionReportsAuditResult> aClass = sRaActionReportsAuditResult.getClass();
        for (Field declaredField : aClass.getDeclaredFields()) {
            if (declaredField.isAnnotationPresent(AuditWordField.class)) {
                AuditWordField annotation = declaredField.getAnnotation(AuditWordField.class);
                declaredField.setAccessible(true);
                String value = (String)declaredField.get(sRaActionReportsAuditResult);
                //初始化
                map.put("chose"+annotation.orderNum()+"Yes","");
                map.put("chose"+annotation.orderNum()+"No","");
                if(StringUtils.isNotBlank(value) && value.equalsIgnoreCase("是")){
                    map.put("chose"+annotation.orderNum()+"Yes","✓");
                }else{
                    map.put("chose"+annotation.orderNum()+"No","✓");
                }
            }
        }
        ExportWordUtil.exportToWord(map,response, "wordReport/AuditRecordFactoryDetailBase.ftl", "wordReport/outFile.docx","企业详情报告.docx");
    }

    /**
     * 发送企业问题给报警表
     * @param id
     * @return
     */
    @Override
    public AjaxResults<String> sendQuestion(String id) {
        SRaActionReportsAuditResult byId = this.getById(id);
        if(byId == null){
            return AjaxResults.error("没有找到该条审核数据");
        }
        BEnterpriseIrNotifierproNotice bEnterpriseIrNotifierproNotice = new BEnterpriseIrNotifierproNotice();
        bEnterpriseIrNotifierproNotice.setFactoryId(byId.getFactoryId());
        bEnterpriseIrNotifierproNotice.setAnnouncementType("企业执行报告异常通告");
        bEnterpriseIrNotifierproNotice.setAnnouncementTitle("请检查企业执行报告完整性");
        bEnterpriseIrNotifierproNotice.setAnnouncementDetails("请检查企业执行报告完整性");
        bEnterpriseIrNotifierproNotice.setAnnouncementTime(new Date());
        bEnterpriseIrNotifierproNotice.setCreateTime(new Date());
        this.enterpriseIrNotifierproNoticeService.save(bEnterpriseIrNotifierproNotice);
        return AjaxResults.success("问题发送成功");
    }
}
