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

import com.alibaba.fastjson.JSONObject;
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.chinaitop.depot.intelligent.weather.mapper.TWeatherhsitoryMapper;
import com.chinaitop.depot.intelligent.weather.mapper.TWertherinfoMapper;
import com.chinaitop.depot.intelligent.weather.model.TWeatherhsitory;
import com.chinaitop.depot.intelligent.weather.model.TWeatherhsitoryExample;
import com.chinaitop.depot.intelligent.weather.model.TWertherinfo;
import com.chinaitop.depot.intelligent.weather.model.TWertherinfoExample;
import com.chinaitop.depot.intelligent.weather.service.WertherinfoService;
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.Date;
import java.util.List;
import java.util.Map;

@Service
@SuppressWarnings("all")
public class WertherinfoServiceImpl implements WertherinfoService {
    @Resource
    private TWertherinfoMapper tWertherinfoMapper;

    @Resource
    private TWeatherhsitoryMapper tWeatherhsitoryMapper;

    @Resource
    private WareHouseBasicInfoService wareHouseBasicInfoService;
    @Resource
    private UuidUtils uUidUtils;
    
    @Override
    public List<TWertherinfo> getList(String vqxzname, String searchStartDate, String searchEndDate) {
        TWertherinfoExample example = new TWertherinfoExample();
        TWertherinfoExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(vqxzname)) {
            criteria.andVqxznameLike("%" + vqxzname + "%");
        }
        if (StringUtils.isNotBlank(searchStartDate) && StringUtils.isNotBlank(searchEndDate)) {
            criteria.andUpdatetimeBetween(DateUtils.stringToData(searchStartDate + DateUtils.startTime), DateUtils.stringToData(searchEndDate + DateUtils.endTime));
        }
        criteria.andDelFlagEqualTo(1);//未删除
        example.setOrderByClause("updatetime desc");
        return tWertherinfoMapper.selectByExample(example);
    }

    @Override
    public TWertherinfo getById(String id) {
        return tWertherinfoMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(String grainJson) {
        if (StringUtils.isNotBlank(grainJson)) {
            TWertherinfo tWertherinfo = (TWertherinfo) JsonToObjectUtils.jsonToObject(grainJson, TWertherinfo.class);
            tWertherinfo.setUpdatetime(DateUtils.getDate());
            // 推送基础数据-转json对象过滤属性
            JSONObject jsonObject = SendEntity.synchronizationConvert(grainJson);
            // 更新时间
            jsonObject.put("updatetime",DateUtils.dateToString(tWertherinfo.getUpdatetime()));
            if (StringUtils.isNotBlank(tWertherinfo.getId())) {
                tWertherinfoMapper.updateByPrimaryKey(tWertherinfo);
                // 推送数据并返回结果
                SendEntity.cxfFactor().saveWeatherDevice(SendEntity.sendAndConvert(jsonObject, "U"));
            } else {
                // 获取Max+1值
                Map<String, Object> valueMax = wareHouseBasicInfoService.getCode("t_wertherinfo", "sitecode", "vqxzcode", null);
                // 替换值
                jsonObject.put("vqxzcode",valueMax.get("column2"));
                // 保存数据
                tWertherinfo.setId(uUidUtils.getCodeId(tWertherinfo.getOrgId(), "t_wertherinfo"));
                tWertherinfo.setVqxzcode(valueMax.get("column2").toString());
                tWertherinfo.setDelFlag(1); //未删除
                tWertherinfoMapper.insert(tWertherinfo);
                // 推送数据并返回结果
                SendEntity.cxfFactor().saveWeatherDevice(SendEntity.sendAndConvert(jsonObject, "I"));
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteById(String id, String wertherinfojson) {
        TWertherinfo tWertherinfo = (TWertherinfo) JsonToObjectUtils.jsonToObject(wertherinfojson, TWertherinfo.class);
        tWertherinfo.setDelFlag(0); //删除
        int i = tWertherinfoMapper.updateByPrimaryKeySelective(tWertherinfo);
        //int i = tWertherinfoMapper.deleteByPrimaryKey(id);
        // 推送基础数据
        if (i > 0) {
            // 转json对象过滤属性
            JSONObject jsonObject = SendEntity.synchronizationConvert(wertherinfojson);
            // 推送数据并返回结果
            SendEntity.cxfFactor().saveWeatherDevice(SendEntity.sendAndConvert(jsonObject, "D"));
        }
        return i;
    }

    @Override
    public boolean getBycfCode(String vdevcode, String vdevname) {
        TWertherinfoExample example = new TWertherinfoExample();
        TWertherinfoExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(vdevcode)) {
            criteria.andVqxzcodeEqualTo(vdevcode);
        }
        if (StringUtils.isNotEmpty(vdevname)) {
            criteria.andVqxznameEqualTo(vdevname);
        }
        List<TWertherinfo> tWertherinfos = tWertherinfoMapper.selectByExample(example);
        if (tWertherinfos.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<TWeatherhsitory> getByCodeAndTimes(List<String> cDcsCodes, List<Date> cTimes) {
        TWeatherhsitoryExample example = new TWeatherhsitoryExample();
        TWeatherhsitoryExample.Criteria criteria = example.createCriteria();
        if ((cDcsCodes != null && cDcsCodes.size() > 0) && (cTimes != null && cTimes.size() > 0)) {
            criteria.andVqxzcodeIn(cDcsCodes);
            criteria.andUpdateTimeIn(cTimes);
            List<TWeatherhsitory> tWertherinfos = tWeatherhsitoryMapper.selectByExample(example);
            return tWertherinfos;
        }
        return null;
    }

    @Transactional
    @Override
    public void insertBatch(List<TWeatherhsitory> wertherinfos) {
        tWeatherhsitoryMapper.insertBatch(wertherinfos);
    }
    
    @Override
    public List<TWertherinfo> getByCode(String cDcsCode) {
        TWertherinfoExample example = new TWertherinfoExample();
        TWertherinfoExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(cDcsCode)){
            criteria.andVqxzcodeEqualTo(cDcsCode);
            return tWertherinfoMapper.selectByExample(example);
        }
        return null;
    }

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

    public TWertherinfo getVinfo(String vdevcode) {
        TWertherinfoExample example = new TWertherinfoExample();
        TWertherinfoExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(vdevcode)) {
            criteria.andSitecodeEqualTo(vdevcode);
        }
        List<TWertherinfo> infos = tWertherinfoMapper.selectByExample(example);
        if (infos.size() > 0) {
            return infos.get(0);
        }
        return null;
    }
}
