package com.hzya.frame.sysnew.messageManageLog.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.basedao.service.impl.BaseService;
import com.hzya.frame.sysnew.application.dao.ISysApplicationDao;
import com.hzya.frame.sysnew.application.entity.SysApplicationEntity;
import com.hzya.frame.sysnew.messageManageLogBack.detail.server.ISysMessageManageLogDetailBackService;
import com.hzya.frame.sysnew.messageManageLogBack.service.ISysMessageManageLogBackService;
import com.hzya.frame.sysnew.messageManageLogDetail.dao.ISysMessageManageLogDetailDao;
import com.hzya.frame.sysnew.messageManageLogDetail.entity.SysMessageManageLogDetailEntity;
import com.hzya.frame.sys.service.IEsbService;
import com.hzya.frame.sysnew.messageManageLog.dao.ISysMessageManageLogDao;
import com.hzya.frame.sysnew.messageManageLog.entity.SysMessageManageLogEntity;
import com.hzya.frame.sysnew.messageManageLog.entity.SysMessageManageLogPageVo;
import com.hzya.frame.sysnew.messageManageLog.service.ISysMessageManageLogService;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 消息管理日志(SysMessageManageLog)表服务实现类
 *
 * @author makejava
 * @since 2023-08-28 16:48:36
 */
@Service(value="sysMessageManageLogService")
public class SysMessageManageLogServiceImpl extends BaseService<SysMessageManageLogEntity, String> implements ISysMessageManageLogService {

    @Resource
    private ISysMessageManageLogDao sysMessageManageLogDao;
    @Resource
    private IEsbService esbService;
    @Resource
    private ISysApplicationDao sysApplicationDao;
    @Resource
    private ISysMessageManageLogBackService sysMessageManageLogBackService;
    @Resource
    private ISysMessageManageLogDetailDao sysMessageManageLogDetailDao;
    @Resource
    private ISysMessageManageLogDetailBackService sysMessageManageLogDetailBackService;

    @Autowired
    public void setSysMessageManageLogDao(ISysMessageManageLogDao dao) {
        this.sysMessageManageLogDao = dao;
        this.dao = dao;
    }
    /**
     * @Author lvleigang
     * @Description  分页查询，转换显示字段，以及返回VO对象
     * @Date 9:45 上午 2023/8/29
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     **/
    @Override
    public JsonResultEntity queryPagedVo(JSONObject object) {
        SysMessageManageLogEntity entity = getData("jsonStr", object, SysMessageManageLogEntity.class);
        //判断分页
        if (entity == null || entity.getPageNum() == null || entity.getPageSize() == null) {
            return BaseResult.getFailureMessageEntity("分页查询参数不存在");
        }
        PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
        List<SysMessageManageLogPageVo> sysMessageManageEntities = sysMessageManageLogDao.queryListByPageVo(entity);
        PageInfo pageInfo = new PageInfo(sysMessageManageEntities);
        return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
    }
    /**
     * @Author lvleigang
     * @Description  根据id查询对象
     * @Date 9:45 上午 2023/8/29
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     **/
    @Override
    public JsonResultEntity queryEntity(JSONObject object) {
        SysMessageManageLogEntity entity = getData("jsonStr", object, SysMessageManageLogEntity.class);
        //判断主键
        if (entity == null || entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("请先传递请求主键");
        }
        entity = sysMessageManageLogDao.queryOne(entity);
        if (entity != null && entity.getId() != null) {
            return BaseResult.getSuccessMessageEntity("查询数据成功", entity);
        } else {
            return BaseResult.getFailureMessageEntity("未查询到数据");
        }
    }
    /**
     * @Author lvleigang
     * @Description  修改数据
     * @Date 9:45 上午 2023/8/29
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     **/
    @Override
    public JsonResultEntity updateEntity(JSONObject object) {
        SysMessageManageLogEntity entity = getData("jsonStr", object, SysMessageManageLogEntity.class);
        //判断编码是否重复
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("请先填写内容");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //修改
        entity.setModify_user_id(StpUtil.getLoginIdAsString());
        entity.setModify_time(new Date());
        entity.setSts("Y");
        sysMessageManageLogDao.update(entity);
        return BaseResult.getSuccessMessageEntity("保存成功");
    }
    /**
     * @Author lvleigang
     * @Description  重新发送
     * @Date 9:45 上午 2023/8/29
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     **/
    @Override
    public JsonResultEntity resendData(JSONObject object) {
        SysMessageManageLogEntity entity = getData("jsonStr", object, SysMessageManageLogEntity.class);
        //判断主键
        if (entity == null || entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("请先传递请求主键");
        }
        entity = sysMessageManageLogDao.queryOne(entity);
        if (entity != null && entity.getId() != null) {
            if(esbService.doEsbTaskOne(entity)){
                return BaseResult.getSuccessMessageEntity("重新发送成功");
            }else {
                return BaseResult.getFailureMessageEntity("重新发送失败");
            }
        } else {
            return BaseResult.getFailureMessageEntity("重新发送错误，请联系管理员");
        }
    }

