package com.fkzy.warn.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.LawCase;
import com.fkzy.warn.model.LitigantInfo;
import com.fkzy.warn.model.MonitorUsers;
import com.fkzy.warn.service.AlarmCaseService;
import com.fkzy.warn.service.LawCaseService;
import com.fkzy.warn.service.LitigantInfoService;
import com.fkzy.warn.service.MonitorUsersService;
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;

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

    @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() {
      String result =  LawResearchUtil.queryMonitorCases(TimeUtil.formatTimeStr(new Date()),null,null);
      if (result==null){
          return;
      }
      JSONObject jsonObject = new JSONObject().parseObject(result);
      if (jsonObject==null){
          return;
      }
      JSONArray arr = jsonObject.getJSONArray("data");
      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());
              });
          }
          saveBatch(alarmCaseList);

          //如果有预警，需要再查司法全量数据有待考虑,先存预警信息
          List<LawCase> lawCaseList = arr.toJavaList(LawCase.class);
          lawCaseService.saveOrUpdateBatch(lawCaseList);
          //保存原告被告
          for (int i = 0; i < arr.size(); i++) {
              List<LitigantInfo> litigantInfoList = new ArrayList<>();
              JSONObject object = arr.getJSONObject(i);
              JSONArray cDsrxx = object.getJSONArray("c_dsrxx");
              String  ajbs = object.getString("n_ajbs");
              String  ah = object.getString("n_ah");
                  for (int j = 0; j < cDsrxx.size(); j++) {
                      JSONObject obj = cDsrxx.getJSONObject(j);
                      LitigantInfo litigantInfo = JSON.parseObject(obj.toString(), LitigantInfo.class);
                      litigantInfo.setNAjbs(ajbs);
                      litigantInfo.setCAh(ah);
                      litigantInfoList.add(litigantInfo);
                  }
              litigantInfoService.remove(new LambdaQueryWrapper<LitigantInfo>()
                  .eq(StringUtils.isNotBlank(ajbs),LitigantInfo::getNAjbs,ajbs)
                  .eq(StringUtils.isNotBlank(ah),LitigantInfo::getCAh,ah)
              );
              litigantInfoService.saveBatch(litigantInfoList);
          };


      }
    }


}
