package com.mosty.operation.log.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.common.base.entity.log.SysOperLog;
import com.mosty.common.base.util.DateUtils;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.common.util.PermissionsUtil;
import com.mosty.operation.log.entity.VO.OperlogPage;
import com.mosty.operation.log.mapper.SysOperLogMapper;
import com.mosty.operation.log.service.SysOperLogService;
import com.mosty.operation.log.util.Convert;
import com.mosty.operation.log.util.OkHttpUtils;
import org.apache.catalina.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * 操作日志 服务层处理
 */

@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog>
        implements SysOperLogService {

    @Resource
    private SysOperLogMapper operLogMapper;

    // 新增操作日志
    @Override
    public void insertOperlog(SysOperLog operLog) {
        if (operLog.getOperTime() == null) {
            operLog.setOperTime(new Date());
        }
        System.out.println("operLog：" + JSONObject.toJSONString(operLog.getOperParam()));
        //判断是否上传数据
        if (operLog.getSfsc() == 1 && 0 == operLog.getStatus() && StringUtils.isNotBlank(operLog.getOperParam())) {
            String sync = OkHttpUtils.builder().url("http://47.108.232.77:56253/interface/push")
                    .addHeader("Content-Type", "application/json;charset=utf-8")
                    .addParam("ywlx", "ZY")
                    .addParam("url", operLog.getOperUrl())
                    .addParam("token", "46296c279b51420cb64bd5cb6d8ec3d8")
                    .addParam("sbid", "ZY")
                    .addParam("sbzid", "ZY")
                    .addParam("data", JSONObject.parseObject(operLog.getOperParam()))
                    .post(true)
                    .sync();
            operLog.setFhnr(sync);
        }
        operLogMapper.insertOperlog(operLog);
    }

    // 查询系统操作日志集合
    @Override
    public List<SysOperLog> selectOperLogList(SysOperLog operLog) {
        return operLogMapper.selectOperLogList(operLog);
    }

    // 批量删除系统操作日志
    @Override
    public void deleteOperLogByIds(List<Long> ids) {
        operLogMapper.delete(new LambdaQueryWrapper<SysOperLog>()
                .in(SysOperLog::getOperId, ids));
    }

    // 查询操作日志详细
    @Override
    public SysOperLog selectOperLogById(Long operId) {
        return operLogMapper.selectOperLogById(operId);
    }

    // 清空操作日志
    @Override
    public void cleanOperLog() {
        operLogMapper.cleanOperLog();
    }

    @Override
    public IPage<SysOperLog> selectByPage(OperlogPage dto) {
        LambdaQueryWrapper<SysOperLog> qw = new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotBlank(dto.getTitle()), SysOperLog::getTitle, dto.getTitle())
                .like(StringUtils.isNotBlank(dto.getOperName()), SysOperLog::getOperName, dto.getOperName())
                .eq(Objects.nonNull(dto.getBusinessType()), SysOperLog::getBusinessType, dto.getBusinessType())
                .eq(Objects.nonNull(dto.getStatus()), SysOperLog::getStatus, dto.getStatus())
                .orderByDesc(SysOperLog::getOperId);
        return operLogMapper.selectPage(dto, qw);
    }

    @Override
    public Map<String, Object> statistics(String type) {
        UserInfo user = UserInfoManager.get();
        String useSql = PermissionsUtil.createSql("", user);
        String kssj = null, jssj;
        jssj = DateUtils.getTime();
        if ("1".equals(type)) {
            kssj = DateUtils.getDate() + " 00:00:00";
        } else if ("2".equals(type)) {
            kssj = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",
                    DateUtils.getNextDate(new Date(), "D", -8));
        } else if ("3".equals(type)) {
            kssj = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",
                    DateUtils.getNextDate(new Date(), "D", -31));
        } else if ("4".equals(type)) {
            kssj = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",
                    DateUtils.getNextDate(new Date(), "D", -90));
        }
        Map<String, Object> map = new HashMap<>();
        //  请求成功次数
        map.put("qqcgcs", this.operLogMapper.getSycs(kssj, jssj, useSql, "0"));
        //  请求失败次数
        map.put("qqsbcs", this.operLogMapper.getSycs(kssj, jssj, useSql, "1"));
        //  访问人数
        map.put("fwrs", this.operLogMapper.getFwrs(kssj, jssj, useSql));
        return map;
    }

    @Override
    public List<Map<String, Object>> bmStatistics(String type) {
        UserInfo user = UserInfoManager.get();
        String aUseSql = PermissionsUtil.createSql("a", user);
        String bUseSql = PermissionsUtil.createSql("b", user);
        String kssj = null, jssj;
        jssj = DateUtils.getTime();
        if ("1".equals(type)) {
            kssj = DateUtils.getDate() + " 00:00:00";
        } else if ("2".equals(type)) {
            kssj = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",
                    DateUtils.getNextDate(new Date(), "D", -8));
        } else if ("3".equals(type)) {
            kssj = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",
                    DateUtils.getNextDate(new Date(), "D", -31));
        } else if ("4".equals(type)) {
            kssj = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",
                    DateUtils.getNextDate(new Date(), "D", -90));
        }
        return this.baseMapper.bmStatistics(kssj, jssj, aUseSql, bUseSql);
    }

    @Override
    public List<Map<String, Object>> ryStatistics(String type) {
        UserInfo user = UserInfoManager.get();
        String aUseSql = PermissionsUtil.createSql("a", user);
        String bUseSql = PermissionsUtil.createSql("b", user);
        String kssj = null, jssj;
        jssj = DateUtils.getTime();
        if ("1".equals(type)) {
            kssj = DateUtils.getDate() + " 00:00:00";
        } else if ("2".equals(type)) {
            kssj = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",
                    DateUtils.getNextDate(new Date(), "D", -8));
        } else if ("3".equals(type)) {
            kssj = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",
                    DateUtils.getNextDate(new Date(), "D", -31));
        } else if ("4".equals(type)) {
            kssj = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",
                    DateUtils.getNextDate(new Date(), "D", -90));
        }
        return this.baseMapper.ryStatistics(kssj, jssj, aUseSql, bUseSql);
    }

    @Override
    public List<Map<String, Object>> mkStatistics(String type) {
        UserInfo user = UserInfoManager.get();
        String aUseSql = PermissionsUtil.createSql("a", user);
        String bUseSql = PermissionsUtil.createSql("b", user);
        String kssj = null, jssj;
        jssj = DateUtils.getTime();
        if ("1".equals(type)) {
            kssj = DateUtils.getDate() + " 00:00:00";
        } else if ("2".equals(type)) {
            kssj = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",
                    DateUtils.getNextDate(new Date(), "D", -8));
        } else if ("3".equals(type)) {
            kssj = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",
                    DateUtils.getNextDate(new Date(), "D", -31));
        } else if ("4".equals(type)) {
            kssj = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",
                    DateUtils.getNextDate(new Date(), "D", -90));
        }
        return this.baseMapper.mkStatistics(kssj, jssj, aUseSql, bUseSql);
    }
}