package com.dgtly.wxportal.service.impl;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.dgtly.common.utils.DateUtils;
import com.dgtly.system.domain.Mail;
import com.dgtly.system.domain.OrderTucEarlyWarning;
import com.dgtly.system.domain.SysDictData;
import com.dgtly.system.domain.UserVO;
import com.dgtly.system.mapper.OrderTucEarlyWarningMapper;
import com.dgtly.system.mapper.SysBatchSignForMapper;
import com.dgtly.system.mapper.SysUserMapper;
import com.dgtly.system.service.IOrderTucEarlyWarningService;
import com.dgtly.system.service.ISysConfigService;
import com.dgtly.system.service.ISysDictDataService;
import com.dgtly.system.util.MailUtils;
import com.dgtly.wxportal.utils.qywxutils.QyWxSendMessageUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.dgtly.wxportal.mapper.WxSendMessageMapper;
import com.dgtly.wxportal.domain.WxSendMessage;
import com.dgtly.wxportal.service.IWxSendMessageService;
import com.dgtly.common.core.text.Convert;

import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;

/**
 * 企业微信发送消息历史记录Service业务层处理
 * 
 * @author qxp
 * @date 2020-09-02
 */
@Service
public class WxSendMessageServiceImpl implements IWxSendMessageService
{
    @Autowired
    private WxSendMessageMapper wxSendMessageMapper;
    @Autowired
    private QyWxSendMessageUtil qyWxSendMessageUtil;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private SysBatchSignForMapper batchSignForMapper;
    @Autowired
    private ISysDictDataService sysDictDataService;
    @Autowired
    private OrderTucEarlyWarningMapper tucEarlyWarningMapper;
    @Autowired
    private MailUtils mailUtils;
    @Value(value = "${spring.mail.username}")
    private String myEmailAccount;
    @Value(value = "${spring.mail.password}")
    public  String myEmailPassword;
    @Value(value = "${spring.mail.host}")
    public  String myEmailSMTPHost;
    /**
     * 查询企业微信发送消息历史记录
     * 
     * @param id 企业微信发送消息历史记录ID
     * @return 企业微信发送消息历史记录
     */
    @Override
    public WxSendMessage selectWxSendMessageById(Long id)
    {
        return wxSendMessageMapper.selectWxSendMessageById(id);
    }

    /**
     * 查询企业微信发送消息历史记录列表
     * 
     * @param wxSendMessage 企业微信发送消息历史记录
     * @return 企业微信发送消息历史记录
     */
    @Override
    public List<WxSendMessage> selectWxSendMessageList(WxSendMessage wxSendMessage)
    {
        return wxSendMessageMapper.selectWxSendMessageList(wxSendMessage);
    }

    /**
     * 新增企业微信发送消息历史记录
     * 
     * @param wxSendMessage 企业微信发送消息历史记录
     * @return 结果
     */
    @Override
    public int insertWxSendMessage(WxSendMessage wxSendMessage)
    {
        wxSendMessage.setCreateTime(DateUtils.getNowDate());
        return wxSendMessageMapper.insertWxSendMessage(wxSendMessage);
    }

    /**
     * 修改企业微信发送消息历史记录
     * 
     * @param wxSendMessage 企业微信发送消息历史记录
     * @return 结果
     */
    @Override
    public int updateWxSendMessage(WxSendMessage wxSendMessage)
    {
        wxSendMessage.setUpdateTime(DateUtils.getNowDate());
        return wxSendMessageMapper.updateWxSendMessage(wxSendMessage);
    }

    /**
     * 删除企业微信发送消息历史记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWxSendMessageByIds(String ids)
    {
        return wxSendMessageMapper.deleteWxSendMessageByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除企业微信发送消息历史记录信息
     * 
     * @param id 企业微信发送消息历史记录ID
     * @return 结果
     */
    @Override
    public int deleteWxSendMessageById(Long id)
    {
        return wxSendMessageMapper.deleteWxSendMessageById(id);
    }

    /**
     * @description: 自提未提货企微消息通知
     * @param:
     * @return:
     * @author: njs
     * @date: 2023/5/15 11:27
     */
    @Override
    public  void sendWxTmsMessage() {
        Map<String, Map<String, Object>> tmsMap =  batchSignForMapper.selectDeliverySelf();
        if(tmsMap !=null && tmsMap.size()>0){
            //自提提货通知
            String temple = configService.selectConfigByKey("wx.notification.self.factory");
            for (String customerId:tmsMap.keySet()
                 ) {
                String user =null;
                String deliverNumber =null;
                String orderFormat =null;
                String customerName = userMapper.getCustomNameByCode(customerId);
                Map<String, Map<String,Object>> maps = userMapper.selectLoginNamesByOrgCodeSelf(customerId);
                Iterator<Map.Entry<String, Object>> iterator = tmsMap.get(customerId).entrySet().iterator();
                while (iterator.hasNext()){
                    Map.Entry<String, Object> next = iterator.next();
                    if(next.getKey().equals("deliveryNumber")){
                       if(deliverNumber !=null){
                           deliverNumber = deliverNumber+","+next.getValue().toString();
                       }else{
                           deliverNumber=next.getValue().toString();
                       }
                    }
                }
                String empName=userMapper.selectLoginNameByCustomerCode(customerId);
                if(empName !=null){
                    if(maps !=null && maps.size()>0 && maps.get(customerId).get("touser").toString() !=null){
                        user=maps.get(customerId).get("touser").toString()+"|"+empName;
                    }else{
                        user=empName;
                    }

                }else if(maps !=null && maps.size()>0){
                    user=maps.get(customerId).get("touser").toString();
                }
                String CustomerName=customerName+"("+customerId+")";
                String messageDetail = String.format(temple, CustomerName,deliverNumber);
                //消息
                if(user !=null){
                    qyWxSendMessageUtil.sendMsgSelfToCustomer(user,messageDetail, "48");
                }
            }
        }

    }

