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

import com.alibaba.fastjson.JSONObject;
import com.chinaitop.depot.intelligent.basicdata.service.WareHouseBasicInfoService;
import com.chinaitop.depot.intelligent.utils.JsonToObjectUtils;
import com.chinaitop.depot.intelligent.utils.UuidUtils;
import com.chinaitop.depot.intelligent.ventilation.mapper.TCtldevinfoMapper;
import com.chinaitop.depot.intelligent.ventilation.model.TCtldevinfo;
import com.chinaitop.depot.intelligent.ventilation.model.TCtldevinfoExample;
import com.chinaitop.depot.intelligent.ventilation.service.AerationParameterService;
import com.unissoft.model.BasicEntity;
import com.unissoft.model.SendEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 AerationParameterServiceImpl implements AerationParameterService {
    @Resource
    private TCtldevinfoMapper tCtldevinfoMapper;
	@Resource
	private WareHouseBasicInfoService wareHouseBasicInfoService;
	@Resource
    private UuidUtils uUidUtils;
   

    @Override
    public List<TCtldevinfo> getList(String orgId,String vcfcode) {
    	TCtldevinfoExample tCtldevinfoExample = new TCtldevinfoExample();
    	tCtldevinfoExample.setDistinct(true);
    	TCtldevinfoExample.Criteria criteria = tCtldevinfoExample.createCriteria();
		if (StringUtils.isNotEmpty(orgId)) {
			criteria.andOrgIdEqualTo(Integer.valueOf(orgId));
		}
        if (StringUtils.isNotEmpty(vcfcode)) {
            criteria.andVcfcodeEqualTo(vcfcode);
        }
        criteria.andDelFlagEqualTo(1);//未删除
        //tCtldevinfoExample.setOrderByClause("v_update_time desc");
        return tCtldevinfoMapper.selectByExample(tCtldevinfoExample);
    }



    //保存
    @Transactional(rollbackFor = Exception.class)
	@Override
	public void save(String aerationParameterJson,Integer orgId) {
		if (StringUtils.isNotBlank(aerationParameterJson)) {
			TCtldevinfo tCtldevinfo = (TCtldevinfo) JsonToObjectUtils.jsonToObject(aerationParameterJson, "com.chinaitop.depot.intelligent.ventilation.model.TCtldevinfo");
			String vdevkindcode = tCtldevinfo.getVdevkindcode();
        	tCtldevinfo.setVdevkindcode(vdevkindcode);
        	tCtldevinfo.setOrgId(orgId);
			// 推送基础数据
			JSONObject jsonObject = SendEntity.synchronizationConvert(aerationParameterJson);
			if (StringUtils.isNotBlank(tCtldevinfo.getId())) {
            	tCtldevinfoMapper.updateByPrimaryKey(tCtldevinfo);
				// 推送数据并返回结果
				SendEntity.cxfFactor().saveWindDevice(SendEntity.sendAndConvert(jsonObject, "U"));
			} else {
				// 获取Max+1值
				Map<String, Object> valueMax = wareHouseBasicInfoService.getCode("t_ctldevinfo", "sitecode", "vdevcode", null);
				// 替换值
				jsonObject.put("vdevcode",valueMax.get("column2"));
				tCtldevinfo.setVdevcode(valueMax.get("column2").toString());
				tCtldevinfo.setId(uUidUtils.getCodeId(String.valueOf(orgId), "t_ctldevinfo"));
				tCtldevinfo.setDelFlag(1); //未删除
                tCtldevinfoMapper.insert(tCtldevinfo);
				// 推送数据并返回结果
				SendEntity.cxfFactor().saveWindDevice(SendEntity.sendAndConvert(jsonObject, "I"));
			}
        }
	}


    //删除
	@Transactional(rollbackFor = Exception.class)
	@Override
	public int deleteById(String id, String aerationParamJson) {
		TCtldevinfo tCtldevinfo = (TCtldevinfo) JsonToObjectUtils.jsonToObject(aerationParamJson, "com.chinaitop.depot.intelligent.ventilation.model.TCtldevinfo");
		tCtldevinfo.setDelFlag(0); //删除
		//int i = tCtldevinfoMapper.deleteByPrimaryKey(id);
		int i = tCtldevinfoMapper.updateByPrimaryKeySelective(tCtldevinfo);
		if (i > 0) {
			// 推送同步数据
			JSONObject jsonObject = SendEntity.synchronizationConvert(aerationParamJson);
			SendEntity.cxfFactor().saveWindDevice(SendEntity.sendAndConvert(jsonObject, "D"));
		}
		return i;
	}



	//根据id查找
	@Override
	public TCtldevinfo getById(String id) {
		// TODO Auto-generated method stub
		return tCtldevinfoMapper.selectByPrimaryKey(id);
	}



	//根据仓房查找设备信息
	@Override
	public List<TCtldevinfo> getBycf(Map<String, Object> modelMap) {
		// TODO Auto-generated method stub
		return tCtldevinfoMapper.selectBycf(modelMap);
	}

	@Transactional
	@Override
	public String synchronizationAll(String orgId) {
		//获取所有基础数据getWindDevice
		String device = SendEntity.cxfFactor().getWindDevice();
		//转换对象
		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, TCtldevinfo.class,"yyyy-MM-dd'T'HH:mm:ss");
				objects.forEach(t -> {
					TCtldevinfo tc = (TCtldevinfo) t;
					tc.setId(uUidUtils.getCodeId(orgId, "t_ctldevinfo"));
					tc.setOrgId(Integer.valueOf(orgId));
					tc.setDelFlag(1); //未删除sitecode
					String vdevcode = tc.getVdevcode();
					TCtldevinfo info1 = getVinfo(vdevcode);
					if (info1 != null) {
						tc.setId(info1.getId());
						tCtldevinfoMapper.updateByPrimaryKey(tc);
					} else {
						tCtldevinfoMapper.insert(tc);
					}
				});
			}
		} else {
			return basicEntity.getMsg();
		}
		return basicEntity.getMsg();
	}

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