package com.fkzy.warn.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fkzy.warn.common.constants.EntityConstants;
import com.fkzy.warn.common.util.LawResearchUtil;
import com.fkzy.warn.common.util.TimeUtil;
import com.fkzy.warn.mapper.AlarmCaseMapper;
import com.fkzy.warn.model.AlarmCase;
import com.fkzy.warn.model.InvocationRecord;
import com.fkzy.warn.model.MonitorUsers;
import com.fkzy.warn.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhangjing
 * @date 2023/10/16 18:17
 * @description
 */
@Service
public class AlarmCaseServiceImpl extends ServiceImpl<AlarmCaseMapper, AlarmCase> implements
    AlarmCaseService {

    @Resource
    LawCaseService lawCaseService;
    @Resource
    LitigantInfoService litigantInfoService;
    @Resource
    MonitorUsersService monitorUsersService;
    @Resource
    InvocationRecordService invocationRecordService;


    @Override
    public void saveModel(AlarmCase entity, String ticket) {

    }

    @Override
    public void updateModel(AlarmCase entity, String ticket) {
        this.updateById(entity);
    }

    @Override
    public void deleteModel(List<String> ids) {
        AlarmCase entity = new AlarmCase();
        entity.setIsDel(EntityConstants.DEL);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in("id", ids);
        update(entity, wrapper);
    }

    @Override
    public void getAlarmInfo() {
        InvocationRecord result = LawResearchUtil.queryMonitorCases(TimeUtil.formatTimeStr(new Date()), null, null);
        invocationRecordService.saveModel(result);
        if (result.getApiResult() == null||result.getIsSuccess().equals(0)) {
            return;
        }
        JSONObject jsonObject = new JSONObject().parseObject(result.getApiResult());
        if (jsonObject == null) {
            return;
        }
        JSONArray arr = jsonObject.getJSONArray("data");

        List<MonitorUsers> monitorUsersList = new ArrayList<>();
        if (arr != null && !arr.isEmpty()) {
            List<AlarmCase> alarmCaseList = arr.toJavaList(AlarmCase.class);
            if (alarmCaseList != null && !alarmCaseList.isEmpty()) {
                alarmCaseList.forEach(item -> {
                    MonitorUsers monitorUsers = monitorUsersService.getOne(new LambdaQueryWrapper<MonitorUsers>()
                        .eq(MonitorUsers::getCompanyName, item.getName())
                    );
                    item.setCompanyName(monitorUsers != null ? monitorUsers.getCompanyName() : null);
                    item.setCreditCode(monitorUsers != null ? monitorUsers.getCreditCode() : null);
                    item.setAlarmDate(new Date());
                    monitorUsersList.add(monitorUsers);
                });
            }
            this.saveBatch(alarmCaseList);

            //如果有预警，需要再查司法全量数据有待考虑,先存预警信息
            monitorUsersList.forEach(item -> {
                InvocationRecord lawResult = LawResearchUtil.querySxx(item.getCompanyName(), item.getCreditCode());
                invocationRecordService.saveModel(result);
                JSONObject object = JSONObject.parseObject(lawResult.getApiResult());
                lawCaseService.judicialModelSave(object);
            });


        }
    }


}
