package com.sooka.sponest.monitor.dahua.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.sooka.sponest.monitor.base.service.impl.BaseService;
import com.sooka.sponest.monitor.dahua.controller.DahuaOperateLogWhiteTask;
import com.sooka.sponest.monitor.dahua.domain.CenternonitorTDahuaOperateLog;
import com.sooka.sponest.monitor.dahua.enums.OperateTypeEnum;
import com.sooka.sponest.monitor.dahua.mapper.CenternonitorTDahuaOperateLogMapper;
import com.sooka.sponest.monitor.dahua.service.ICenternonitorTDahuaOperateLogService;
import com.sooka.sponest.monitor.dahua.utils.HttpTestUtils;
import com.sooka.sponest.monitor.dahua.vo.ChannelCodeVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-09
 */
@Slf4j
@Service
public class CenternonitorTDahuaOperateLogServiceImpl extends BaseService implements ICenternonitorTDahuaOperateLogService {
    @Autowired
    private CenternonitorTDahuaOperateLogMapper centernonitorTDahuaOperateLogMapper;
    @Autowired
    private DahuaOperateLogWhiteTask dahuaOperateLogTask;
    @Value("${sooka.dahua_interface_server.loginIp}")
    private String loginIp;
    @Value("${sooka.dahua_interface_server.loginPort}")
    private String loginPort;
    @Value("${sooka.dahua_interface_server.userName}")
    private String userName;
    @Value("${sooka.dahua_interface_server.userPwd}")
    private String userPwd;
    private final List<String> operateTypeWhiteList = Arrays.asList("视频", "云台控制");

    /**
     * 查询【请填写功能名称】
     *
     * @param centernonitorTDahuaOperateLog
     * @return 【请填写功能名称】
     */
    @Override
    public CenternonitorTDahuaOperateLog selectCenternonitorTDahuaOperateLogById(CenternonitorTDahuaOperateLog centernonitorTDahuaOperateLog) {
        return centernonitorTDahuaOperateLogMapper.selectCenternonitorTDahuaOperateLogById(centernonitorTDahuaOperateLog);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param centernonitorTDahuaOperateLog 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<CenternonitorTDahuaOperateLog> selectCenternonitorTDahuaOperateLogList(CenternonitorTDahuaOperateLog centernonitorTDahuaOperateLog) {
        Map<String, Object> params = centernonitorTDahuaOperateLog.getParams();
        params.put("beginTime", params.get("beginTime").toString().split(" ")[0] + " 00:00:00");
        params.put("endTime", params.get("endTime").toString().split(" ")[0] + " 23:59:59");
        centernonitorTDahuaOperateLog.setParams(params);
        return centernonitorTDahuaOperateLogMapper.selectCenternonitorTDahuaOperateLogList(centernonitorTDahuaOperateLog);
    }

    @Override
    public Integer selectExportList(CenternonitorTDahuaOperateLog centernonitorTDahuaOperateLog) {
        return centernonitorTDahuaOperateLogMapper.selectExportList(centernonitorTDahuaOperateLog);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param centernonitorTDahuaOperateLog 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertCenternonitorTDahuaOperateLog(CenternonitorTDahuaOperateLog centernonitorTDahuaOperateLog) {
        centernonitorTDahuaOperateLog.setCreateTime(DateUtils.getNowDate());
        return centernonitorTDahuaOperateLogMapper.insertCenternonitorTDahuaOperateLog(centernonitorTDahuaOperateLog);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param centernonitorTDahuaOperateLog 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateCenternonitorTDahuaOperateLog(CenternonitorTDahuaOperateLog centernonitorTDahuaOperateLog) {
        return centernonitorTDahuaOperateLogMapper.updateCenternonitorTDahuaOperateLog(centernonitorTDahuaOperateLog);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteCenternonitorTDahuaOperateLogByIds(Long[] ids) {
        return centernonitorTDahuaOperateLogMapper.deleteCenternonitorTDahuaOperateLogByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteCenternonitorTDahuaOperateLogById(Long id) {
        return centernonitorTDahuaOperateLogMapper.deleteCenternonitorTDahuaOperateLogById(id);
    }

    /**
     * 同步大华操作日志
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<?> getAllLogData(String startTime, String endTime, String operateTypeValue) throws Exception {
        startTime = startTime + " 00:00:00";
        endTime = endTime + " 23:59:59";
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        R<?> result = HttpTestUtils.getToken(loginIp, Integer.valueOf(loginPort), userName, userPwd);
        if (result.getCode() != 200) {
            return R.fail(result.getMsg());
        }
        //同步之前删除已经入库的记录
        centernonitorTDahuaOperateLogMapper.deleteByDate(startTime, endTime, operateTypeValue);

        String token = result.getData().toString();
        log.info("获取token返回：{}", token);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
        try {
            Date startDate = dateFormat.parse(startTime);
            Date endDate = dateFormat.parse(endTime);
            startTime = sdf.format(startDate);
            endTime = sdf.format(endDate);
        } catch (ParseException e) {
            return R.fail("时间转换错误");
        }
        dahuaOperateLogTask.extractedWhite(token, startTime, endTime, operateTypeValue);
        return R.ok();
    }

    /**
     * 获取设备名称
     */
    @Override
    public List<ChannelCodeVO> selectDeviceNameList() {
        return centernonitorTDahuaOperateLogMapper.selectDeviceNameList();
    }

    /**
     * 批量保存大华云台日志
     */
    @Override
    public int insertBatch(List<CenternonitorTDahuaOperateLog> list) {
        return centernonitorTDahuaOperateLogMapper.insertBatch(list);
    }

    /**
     * 根据时间和类型删除华云台日志
     */
    @Override
    public void deleteByDate(String startTime, String endTime, String operateTypeValue) {
        extracted(startTime, endTime, operateTypeValue);
    }

    @Override
    public void updateFourField(String year) {
        log.info("开始清洗摄像头操作日志数据");
        while (true) {
            List<Long> longs = centernonitorTDahuaOperateLogMapper.selectOperLogLimit(year);
            if (!longs.isEmpty()) {
                CenternonitorTDahuaOperateLog centernonitorTDahuaOperateLog = new CenternonitorTDahuaOperateLog();
                centernonitorTDahuaOperateLog.setYear(year);
                centernonitorTDahuaOperateLog.setIds(longs);
                setSookaDataBase(centernonitorTDahuaOperateLog);
                int i = centernonitorTDahuaOperateLogMapper.updateFourField(centernonitorTDahuaOperateLog);
                log.info("清洗摄像头操作日志数据，影响行数：" + longs.size());
                log.info("清洗摄像头数量：" + i);
            } else {
                log.info("结束清洗摄像头操作日志数据");
                break;
            }
        }
    }

    @Override
    public LinkedHashMap<String, Object> selectOperateLogCountByDeptId(JSONObject jsonObject) {
        return centernonitorTDahuaOperateLogMapper.selectOperateLogCountByDeptId(jsonObject);
    }


    private void extracted(String startTime, String endTime, String operateTypeValue) {
        if (StringUtils.isNotEmpty(operateTypeValue)) {
            centernonitorTDahuaOperateLogMapper.deleteByDate(startTime, endTime, OperateTypeEnum.getValueByCode(operateTypeValue));
        } else {
            for (String var : operateTypeWhiteList) {
                centernonitorTDahuaOperateLogMapper.deleteByDate(startTime, endTime, var);
            }
        }
    }

}
