package com.zhongdao.jlr.business.service.impl;
import com.util.UtilTools;
import com.util.Vo;
import com.zhongdao.jlr.business.dao.CaseDao;
import com.zhongdao.jlr.business.service.CasesListService;

import com.zhongdao.jlr.business.vo.LoginUserVo;
import com.zhongdao.jlr.business.vo.UserOrderCSVVO;
import com.zhongdao.jlr.business.vo.UserOrderExcelVO;
import com.zhongdao.jlr.util.ExportCSVUtil;
import com.zhongdao.jlr.util.MTextUtils;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.util.RequestUtil.ReturnObjStr;

/**
 * 处理道路救援的业务
 */
@Service(value = "casesListServiceImpl")
public class CasesListServiceImpl implements CasesListService {

    @Autowired
    private CaseDao dao;

    @Override
    public Vo casesListService(String state,HttpServletRequest request, String caseCode, String frame_code, String rescueState, String createTime, String finishTime, String companyName,String plateNumber) {
        return dao.casesListService(state,request,caseCode,frame_code,rescueState,createTime,finishTime,companyName,plateNumber);
    }

    @Override
    public Vo caseEnterQuery(String state,HttpServletRequest request, String caseCode, String frame_code, String createTime, String finishTime) {
        return dao.caseEnterQuery(state,request,caseCode,frame_code,createTime,finishTime);
    }

    @Override
    public Vo caseEnterInfo(HttpServletRequest request, String caseCode,String tid) {
        return dao.caseEnterInfo(request,caseCode,tid);
    }

    @Override
    public JSONObject getTroubleCode(HttpServletRequest request) throws Exception {
        return dao.getTroubleCode(request);
    }

    @Override
    public JSONObject caseDetailSave(HttpServletRequest request) throws Exception {
        return dao.caseDetailSave(request);
    }

