//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tohi.app.rest;

import com.tohi.common.dto.*;
import com.tohi.common.pwdutil.*;

import java.io.*;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.activation.DataHandler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource;
import org.springframework.stereotype.Component;

@Component
public class pitaskSericeImpl implements pitaskSerice {
    public pitaskSericeImpl() {
        System.out.println("start rest ----" + new Date());
    }

    public Response GetPhoneManage(String paramString1, String paramString2, String paramString3) {
        String result = "[{'result':'true'}]";
        return Response.ok().entity(result).build();
    }

    public Response downloadPA_ROLE(String paramString1,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers helper = new dbhelpers();
        System.out.println("========downloadPA_ROLE========,参数:" + paramString1);
        List<PA_Group_TempModel> list = helper.downloadPA_ROLE(paramString1);
        JSONArray o = JSONArray.fromObject(list);
        String s = o.toString();
        System.out.println(s.replaceAll("name", "Name") + "-----s");
        return Response.ok().entity(s.replaceAll("name", "Name")).build();
    }

    public Response getNFCList(String areaName,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers helper = new dbhelpers();
        List<UdGeneral> list = helper.downloadPA_BSP(areaName,"");
        JSONArray o = JSONArray.fromObject(list);
        return Response.ok().entity(o).build();
    }

    public Response getNFCDetail(String areaNum,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers helper = new dbhelpers();
        List<UdGeneral> list = helper.downloadPA_BSP("",areaNum);
        JSONArray o = JSONArray.fromObject(list);
        return Response.ok().entity(o).build();
    }

    public Response getNFCList(HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers helper = new dbhelpers();
        List<UdGeneral> list = helper.downloadPA_BSP("","");
        JSONArray o = JSONArray.fromObject(list);
        return Response.ok().entity(o).build();
    }

    public Response bindNFC(Map<String,String> nfcMap,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers helper = new dbhelpers();
        String r = helper.bindNFC(nfcMap.get("NFCID"), nfcMap.get("areaNum"));
        return Response.ok().entity(r).build();
    }

    public Response downloadPA_User(String paramString1, String paramString2,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers helper = new dbhelpers();
        System.out.println("========downloadPA_User");
        System.out.println("paramString1:" + paramString1);
        System.out.println("paramString2:" + paramString2);
        List<Phone_C_USER> list = helper.downloadPA_User(paramString1, paramString2);
        JSONArray o = JSONArray.fromObject(list);
        System.out.println("downloadPA_User:(" + o + ")");
        return Response.ok().entity(o).build();
    }

    public Response GetPhoneAPP(String paramString, String paramString1) {
        dbhelpers helper = new dbhelpers();
        String m = helper.GetPhoneAPP(paramString, paramString1);
        System.out.println(m + "---------242");
        return Response.ok().entity(m).build();
    }

    public Response GetNotice(String postId,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers helper = new dbhelpers();
        List<PA_GG> list = helper.GetNotice(postId);
        JSONArray o = JSONArray.fromObject(list);
        return Response.ok().entity(o).build();
    }

    public String GetPItask(String paramString1) {
        String result = "无";
        dbhelpers helper = new dbhelpers();
        result = helper.GetPItask(paramString1);
        return result;
    }

    public String DateTimeNow(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = sdf.format(new Date());
        System.out.println(str + "--str");
        return str;
    }

    public Response downloadPA_ROUTINE_GROUP(String paramString1, String paramString2, String paramString3) {
        dbhelpers helper = new dbhelpers();
        System.out.println(paramString1 + "=" + paramString2 + "==" + paramString3 + "======downloadPA_ROUTINE_GROUP");
        List<Phone_PA_ROUTINE_GROUP> list = helper.downloadPA_ROUTINE_GROUP(paramString1, paramString2);
        JSONArray o = JSONArray.fromObject(list);
        System.out.println("Phone_PA_ROUTINE_GROUP" + o);
        return Response.ok().entity(o).build();
    }

    public Response downloadPA_ROUTINE(String paramString1, String paramString2, String paramString3) {
        System.out.println(paramString1 + "=" + paramString2 + "==" + paramString3 + "====downloadPA_ROUTINE");
        dbhelpers helper = new dbhelpers();
        List<Phone_PA_ROUTINE> list = helper.downloadPA_ROUTINE(paramString1, paramString2);
        JSONArray o = JSONArray.fromObject(list);
        System.out.println("Phone_PA_ROUTINE" + o);
        return Response.ok().entity(o).build();
    }

