package com.mosty.lzother.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.dto.yjzl.GetLastYjDto;
import com.mosty.base.model.entity.lzother.FKDB;
import com.mosty.base.model.entity.rh.RhMyhtWarn;
import com.mosty.base.model.entity.sjzx.TbJqCjdb;
import com.mosty.base.model.entity.xinzhi.Czxx;
import com.mosty.base.model.entity.xinzhi.Jjdb;
import com.mosty.base.model.entity.yjzl.TbYjxx;
import com.mosty.base.model.entity.yjzl.TbYjxxDypzYjjb;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.model.vo.sjzx.TbJqVo;
import com.mosty.base.utils.Constant;
import com.mosty.base.utils.DateUtils;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.common.redis.service.RedisService;
import com.mosty.lzother.mapper.CzxxMapper;
import com.mosty.lzother.mapper.FkdbMapper;
import com.mosty.lzother.mapper.JjdbMapper;
import com.mosty.lzother.mapper.RhMyhtWarnMapper;
import com.mosty.lzother.remote.TbBaseAdaptRemoteService;
import com.mosty.lzother.remote.TbSjzxAdaptRemoteService;
import com.mosty.lzother.remote.TbYjzlAdaptRemoteService;
import com.mosty.lzother.service.VLz110TxjService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service
@AllArgsConstructor
public class VLz110TxjServiceImpl extends ServiceImpl<JjdbMapper, Jjdb>
        implements VLz110TxjService {

    private final TbSjzxAdaptRemoteService tbSjzxAdaptRemoteService;
    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;
    private final TbYjzlAdaptRemoteService tbYjzlAdaptRemoteService;
    private final FkdbMapper fkdbMapper;
    private final RhMyhtWarnMapper rhMyhtWarnMapper;
    private final CzxxMapper czxxMapper;
    private final RedisService redisService;

    @Override
    public void syncJqCjd(String time) {
        // 获取最后一条数据的时间
        TbJqCjdb cjd = this.tbSjzxAdaptRemoteService.getLastJqCjdb();
        if (StringUtils.isBlank(time) && ObjectUtils.isNotEmpty(cjd)) {
            time = DateUtils.getQueryDateString(cjd.getRksj(), "02");
        } else {
            time = time + " 00:00:00";
        }
        List<FKDB> list = this.fkdbMapper.selectList(
                new LambdaQueryWrapper<FKDB>()
                        .first("select * from ( ")
                        .last(" ) a  where  to_char(cjsj,'yyyy-MM-dd HH24:mi:ss') > '" + time + "' order by cjsj asc")

        );
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(item -> {
                TbJqCjdb cjdb = new TbJqCjdb();
                BeanUtils.copyProperties(item, cjdb);
                cjdb.setId(UUIDGenerator.getUUID());
                cjdb.setCjdbh(item.getFkdbh());
                cjdb.setJjdbh(item.getJjdbh());
                cjdb.setTzdbh(item.getPjdbh());
                cjdb.setCjdwbh(item.getFkdwdm());
                cjdb.setCjyxm(item.getFkyxm());
                cjdb.setCjybh(item.getFkybh());
                cjdb.setScfksj(item.getFksj());
                cjdb.setCjsj(item.getCjsj01());
                cjdb.setDdxcsj(item.getDdxcsj());
                cjdb.setCjwbsj(item.getXcclwbsj());
                cjdb.setFknr(item.getCjczqk());
                cjdb.setCjqk(item.getCjczqk());
                cjdb.setCljglx(item.getJqcljgdm());
                cjdb.setCljg(item.getJqcljgsm());
                cjdb.setCdry(item.getCdrc());
                cjdb.setCdcl(item.getCdcc());
                cjdb.setXzqh(item.getXzqhdm());
                cjdb.setRksj(item.getCjsj());
                cjdb.setFkdwxzb(item.getFkdwxzb());
                cjdb.setFkdwyzb(item.getFkdwyzb());
                DeptInfoVo dept;
                if (StringUtils.isNotBlank(item.getFkdwdm())) {
                    dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(item.getFkdwdm());
                    if (dept != null) {
                        cjdb.setCjdwmc(dept.getDeptname());
                    }
                }
                cjdb.setXzqh(item.getXzqhdm());
                cjdb.setXtSjly("1");
                cjdb.setCjwbsj(item.getXcclwbsj());
                cjdb.setCljg(item.getJqclztdm());
                // TODO 翻译类型
                cjdb.setCljglx(item.getJqcljgdm());
                this.tbSjzxAdaptRemoteService.addRsCjdb(cjdb);
            });
        }
    }


    @Override
    public void syncHndJq(String time) {
        TbJqVo jqVo = this.tbSjzxAdaptRemoteService.getLastJq();
        if (StringUtils.isBlank(time) && ObjectUtils.isNotEmpty(jqVo)) {
            time = DateUtils.getQueryDateString(jqVo.getBjsj(), "02");
        } else {
            time = time + " 00:00:00";
        }
        List<Jjdb> list = this.baseMapper.selectList(
                new LambdaQueryWrapper<Jjdb>()
                        .last(" where  LENGTH(jqlbdm) > 0  and LENGTH(bjnr) > 0  and LENGTH(gxdwdm) > 0  and to_char(\"bjsj\",'yyyy-MM-dd HH24:mi:ss') > '" + time + "' order by bjsj asc")
        );
        if (!CollectionUtils.isEmpty(list)) {
            for (Jjdb jjdb : list) {
                this.tbSjzxAdaptRemoteService.addLzHndJq(jjdb);
            }
        }
    }

    @Override
    @DS("rh_yj")
    public void rhyj(String time) {
        if (StringUtils.isEmpty(time)) {
            TbYjxx lastYj = this.tbYjzlAdaptRemoteService.getLastYj(new GetLastYjDto().setYjlyid("02"));
            if (ObjectUtils.isNotEmpty(lastYj)) {
                time = DateUtils.getQueryDateString(lastYj.getYjSj(), "02");
            }
        }
        long gjsj = DateUtils.strToDate(StringUtils.isNoneBlank(time) ? time : DateUtils.getSystemDateTimeString(), "02").getTime() / 1000;
        List<RhMyhtWarn> rhMyhtWarns = this.rhMyhtWarnMapper.selectList(new LambdaQueryWrapper<RhMyhtWarn>().gt(RhMyhtWarn::getCreateTime, gjsj));
        if (!CollectionUtils.isEmpty(rhMyhtWarns)) {
            rhMyhtWarns.forEach(this.tbYjzlAdaptRemoteService::addRhyj);
        }
    }


    public void selectJjdbh() {
        List<String> jjdbh = this.tbSjzxAdaptRemoteService.selectJjdbh();
        if (!CollectionUtils.isEmpty(jjdbh)) {
            jjdbh.forEach(v -> {
                Jjdb jjdb = this.baseMapper.selectOne(
                        new LambdaQueryWrapper<Jjdb>()
                                .eq(Jjdb::getJjdbh, v)
                                .isNotNull(Jjdb::getFxdwwd)
                                .isNotNull(Jjdb::getFxdwjd)
                );
                if (ObjectUtils.isNotEmpty(jjdb)) {
                    this.tbSjzxAdaptRemoteService.updateJqJwd(jjdb);
                }

            });
        }
        //补全警情 查询今日接警单号
        String time = DateUtils.getSystemDateTimeString();
        time = DateUtils.getQueryDateString(DateUtils.getNextDate(DateUtils.strToDate(time, "02"), "H", -3), "02");
        List<Jjdb> list = this.baseMapper.selectList(
                new LambdaQueryWrapper<Jjdb>()
                        .last(" where  LENGTH(jqlbdm) > 0  and LENGTH(bjnr) > 0  and LENGTH(gxdwdm) > 0  and to_char(\"bjsj\",'yyyy-MM-dd HH24:mi:ss') >= '" + time + "' order by bjsj asc")
        );
        //判断
        if (!CollectionUtils.isEmpty(list)) {
            for (Jjdb jjdb : list) {
                if (!redisService.hasKey(Constant.JQ + jjdb.getJjdbh())) {
                    this.tbSjzxAdaptRemoteService.addLzHndJq(jjdb);
                }
            }
        }

    }
}
