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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.persistens.PageUtil;
import com.zjrs.zwnw.dbgl.blo.DbrwglCommPLO;
import com.zjrs.zwnw.dbgl.blo.DbrwjzcxPLO;
import com.zjrs.zwnw.dbgl.dto.DbrwglDbrwQueryDTO;
import com.zjrs.zwnw.dbgl.dto.DbrwglExportrwcxDTO;
import com.zjrs.zwnw.dbgl.entity.DbrwglCommMapper;
import com.zjrs.zwnw.dbgl.entity.DbrwjzcxMapper;
import com.zjrs.zwnw.dbgl.entity.SendSmsMapper;
import com.zjrs.zwnw.dbgl.po.SendSmsPO;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dagou
 * @date 2022/11/24 18:15
 **/
@Service
public class DbrwjzcximplPLO implements DbrwjzcxPLO {

    @Resource
    private DbrwglCommMapper dbrwglCommMapper;
    @Resource
    private DbrwjzcxMapper dbrwjzcxMapper;

    @Resource
    private SendSmsMapper sendSmsMapper;

    @Resource
    private DbrwglCommPLO dbrwglCommPLO;
    @Override
    public PageResult getDbrwjzcxList(DbrwglDbrwQueryDTO dto, Integer pagenum, Integer pagesize) {
        Page<Map<String, Object>> page = new Page<>(pagenum, pagesize);
        Page<Map<String, Object>> pageResult = (Page) dbrwjzcxMapper.getDbrwjzcxList(page,dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void exportDbrwjzcxList(DbrwglDbrwQueryDTO dto, HttpServletResponse res) {
        List<DbrwglExportrwcxDTO> result = dbrwjzcxMapper.exportDbrwjzcxList(dto);
        String title = createTitle(dto);
        Map<String,String> titleMap = new HashMap<>();
        titleMap.put("title",title);
        InputStream inputStream = null;
        try {
            String fileName = "督办列表.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 void insertSMS() {
        DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String msg = "您好，%s任务已经红灯预警，请尽快登录智慧人社系统办理。";
        //查找需要发预警信息的督办任务
        List<Map<String,String>> resultList = dbrwjzcxMapper.getNeedSendSMSList();
        for(Map<String,String> item: resultList){
            Integer seq = sendSmsMapper.getSmsSeq();//获取序列号
            SendSmsPO smsPO = new SendSmsPO();
            smsPO.setSmscontent(String.format(msg, item.get("rwmc")));
            smsPO.setPhonenumber(item.get("phonenumber"));
            smsPO.setStatus(0);
            smsPO.setResultdesc(LocalDateTime.now().format(formatters));//创建时间
            smsPO.setSmsindex(seq);
            sendSmsMapper.insert(smsPO);
        }
    }

    public String createTitle(DbrwglDbrwQueryDTO dto){
        //数据库转换字典
        Map<String,String> dictMap = dbrwjzcxMapper.getDictMap(dto);
        StringBuilder resultString = new StringBuilder();
        if(StringUtils.isNotBlank(dto.getRwly())){
            resultString.append("任务来源：").append(dto.getRwly()+";");
        }
        if(StringUtils.isNotBlank(dto.getRwlyfl())){
            resultString.append("任务来源分类：").append(MapUtils.getString(dictMap,"rwlyfl")+";");
        }
        if(StringUtils.isNotBlank(dto.getFgld())){
            resultString.append("分管领导：").append(MapUtils.getString(dictMap,"fgld")+";");
        }
        if(StringUtils.isNotBlank(dto.getQtbm())){
            resultString.append("牵头部门：").append(MapUtils.getString(dictMap,"qtbm")+";");
        }
        if(StringUtils.isNotBlank(dto.getXbbm())){
            resultString.append("协办部门：").append(MapUtils.getString(dictMap,"xbbm")+";");
        }
        if(StringUtils.isNotBlank(dto.getSdpj())){
            resultString.append("速度评价：").append(MapUtils.getString(dictMap,"sdpj")+";");
        }
        if(StringUtils.isNotBlank(dto.getZlpj())){
            resultString.append("质量评价：").append(MapUtils.getString(dictMap,"zlpj")+";");
        }
        if(StringUtils.isNotBlank(dto.getRwzt())){
            resultString.append("督办状态：").append(MapUtils.getString(dictMap,"rwzt")+";");
        }
        if(StringUtils.isNotBlank(dto.getBae003Start()) || StringUtils.isNotBlank(dto.getBae003End())){
            String msg = "督办任务创建时间从：%s至%s;";
            String start = StringUtils.isNotBlank(dto.getBae003Start()) ? dto.getBae003Start():" ";
            String end = StringUtils.isNotBlank(dto.getBae003End()) ? dto.getBae003End():" ";
            resultString.append(String.format(msg,start,end));
        }
        if(StringUtils.isNotBlank(dto.getBae001())){
            resultString.append("督办任务创建部门：").append(MapUtils.getString(dictMap,"bae001")+";");
        }
        return resultString.toString();
    }
}