    public Response UploadingPALS(String strContent) {
        dbhelpers helper = new dbhelpers();

        try {
            strContent = URLDecoder.decode(strContent, "UTF-8");
        } catch (UnsupportedEncodingException var9) {
            var9.printStackTrace();
        }

        Integer upload = helper.methodBls(strContent);
        List<Results> runtime = new ArrayList();
        Results obj = new Results();
        String m = null;
        if (upload == 1) {
            obj.setResult("true");
            obj.setPhoneResult("true");
            m = "{\"PhoneResult\":\"true\",\"result\":\"true\",\"UpdateNow\":\"true\"}";
        } else {
            obj.setResult("false");
            obj.setPhoneResult("false");
//            obj.setUpdateNow("false");
        }

        runtime.add(obj);
        JSONArray o = JSONArray.fromObject(runtime);
        System.out.println("UploadingPA:" + o);
        String s = o.toString();
        s = s.replaceAll("phoneResult", "PhoneResult");
        return Response.ok().entity(m).build();
    }

    public Response UploadingPA(String strContent) {
        System.out.println("========"+strContent);
        dbhelpers helper = new dbhelpers();

        try {
            strContent = URLDecoder.decode(strContent, "UTF-8");
        } catch (UnsupportedEncodingException var9) {
            var9.printStackTrace();
        }

        System.out.println("--------------------------143");
        Integer upload = helper.methodB(strContent);
        System.out.println(upload + "===upload");
        List<Results> runtime = new ArrayList();
        Results obj = new Results();
        String m = null;
        if (upload == 1) {
            obj.setResult("true");
            obj.setPhoneResult("true");
            m = "{\"PhoneResult\":\"true\",\"result\":\"true\",\"UpdateNow\":\"true\"}";
        } else {
            obj.setResult("false");
            obj.setPhoneResult("false");
//            obj.setUpdateNow("false");
        }

        System.out.println(obj.getResult() + "--------------------133");
        runtime.add(obj);
        JSONArray o = JSONArray.fromObject(runtime);
        System.out.println("UploadingPA:" + o);
        String s = o.toString();
        s = s.replaceAll("phoneResult", "PhoneResult");
        System.out.println(m + "--------141");
        return Response.ok().entity(m).build();
    }

    public Response queryPiTask(String personid, String orgid) {
        dbhelpers helper = new dbhelpers();
        List<Phone_PA_ROUTINE> list = helper.downloadPA_ROUTINE(personid, orgid);
        JSONArray o = JSONArray.fromObject(list);
        System.out.println("queryPiTask" + o);
        return Response.ok().entity(o).build();
    }

    public Response checkUser(String loginid, String password,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        String result = "";

        try {
            dbhelpers helper = new dbhelpers();
            String sql = "select personid,password from maxuser where loginid = '" + loginid + "'";
            System.out.println(sql);
            String jsonObj = helper.executeQuery(sql);
            JSONObject o = JSONObject.fromObject(jsonObj);
            JSONObject o1 = (JSONObject)o.get("0");
            if (o1 != null) {
                String o2 = (String)o1.get("PASSWORD");
                PropertiesUtil pu = new PropertiesUtil();
                MXCipherX mx = new MXCipherX(true, pu);
                String spwd = mx.encData(password);
                if (spwd.equalsIgnoreCase(o2)) {
                    result = o1.toString();
                } else {
                    result = "用户名或密码错误";
                }
            } else {
                result = "用户名或密码错误";
            }
        } catch (Exception var13) {
            result = var13.getMessage();
        }

        System.out.println(result);
        return Response.ok().entity(result).build();
    }

    public Response executeUpdate() {
        dbhelpers db = new dbhelpers();
        String sql = " update pi_task set END_TIME = to_date(char(current date)||' '||char(current time),'yyyy/mm/dd hh24:mi:ss')";
        Integer i = db.executeUpdate(sql);
        System.out.println("executeUpdate===" +sql);
        return Response.ok().entity(i).build();
    }

    public Response queryPi_Item_Status(String personid) {
        System.out.println("====downloadPA_ITEM_STATU");
        dbhelpers helper = new dbhelpers();
        List<Phone_PA_ITEM_STATU> list = helper.downloadPA_ITEM_STATU(personid);
        JSONArray o = JSONArray.fromObject(list);
        System.out.println("Phone_PA_ITEM_STATU" + o);
        return Response.ok().entity(o).build();
    }

    public Response Login(Map<String,String> User,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers helper = new dbhelpers();
        Phone_C_USER user = new Phone_C_USER();
        String result = helper.login(User.get("loginid"), User.get("password"));
        if (result == "") {
            dbhelpers helper1 = new dbhelpers();
            user = helper1.getUserinfo(User.get("loginid"), User.get("password"));
            JSONObject o = JSONObject.fromObject(user);
            return Response.ok().entity(o).build();
        }

        return Response.ok().entity(result).build();
    }

