package com.gic.haoban.manage.service.service.fee.impl;

import java.util.Date;
import java.util.List;

import com.gic.commons.util.DateUtil;
import com.gic.haoban.manage.service.dao.mapper.TabHaobanAppOrderMapper;
import com.gic.haoban.manage.service.dao.mapper.WxEnterpriseMapper;
import com.gic.haoban.manage.service.dao.mapper.fee.TabHaobanQywxFeeOrderAccountMapper;
import com.gic.haoban.manage.service.entity.TabHaobanAppOrder;
import com.gic.haoban.manage.service.entity.TabHaobanWxEnterprise;
import com.gic.haoban.manage.service.util.DingUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gic.haoban.manage.service.dao.mapper.fee.TabHaobanQywxFeeOrderMapper;
import com.gic.haoban.manage.service.entity.fee.TabHaobanQywxFeeOrder;
import com.gic.haoban.manage.service.service.fee.HaobanQywxFeeOrderService;

/**
 * 企业微信收费订单(TabHaobanQywxFeeOrder)表服务实现类
 *
 * @author mozhu
 * @since 2022-08-03 16:12:33
 */
@Service
public class HaobanQywxFeeOrderServiceImpl implements HaobanQywxFeeOrderService {
    
    
    private static Logger logger = LogManager.getLogger(HaobanQywxFeeOrderService.class);

    
    @Autowired
    private TabHaobanQywxFeeOrderMapper tabHaobanQywxFeeOrderMapper;
    @Autowired
    private TabHaobanQywxFeeOrderAccountMapper tabHaobanQywxFeeOrderAccountMapper ;
    @Autowired
    private WxEnterpriseMapper wxEnterpriseMapper;
    @Autowired
    private TabHaobanAppOrderMapper tabHaobanAppOrderMapper;

    /**
     * 新增数据
     *
     * @param tabHaobanQywxFeeOrder 实例对象
     * @return 实例对象
     */
    @Override
    public TabHaobanQywxFeeOrder insert(TabHaobanQywxFeeOrder tabHaobanQywxFeeOrder) {
        String wxEnterpriseId = tabHaobanQywxFeeOrder.getWxEnterpriseId() ;
        String orderId = tabHaobanQywxFeeOrder.getOrderId() ;
        TabHaobanQywxFeeOrder tab = this.tabHaobanQywxFeeOrderMapper.selectByOrderId(wxEnterpriseId, orderId) ;
        if(null != tab) {
            logger.info("订单记录已经存在,orderId={}",orderId);
            return tab ;
        }
        this.tabHaobanQywxFeeOrderMapper.insert(tabHaobanQywxFeeOrder);
        return tabHaobanQywxFeeOrder;
    }

    @Override
    public int countExternalContactCount(String wxEnterpriseId) {
        return tabHaobanQywxFeeOrderMapper.countExternalContactCount(wxEnterpriseId);
    }
    
    @Override
    public List<TabHaobanQywxFeeOrder> listOrder(String wxEnterpriseId) {
        return this.tabHaobanQywxFeeOrderMapper.listOrder(wxEnterpriseId);
    }

    @Override
    public TabHaobanQywxFeeOrder selectByOrderId(String wxEnterpriseId, String orderId) {
        return this.tabHaobanQywxFeeOrderMapper.selectByOrderId(wxEnterpriseId, orderId);
    }

    @Override
    public void refund(Long feeOrderId , String wxEnterpriseId , String orderId) {
        this.tabHaobanQywxFeeOrderMapper.refund(feeOrderId);
        this.tabHaobanQywxFeeOrderAccountMapper.refund(wxEnterpriseId,orderId);
    }

    @Override
    public void qywxAppOverTimer(String params) {
        // https://oapi.dingtalk.com/robot/send?access_token=62053271b8f98ac1d7e0695d98639be00ac1963e101f80e022200b5af9632ec7
        List<TabHaobanWxEnterprise> enList = this.wxEnterpriseMapper.listAllNotSelf() ;
        for(TabHaobanWxEnterprise wxEnterprise : enList) {
            String wxEnterpriseId = wxEnterprise.getWxEnterpriseId() ;
            List<TabHaobanAppOrder> orderList = this.tabHaobanAppOrderMapper.listOrderForNotice(wxEnterpriseId) ;
            if(CollectionUtils.isNotEmpty(orderList)) {
                TabHaobanAppOrder order = orderList.get(0) ;
                Date endTime = order.getEndTime() ;
                // 3天内过期
                if(DateUtil.getStartTimeOfDay(endTime).getTime()  - 1000*3600*24*3 <= DateUtil.getStartTimeOfDay().getTime()) {
                    if(DateUtil.getStartTimeOfDay(endTime).getTime()<DateUtil.getStartTimeOfDay().getTime() && DateUtil.daysBetween(DateUtil.getStartTimeOfDay(endTime),DateUtil.getStartTimeOfDay())>1) {
                        continue;
                    }
                    logger.info("应用即将到期={}",wxEnterpriseId);
                    DingUtils.send("企微企业名称："+wxEnterprise.getCorpName()+"-"+wxEnterprise.getOpenCorpid() + "\n应用到期时间：" + DateUtil.dateToStr(endTime,"yyyy-MM-dd HH:mm:ss") , "https://oapi.dingtalk.com/robot/send?access_token=62053271b8f98ac1d7e0695d98639be00ac1963e101f80e022200b5af9632ec7",false);
                }
            }
        }
    }
}
