/*     */ package com.picc.grab.service.impl;
/*     */ 
/*     */ import com.alibaba.fastjson.JSONArray;
/*     */ import com.alibaba.fastjson.JSONObject;
/*     */ import com.baomidou.mybatisplus.mapper.EntityWrapper;
/*     */ import com.baomidou.mybatisplus.mapper.Wrapper;
/*     */ import com.baomidou.mybatisplus.plugins.Page;
/*     */ import com.baomidou.mybatisplus.service.impl.ServiceImpl;
/*     */ import com.picc.grab.config.PiccGrabConfig;
/*     */ import com.picc.grab.domain.*;
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */ import com.picc.grab.entity.grab.dto.BbpAuctionSelectVoList;
/*     */ import com.picc.grab.entity.grab.dto.CrmListDTO;
/*     */ import com.picc.grab.entity.grab.dto.ListData;
/*     */ import com.picc.grab.mapper.CarinfoMapper;
/*     */ import com.picc.grab.mapper.PiccgrabaccountMapper;
/*     */ import com.picc.grab.mapper.PiccgrabparamMapper;
/*     */ import com.picc.grab.mapper.UserMapper;
/*     */ import com.picc.grab.mapper.WtfInfoMapper;
/*     */ import com.picc.grab.mapper.YwypzMapper;
/*     */ import com.picc.grab.service.ICarinfoService;
/*     */ import com.picc.grab.service.IPiccgrabaccountService;
/*     */ import com.picc.grab.util.GrabUtil;
/*     */ import com.picc.grab.util.ImageDownLocad;
/*     */ import com.picc.grab.util.StringUtil;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.scheduling.annotation.Async;
/*     */ import org.springframework.stereotype.Service;
/*     */ 
/*     */ 
/*     */ 
/*     */ @Service
/*     */ public class PiccgrabaccountServiceImpl extends ServiceImpl<PiccgrabaccountMapper, Piccgrabaccount> implements IPiccgrabaccountService {
/*     */   @Autowired
/*     */   private PiccgrabaccountMapper piccgrabaccountMapper;
/*     */   @Autowired
/*     */   private CarinfoMapper carinfoMapper;
/*     */   @Autowired
/*     */   private ICarinfoService iCarinfoService;
/*     */   @Autowired
/*     */   private PiccGrabConfig piccGrabConfig;
/*  56 */   private static final Logger logger = LoggerFactory.getLogger(PiccgrabaccountServiceImpl.class); @Autowired private WtfInfoMapper wtfInfoMapper; @Autowired
/*     */   private YwypzMapper ywypzMapper; @Autowired
/*     */   private PiccgrabparamMapper piccgrabparamMapper; @Autowired
/*     */   private UserMapper userMapper; @Autowired

/*  60 */   private ImageDownLocad imageDownLocad;

            public Page<Piccgrabaccount> pagePiccGrabList(CrmListDTO dto) {
                Page<Piccgrabaccount> page = new Page(dto.pageCurrent(), dto.pageSize());
/*  61 */     List<Piccgrabaccount> carjjList = this.piccgrabaccountMapper.getGrabListPage(page);
/*  62 */     if (carjjList != null && carjjList.size() > 0) {
/*  63 */       page.setRecords(carjjList);
/*     */     }
/*  65 */     return page; }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @Async
/*     */   public void getData(Piccgrabaccount model) {
/*     */     try {
/*  73 */       GrabUtil httpsUtil = new GrabUtil();
/*  74 */       Map<String, Object> param = getListParam(1, "01");
/*  75 */       String str = httpsUtil.getHttpsByCookieConn(this.piccGrabConfig.getListurl(), param, model.getCookie(), model.getToken());
/*  76 */       JSONObject modelList = JSONObject.parseObject(str);
/*  77 */       if (modelList != null && modelList.get("code").equals("200") && modelList.get("data") != null) {
/*  78 */         ListData data = (ListData)JSONObject.parseObject(modelList.get("data").toString(), ListData.class);
/*  79 */         for (BbpAuctionSelectVoList temp : data.getBbpAuctionSelectVoList()) {
/*  80 */           logger.info("抓取竞价编号是:" + temp.getBbpCode());
/*  81 */           SaveCarindo(temp, model);
/*     */         } 
/*  83 */       } else if (modelList == null || modelList.get("code").equals("-1") || modelList.get("code").equals("401")) {
/*  84 */         logger.info("抓取列表:" + model.getFgsId() + ",退出登录,返回结果" + str);
/*  85 */         model.setLogIn(Integer.valueOf(0));
/*  86 */         model.setCookie("");
/*  87 */         model.setToken("");
/*  88 */         this.piccgrabaccountMapper.updateAllColumnById(model);
/*     */       } 
/*  90 */     } catch (Exception ex) {
/*  91 */       logger.info("分公司抓取报错" + model.getFgsId() + ",错误信息:" + ex.toString());
/*     */     } 
/*     */   }
/*     */ 
/*     */   
/*     */   private void SaveCarindo(BbpAuctionSelectVoList bbp, Piccgrabaccount account) {
/*  97 */     EntityWrapper<Carinfo> carEw = new EntityWrapper();
/*  98 */     carEw.eq("jjbh", bbp.getBbpCode());
/*  99 */     List<Carinfo> carinfos = this.carinfoMapper.selectList((Wrapper)carEw);
/* 100 */     if (carinfos.size() != 0) {
/*     */       return;
/*     */     }
/* 103 */     GrabUtil httpsUtil = new GrabUtil();
/* 104 */     Map<String, Object> map = new HashMap<>();
/* 105 */     map.put("id", bbp.getId());
/* 106 */     map.put("reqSource", "saas");
/* 107 */     map.put("selectType", "03");
/* 108 */     String modelStr = httpsUtil.getHttpsByCookieConn(this.piccGrabConfig.getDetailsurl(), map, account
/* 109 */         .getCookie(), account.getToken());
/* 110 */     JSONObject modelList = JSONObject.parseObject(modelStr);
/* 111 */     if (modelList != null && modelList.get("code").equals("200")) {
/* 112 */       JsonToCar(modelList.getJSONObject("data"), account);
/*     */     }
/*     */   }
/*     */   
/*     */   private void JsonToCar(JSONObject data, Piccgrabaccount account) {
/*     */     try {
/* 118 */       Carinfo car = new Carinfo();
/* 119 */       car.setJjbh(data.get("bbpCode").toString());
/* 120 */       car.setFgsId(account.getFgsId());
/* 121 */       car.setCpxh(data.getString("vehkindName"));
/* 122 */       car.setCldfdShen(data.getString("carStopProvinceName"));
/* 123 */       car.setCldfdShi(data.getString("carStopCityName"));
/* 124 */       car.setCph(data.getString("carNo"));
/* 125 */       car.setChexi(data.getString("vehkindName"));
/* 126 */       car.setChexing(data.getString("vehseriName"));
/* 127 */       car.setPinpai(data.getString("brandName"));
/* 128 */       car.setCjh(data.getString("vinNo"));
/* 129 */       car.setFdjh(data.getString("engineNo"));
/* 130 */       car.setSyrxz(Integer.valueOf(data.getString("ownerNature").equals("0") ? 1 : (data.getString("ownerNature").equals("1") ? 2 : 0)));
/* 131 */       car.setCllx(data.getString("vehicleTypeCode").equals("01") ? "1" : "6");
/* 132 */       car.setSyxz(getSyxz(data.getString("carNature")));
/* 133 */       car.setDjzrq(data.getString("recordDate"));
/* 134 */       car.setPailia(data.getString("carDisplacement"));
/* 135 */       car.setPldw("L");
/* 136 */       car.setSgyy(getSgyy(data.getString("damageCause")));
/* 137 */       car.setSfecsg(Integer.valueOf(Integer.parseInt(data.getString("secondaryAccident"))));
/* 138 */       car.setWtqfy(data.getString("otherCost"));
/* 139 */       StringBuffer xjbz = new StringBuffer();
/* 140 */       xjbz.append("【报案时间】" + data.getString("registDate") + "，");
/* 141 */       xjbz.append("【车架号受损】" + getNum(data.getString("vinDamaged")) + "，");
/* 142 */       xjbz.append("【保险金额】" + data.getString("insurance") + "，");
/* 143 */       xjbz.append("【预估修理费】" + data.getString("estimatRepairCost") + "，");
/* 144 */       xjbz.append(data.getString("remark"));
/* 145 */       car.setXjbz(xjbz.toString());
/* 146 */       car.setBsxlx(Integer.valueOf(data.getString("gearboxType").equals("0") ? 2 : 1));
/* 147 */       car.setXjks(StringUtil.strToDate(data.getString("auctionStartTime"), "yyyy-MM-dd HH:mm:ss"));
/* 148 */       car.setXjjs(StringUtil.strToDate(data.getString("auctionEndTime"), "yyyy-MM-dd HH:mm:ss"));
/* 149 */       car.setBjjs(car.getXjjs());
/* 150 */       logger.info(car.getJjbh() + "图片开始抓取");
/*     */       
/* 152 */       JSONArray carImagArry = data.getJSONArray("bbpImgVoList");
/* 153 */       if (carImagArry.size() > 0) {
/* 154 */         List<Carimg> imgList = new ArrayList<>();
/* 155 */         Integer sort = Integer.valueOf(0);
/* 156 */         for (Object temp : carImagArry) {
/* 157 */           JSONObject obj = (JSONObject)temp;
/* 158 */           Carimg imgModel = this.imageDownLocad.findImg(obj.getString("url"));
/* 159 */           if (imgModel != null) {
/* 160 */             Integer integer1 = sort, integer2 = sort = Integer.valueOf(sort.intValue() + 1); imgModel.setSortnum(integer1);
/* 161 */             imgModel.setType(Integer.valueOf(1));
/* 162 */             imgList.add(imgModel);
/*     */           } 
/*     */         } 
/* 165 */         if (imgList.size() > 0) {
/* 166 */           car.setImgList(imgList);
/*     */         }
/*     */       } 
/* 169 */       logger.info(car.getJjbh() + "图片结束抓取");
/* 170 */       car.setWtfId(getWtfId("人保保险").getId());
/* 171 */       car.setWtfName("人保保险");
/*     */       
/* 173 */       EntityWrapper<Ywypz> ywyEw = new EntityWrapper();
/* 174 */       ywyEw.eq("wtf_id", car.getWtfId());
/* 175 */       ywyEw.eq("fgs_id", car.getFgsId());
/* 176 */       List<Ywypz> ywypzs = this.ywypzMapper.selectList((Wrapper)ywyEw);
/* 177 */       String userId = "";
/* 178 */       if (userId != null && ywypzs.size() > 0) {
/* 179 */         userId = ((Ywypz)ywypzs.get(0)).getUserId();
/*     */       }
/* 181 */       car.setXjfs(Integer.valueOf(1));
/* 182 */       car.setRylx(Integer.valueOf(1));
/* 183 */       car.setStatus(Integer.valueOf(0));
/* 184 */       car.setCldfdJtdz(car.getCldfdShen() + car.getCldfdShi());
/* 185 */       logger.info(car.getJjbh() + "车辆数据保存开始");
/* 186 */       boolean istrue = this.iCarinfoService.saveZq(car, userId, account.getSalesman());
/* 187 */       logger.info(car.getJjbh() + "车辆数据保存结束");
/* 188 */       if (istrue) {
/* 189 */         Piccgrabparam piccgrabparam = new Piccgrabparam();
/* 190 */         piccgrabparam.setBbpCode(car.getJjbh());
/* 191 */         piccgrabparam.setMainId(data.getString("mainId"));
/* 192 */         piccgrabparam.setRelationId(data.getString("id"));
/* 193 */         this.piccgrabparamMapper.insert(piccgrabparam);
/* 194 */         logger.info("抓取成功:" + data.getString("vinNo"));
/*     */       } 
/* 196 */     } catch (Exception ex) {
/* 197 */       logger.info("抓取报错:" + data.getString("bbpCode") + ",错误信息:" + ex.toString());
/*     */     } 
/*     */   }
/*     */   
/*     */   private String getNum(String num) {
/* 202 */     String type = "不详";
/* 203 */     switch (num) {
/*     */       case "1":
/* 205 */         type = "是";
/*     */         break;
/*     */       case "2":
/* 208 */         type = "否";
/*     */         break;
/*     */       case "3":
/* 211 */         type = "不详";
/*     */         break;
/*     */     } 
/* 214 */     return type;
/*     */   }
/*     */   
/*     */   private String getSgyy(String dd) {
/* 218 */     String sgyy = "";
/* 219 */     switch (dd) {
/*     */       case "A10029":
/* 221 */         sgyy = "1";
/*     */         break;
/*     */       case "A10101":
/* 224 */         sgyy = "4";
/*     */         break;
/*     */       case "A10001":
/* 227 */         sgyy = "3";
/*     */         break;
/*     */       case "A10003":
/* 230 */         sgyy = "2";
/*     */         break;
/*     */       case "A10014":
/* 233 */         sgyy = "2";
/*     */         break;
/*     */       case "A10061":
/* 236 */         sgyy = "3";
/*     */         break;
/*     */       case "A10031":
/* 239 */         sgyy = "1";
/*     */         break;
/*     */       case "A10004":
/* 242 */         sgyy = "1";
/*     */         break;
/*     */       case "A10011":
/* 245 */         sgyy = "4";
/*     */         break;
/*     */       case "A10012":
/* 248 */         sgyy = "1";
/*     */         break;
/*     */       case "A10013":
/* 251 */         sgyy = "1,2";
/*     */         break;
/*     */       case "A10015":
/* 254 */         sgyy = "2";
/*     */         break;
/*     */       case "A10017":
/* 257 */         sgyy = "1";
/*     */         break;
/*     */       case "A10056":
/* 260 */         sgyy = "4";
/*     */         break;
/*     */       case "A10057":
/* 263 */         sgyy = "4";
/*     */         break;
/*     */       case "A10060":
/* 266 */         sgyy = "4";
/*     */         break;
/*     */       case "A10062":
/* 269 */         sgyy = "1";
/*     */         break;
/*     */       case "A10002":
/* 272 */         sgyy = "1,3";
/*     */         break;
/*     */       case "A10005":
/* 275 */         sgyy = "3";
/*     */         break;
/*     */       case "A10009":
/* 278 */         sgyy = "1";
/*     */         break;
/*     */       case "A10064":
/* 281 */         sgyy = "4";
/*     */         break;
/*     */       case "A10022":
/* 284 */         sgyy = "4";
/*     */         break;
/*     */       case "A10204":
/* 287 */         sgyy = "1";
/*     */         break;
/*     */       case "A10025":
/* 290 */         sgyy = "1";
/*     */         break;
/*     */       case "A10096":
/* 293 */         sgyy = "4";
/*     */         break;
/*     */       case "A10099":
/* 296 */         sgyy = "4";
/*     */         break;
/*     */       case "A10205":
/* 299 */         sgyy = "1";
/*     */         break;
/*     */     } 
/* 302 */     return sgyy;
/*     */   }
/*     */   
/*     */   private Integer getSyxz(String carNature) {
/* 306 */     Integer type = Integer.valueOf(0);
/* 307 */     switch (carNature)
/*     */     { case "01":
/* 309 */         type = Integer.valueOf(1);
/*     */
/* 336 */         return type;case "02": type = Integer.valueOf(4); return type;case "03": type = Integer.valueOf(3); return type;case "04": type = Integer.valueOf(2); return type;case "05": type = Integer.valueOf(3); return type;case "06": type = Integer.valueOf(3); return type;case "07": type = Integer.valueOf(5); return type;case "08": type = Integer.valueOf(6); return type; }  type = Integer.valueOf(0); return type;
/*     */   }
/*     */   
/*     */   private Map getListParam(int pageNom, String state) {
/* 340 */     Map<String, Object> param = new HashMap<>();
/* 341 */     param.put("auctionStatus", state);
/* 342 */     param.put("pageCount", Integer.valueOf(10000000));
/* 343 */     param.put("pageNo", Integer.valueOf(pageNom));
/* 344 */     param.put("reqSource", "saas");
/* 345 */     return param;
/*     */   }
/*     */ 
/*     */   
/*     */   private WtfInfo getWtfId(String name) {
/* 350 */     EntityWrapper<WtfInfo> wtfEw = new EntityWrapper();
/* 351 */     wtfEw.eq("yjwtf", name);
/* 352 */     List<WtfInfo> wtfInfos = this.wtfInfoMapper.selectList((Wrapper)wtfEw);
/* 353 */     if (wtfInfos != null && wtfInfos.size() > 0) {
/* 354 */       return wtfInfos.get(0);
/*     */     }
/* 356 */     return null;
/*     */   }
/*     */ 
/*     */ 
/*     */   
/*     */   public void sendBidPrice(int id) {
/* 362 */     Piccgrabaccount model = (Piccgrabaccount)this.piccgrabaccountMapper.selectById(Integer.valueOf(id));
/* 363 */     if (model == null || model.getIsdel().intValue() == 1 || model.getLogIn().intValue() == 0) {
/*     */       return;
/*     */     }
/* 366 */     WtfInfo wtf = getWtfId("人保保险");
/* 367 */     List<String> sendVehicleList = this.carinfoMapper.findVehicleByTenMinute(wtf.getId(), model.getFgsId());
/* 368 */     if (sendVehicleList != null && sendVehicleList.size() > 0) {
/* 369 */       for (String vehiclId : sendVehicleList) {
/* 370 */         SendVehicleQuoto(vehiclId, model);
/*     */       }
/*     */     }
/*     */   }

    /**
     * 查询已登录的token
     * @return
     */
    @Override
    public List<String> findTokenByLogIn() {
        return piccgrabaccountMapper.findTokenByLogIn();
    }

    @Override
    public List<Zbwt> getZbwt(String carNo) {
        return piccgrabaccountMapper.getZbwt(carNo);
    }

    /*     */
/*     */   private void SendVehicleQuoto(String vehiclid, Piccgrabaccount account) {
/* 376 */     Carinfo carinfo = (Carinfo)this.carinfoMapper.selectById(vehiclid);
/* 377 */     String bjdje = this.carinfoMapper.findBjdjeByCarid(carinfo.getId());
/* 378 */     EntityWrapper<Piccgrabparam> wtfEw = new EntityWrapper();
/* 379 */     wtfEw.eq("bbpCode", carinfo.getJjbh());
/* 380 */     List<Piccgrabparam> piccgrabparams = this.piccgrabparamMapper.selectList((Wrapper)wtfEw);
/* 381 */     if (piccgrabparams != null && piccgrabparams.size() > 0) {
/* 382 */       Piccgrabparam param = piccgrabparams.get(0);
/* 383 */       GrabUtil httpsUtil = new GrabUtil();
/* 384 */       Map<String, Object> map = new HashMap<>();
/* 385 */       map.put("mainId", param.getMainId());
/* 386 */       if (bjdje.equals("0")) {
/* 387 */         bjdje = "1";
/*     */       }
/* 389 */       map.put("bidPrice", bjdje);
/* 390 */       map.put("relationId", param.getRelationId());
/* 391 */       String modelStr = httpsUtil.getHttpsByCookieConn(this.piccGrabConfig.getBidprice(), map, account
/* 392 */           .getCookie(), account.getToken());
/* 393 */       JSONObject modelList = JSONObject.parseObject(modelStr);
/* 394 */       if (modelList.get("code").equals("200") && modelList != null) {
/* 395 */         logger.info("carid:" + carinfo.getId() + "发送报价单成功");
/* 396 */         User user = this.userMapper.findByName(this.piccGrabConfig.getUsername());
/* 397 */         this.iCarinfoService.fsbjdEmail(carinfo.getId(), user.getId());
/* 398 */       } else if (modelList != null && modelList.get("code").equals("401")) {
/* 399 */         logger.info("carid:" + carinfo.getId() + ",退出登录" + modelStr);
/* 400 */         account.setLogIn(Integer.valueOf(0));
/* 401 */         account.setCookie("");
/* 402 */         account.setToken("");
/* 403 */         this.piccgrabaccountMapper.updateAllColumnById(account);
/*     */       } else {
/* 405 */         logger.info("carid:" + carinfo.getId() + "发送报价单失败." + modelStr);
/*     */       } 
/*     */     } 
/*     */   }
/*     */ }