    @Override
    public JSONObject exportData(HttpServletRequest request) throws Exception {
        //获取参数
        String caseCode = request.getParameter("caseCode");
        String frame_code = request.getParameter("frame_code");
        String createTime = request.getParameter("createTime");
        String finishTime = request.getParameter("finishTime");
        String state = request.getParameter("state");
        //声明一个报表集合用来保存工单信息
        List listExcel = new ArrayList();
        String userName = null;
        //获取用户信息
        LoginUserVo logininfo = (LoginUserVo) request.getSession().getAttribute("logininfo");
        if (logininfo != null) {
            userName = logininfo.getLogin_name().toString();
        }
        //创建sql
        StringBuffer sql=null;
        if("0".equals(state)){
            sql = new StringBuffer("select DISTINCT u.id uid,u.tid tid,u.code orderCode,u.create_time " +
                    "create_time,u.vin_no vin_no,s.name name, " +
                    "s.code code,b.name serviceName,u.model model,u.plate_number plate_number,m.start_time start_time,j.level_name levelName," +
                    "u.finish_time finish_time,t.level_name levelNames,d.name dName,d.brand_name brandName,e.dms_code dms_code," +
                    "e.trouble_reason trouble_reason,e.mileage mileage,e.dphm dphm,e.arrive_time arrive_time,e.remaer remaer," +
                    "e.choice_type choice_type,e.trouble_code trouble_code,e.repair repair,e.ref_time ref_time,e.finish_time finishes_time" +
                    ",e.hand_time hand_time,e.use_time use_time from " +
                    "(select u.code code,u.create_time,u.vin_no,u.order_status order_status," +
                    "u.service_id,t.service_supplier_id,u.contract_id contract_id,u.contract_destination_id destination_code," +
                    "u.model model,u.plate_number plate_number,u.member_service_group_id member_service_group_id,u.finish_time finish_time," +
                    "u.malfunction_level1 malfunction_level1,u.malfunction_level2 malfunction_level2,u.id id,u.contract_destination_id contract_destination_id,t.id tid " +
                    "from user_order_his u " +
                    "left join task_order_his t on t.user_order_id=u.id " +
                    "union select u.code code,u.create_time create_time,u.vin_no vin_no," +
                    "u.order_status order_status,u.service_id service_id " +
                    ",t.service_supplier_id service_supplier_id,u.contract_id contract_id,u.contract_destination_id destination_code," +
                    "u.model model,u.plate_number plate_number,u.member_service_group_id member_service_group_id,u.finish_time finish_time," +
                    "u.malfunction_level1 malfunction_level1,u.malfunction_level2 malfunction_level2,u.id id,u.contract_destination_id contract_destination_id,t.id tid " +
                    "from user_order u " +
                    "left join task_order t on t.user_order_id=u.id " +
                    ") u " +
                    "left join supplier s on u.service_supplier_id = s.id " +
                    "left join base_service b on b.id = u.service_id " +
                    "left join contract_member_service_group m on u.member_service_group_id=m.id " +
                    "left join contract_trouble_level j on j.id = u.malfunction_level1 " +
                    "left join contract_trouble_level t on t.id = u.malfunction_level2 " +
                    "left join jlr_epqr w on w.order_id = u.id " +
                    "left join contract_destination d on d.id = u.contract_destination_id " +
                    "left join jlr_epqr e on e.task_order_id = u.tid " +
                    "where b.id in (1041,1050,1500,1501,1520,1521) and " +
                    "u.order_status in (21,23,25,28,31) " +
                    "and u.contract_id in (select contract_id from jlr_contract_visual where jlr_user_id =" + logininfo.getId() + ")");
        }else{
            sql = new StringBuffer("select DISTINCT u.id uid,u.tid tid,u.code orderCode,u.create_time " +
                    "create_time,u.vin_no vin_no,s.name name, " +
                    "s.code code,b.name serviceName,u.model model,u.plate_number plate_number,m.start_time start_time,j.level_name levelName," +
                    "u.finish_time finish_time,t.level_name levelNames,d.name dName,d.brand_name brandName,e.dms_code dms_code," +
                    "e.trouble_reason trouble_reason,e.mileage mileage,e.dphm dphm,e.arrive_time arrive_time,e.remaer remaer," +
                    "e.choice_type choice_type,e.trouble_code trouble_code,e.repair repair,e.ref_time ref_time,e.finish_time finishes_time" +
                    ",e.hand_time hand_time,e.use_time use_time from " +
                    "(select u.code code,u.create_time,u.vin_no,u.order_status order_status," +
                    "u.service_id,t.service_supplier_id,u.contract_id contract_id,u.contract_destination_id destination_code," +
                    "u.model model,u.plate_number plate_number,u.member_service_group_id member_service_group_id,u.finish_time finish_time," +
                    "u.malfunction_level1 malfunction_level1,u.malfunction_level2 malfunction_level2,u.id id,u.contract_destination_id contract_destination_id,t.id tid " +
                    "from user_order_his u " +
                    "left join task_order_his t on t.user_order_id=u.id " +
                    "union select u.code code,u.create_time create_time,u.vin_no vin_no," +
                    "u.order_status order_status,u.service_id service_id " +
                    ",t.service_supplier_id service_supplier_id,u.contract_id contract_id,u.contract_destination_id destination_code," +
                    "u.model model,u.plate_number plate_number,u.member_service_group_id member_service_group_id,u.finish_time finish_time," +
                    "u.malfunction_level1 malfunction_level1,u.malfunction_level2 malfunction_level2,u.id id,u.contract_destination_id contract_destination_id,t.id tid " +
                    "from user_order u " +
                    "left join task_order t on t.user_order_id=u.id " +
                    ") u " +
                    "left join supplier s on u.service_supplier_id = s.id " +
                    "left join base_service b on b.id = u.service_id " +
                    "left join contract_member_service_group m on u.member_service_group_id=m.id " +
                    "left join contract_trouble_level j on j.id = u.malfunction_level1 " +
                    "left join contract_trouble_level t on t.id = u.malfunction_level2 " +
                    "left join jlr_epqr w on w.order_id = u.id " +
                    "left join contract_destination d on d.id = u.contract_destination_id " +
                    "left join jlr_epqr e on e.task_order_id = u.tid " +
                    "where b.id in (1041,1050,1500,1501,1520,1521) and " +
                    "u.order_status in (21,23,25,28,31) " +
                    "and u.destination_code in (select contract_destination_id from jlr_destination_visual where jlr_user_id ="+logininfo.getId()+")");
        }
        //sql动态拼接
        if (StringUtils.isNotBlank(frame_code)) {
            sql.append("  and u.vin_no like '%" + frame_code + "%'");
        }
        if (StringUtils.isNotBlank(caseCode)) {
            sql.append("  and u.code like '%" + caseCode + "%'");
        }
        //开始时间
        if (StringUtils.isNotBlank(createTime)) {
            sql.append("  and u.create_time >= '"+createTime+"'");
        }
        // 结束时间
        if (StringUtils.isNotBlank(finishTime)) {
            sql.append("  and u.create_time <= '"+finishTime+"'");
        }
        sql.append(" order by u.create_time desc");
        JSONObject jsonObject = new JSONObject();
        //获取进店工单案件详情信息
        List list = dao.getRsBySql(sql.toString());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (list != null && list.size() > 0) {
            UserOrderExcelVO header = new UserOrderExcelVO("CLUB_ID_NUMBER", "BREAKDOWN_DATE", "VIN", "CURRENT_MILEAGE", "COMPONENT_DESCRIPTION",
                    "JLR_COMPONENT", "FAULT_DESCRIPTION", "VRT_CODE", "VFG_CODE", "COMPLETION_DESCRIPTION", "VERBATIM_COMMENTS", "INITIAL_DIAGNOSED_FAULT",
                    "ATTENDING_RESOURCE", "ACCEPTED", "SUPPRESS", "SHORT_VIN", "CTRY_OF_INCIDENT", "DEALER_CODE_REPAIR", "DEALER_NAME_REPAIR");
            listExcel.add(header);
            //遍历获取到的进店工单信息并将其放入excel表中
            for (int i = 0; i < list.size(); i++) {
                Object[] objects = (Object[]) list.get(i);
                UserOrderExcelVO userOrderExcelVO = new UserOrderExcelVO();
                userOrderExcelVO.setClubIdNumber(ReturnObjStr(objects[2]));
                userOrderExcelVO.setCtryOfIncident("CHN");
                userOrderExcelVO.setVerbatimComments(ReturnObjStr(objects[21]));
                userOrderExcelVO.setBreakdownDate(sdf.format(sdf.parse(ReturnObjStr(objects[3]))));
                userOrderExcelVO.setCurrentMileage(ReturnObjStr(objects[18]));
                userOrderExcelVO.setAttendingResource("sino");
                userOrderExcelVO.setDealerCodeRepair("sino");
                userOrderExcelVO.setDealerNameRepair("中道救援");
                userOrderExcelVO.setAttendingResource(ReturnObjStr(objects[6]));
                userOrderExcelVO.setDealerCodeRepair(ReturnObjStr(objects[15]));
                userOrderExcelVO.setDealerNameRepair(ReturnObjStr(objects[14]));
                String vin = ReturnObjStr(objects[4]);
                userOrderExcelVO.setVin(vin);
                userOrderExcelVO.setShortVin(vin.length() > 6 ? vin.substring(vin.length() - 6) : vin);
                userOrderExcelVO.setJlrComponent("");
                userOrderExcelVO.setAccepted("");
                userOrderExcelVO.setSuppress("");
                userOrderExcelVO.setComponentDescription(ReturnObjStr(objects[23]));
                userOrderExcelVO.setInitialDiagnosedFault(ReturnObjStr(objects[12]));
                userOrderExcelVO.setFaultDescription(MTextUtils.getReason(ReturnObjStr(objects[17])));
                userOrderExcelVO.setVrtCode("");
                userOrderExcelVO.setVfgCode("");
                userOrderExcelVO.setCompletionDescription(MTextUtils.getRepairResult(ReturnObjStr(objects[24])));
                listExcel.add(userOrderExcelVO);
            }
            //格式化日期
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String dateTime = simpleDateFormat.format(new Date());
            //创建导出路径
            String path = request.getRealPath("attached") + File.separator
                    + userName + File.separator + "EPQR_" + dateTime + ".xlsx";
            String webUrl = UtilTools.getWebAddress(request) + File.separator
                    + "attached" + File.separator + userName + File.separator + "EPQR_" + dateTime + ".xlsx";
            boolean b = new UserOrderExcelVO().saveAll(listExcel, path);
            if (b) {
                jsonObject.put("result", "0");// 0:成功，1失败
                jsonObject.put("msg", "导出成功！");
                jsonObject.put("webUrl", webUrl);
            }
        }
        return jsonObject;
    }