    public Response executeUpdate(List<String> Listsql) {
        dbhelpers db = new dbhelpers();
        Integer i = db.executeUpdate(Listsql);
        System.out.println("executeUpdate===" + i);
        return Response.ok().entity(i).build();
    }

    public Response uploadFileList(List<Attachment> attachments, HttpServletRequest request) {
        System.out.println("235-----------------------图片");
        dbhelpers helper = new dbhelpers();
        String result = helper.uploadFileList(attachments, request);
        return Response.ok().entity(result).build();
    }

    public Response downloadPA_Createpitask(String paramString1, String paramString2, String paramString3) {
        System.out.println(paramString1 + "==========" + paramString2 + "=====" + paramString3);
        String result = "[{'result':'true'}]";
        dbhelpers helper = new dbhelpers();
        String jsonObj = helper.downloadPerson(paramString3);
        System.out.println("=========266===" + jsonObj);
        if (!"0".equalsIgnoreCase(jsonObj)) {
            System.out.println("=======================275");
            helper.downloadPA_Createpitask(paramString1, paramString2);
        } else {
            result = "[{'result':'false'}]";
        }

        return Response.ok().entity(result).build();
    }

    public Response CreaUdbug(List<Attachment> attachments, HttpServletRequest request) {
        System.out.println("253----------CreaUdbug-------------图片");
        dbhelpers helper = new dbhelpers();
        String result = helper.CreaUdbug(attachments, request);
        System.out.println(result + "=========256");
        return Response.ok().entity(result).build();
    }

    public Response getTaskByPostId(String postId, String status,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers dbhelpers = new dbhelpers();
       Map<String,String> routeMap = new HashMap();
       routeMap.put("postId",postId);
       routeMap.put("status",status);
        List<PI_TASK> taskList = dbhelpers.getTaskList(routeMap);
        JSONArray o = JSONArray.fromObject(taskList);
        return Response.ok().entity(o).build();
    }

    public Response getRouteTaskByPostId(String postId, String status,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers dbhelpers = new dbhelpers();
        List<PI_TASK> taskList = dbhelpers.getTaskList(postId,status);
        JSONArray o = JSONArray.fromObject(taskList);
        return Response.ok().entity(o).build();
    }

    public Response getTaskByRouteNum(Map<String,String> routMap,HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        dbhelpers dbhelpers = new dbhelpers();
        List<PI_TASK> taskList = dbhelpers.getTaskList(routMap);
        JSONArray o = JSONArray.fromObject(taskList);
        return Response.ok().entity(o).build();
    }

    public Response getAreaByTaskNum(String taskNum,String status,String page,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers dbhelpers = new dbhelpers();
        List<PI_TASK_AREA> areaList = dbhelpers.getAreaList(taskNum,status,page);
        dbhelpers dbhelpers1 = new dbhelpers();
        Page page1 = new Page();
        page1.setData(areaList);
        page1.setTotal(dbhelpers1.getAreaListTotal(taskNum,status));
        JSONObject data = JSONObject.fromObject(page1);
        return Response.ok().entity(data).build();
    }
    public Response getAreaByTaskNum(String taskNum,String page,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers dbhelpers = new dbhelpers();
        List<PI_TASK_AREA> areaList = dbhelpers.getAreaList(taskNum,null,page);
        dbhelpers dbhelpers1 = new dbhelpers();
        Page page1 = new Page();
        page1.setData(areaList);
        page1.setTotal(dbhelpers1.getAreaListTotal(taskNum,null));
        JSONObject data = JSONObject.fromObject(page1);
        return Response.ok().entity(data).build();
    }

    public Response getItemByDeviceNum(String taskNum,String areaNum,String deviceNum,String page,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers dbhelpers = new dbhelpers();
        List<PI_TASK_ITEM> areaList = dbhelpers.getItemList(taskNum,areaNum,deviceNum,page);
        JSONArray o = JSONArray.fromObject(areaList);

        return Response.ok().entity(o).build();
    }

    public Response getDeviceByAreaNum(String taskNum,String areaNum,String page,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers dbhelpers = new dbhelpers();
        List<PI_TASK_DEVICE> deviceList = dbhelpers.getDeviceList(taskNum,areaNum,page);
        JSONArray o = JSONArray.fromObject(deviceList);
        return Response.ok().entity(o).build();
    }

    public Response selectItemDetail(String taskNum, String itemNum,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers dbhelpers = new dbhelpers();
        PI_TASK_ITEM pi_task_item = dbhelpers.selectItemDetail(taskNum, itemNum);
        JSONObject o = JSONObject.fromObject(pi_task_item);
        return Response.ok().entity(o).build();
    }

    public Response punchCard(Map<String,String> areaMap,
                              HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        String result = "成功";
        dbhelpers dbhelpers = new dbhelpers();
        result = dbhelpers.punchCard(areaMap.get("useCode"),areaMap.get("taskNum"),areaMap.get("cardId"),areaMap.get("IsiOS"));
        return Response.ok().entity(result).build();
    }

