package com.pistonint.bpm.module.cars.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pistonint.bpm.module.cars.dao.CarsMapper;
import com.pistonint.bpm.module.cars.entity.Cars;
import com.pistonint.bpm.module.cars.entity.vo.TaskInfoResp;
import com.pistonint.bpm.module.cars.service.CarsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import static org.springframework.context.annotation.ScopedProxyMode.TARGET_CLASS;
/**
* @description: CarsServiceImpl
* @author: crs
* @create: 2020-10-15 19:37
* Copyright (c) 2019 广东数鼎科技有限公司 All rights reserved.
*/
@RestController
@RequestMapping("cars")
@Scope(proxyMode = TARGET_CLASS)
@Transactional
public class CarsServiceImpl extends ServiceImpl<CarsMapper, Cars> implements CarsService {
private static Logger logger = LoggerFactory.getLogger(CarsServiceImpl.class);
@Autowired
private CarsMapper carsMapper;
@GetMapping("/Info")
public void colorFind(String keywords) {
List<Cars> carsList = carsMapper.selectList(new QueryWrapper<Cars>());
for (int i = 0; i < carsList.size(); i++) {
Cars cars = new Cars();
HttpHeaders headers = new HttpHeaders();
headers.add("X-TOKEN", "eyJhbGciOiJIUzUxMiJ9.eyJ1c2VySWQiOiIxIiwiYWNjb3VudCI6ImFkbWluIiwiaWF0IjoxNjAyNzU2NTc3LCJleHAiOjE2MDI3NzQ1Nzd9.f8Ns1XZ3bYlVW1IFc8U7gaowp-b2HYD2DVpy_Sg3XXw78WJ3Rrx92q0qhkzJhweBYGVWpVI2PTnD-CiWqLXBNA");
RestTemplate restTemplate = new RestTemplate();
String basicTrimMappingUrl = "http://47.112.179.158:6762/work/getTaskInfoBykeywords?keywords=" + carsList.get(i).getCarId();
HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
ResponseEntity<String> resEntity = restTemplate.exchange(basicTrimMappingUrl, HttpMethod.GET, requestEntity, String.class);
logger.info("接口返回:" + resEntity.getBody());
JSONObject jsonObject = JSONObject.parseObject(resEntity.getBody());
TaskInfoResp colorResp = JSONObject.toJavaObject(jsonObject, TaskInfoResp.class);
cars.setCarId(carsList.get(i).getCarId());
if (colorResp.getRCode() == 1) {
cars.setInfo(colorResp.getData().getTaskHistoryList().get(0).getTaskHistoryInfoList().get(0).getTaskState());
}
carsMapper.update(cars, new QueryWrapper<Cars>().eq("car_id", carsList.get(i).getCarId()));
}
}
@GetMapping("/sql")
public void sql() throws IOException {
List<Cars> carsList = carsMapper.selectList(new QueryWrapper<Cars>().eq("info", "车源入库"));
File file = new File("src/main/java/1.sql");
//如果文件不存在,创建一个文件
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(file);
String content="";
for(int i=0 ;i<carsList.size();i++){
content += "UPDATE `bpm`.`tb_schedule` \n" +
"SET `schedule_type` = 1,\n" +
"`user_id` = NULL,\n" +
"`group_id` = NULL,\n" +
"`group_name` = NULL,\n" +
"`user_name` = NULL,\n" +
"`remarks` = NULL,\n" +
"`finish_time` = NULL,\n" +
"`create_time` = '2020-10-10 18:51:04',\n" +
"`update_time` = '2020-10-10 18:51:04',\n" +
"`approval_status` = NULL,\n" +
"`is_cancel` = - 1,\n" +
"`is_schedule` = - 1,\n" +
"`batch_id` = NULL,\n" +
"`publish_time` = NULL,\n" +
"`approval_remarks` = NULL,\n" +
"`is_adjust` = NULL \n" +
"WHERE `car_id` = '"+carsList.get(i).getCarId()+"';\n" ;
}
bw = new BufferedWriter(fw);
bw.write(content);
} catch (IOException e) {
} finally {
bw.close();
fw.close();
}
}
}