package com.chinaitop.depot.intelligent.basicdata.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.chinaitop.depot.intelligent.basicdata.mapper.TKtinfoMapper;
import com.chinaitop.depot.intelligent.basicdata.model.TJctable;
import com.chinaitop.depot.intelligent.basicdata.model.TJctableExample;
import com.chinaitop.depot.intelligent.basicdata.model.TKtinfo;
import com.chinaitop.depot.intelligent.basicdata.model.TKtinfoExample;
import com.chinaitop.depot.intelligent.basicdata.service.TKtinfoService;
import com.chinaitop.depot.intelligent.basicdata.service.WareHouseBasicInfoService;
import com.chinaitop.depot.intelligent.utils.DateUtils;
import com.chinaitop.depot.intelligent.utils.JsonToObjectUtils;
import com.chinaitop.depot.intelligent.utils.UuidUtils;
import com.unissoft.model.BasicEntity;
import com.unissoft.model.SendEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class TKtinfoServiceImpl implements TKtinfoService {
    @Resource
    private TKtinfoMapper tKtinfoMapper;
    @Resource
    private WareHouseBasicInfoService wareHouseBasicInfoService;
    @Resource
    private UuidUtils uUidUtils;
    
    
    @Override
    public List<TKtinfo> getList(String vCfCode) {
        TKtinfoExample example = new TKtinfoExample();
        TKtinfoExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(vCfCode)) {
           criteria.andVcfcodeEqualTo(vCfCode);
        }
        criteria.andDelFlagEqualTo(1);//未删除
        example.setOrderByClause("updatetime desc");
        return tKtinfoMapper.selectByExample(example);
    }

    @Override
    public TKtinfo getById(String id) {
        return tKtinfoMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(String grainJson) {
        if (StringUtils.isNotBlank(grainJson)) {
            TKtinfo tKtinfo = (TKtinfo) JsonToObjectUtils.jsonToObject(grainJson, TKtinfo.class);
            tKtinfo.setUpdatetime(DateUtils.getDate());
            // 推送基础数据
            JSONObject jsonObject = SendEntity.synchronizationConvert(grainJson);
            // 更新时间
            jsonObject.put("updatetime",DateUtils.dateToString(tKtinfo.getUpdatetime()));
            if (StringUtils.isNotBlank(tKtinfo.getId())) {
                tKtinfoMapper.updateByPrimaryKey(tKtinfo);
                // 推送数据并返回结果
                SendEntity.cxfFactor().saveAirData(SendEntity.sendAndConvert(jsonObject, "U"));
            } else {
                // 获取Max+1值
                Map<String, Object> valueMax = wareHouseBasicInfoService.getCode("t_ktinfo", "sitecode", "vdevcode", null);
                // 替换值
                jsonObject.put("vdevcode",valueMax.get("column2"));
                tKtinfo.setVdevcode(valueMax.get("column2").toString());
                tKtinfo.setId(uUidUtils.getCodeId(tKtinfo.getOrgId(), "t_ktinfo"));
                tKtinfo.setDelFlag(1); //未删除
                tKtinfoMapper.insert(tKtinfo);
                // 推送数据并返回结果
                SendEntity.cxfFactor().saveAirData(SendEntity.sendAndConvert(jsonObject, "I"));
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteById(String id, String ktinfoJson) {
        TKtinfo tKtinfo = (TKtinfo) JsonToObjectUtils.jsonToObject(ktinfoJson, TKtinfo.class);
        tKtinfo.setDelFlag(0); //删除
        int i = tKtinfoMapper.updateByPrimaryKeySelective(tKtinfo);
        // 推送基础数据
        if (i > 0) {
            JSONObject jsonObject = SendEntity.synchronizationConvert(ktinfoJson);
            SendEntity.cxfFactor().saveAirData(SendEntity.sendAndConvert(jsonObject, "D"));
        }
        return i;
    }

    @Override
    public boolean getBycfCode(String vdevcode, String vdevname) {
        TKtinfoExample example = new TKtinfoExample();
        TKtinfoExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(vdevcode)) {
            criteria.andVdevcodeEqualTo(vdevcode);
        }
        if (StringUtils.isNotEmpty(vdevname)) {
            criteria.andVdevnameEqualTo(vdevname);
        }
        List<TKtinfo> tKtinfos = tKtinfoMapper.selectByExample(example);
        if (tKtinfos.size() > 0) {
            return true;
        }
        return false;
    }

    @Transactional
    @Override
    public String synchronizationAll(String orgId) {
        //获取所有基础数据getAirData
        String device = SendEntity.cxfFactor().getAirData();
        //转换对象
        BasicEntity basicEntity = (BasicEntity) JsonToObjectUtils.jsonToObject(device, BasicEntity.class);
        if (basicEntity.getState().equals("0")) {
            List<Object> data = basicEntity.getData();
            if (data.size() > 0) {
                String tt = JSONObject.toJSONString(data);
                ArrayList<Object> objects = JsonToObjectUtils.jsonToObjectsDiverse(tt, TKtinfo.class,"yyyy-MM-dd'T'HH:mm:ss");
                objects.forEach(t -> {
                    TKtinfo tc = (TKtinfo) t;
                    tc.setId(uUidUtils.getCodeId(orgId, "t_ktinfo"));
                    tc.setOrgId(orgId);
                    tc.setDelFlag(1); //未删除sitecode
                    String vdevcode = tc.getVdevcode();
                    TKtinfo info1 = getVinfo(vdevcode);
                    if (info1 != null) {
                        tc.setId(info1.getId());
                        tKtinfoMapper.updateByPrimaryKey(tc);
                    } else {
                        tKtinfoMapper.insert(tc);
                    }
                });
            }
        } else {
            return basicEntity.getMsg();
        }
        return basicEntity.getMsg();
    }

    public TKtinfo getVinfo(String vdevcode) {
        TKtinfoExample example = new TKtinfoExample();
        TKtinfoExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(vdevcode)) {
            criteria.andVdevcodeEqualTo(vdevcode);
        }
        List<TKtinfo> infos = tKtinfoMapper.selectByExample(example);
        if (infos.size() > 0) {
            return infos.get(0);
        }
        return null;
    }
}