    public Response saveRecord(String taskNum,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers dbhelpers = new dbhelpers();
        List<PI_TASK_ITEM> areaList = dbhelpers.saveRecord(taskNum);
        if(areaList.size()!=0){
            return  Response.ok().entity("有未检项").build();
        }
        return  Response.ok().entity("成功").build();
    }

    public Response exemptFromInspection(String taskNum, String areaNum,String deviceNum, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers dbhelpers = new dbhelpers();
        String r = dbhelpers.exemptFromInspection(taskNum,areaNum,deviceNum);

        return Response.ok().entity(r).build();
    }

    public Response getExemptFromInspection(String taskNum, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers dbhelpers = new dbhelpers();
        List<PI_TASK_ITEM> areaList = dbhelpers.selectExpItemList(taskNum,"免检");
        if(areaList.size()==0){
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        JSONArray o = JSONArray.fromObject(areaList);
        return Response.ok().entity(o).build();
    }

    public Response selectNotFinishItemList(String taskNum,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers dbhelpers = new dbhelpers();
        List<PI_TASK_ITEM> areaList = dbhelpers.selectNotFinishItemList(taskNum,"草稿");
        if(areaList.size()==0){
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        JSONArray o = JSONArray.fromObject(areaList);
        return Response.ok().entity(o).build();
    }

    public Response selectExpItemList(String taskNum, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers dbhelpers = new dbhelpers();
        List<PI_TASK_ITEM> areaList = dbhelpers.selectExpItemList(taskNum,"异常");
        if(areaList.size()==0){
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        JSONArray o = JSONArray.fromObject(areaList);
        return Response.ok().entity(o).build();
    }

    public Response updateItem(Map<String,String> itemMap,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");

        dbhelpers dbhelpers = new dbhelpers();
        String r = dbhelpers.updateItem(itemMap.get("useCode"),itemMap.get("taskNum"),itemMap.get("itemNum"),itemMap.get("result"),
                itemMap.get("valueResult"),itemMap.get("remark"));

        return Response.ok().entity(r).build();
    }
    public Response uploadPunchCardImg(String useCode,String taskNum,String cardId,String IsiOS,Attachment files,
                                  HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers dbhelpers = new dbhelpers();
        String r = dbhelpers.uploadPunchCardImg(useCode,taskNum,cardId,IsiOS,files);

        return Response.ok().entity(r).build();
    }

    public Response uploadItemImg(String useCode, String taskNum, String itemNum,Attachment files,
                                  HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers dbhelpers = new dbhelpers();
        String r = dbhelpers.uploadItemImg(useCode,taskNum,itemNum,files);

        return Response.ok().entity(r).build();
    }

    public Response previewImg(String imgUrl, HttpServletResponse response) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        imgUrl = JdbcUtil.doc+"\\"+imgUrl;
        File file = new File(imgUrl);
        if(!file.exists()){
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
        //获取url的后缀名
        String fileExtName = imgUrl.substring(imgUrl.lastIndexOf(".") + 1);
        if(fileExtName==null||"".equals(fileExtName)){
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        response.setHeader("Content-Type", "image/"+fileExtName);
        byte b[] = new byte[1024];
        int read;
        try {
            while ((read = bis.read(b)) != -1) {
                bos.write(b, 0, read);
            }
            //request.getRequestDispatcher("/components/hazard/yscchird.html").forward(request, response);
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            if (bos != null) {
                bos.close();
            }
            if (bis != null) {
                bis.close();
            }
        }
        return Response.ok().entity("完成").build();
    }

    public Response getToken(Map<String,String> User,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String,String> map = new HashMap();
        map.put("token", TokenUtils.token(User.get("loginid"), User.get("password")));

        JSONObject jsonObject = JSONObject.fromObject(map);
        return Response.ok().entity(jsonObject).build();
    }

    public Response getConfig(HttpServletResponse response)  {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map config = WxUtils.getConfig("http://mmapp.b2bsun.com/SGXJ/index.html");
        if(config.size() == 0){
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        JSONObject jsonObject = JSONObject.fromObject(config);
        return Response.ok().entity(jsonObject).build();
    }

    public Response selectItemPicture(String taskNum,String itemNum,HttpServletResponse response)  {
        response.setHeader("Access-Control-Allow-Origin", "*");
        dbhelpers dbhelpers = new dbhelpers();
        List<Map<String,String>> config = dbhelpers.selectItemPicture(taskNum,itemNum);
        if(config.size() == 0){
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        JSONArray jsonArray= JSONArray.fromObject(config);
        return Response.ok().entity(jsonArray).build();
    }

}