    /**
     * @description: 业绩进度指标预警邮件/企微消息通知
     * @param:
     * @return:
     * @author: njs
     * @date: 2024/2/4 9:21
     */
    @Override
    public void sendMailAndWxEarlyTucMessage() {
        int year = Calendar.getInstance().get(Calendar.YEAR);
        int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
        String MonthStr = "";
        if(month < 10){
            MonthStr = "0"+month;
        } else {
            MonthStr = String.valueOf(month);
        }
        String startTime = String.valueOf(year)+"01";
        String endTime =  String.valueOf(year)+ MonthStr;
        List<OrderTucEarlyWarning> tucSales = tucEarlyWarningMapper.selectTucGroupSales(startTime,endTime);
        if(tucSales !=null && tucSales.size()>0){
            OrderTucEarlyWarning warning = new OrderTucEarlyWarning();
            List<OrderTucEarlyWarning> earlyWarnings = tucEarlyWarningMapper.selectOrderTucEarlyWarningList(warning);
            if(earlyWarnings !=null && earlyWarnings.size()>0) {
                //比较指标是否有误
                for (OrderTucEarlyWarning early : tucSales
                ) {
                    OrderTucEarlyWarning earlyWarning = tucEarlyWarningMapper.selectOrderTucEarlyWarning(early.getOrgCode());
                    BigDecimal tucNetValue = new BigDecimal(0);
                    if (StringUtils.isNotBlank(early.getOrgNetValue())) {
                        tucNetValue = new BigDecimal(early.getOrgNetValue());
                        tucNetValue = tucNetValue.setScale(0, BigDecimal.ROUND_HALF_UP);
                    }
                    BigDecimal earlyNetValue = new BigDecimal(0);
                    if (StringUtils.isNotBlank(earlyWarning.getOrgNetValue())) {
                        earlyNetValue = new BigDecimal(earlyWarning.getOrgNetValue());
                        earlyNetValue = earlyNetValue.setScale(0, BigDecimal.ROUND_HALF_UP);
                    }
                    if (tucNetValue.compareTo(earlyNetValue) == -1) {
                        sendQw(tucNetValue,earlyNetValue,1,early.getOrgCode(),early.getOrgName());
                        sendMail(tucNetValue,earlyNetValue,1,early.getOrgCode(),early.getOrgName());
                        tucEarlyWarningMapper.deleteOrderTucEarlyWarning();
                        break;
                    }
                    BigDecimal tucTarget = new BigDecimal(0);
                    if (StringUtils.isNotBlank(early.getOrgTarget())) {
                        tucTarget = new BigDecimal(early.getOrgTarget());
                        tucTarget = tucTarget.setScale(0, BigDecimal.ROUND_HALF_UP);
                    }
                    BigDecimal earlyTarget = new BigDecimal(0);
                    if (StringUtils.isNotBlank(earlyWarning.getOrgTarget())) {
                        earlyTarget = new BigDecimal(earlyWarning.getOrgTarget());
                        earlyTarget = earlyTarget.setScale(0, BigDecimal.ROUND_HALF_UP);
                    }
                    if (tucTarget.compareTo(earlyTarget) == -1) {
                        sendQw(tucTarget,earlyTarget,2,early.getOrgCode(),early.getOrgName());
                        sendMail(tucTarget,earlyTarget,2,early.getOrgCode(),early.getOrgName());
                        tucEarlyWarningMapper.deleteOrderTucEarlyWarning();
                        break;
                    }
                    early.setUpdateTime(DateUtils.getNowDate());
                    tucEarlyWarningMapper.updateOrderTucEarlyWarning(early);

                }
            }else{
                tucEarlyWarningMapper.batchInsert(tucSales);
            }
        }
    }

    public Set<String> getConfigValueSet(String dictType){
        Set<String> res = new HashSet<>();
        List<SysDictData> dictDatas = sysDictDataService.selectSimpleDictDataByType(dictType);
        for(SysDictData d: dictDatas){
            res.add(d.getDictValue());
        }
        return res;
    }