    /**
     *
     * @content 每月定时备份数据到备份表中
     * @Param
     * @Return
     * @Author hecan
     * @Date 2024/3/7 10:18
     * **/
    @Override
    public JsonResultEntity queryBackupData(JSONObject jsonObject) {
        try {
            //设置开始时间和结束时间
            LocalDate today = LocalDate.now();// 获取当前日期
            LocalDate lastMonth16th = today.minusMonths(1).withDayOfMonth(16);//减一个月得到上个月的日期
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String create_time = lastMonth16th.format(formatter);//上个月16号的日期

            Calendar calendar = Calendar.getInstance(); // 获取当前时间的Calendar实例
            calendar.add(Calendar.MONTH, -1); // 将月份减一以获取上个月
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));// 设置日期为本月的最大天数，即最后一天
            // 调整时分秒为0，确保只获取日期部分
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            calendar.set(Calendar.MILLISECOND, 0);
            Date date = calendar.getTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String modify_time = sdf.format(date);//上个月最后一天的23：59：59时候
            SysMessageManageLogEntity sysMessageManageLogEntity=new SysMessageManageLogEntity();
            sysMessageManageLogEntity.setCreate_time(DateUtil.parse(create_time));
            sysMessageManageLogEntity.setModify_time(DateUtil.parse(modify_time));
            List<SysMessageManageLogEntity> list = sysMessageManageLogDao.queryBackupData(sysMessageManageLogEntity);
            //开始备份日志数据
            logger.info("=======ESB消息管理日志备份开始，开始传递数据=======");
            Integer integer = sysMessageManageLogBackService.saveSysMessageManageLogBack(list);
            if(integer !=null && integer>1){
                //批量更新备份完成之后的数据
                sysMessageManageLogDao.updateBackupData(list);
                //sysMessageManageLogDao.deleteBackupData(list);
            }

            //开始备份日志明细数据
            SysMessageManageLogDetailEntity sysMessageManageLogDetailEntity=new SysMessageManageLogDetailEntity();
            sysMessageManageLogDetailEntity.setCreate_time(DateUtil.parse(create_time));
            sysMessageManageLogDetailEntity.setModify_time(DateUtil.parse(modify_time));
            //根据日期查询日志明细数据
            List<SysMessageManageLogDetailEntity> listDetail = sysMessageManageLogDetailDao.queryBackupDataDetail(sysMessageManageLogDetailEntity);
            //开始备份日志明细数据
            logger.info("=======ESB消息管理日志明细备份开始，开始传递数据=======");
            Integer integerDetail = sysMessageManageLogDetailBackService.saveSysMessageManageLogDetail(listDetail);
            if(integerDetail !=null && integerDetail>1){
                //日志明细备份成功之后，更新日志明细
                sysMessageManageLogDetailDao.updateBackupDataDetail(listDetail);
                //sysMessageManageLogDetailDao.deleteBackupDataDetail(listDetail);
            }
            return BaseResult.getSuccessMessageEntity("备份成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Author lvleigang
     * @Description  根据id查询对象
     * @Date 9:45 上午 2023/8/29
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     **/
    @Override
    public JsonResultEntity thirdInterfacequeryEntity(JSONObject object) {
        SysMessageManageLogEntity entity = getData("jsonStr", object, SysMessageManageLogEntity.class);
        //判断主键
        if (entity == null || entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("请先传递请求主键");
        }
        entity = sysMessageManageLogDao.queryOne(entity);
        if (entity != null && entity.getId() != null) {
            if(entity.getSendApp()!= null){
                SysApplicationEntity sysApplicationEntity = new SysApplicationEntity();
                sysApplicationEntity.setId(entity.getSendApp());
                sysApplicationEntity = sysApplicationDao.queryOne(sysApplicationEntity);
                if(sysApplicationEntity!= null && sysApplicationEntity.getName() != null){
                    entity.setSendAppName(sysApplicationEntity.getName());
                }
            }
            return BaseResult.getSuccessMessageEntity("查询数据成功", entity);
        } else {
            return BaseResult.getFailureMessageEntity("未查询到数据");
        }
    }
}
