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

import com.alibaba.fastjson.JSONObject;
import com.chinaitop.depot.intelligent.basicdata.mapper.TStationMapper;
import com.chinaitop.depot.intelligent.basicdata.model.TLqinfo;
import com.chinaitop.depot.intelligent.basicdata.model.TLqinfoExample;
import com.chinaitop.depot.intelligent.basicdata.model.TStation;
import com.chinaitop.depot.intelligent.basicdata.model.TStationExample;
import com.chinaitop.depot.intelligent.basicdata.service.StationService;
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.wsClient.WebServiceSer;
import com.chinaitop.depot.intelligent.wsClient.WebServiceSerSoap;
import com.google.common.collect.Maps;
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.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@SuppressWarnings("all")
public class StationServiceImpl implements StationService {
    @Resource
    private TStationMapper tStationMapper;
    @Resource
    private WareHouseBasicInfoService wareHouseBasicInfoService;
    @Resource
    private UuidUtils uUidUtils;
    
    
    @Override
    public List<TStation> getList(String zdname) {
        TStationExample example = new TStationExample();
        TStationExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(zdname)) {
            criteria.andSitenameLike("%" + zdname + "%");
        }
        criteria.andDelFlagEqualTo(1);//未删除
        example.setOrderByClause("sitename asc");
        return tStationMapper.selectByExample(example);
    }

    @Override
    public TStation getById(String id) {
        return tStationMapper.selectByPrimaryKey(id);
    }

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

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteById(String id, String stationJson) {
        TStation tStation = (TStation) JsonToObjectUtils.jsonToObject(stationJson, "com.chinaitop.depot.intelligent.basicdata.model.TStation");
        tStation.setDelFlag(0); //删除
        int i = tStationMapper.updateByPrimaryKeySelective(tStation);
        // 推送基础数据
        if (i>0){
            JSONObject jsonObject = SendEntity.synchronizationConvert(stationJson);
            SendEntity.cxfFactor().saveSite(SendEntity.sendAndConvert(jsonObject, "D"));
        }
        return i;
    }

    @Override
    public Map<String, TStation> getStations() {
        TStationExample example = new TStationExample();
        example.setOrderByClause("sitename asc");
        List<TStation> tStations = tStationMapper.selectByExample(example);
        if (tStations.size() > 0) {
            LinkedHashMap<String, TStation> map = Maps.newLinkedHashMap();
            for (TStation t : tStations) {
                map.put(t.getSitecode(), t);
            }
            return map;
        }
        return null;
    }

    @Override
    public boolean getBycfCode(String vdevcode, String vdevname) {
        TStationExample example = new TStationExample();
        TStationExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(vdevcode)) {
            criteria.andSitecodeEqualTo(vdevcode);
        }
        if (StringUtils.isNotBlank(vdevname)) {
            criteria.andSitenameEqualTo(vdevname);
        }
        List<TStation> tStations = tStationMapper.selectByExample(example);
        if (tStations.size() > 0) {
            return true;
        }
        return false;
    }

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

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