package com.zcloud.service.integratedmanagement.access.impl;

import com.alibaba.fastjson.JSONObject;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.integratedmanagement.GateMachineAreaGateMapper;
import com.zcloud.service.integratedmanagement.access.GateMachineAreaGateService;
import com.zcloud.util.InitPageDataUtil;
import com.zcloud.util.Jurisdiction;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GateMachineAreaGateServiceImpl implements GateMachineAreaGateService {

    @Resource
    private GateMachineAreaGateMapper gateMachineAreaGateMapper;
    @Resource
    private InitPageDataUtil initPageDataUtil;

    @Resource
    private RestTemplate restTemplateHbsy;

    @Override
    public PageData findById(PageData pageData) {
        return gateMachineAreaGateMapper.findById(pageData);
    }

    @Override
    public List<PageData> list(Page page) {
        return gateMachineAreaGateMapper.datalistPage(page);
    }

    @Override
    @Transactional
    public void edit(PageData pageData) {
        initPageDataUtil.initEdit(pageData);
        gateMachineAreaGateMapper.edit(pageData);
    }

    @Override
    @Transactional
    public void save(PageData pageData) {
        initPageDataUtil.initSave(pageData, "AREA_GATE_ID");
        pageData.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        gateMachineAreaGateMapper.save(pageData);
    }

    @Override
    @Transactional
    public void removeByIds(PageData pageData) {
        gateMachineAreaGateMapper.deleteAll(pageData);
    }

    @Override
    public void initGateMachine(PageData pageData) {
        pageData.put("CORPINFO_ID",Jurisdiction.getCORPINFO_ID());
        // 汇博石油 初始化门禁闸机数据
        if (pageData.getString("CORPINFO_ID").equals("480f8568a499470cb203a51e7e8c09d2")) {
            ResponseEntity<String> forEntity = restTemplateHbsy.getForEntity("https://121.22.67.98:18098/api/device/accList?access_token=13DB557E69044C31B4121E655C77C95017329C5230E100C0296533FC6DF6DB8D&pageNo=1&pageSize=1000", String.class);
            PageData parse = JSONObject.parseObject(forEntity.getBody(), PageData.class);
            List<PageData> dataList = JSONObject.parseArray(JSONObject.toJSONString(parse.get("data")), PageData.class);
            dataList.forEach(item -> {
                initPageDataUtil.initSave(item, "AREA_GATE_ID");
                item.put("AREA_ID",pageData.getString("AREA_ID"));
                item.put("GATE_NUMBER",item.getString("sn"));
                item.put("GATE_NAME",item.getString("name"));
                item.put("GATE_CATEGORY",0);
                item.put("GATE_MODEL",item.getString("module")); //所属模块，acc 门禁，att 考勤，ele 梯控
                item.put("CORPINFO_ID",pageData.getString("CORPINFO_ID")); //所属模块，acc 门禁，att 考勤，ele 梯控
            });
            gateMachineAreaGateMapper.saveBatch(dataList);
        }
        // todo 测试企业
        if (pageData.getString("CORPINFO_ID").equals("8a6fbef442a34622abfe3a9f95dc2a34")) {
            ResponseEntity<String> forEntity = restTemplateHbsy.getForEntity("https://121.22.67.98:18098/api/device/accList?access_token=13DB557E69044C31B4121E655C77C95017329C5230E100C0296533FC6DF6DB8D&pageNo=1&pageSize=1000", String.class);
            PageData parse = JSONObject.parseObject(forEntity.getBody(), PageData.class);
            List<PageData> dataList = JSONObject.parseArray(JSONObject.toJSONString(parse.get("data")), PageData.class);
            dataList.forEach(item -> {
                initPageDataUtil.initSave(item, "AREA_GATE_ID");
                item.put("AREA_ID",pageData.getString("AREA_ID"));
                item.put("GATE_NUMBER",item.getString("sn"));
                item.put("GATE_NAME",item.getString("name"));
                item.put("GATE_CATEGORY",0);
                item.put("GATE_MODEL",item.getString("module")); //所属模块，acc 门禁，att 考勤，ele 梯控
                item.put("CORPINFO_ID",pageData.getString("CORPINFO_ID")); //所属模块，acc 门禁，att 考勤，ele 梯控
            });
            gateMachineAreaGateMapper.saveBatch(dataList);
        }
    }
}
