package com.sky.ioc.service.security.impl;

import com.alibaba.fastjson.JSONObject;
import com.sky.ioc.config.RestTemplateConfig;
import com.sky.ioc.entity.domain.security.SecurityDoorRecord;
import com.sky.ioc.entity.domain.space.Department;
import com.sky.ioc.entity.params.scene.DoorAccessParam;
import com.sky.ioc.mapper.security.SecurityDoorRecordMapper;
import com.sky.ioc.mapper.space.DepartmentMapper;
import com.sky.ioc.service.security.SecurityDoorRecordService;
import com.sky.ioc.service.space.DepartmentService;
import com.sky.ioc.tool.ReturnMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

@Service
@Slf4j
public class SecurityDoorRecordServiceImpl implements SecurityDoorRecordService {
    @Autowired
    RestTemplateConfig restTemplateConfig;

    @Autowired
    SecurityDoorRecordMapper securityDoorRecordMapper;

    @Autowired
    DepartmentMapper departmentMapper;

    final static  String SECURITY_DOOR_RECORD_URL="/api/safety/Hikvision/record/list";
    @Override
    public ReturnMsg getSecurityDoorRecordData() {
        String[] persons = {"李劲","李良","宋明宇","陈涵亮","李超","章峰","梁松","方运诚","徐涵衍","万浩波",
                "周浩广","许鸿博","荣浩博","许鸿畴","徐瀚文","陈浩初","程涵煦","许鸿与","徐廖文","吴初"};
        String url = restTemplateConfig.addBaseUrl(SECURITY_DOOR_RECORD_URL)+"?pageNum=1&pageSize=1000";;
        RestTemplate restTemplate = restTemplateConfig.build();
        ResponseEntity<Map> responseEntity = restTemplate.getForEntity(url, Map.class);
        Map<String, Object> body = responseEntity.getBody();
        Integer code = (Integer)body.get("code");
        String message = (String)body.get("msg");
        if(code==200){
            ThreadLocalRandom random = ThreadLocalRandom.current();
            ArrayList list = (ArrayList) body.get("rows");
            if(list!=null&&list.size()>0){
                securityDoorRecordMapper.delete(null);
                for (int i=0;i<list.size();i++){
                    Integer flag = random.nextInt(0,20);
                    Integer flag2 = random.nextInt(1,322);
                    String name = persons[flag];
                    LinkedHashMap json = (LinkedHashMap) list.get(i);
                    SecurityDoorRecord securityDoorRecord = new SecurityDoorRecord();
                    securityDoorRecord.setId((Integer) json.get("id"));
                    if(json.get("employeeName")!=null){
                        securityDoorRecord.setName(String.valueOf(json.get("employeeName")));
                    }else{
                        securityDoorRecord.setName(name);
                    }
                    securityDoorRecord.setDept_id(flag2);
                    securityDoorRecord.setTime(String.valueOf(json.get("eventTime")));
                    securityDoorRecord.setDoorId(Integer.parseInt(String.valueOf(json.get("doorId"))));
                    securityDoorRecord.setInOutType(String.valueOf(json.get("inAndOutType")));
                    securityDoorRecord.setType(String.valueOf(json.get("eventType")));
                    securityDoorRecordMapper.insert(securityDoorRecord);
                }
            }
            return ReturnMsg.ok();

        }else{
            log.info("获取门禁记录数据："+message);
            return  ReturnMsg.fail(message);
        }
    }

    @Override
    public ReturnMsg pageList(DoorAccessParam doorAccessParam) {
        if(doorAccessParam.getName()!=null&&!doorAccessParam.getName().isEmpty()){
            doorAccessParam.setName("%"+doorAccessParam.getName()+"%");
        }
        if(doorAccessParam.getEquipment()!=null&&!doorAccessParam.getEquipment().isEmpty()){
            doorAccessParam.setEquipment("%"+doorAccessParam.getEquipment()+"%");
        }
        long total = securityDoorRecordMapper.pageCount(doorAccessParam);
        List<Map<String,Object>> lists = securityDoorRecordMapper.pageList(doorAccessParam);
        for(Map<String,Object> map:lists){
          //  map.get("deptId");
           Department department = departmentMapper.selectById(Integer.parseInt(String.valueOf(map.get("deptid"))));
           map.put("department",department.getName());
           map.put("person_type","");
            map.put("direction","");
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",total);
        jsonObject.put("data",lists);

        return ReturnMsg.ok(jsonObject);
    }
}
