package com.huimv.video.dhicc.test.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.dahuatech.icc.exception.ClientException;
import com.huimv.common.utils.StringUtilsWork;
import com.huimv.video.dhicc.result.R;
import com.huimv.video.dhicc.test.config.OauthConfigBaseInfo;
import com.huimv.video.dhicc.test.config.OauthConfigBaseInfo02;
import com.huimv.video.dhicc.test.config.OauthConfigBaseInfo03;
import com.huimv.video.dhicc.test.request.*;
import com.huimv.video.dhicc.test.request.DepartmentRequest.Cn.DepartmentListRequestCn;
import com.huimv.video.dhicc.test.request.DepartmentRequest.Hn.DepartmentListRequestHn;
import com.huimv.video.dhicc.test.request.DepartmentRequest.Sg.DepartmentListRequestSg;
import com.huimv.video.dhicc.test.request.Person.Cn.*;
import com.huimv.video.dhicc.test.request.Person.Hn.*;
import com.huimv.video.dhicc.test.request.Person.Sg.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("personTest")
public class ClientPersonManageControllerTest {
    @Autowired
    private OauthConfigBaseInfo oauthConfigBaseInfo;
    @Autowired
    private OauthConfigBaseInfo02 oauthConfigBaseInfo02;
    @Autowired
    private OauthConfigBaseInfo03 oauthConfigBaseInfo03;