    public String getConfigValueUserAccount(String dictType){
        String res = "";
        List<SysDictData> dictDatas = sysDictDataService.selectSimpleDictDataByType(dictType);
        if(dictDatas !=null && dictDatas.size()>0){
            for(int i=0;i<dictDatas.size();i++){
                if(i == 0){
                    res = dictDatas.get(0).getDictValue();
                }else{
                    res = res+"|"+dictDatas.get(i).getDictValue();
                }
            }
        }
        return res;
    }
    void sendQw(BigDecimal tucValue,BigDecimal oldValue,int type,String orgCode,String orgName){
        String sendQwUser = getConfigValueUserAccount("sendQwEarlyWarning");
        if(sendQwUser !=null && !("").equals(sendQwUser) ){
            if(type == 1 ){
                String messageDetail="数据源业绩指标预警：原已复核："+oldValue+"  本次已复核："+tucValue+"  已复核累计减少，请排查是否有误，该组织名称："+orgName+"  组织编码："+orgCode;
                qyWxSendMessageUtil.sendMsgSelfToCustomer(sendQwUser,messageDetail, "66");
            }
            if(type == 2){
                String messageDetail="数据源业绩指标预警：原预算目标："+oldValue+"  本次预算目标："+tucValue+"   预算目标累计减少，请排查是否有误，该组织名称："+orgName+"  组织编码："+orgCode;
                qyWxSendMessageUtil.sendMsgSelfToCustomer(sendQwUser,messageDetail, "66");
            }
        }

    }
    void sendMail(BigDecimal tucValue,BigDecimal oldValue,int type,String orgCode,String orgName){
        // 1. 创建参数配置, 用于连接邮件服务器的参数配置
        Properties props = new Properties();                    // 参数配置
        props.setProperty("mail.transport.protocol", "smtp");   // 使用的协议（JavaMail规范要求）
        props.setProperty("mail.smtp.host", myEmailSMTPHost);   // 发件人的邮箱的 SMTP 服务器地址
        props.setProperty("mail.smtp.port", "25");
        props.setProperty("mail.smtp.auth", "true");            // 需要请求认证
        props.setProperty("mail.smtp.starttls.enable", "false");            // 需要请求认证
        props.setProperty("mail.smtp.ssl.enable", "false");            // 需要请求认证
        Set<String> sendEmailUser = getConfigValueSet("sendMailEarlyWarning");
        Session session = Session.getDefaultInstance(props);
        session.setDebug(true);

        for (String email : sendEmailUser
        ) {
            Mail mail = new Mail();
            try {
                mail.setFrom(myEmailAccount);
                mail.setTo(email);
                mail.setSubject("立邦随身邦业绩进度数据源预警");
                mail.setContent(getHtmlUserContextList(tucValue,oldValue,type,orgCode,orgName));
                //mailUtils.sendMailHtml(mailFromUsername, email, "用户账号重复通知", "您好，重复用户信息如下<br>" + userList);
                MimeMessage message = mailUtils.createMimeMessage(session, myEmailAccount, email, mail);

                // 4. 根据 Session 获取邮件传输对象
                Transport transport = session.getTransport();

                transport.connect(myEmailAccount, myEmailPassword);

                // 6. 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
                transport.sendMessage(message, message.getAllRecipients());

                // 7. 关闭连接
                transport.close();
            } catch (NoSuchProviderException e) {
                e.printStackTrace();
            } catch (MessagingException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

}

    public String getHtmlUserContextList(BigDecimal tucValue,BigDecimal oldValue,int type,String orgCode,String orgName) {

        String str = "";
        String html ="";
        if(type == 1){
            str += "<tr>" +
                    "   <td>" + tucValue
                    + "   </td><td>" + oldValue
                    + "   </td><td>" + orgCode
                    + "   </td><td>" + orgName
                    + "   </td><td>" + "已复核累计减少，请排查是否有误"
                    + "   </td>" +
                    "</tr>";
             html = "<table border='1'  Cellspacing='0'>" +
                    "    <tr>" +
                    "        <th>本次已复核</th>" +
                    "        <th>上次已复核</th>" +
                     "       <th>组织编码</th>" +
                     "       <th>组织名称</th>" +
                    "        <th>检测结果</th>" +
                    "    </tr>" +
                    str +
                    "</table>";

        }

        if(type == 2){
            str += "<tr>" +
                    "   <td>" + tucValue
                    + "   </td><td>" + oldValue
                    + "   </td><td>" + orgCode
                    + "   </td><td>" + orgName
                    + "   </td><td>" + "预算目标累计减少，请排查是否有误"
                    + "   </td>" +
                    "</tr>";
            html = "<table border='1'  Cellspacing='0'>" +
                    "    <tr>" +
                    "        <th>本次预算目标</th>" +
                    "        <th>上次预算目标</th>" +
                    "       <th>组织编码</th>" +
                    "       <th>组织名称</th>" +
                    "        <th>检测结果</th>" +
                    "    </tr>" +
                    str +
                    "</table>";

        }

        return html;
    }
}