    @Override
    public JSONObject exportCSVData(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //获取参数
        String caseCode = request.getParameter("caseCode");
        String frame_code = request.getParameter("frame_code");
        String createTime = request.getParameter("createTime");
        String finishTime = request.getParameter("finishTime");
        String state = request.getParameter("state");
        //声明一个报表集合用来保存工单信息
        List listExcel = new ArrayList();
        String userName = null;
        //获取用户信息
        LoginUserVo logininfo = (LoginUserVo) request.getSession().getAttribute("logininfo");
        if (logininfo != null) {
            userName = logininfo.getLogin_name().toString();
        }
        //创建sql
        StringBuffer sql=null;
        if("0".equals(state)){
            sql = new StringBuffer("select DISTINCT u.id uid,u.tid tid,u.code orderCode,u.create_time " +
                    "create_time,u.vin_no vin_no,s.name name, " +
                    "s.code code,b.name serviceName,u.model model,u.plate_number plate_number,m.start_time start_time,j.level_name levelName," +
                    "u.finish_time finish_time,t.level_name levelNames,d.name dName,d.brand_name brandName,e.dms_code dms_code," +
                    "e.trouble_reason trouble_reason,e.mileage mileage,e.dphm dphm,e.arrive_time arrive_time,e.remaer remaer," +
                    "e.choice_type choice_type,e.trouble_code trouble_code,e.repair repair,e.ref_time ref_time,e.finish_time finishes_time" +
                    ",e.hand_time hand_time,e.use_time use_time from " +
                    "(select u.code code,u.create_time,u.vin_no,u.order_status order_status," +
                    "u.service_id,t.service_supplier_id,u.contract_id contract_id,u.contract_destination_id destination_code," +
                    "u.model model,u.plate_number plate_number,u.member_service_group_id member_service_group_id,u.finish_time finish_time," +
                    "u.malfunction_level1 malfunction_level1,u.malfunction_level2 malfunction_level2,u.id id,u.contract_destination_id contract_destination_id,t.id tid " +
                    "from user_order_his u " +
                    "left join task_order_his t on t.user_order_id=u.id " +
                    "union select u.code code,u.create_time create_time,u.vin_no vin_no," +
                    "u.order_status order_status,u.service_id service_id " +
                    ",t.service_supplier_id service_supplier_id,u.contract_id contract_id,u.contract_destination_id destination_code," +
                    "u.model model,u.plate_number plate_number,u.member_service_group_id member_service_group_id,u.finish_time finish_time," +
                    "u.malfunction_level1 malfunction_level1,u.malfunction_level2 malfunction_level2,u.id id,u.contract_destination_id contract_destination_id,t.id tid " +
                    "from user_order u " +
                    "left join task_order t on t.user_order_id=u.id " +
                    ") u " +
                    "left join supplier s on u.service_supplier_id = s.id " +
                    "left join base_service b on b.id = u.service_id " +
                    "left join contract_member_service_group m on u.member_service_group_id=m.id " +
                    "left join contract_trouble_level j on j.id = u.malfunction_level1 " +
                    "left join contract_trouble_level t on t.id = u.malfunction_level2 " +
                    "left join jlr_epqr w on w.order_id = u.id " +
                    "left join contract_destination d on d.id = u.contract_destination_id " +
                    "left join jlr_epqr e on e.task_order_id = u.tid " +
                    "where b.id in (1041,1050,1500,1501,1520,1521) and " +
                    "u.order_status in (21,23,25,28,31) " +
                    "and u.contract_id in (select contract_id from jlr_contract_visual where jlr_user_id =" + logininfo.getId() + ")");
        }else{
            sql = new StringBuffer("select DISTINCT u.id uid,u.tid tid,u.code orderCode,u.create_time " +
                    "create_time,u.vin_no vin_no,s.name name, " +
                    "s.code code,b.name serviceName,u.model model,u.plate_number plate_number,m.start_time start_time,j.level_name levelName," +
                    "u.finish_time finish_time,t.level_name levelNames,d.name dName,d.brand_name brandName,e.dms_code dms_code," +
                    "e.trouble_reason trouble_reason,e.mileage mileage,e.dphm dphm,e.arrive_time arrive_time,e.remaer remaer," +
                    "e.choice_type choice_type,e.trouble_code trouble_code,e.repair repair,e.ref_time ref_time,e.finish_time finishes_time" +
                    ",e.hand_time hand_time,e.use_time use_time from " +
                    "(select u.code code,u.create_time,u.vin_no,u.order_status order_status," +
                    "u.service_id,t.service_supplier_id,u.contract_id contract_id,u.contract_destination_id destination_code," +
                    "u.model model,u.plate_number plate_number,u.member_service_group_id member_service_group_id,u.finish_time finish_time," +
                    "u.malfunction_level1 malfunction_level1,u.malfunction_level2 malfunction_level2,u.id id,u.contract_destination_id contract_destination_id,t.id tid " +
                    "from user_order_his u " +
                    "left join task_order_his t on t.user_order_id=u.id " +
                    "union select u.code code,u.create_time create_time,u.vin_no vin_no," +
                    "u.order_status order_status,u.service_id service_id " +
                    ",t.service_supplier_id service_supplier_id,u.contract_id contract_id,u.contract_destination_id destination_code," +
                    "u.model model,u.plate_number plate_number,u.member_service_group_id member_service_group_id,u.finish_time finish_time," +
                    "u.malfunction_level1 malfunction_level1,u.malfunction_level2 malfunction_level2,u.id id,u.contract_destination_id contract_destination_id,t.id tid " +
                    "from user_order u " +
                    "left join task_order t on t.user_order_id=u.id " +
                    ") u " +
                    "left join supplier s on u.service_supplier_id = s.id " +
                    "left join base_service b on b.id = u.service_id " +
                    "left join contract_member_service_group m on u.member_service_group_id=m.id " +
                    "left join contract_trouble_level j on j.id = u.malfunction_level1 " +
                    "left join contract_trouble_level t on t.id = u.malfunction_level2 " +
                    "left join jlr_epqr w on w.order_id = u.id " +
                    "left join contract_destination d on d.id = u.contract_destination_id " +
                    "left join jlr_epqr e on e.task_order_id = u.tid " +
                    "where b.id in (1041,1050,1500,1501,1520,1521) and " +
                    "u.order_status in (21,23,25,28,31) " +
                    "and u.destination_code in (select contract_destination_id from jlr_destination_visual where jlr_user_id ="+logininfo.getId()+")");
        }
        //sql动态拼接
        if (StringUtils.isNotBlank(frame_code)) {
            sql.append("  and u.vin_no like '%" + frame_code + "%'");
        }
        if (StringUtils.isNotBlank(caseCode)) {
            sql.append("  and u.code like '%" + caseCode + "%'");
        }
        //开始时间
        if (StringUtils.isNotBlank(createTime)) {
            sql.append("  and u.create_time >= '"+createTime+"'");
        }
        // 结束时间
        if (StringUtils.isNotBlank(finishTime)) {
            sql.append("  and u.create_time <= '"+finishTime+"'");
        }
        sql.append(" order by u.create_time desc");
        JSONObject jsonObject = new JSONObject();
        //获取进店工单案件详情信息
        List list = dao.getRsBySql(sql.toString());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (list != null && list.size() > 0) {
            UserOrderCSVVO header = new UserOrderCSVVO("CLUB_ID_NUMBER", "BREAKDOWN_DATE", "VIN", "CURRENT_MILEAGE", "COMPONENT_DESCRIPTION",
                    "JLR_COMPONENT", "FAULT_DESCRIPTION", "VRT_CODE", "VFG_CODE", "COMPLETION_DESCRIPTION", "VERBATIM_COMMENTS", "INITIAL_DIAGNOSED_FAULT",
                    "ATTENDING_RESOURCE", "ACCEPTED", "SUPPRESS", "SHORT_VIN", "CTRY_OF_INCIDENT", "DEALER_CODE_REPAIR", "DEALER_NAME_REPAIR");
            listExcel.add(header);
            //遍历获取到的进店工单信息并将其放入excel表中
            for (int i = 0; i < list.size(); i++) {
                Object[] objects = (Object[]) list.get(i);
                UserOrderCSVVO userOrderExcelVO = new UserOrderCSVVO();
                userOrderExcelVO.setClubIdNumber(ReturnObjStr(objects[2]));
                userOrderExcelVO.setCtryOfIncident("CHN");
                userOrderExcelVO.setVerbatimComments(ReturnObjStr(objects[21]));
                userOrderExcelVO.setBreakdownDate(sdf.format(sdf.parse(ReturnObjStr(objects[3]))));
                userOrderExcelVO.setCurrentMileage(ReturnObjStr(objects[18]));
                userOrderExcelVO.setAttendingResource("sino");
                userOrderExcelVO.setDealerCodeRepair("sino");
                userOrderExcelVO.setDealerNameRepair("中道救援");
                userOrderExcelVO.setAttendingResource(ReturnObjStr(objects[6]));
                userOrderExcelVO.setDealerCodeRepair(ReturnObjStr(objects[15]));
                userOrderExcelVO.setDealerNameRepair(ReturnObjStr(objects[14]));
                String vin = ReturnObjStr(objects[4]);
                userOrderExcelVO.setVin(vin);
                userOrderExcelVO.setShortVin(vin.length() > 6 ? vin.substring(vin.length() - 6) : vin);
                userOrderExcelVO.setJlrComponent("");
                userOrderExcelVO.setAccepted("");
                userOrderExcelVO.setSuppress("");
                userOrderExcelVO.setComponentDescription(ReturnObjStr(objects[23]));
                userOrderExcelVO.setInitialDiagnosedFault(ReturnObjStr(objects[12]));
                userOrderExcelVO.setFaultDescription(MTextUtils.getReason(ReturnObjStr(objects[17])));
                userOrderExcelVO.setVrtCode("");
                userOrderExcelVO.setVfgCode("");
                userOrderExcelVO.setCompletionDescription(MTextUtils.getRepairResult(ReturnObjStr(objects[24])));
                listExcel.add(userOrderExcelVO);
            }
            //格式化日期
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String dateTime = simpleDateFormat.format(new Date());
            //创建导出路径
            String path = request.getRealPath("attached") + File.separator
                    + userName + File.separator + "EPQR_" + dateTime + ".csv";
            String webUrl = UtilTools.getWebAddress(request) + File.separator
                    + "attached" + File.separator + userName + File.separator + "EPQR_" + dateTime + ".csv";
            boolean b = ExportCSVUtil.doExport(listExcel, path);
            if (b) {
                jsonObject.put("result", "0");// 0:成功，1失败
                jsonObject.put("msg", "导出成功！");
                jsonObject.put("webUrl", webUrl);
            }
        }
        return jsonObject;
    }