    //添加图片
    @RequestMapping("/addPersonPicture")
    public R addPersonPicture(@RequestParam(name = "file") MultipartFile multipartFile,
                              @RequestParam(name = "farmId") Integer farmId) throws ClientException, IOException {

        //文件上传前的名称
        long s = System.currentTimeMillis();
        String fileName = multipartFile.getOriginalFilename();
        File file = new File(fileName);
        OutputStream out = null;
        try {
            //获取文件流，以文件流的方式输出到新文件
            out = new FileOutputStream(file);
            byte[] ss = multipartFile.getBytes();
            for (int i = 0; i < ss.length; i++) {
                out.write(ss[i]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        long e = System.currentTimeMillis();
        System.out.println(e - s);
        HashMap<String, Object> formMap = new HashMap<>();
        formMap.put("file", file);
        Object action = null;
        if (farmId == 1) {
            System.out.println("shuguang");
            ClientPersonAddPictureRequestSg requestSg = new ClientPersonAddPictureRequestSg(oauthConfigBaseInfo);
            requestSg.setFile(formMap);
            Object action1 = requestSg.doAction();
            action = action1;
        }
        if (farmId==22) {
            System.out.println("haining");
            ClientPersonAddPictureRequestHn requestHn = new ClientPersonAddPictureRequestHn(oauthConfigBaseInfo02);
            requestHn.setFile(formMap);
            Object action1 = requestHn.doAction();
            action = action1;
        }
        if (farmId==21) {
            System.out.println("cangnan");
            ClientPersonAddPictureRequestCn requestCn = new ClientPersonAddPictureRequestCn(oauthConfigBaseInfo03);
            requestCn.setFile(formMap);
            Object action1 = requestCn.doAction();
            action = action1;
        }

        // 操作完上的文件 需要删除在根目录下生成的文件
        File f = new File(file.toURI());
        if (f.delete()) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
        JSONObject jsonObject1 = JSONObject.parseObject((String) action);
        Object fileUrl = jsonObject1.get("fileUrl");
        return R.ok("请求成功").put("data", fileUrl.toString());
    }


    //删除人员
    @RequestMapping("/deletePercon")
    public R deletePercon(@RequestBody Map<String, Object> params1) throws ClientException {
        List<Integer> ids = (List<Integer>) params1.get("ids");

        Object farmId = params1.get("farmId");
        JSONObject jsonObject = null;
        if ("1".equals(farmId)) {
            System.out.println("shuguang");
            ClientPersonDeleteRequestSg requestSg = new ClientPersonDeleteRequestSg(oauthConfigBaseInfo);
            requestSg.setIds(ids);
            JSONObject jsonObject1 = requestSg.doAction();
            jsonObject = jsonObject1;
        }
        if ("22".equals(farmId)) {
            System.out.println("haining");
            ClientPersonDeleteRequestHn requestHn = new ClientPersonDeleteRequestHn(oauthConfigBaseInfo02);
            requestHn.setIds(ids);
            JSONObject jsonObject1 = requestHn.doAction();
            jsonObject = jsonObject1;
        }
        if ("21".equals(farmId)) {
            System.out.println("cangnan");
            ClientPersonDeleteRequestCn requestCn = new ClientPersonDeleteRequestCn(oauthConfigBaseInfo03);
            requestCn.setIds(ids);
            JSONObject jsonObject1 = requestCn.doAction();
            jsonObject = jsonObject1;
        }
        return R.ok("请求成功").put("data", jsonObject);
    }


    //人员管理--添加用户
    @RequestMapping("/addPerson")
    public R addPerson(@RequestBody Map<String, Object> params1) throws ClientException {
        Object paperType = params1.put("paperType", 111);
        Object code = params1.put("code", (int) ((Math.random() * 9 + 1) * 10000));
        Object departmentId = params1.put("departmentId", (int) params1.get("departmentId"));
        Object name = params1.put("name", (String) params1.get("name"));
        Object paperNumber = params1.put("paperNumber", "1101011990d3d" + (int) ((Math.random() * 9 + 1) * 10000));
        Object phone = params1.put("phone", 15171709858L + (int) ((Math.random() * 9 + 1) * 10000));
        Object id = params1.put("id", (int) ((Math.random() * 9 + 1) * 10000));
        Object farmId = params1.get("farmId");
        JSONObject jsonObject = null;
        if ("1".equals(farmId)) {
            System.out.println("shuguang");
            ClientPersonAddRequestSg requestSg = new ClientPersonAddRequestSg(oauthConfigBaseInfo);
            requestSg.setPagerType((Integer) paperType);
            requestSg.setCode((Integer) code);
            requestSg.setDepartmentId((Integer)departmentId);
            requestSg.setName((String) name);
            requestSg.setPaperNumber((String) paperNumber);
            requestSg.setPhone((Integer) phone);
            requestSg.setId((Integer) id);
            JSONObject jsonObject1 = requestSg.doAction();
            jsonObject = jsonObject1;
        }
        if ("22".equals(farmId)) {
            System.out.println("haining");
            ClientPersonAddRequestHn requestHn = new ClientPersonAddRequestHn(oauthConfigBaseInfo02);
            requestHn.setPagerType((Integer) paperType);
            requestHn.setCode((Integer) code);
            requestHn.setDepartmentId((Integer)departmentId);
            requestHn.setName((String) name);
            requestHn.setPaperNumber((String) paperNumber);
            requestHn.setPhone((Integer) phone);
            requestHn.setId((Integer) id);
            JSONObject jsonObject1 = requestHn.doAction();
            jsonObject = jsonObject1;
        }
        if ("21".equals(farmId)) {
            System.out.println("cangnan");
            ClientPersonAddRequestCn requestCn = new ClientPersonAddRequestCn(oauthConfigBaseInfo03);
            requestCn.setPagerType((Integer) paperType);
            requestCn.setCode((Integer) code);
            requestCn.setDepartmentId((Integer)departmentId);
            requestCn.setName((String) name);
            requestCn.setPaperNumber((String) paperNumber);
            requestCn.setPhone((Integer) phone);
            requestCn.setId((Integer) id);
            JSONObject jsonObject1 = requestCn.doAction();
            jsonObject = jsonObject1;
        }
        return R.ok("请求成功").put("data", jsonObject);
    }

    //人员管理--添加绑定用户的图片
    @RequestMapping("/addPictureToPerson")
    public Object addPicture(@RequestBody Map<String, Object> params1) throws ClientException {

        Object farmId = params1.get("farmId");
        Map<String, Object> value = new HashMap<>();
        value.put("personId", (int) params1.get("personId"));
        value.put("type", 3);
        value.put("index", 1);
        value.put("path", (String) params1.get("path"));
        Object action = null;
        List mapList = new ArrayList();
        System.out.println(mapList);
        mapList.add(value);
        if ("1".equals(farmId)) {
            System.out.println("shuguang");
            ClientPersonSaveRequestSg requestSg = new ClientPersonSaveRequestSg(oauthConfigBaseInfo);
            requestSg.setPersonBiosignatures(mapList);
            Object action1 = requestSg.doAction();
            action = action1;
        }
        if ("22".equals(farmId)) {
            System.out.println("haining");
            ClientPersonSaveRequestHn requestHn = new ClientPersonSaveRequestHn(oauthConfigBaseInfo02);
            requestHn.setPersonBiosignatures(mapList);
            Object action1 = requestHn.doAction();
            action = action1;
        }
        if ("33".equals(farmId)) {
            System.out.println("cangnan");
            ClientPersonSaveRequestCn requestCn = new ClientPersonSaveRequestCn(oauthConfigBaseInfo03);
            requestCn.setPersonBiosignatures(mapList);
            Object action1 = requestCn.doAction();
            action = action1;
        }

        return action;
    }

    //刷脸失败记录
    @RequestMapping("/getPersonOpenDoorFail")
    public R getAccidentRecord(@RequestBody Map<String, Object> params1) throws ClientException {
        Integer farmId = (int) params1.get("farmId");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        Date date = new Date();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Date A = c.getTime();
        String formatA = format.format(A);
        c.set(Calendar.HOUR, +24);
        Date d = c.getTime();
        String formatD = format.format(d);
        Object action = null;
        if (farmId == 1) {
            System.out.println("shuguang");
            ClientPersonDoorListRequestSg requestSg = new ClientPersonDoorListRequestSg(oauthConfigBaseInfo);
            requestSg.setPageNum(1);
            requestSg.setPageSize("20");
            requestSg.setOpenResult("0");
            if (!StringUtilsWork.isNotEmpty((String) params1.get("startSwingTime"))) {
                requestSg.setStartSwingTime((String) params1.get("startSwingTime"));
                requestSg.setEndSwingTime(formatD);
            } else {
                requestSg.setStartSwingTime(formatA);
                requestSg.setEndSwingTime(formatD);
            }
            params1.put("containDomain", "1");
            params1.put("openResult", 0); //刷脸失败
            Object action1 = requestSg.doAction();
            action = action1;
        }
        if (farmId == 22) {
            System.out.println("haining");
            ClientPersonDoorListRequestHn requestHn = new ClientPersonDoorListRequestHn(oauthConfigBaseInfo02);
            requestHn.setPageNum(1);
            requestHn.setPageSize("20");
            requestHn.setOpenResult("0");
            if (!StringUtilsWork.isNotEmpty((String) params1.get("startSwingTime"))) {
                requestHn.setStartSwingTime((String) params1.get("startSwingTime"));
                requestHn.setEndSwingTime(formatD);
            } else {
                requestHn.setStartSwingTime(formatA);
                requestHn.setEndSwingTime(formatD);
            }
            params1.put("containDomain", "1");
            params1.put("openResult", 0); //刷脸失败
            Object action1 = requestHn.doAction();
            action = action1;
        }
        if (farmId == 21) {
            System.out.println("cangnan");
            ClientPersonDoorListRequestCn requestSg = new ClientPersonDoorListRequestCn(oauthConfigBaseInfo03);
            requestSg.setPageNum(1);
            requestSg.setPageSize("20");
            requestSg.setOpenResult("0");
            if (!StringUtilsWork.isNotEmpty((String) params1.get("startSwingTime"))) {
                requestSg.setStartSwingTime((String) params1.get("startSwingTime"));
                requestSg.setEndSwingTime(formatD);
            } else {
                requestSg.setStartSwingTime(formatA);
                requestSg.setEndSwingTime(formatD);
            }
            params1.put("containDomain", "1");
            params1.put("openResult", 0); //刷脸失败
            Object action1 = requestSg.doAction();
            action = action1;
        }


        return R.ok("请求成功").put("data", action);
    }


    @RequestMapping("/ge_person_list")
    public String ge_person_list(@RequestBody PersonListRequestSg personListRequestSg) throws ClientException {
        Integer farmId = personListRequestSg.getFarmId();
        String request = null;
        if (farmId == 1) {
            System.out.println("曙光");
            PersonListRequestSg requestSg = new PersonListRequestSg(oauthConfigBaseInfo);
            requestSg.setDepartmentId(personListRequestSg.getDepartmentId());
            requestSg.setIsContain(personListRequestSg.getIsContain());
            requestSg.setPageNum(personListRequestSg.getPageNum());
            requestSg.setPageSize(personListRequestSg.getPageSize());
            requestSg.setSearchKey(personListRequestSg.getSearchKey());
            requestSg.setStatusList(personListRequestSg.getStatusList());
            String s = requestSg.doAction();
            request = s;
        } else if (farmId == 22) {
            PersonListRequestHn requestHn = new PersonListRequestHn(oauthConfigBaseInfo02);
            requestHn.setDepartmentId(personListRequestSg.getDepartmentId());
            requestHn.setIsContain(personListRequestSg.getIsContain());
            requestHn.setPageNum(personListRequestSg.getPageNum());
            requestHn.setPageSize(personListRequestSg.getPageSize());
            requestHn.setSearchKey(personListRequestSg.getSearchKey());
            requestHn.setStatusList(personListRequestSg.getStatusList());
            String s = requestHn.doAction();
            request = s;
            System.out.println("海宁");
        } else if (farmId==21) {
            PersonListRequestCn requestCn = new PersonListRequestCn(oauthConfigBaseInfo03);
            requestCn.setDepartmentId(personListRequestSg.getDepartmentId());
            requestCn.setIsContain(personListRequestSg.getIsContain());
            requestCn.setPageNum(personListRequestSg.getPageNum());
            requestCn.setPageSize(personListRequestSg.getPageSize());
            requestCn.setSearchKey(personListRequestSg.getSearchKey());
            requestCn.setStatusList(personListRequestSg.getStatusList());
            String s = requestCn.doAction();
            request = s;
            System.out.println("苍南");
        }
        return request;
    }

    @RequestMapping("/get_person_record")
    public Object getAccidentRecord1(@RequestBody Map<String, Object> params) throws ClientException {
        Integer farmId = (int) params.get("farmId");
        Object action = null;
        String startSwingTime = (String)params.get("startSwingTime");
        String endSwingTime = (String)params.get("endSwingTime");
        Integer openType = (Integer) params.get("openType");

        if (farmId == 1) {
            System.out.println("shuguang");
            ClientPersonDoorRecordRequestSg requestSg = new ClientPersonDoorRecordRequestSg(oauthConfigBaseInfo);
            requestSg.setPageNum(1);
            requestSg.setPageSize(20);
            requestSg.setEndSwingTime(endSwingTime);
            requestSg.setOpenType(openType);
            if (StringUtils.isNotBlank(startSwingTime)){
                requestSg.setStartSwingTime(startSwingTime);
            }
            Object action1 = requestSg.doAction();
            action=action1;
        }
        if (farmId == 22) {
            System.out.println("haining");
            ClientPersonDoorRecordRequestHn requestHn = new ClientPersonDoorRecordRequestHn(oauthConfigBaseInfo02);
            requestHn.setPageNum(1);
            requestHn.setPageSize(20);
            requestHn.setEndSwingTime(endSwingTime);
            requestHn.setOpenType(openType);
            if (StringUtils.isNotBlank(startSwingTime)){
                requestHn.setStartSwingTime(startSwingTime);
            }
            Object action1 = requestHn.doAction();
            action=action1;
        }
        if (farmId == 21) {
            System.out.println("cangnan");
            ClientPersonDoorRecordRequestCn requestCn = new ClientPersonDoorRecordRequestCn(oauthConfigBaseInfo03);
            requestCn.setPageNum(1);
            requestCn.setPageSize(20);
            requestCn.setEndSwingTime(endSwingTime);
            requestCn.setOpenType(openType);
            if (StringUtils.isNotBlank(startSwingTime)){
                requestCn.setStartSwingTime(startSwingTime);
            }
            Object action1 = requestCn.doAction();
            action=action1;
        }
        return action;
    }

    //总页数
    @RequestMapping("/get_person_record_totalpage")
    public Object get_person_record_totalpage(@RequestBody Map<String, Object> params) throws ClientException {
        Integer farmId = (int) params.get("farmId");
        String timestamp = (String) params.get("startSwingTime");
        String endTime = (String) params.get("endSwingTime");

        Integer openType = (Integer) params.get("openType");
        Object action = null;

        if (farmId == 1) {
            System.out.println("shuguang");
            ClientPersonDoorRecordRequestSg requestSg = new ClientPersonDoorRecordRequestSg(oauthConfigBaseInfo);
            requestSg.setPageNum(1);
            requestSg.setPageSize(20);
            requestSg.setStartSwingTime(timestamp);
            requestSg.setEndSwingTime(endTime);
            requestSg.setOpenType(openType);
            Object res = requestSg.doAction();
            JSONObject jsonObject=JSONObject.parseObject((String) res);
            System.out.println(jsonObject);
            JSONObject data = (JSONObject) jsonObject.get("data");
            JSONArray pageData1 = (JSONArray) data.get("pageData");
            System.out.println(pageData1.size());
            Map<String, Object> map = new HashMap<>();
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("count", pageData1.size());
            map.put("data", jsonObject1);
            action = R.ok().put("data", jsonObject1);
        }
        if (farmId == 22) {
            System.out.println("haining");
            ClientPersonDoorRecordRequestHn requestHn = new ClientPersonDoorRecordRequestHn(oauthConfigBaseInfo02);
            requestHn.setPageNum(1);
            requestHn.setPageSize(20);
            requestHn.setStartSwingTime(timestamp);
            requestHn.setOpenType(openType);
            Object res = requestHn.doAction();
            JSONObject jsonObject=JSONObject.parseObject((String) res);
            System.out.println(jsonObject);
            JSONObject data = (JSONObject) jsonObject.get("data");
            JSONArray pageData1 = (JSONArray) data.get("pageData");
            System.out.println(pageData1.size());
            Map<String, Object> map = new HashMap<>();
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("count", pageData1.size());
            map.put("data", jsonObject1);
            action = R.ok().put("data", jsonObject1);
        }
        if (farmId == 21) {
            System.out.println("cangnan");
            ClientPersonDoorRecordRequestCn requestCn = new ClientPersonDoorRecordRequestCn(oauthConfigBaseInfo03);
            requestCn.setPageNum(1);
            requestCn.setPageSize(20);
            requestCn.setStartSwingTime(timestamp);
            requestCn.setOpenType(openType);
            Object res = requestCn.doAction();
            JSONObject jsonObject=JSONObject.parseObject((String) res);
            System.out.println(jsonObject);
            JSONObject data = (JSONObject) jsonObject.get("data");
            JSONArray pageData1 = (JSONArray) data.get("pageData");
            System.out.println(pageData1.size());
            Map<String, Object> map = new HashMap<>();
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("count", pageData1.size());
            map.put("data", jsonObject1);
            action = R.ok().put("data", jsonObject1);
        }
        return action;
    }


    @RequestMapping("/get_person_detail")
    public Object getPersonDetail(@RequestBody Map<String, Object> params) throws ClientException {

        Integer farmId = (int) params.get("farmId");
        Object action = null;
        if (farmId == 1) {
            System.out.println("shuguang");
            ClientPersonDoorDetailRequestSg requestSg = new ClientPersonDoorDetailRequestSg(oauthConfigBaseInfo);
            requestSg.setId((String) params.get("id"));
            Object action1 = requestSg.doAction();
            action = action1;
        }
        if (farmId == 22) {
            System.out.println("haining");
            ClientPersonDoorDetailRequestHn requestHn = new ClientPersonDoorDetailRequestHn(oauthConfigBaseInfo02);
            requestHn.setId((String) params.get("id"));
            Object action1 = requestHn.doAction();
            action = action1;
        }
        if (farmId == 21) {
            System.out.println("cangnan");
            ClientPersonDoorDetailRequestCn requestCn = new ClientPersonDoorDetailRequestCn(oauthConfigBaseInfo03);
            requestCn.setId((String) params.get("id"));
            Object action1 = requestCn.doAction();
            action = action1;
        }

        return action;
    }
    @RequestMapping("departmentList")
    public R departmentList(@RequestBody Map<String,Integer> paramsMap) {
        Integer farmId = paramsMap.get("farmId");
        if (farmId == 1) {
            System.out.println("曙光");
            DepartmentListRequestSg departmentList = new DepartmentListRequestSg(oauthConfigBaseInfo);
            return R.ok("请求成功").put("data", departmentList.doAction());
        } else if (farmId == 22) {
            System.out.println("海宁");
            DepartmentListRequestHn departmentList = new DepartmentListRequestHn(oauthConfigBaseInfo02);
            return R.ok("请求成功").put("data", departmentList.doAction());
        } else if (farmId == 21) {
            System.out.println("苍南");
            DepartmentListRequestCn departmentList = new DepartmentListRequestCn(oauthConfigBaseInfo03);
            System.out.println(departmentList);
            return R.ok("请求成功").put("data", departmentList.doAction());
        } else {
            return R.error();
        }
    }
}