    @Override
    public  net.sf.json.JSONObject completionOfRescue(String code) throws Exception {
        //声明一个解析图片的路径
        String url = "http://192.168.1.220:8888";
        //声明一个对象
        net.sf.json.JSONObject jsonObject=null;
        String sql = "select u.code code,u.vin_no vin_no,u.model model,u.malfunction_remark malfunction_remark," +
                "u.vehicle_point_address vehicle_point_address,u.destination_address destination_address,u.create_time create_time,u.arrive_time arrive_time," +
                "u.finish_time finish_time,s.name supplier_name,s.code supplier_code,b.name name,i.images images from " +
                "(select u.id id,u.code code,u.vin_no vin_no,u.model model,u.malfunction_remark malfunction_remark," +
                "u.vehicle_point_address vehicle_point_address,u.destination_address destination_address,u.create_time create_time,t.arrive_time arrive_time," +
                "t.finish_time finish_time,t.task_status task_status,u.service_id service_id,t.service_supplier_id service_supplier_id from user_order u " +
                "left join task_order t on t.user_order_id = u.id " +
                "union select u.id id,u.code code,u.vin_no vin_no,u.model model,u.malfunction_remark malfunction_remark," +
                "u.vehicle_point_address vehicle_point_address,u.destination_address destination_address,u.create_time create_time,t.arrive_time arrive_time," +
                "t.finish_time finish_time,t.task_status task_status,u.service_id service_id,t.service_supplier_id service_supplier_id from user_order_his u " +
                "left join task_order_his t on t.user_order_id = u.id) u " +
                "left join supplier s on u.service_supplier_id = s.id " +
                "left join base_service b on b.id = u.service_id " +
                "left join order_images i on i.order_id = u.id " +
                "where u.code = '" + code +"' and i.task_status =19001";
        String Hsql="select u.code code,u.vin_no vin_no,u.model model,u.malfunction_remark malfunction_remark," +
                "u.vehicle_point_address vehicle_point_address,u.destination_address destination_address,u.create_time create_time,u.arrive_time arrive_time," +
                "u.finish_time finish_time,s.name supplier_name,s.code supplier_code,b.name name,i.images images from " +
                "(select u.id id,u.code code,u.vin_no vin_no,u.model model,u.malfunction_remark malfunction_remark," +
                "u.vehicle_point_address vehicle_point_address,u.destination_address destination_address,u.create_time create_time,t.arrive_time arrive_time," +
                "t.finish_time finish_time,t.task_status task_status,u.service_id service_id,t.service_supplier_id service_supplier_id from user_order u " +
                "left join task_order t on t.user_order_id = u.id " +
                "union select u.id id,u.code code,u.vin_no vin_no,u.model model,u.malfunction_remark malfunction_remark," +
                "u.vehicle_point_address vehicle_point_address,u.destination_address destination_address,u.create_time create_time,t.arrive_time arrive_time," +
                "t.finish_time finish_time,t.task_status task_status,u.service_id service_id,t.service_supplier_id service_supplier_id from user_order_his u " +
                "left join task_order_his t on t.user_order_id = u.id) u " +
                "left join supplier s on u.service_supplier_id = s.id " +
                "left join base_service b on b.id = u.service_id " +
                "left join order_images i on i.order_id = u.id " +
                "where u.code = '" + code +"' and i.task_status =14001";
        String sqls="select u.code code,u.vin_no vin_no,u.model model,u.malfunction_remark malfunction_remark," +
                "u.vehicle_point_address vehicle_point_address,u.destination_address destination_address,u.create_time create_time,u.arrive_time arrive_time," +
                "u.finish_time finish_time,s.name supplier_name,s.code supplier_code,b.name name from " +
                "(select u.id id,u.code code,u.vin_no vin_no,u.model model,u.malfunction_remark malfunction_remark," +
                "u.vehicle_point_address vehicle_point_address,u.destination_address destination_address,u.create_time create_time,t.arrive_time arrive_time," +
                "t.finish_time finish_time,t.task_status task_status,u.service_id service_id,t.service_supplier_id service_supplier_id from user_order u " +
                "left join task_order t on t.user_order_id = u.id " +
                "union select u.id id,u.code code,u.vin_no vin_no,u.model model,u.malfunction_remark malfunction_remark," +
                "u.vehicle_point_address vehicle_point_address,u.destination_address destination_address,u.create_time create_time,t.arrive_time arrive_time," +
                "t.finish_time finish_time,t.task_status task_status,u.service_id service_id,t.service_supplier_id service_supplier_id from user_order_his u " +
                "left join task_order_his t on t.user_order_id = u.id) u " +
                "left join supplier s on u.service_supplier_id = s.id " +
                "left join base_service b on b.id = u.service_id " +
                "where u.code = '" + code +"'";
        try {
            //声明一个对象
            net.sf.json.JSONObject jsonObjects=null;
            //获取车主签名图片及救援详情信息
            List list = dao.getRsBySql(sql);
            //获取客户接车签名图片及救援信息
            List list1=dao.getRsBySql(Hsql);
            //获取救援详情信息
            List list2=dao.getRsBySql(sqls);
            if (list != null && list.size() > 0) {
                //若两张图片都有则将图片路径放入对象中
                jsonObject=UtilTools.listTojsonObject(list, new String[]{"ORDER_CODE", "VIN_NO", "MODEL", "MAL_REMARK", "START_ADDRESS", "DESTINATION_ADDRESS", "CREATE_TIME","ARRIVE_TIME","FINISH_TIME","NAME","CODE","SERVICE_TIME","URL"});
                if (list1 != null && list1.size() > 0) {
                    jsonObjects=UtilTools.listTojsonObject(list1, new String[]{"ORDER_CODE", "VIN_NO", "MODEL", "MAL_REMARK", "START_ADDRESS", "DESTINATION_ADDRESS", "CREATE_TIME","ARRIVE_TIME","FINISH_TIME","NAME","CODE","SERVICE_TIME","URL"});
                    jsonObject.put("URLS",jsonObjects.get("URL"));
                }else{
                    jsonObject.put("URLS","");
                }
            }else{
                //若两张图片都没有则将救援数据放入对象中
                if (list1 != null && list1.size() > 0) {
                    jsonObject=UtilTools.listTojsonObject(list1, new String[]{"ORDER_CODE", "VIN_NO", "MODEL", "MAL_REMARK", "START_ADDRESS", "DESTINATION_ADDRESS", "CREATE_TIME","ARRIVE_TIME","FINISH_TIME","NAME","CODE","SERVICE_TIME","URL"});
                    jsonObject.put("URLS",jsonObject.get("URL"));
                    jsonObject.put("URL","");
                }else{
                    if(list2 != null && list2.size()>0){
                        jsonObject=UtilTools.listTojsonObject(list2, new String[]{"ORDER_CODE", "VIN_NO", "MODEL", "MAL_REMARK", "START_ADDRESS", "DESTINATION_ADDRESS", "CREATE_TIME","ARRIVE_TIME","FINISH_TIME","NAME","CODE","SERVICE_TIME"});
                        jsonObject.put("URL","");
                        jsonObject.put("URLS","");
                    }
                }
            }
            //截取图片路径动态拼接
            String urls=jsonObject.getString("URL");
            String urll=jsonObject.getString("URLS");
            if(urls.contains("IMAGE_URL_NEW")){
                  String urles=urls.substring(13);
                  jsonObject.put("URL",url+urles);
            }
            if(urll.contains("IMAGE_URL_NEW")){
                String urles=urll.substring(13);
                jsonObject.put("URLS",url+urles);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return jsonObject;
    }
}
