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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chinaitop.depot.intelligent.basicdata.mapper.TDcsqtinfoMapper;
import com.chinaitop.depot.intelligent.basicdata.model.TDcsqtinfo;
import com.chinaitop.depot.intelligent.basicdata.model.TDcsqtinfoExample;
import com.chinaitop.depot.intelligent.basicdata.service.GaseousService;
import com.chinaitop.depot.intelligent.basicdata.service.WareHouseBasicInfoService;
import com.chinaitop.depot.intelligent.utils.JsonToObjectUtils;
import com.chinaitop.depot.intelligent.utils.UuidUtils;
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.Date;
import java.util.List;
import java.util.Map;

@Service
public class GaseousServiceImpl implements GaseousService {
    @Resource
    private TDcsqtinfoMapper tDcsqtinfoMapper;
    @Resource
    private WareHouseBasicInfoService wareHouseBasicInfoService;

    @Override
    public List<TDcsqtinfo> getList(String cfCode) {
        TDcsqtinfoExample example = new TDcsqtinfoExample();
        TDcsqtinfoExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(cfCode)) {
            criteria.andVCfCodeEqualTo(cfCode);
        }
        return tDcsqtinfoMapper.selectByExample(example);
    }

    @Override
    public TDcsqtinfo getById(String id) {
        return tDcsqtinfoMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(String grainJson) {
        if (StringUtils.isNotBlank(grainJson)) {
            TDcsqtinfo tDcsqtinfo = (TDcsqtinfo) JsonToObjectUtils.jsonToObject(grainJson, TDcsqtinfo.class);
            // 推送基础数据,去除id和orgId
            JSONObject jsonObject = SendEntity.synchronizationConvert(grainJson);
            // 去除不同步字段
            jsonObject.remove("vUpdateTime");
            jsonObject.remove("vUpdatePerson");
            if (StringUtils.isNotBlank(tDcsqtinfo.getId())) {
                tDcsqtinfoMapper.updateByPrimaryKey(tDcsqtinfo);
                // 推送数据并返回结果
                SendEntity.cxfFactor().saveDcsTable(SendEntity.sendAndConvert(jsonObject, "U"));
            } else {
                // 获取Max+1值
                Map<String, Object> valueMax = wareHouseBasicInfoService.getCode("t_dcsqtinfo", "v_dcs_code");
                tDcsqtinfo.setvDcsCode(valueMax.get("column1").toString());
                tDcsqtinfo.setId(UuidUtils.getCode());
                tDcsqtinfoMapper.insert(tDcsqtinfo);
                // 推送数据并返回结果
                jsonObject.put("vDcsCode", valueMax.get("column1")); // 替换值
                SendEntity.cxfFactor().saveDcsTable(SendEntity.sendAndConvert(jsonObject, "I"));
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteById(String id, String dcsQtInfoInfoJson) {
        int i = tDcsqtinfoMapper.deleteByPrimaryKey(id);
        // 推送基础数据
        if (i > 0) {
            JSONObject jsonObject = SendEntity.synchronizationConvert(dcsQtInfoInfoJson);
            // 去除不同步字段
            jsonObject.remove("vUpdateTime");
            jsonObject.remove("vUpdatePerson");
            SendEntity.cxfFactor().saveDcsTable(SendEntity.sendAndConvert(jsonObject, "D"));
        }
        return i;
    }

    /**
     * 硬件厂商同步基础数据
     *
     * @param orgId
     * @return
     */
    @Transactional
    @Override
    public String synchronizationAll(String orgId, String vUpdatePerson) {
        //获取所有基础数据getDcsTable
        String DcsTableStr = SendEntity.cxfFactor().getDcsTable();
        if (StringUtils.isNotBlank(DcsTableStr)) {
            // json字符串转json对象
            JSONObject jsonObject = JSON.parseObject(DcsTableStr);
            if ("0".equals(jsonObject.get("state").toString())) {
                Object data = jsonObject.get("data");
                List<TDcsqtinfo> dcsQtInfoList = JSONArray.parseArray(data.toString(), TDcsqtinfo.class);
                dcsQtInfoList.forEach(dcsInfo -> {
                    dcsInfo.setvUpdateTime(new Date());
                    dcsInfo.setvUpdatePerson(vUpdatePerson);
                    dcsInfo.setOrgId(orgId);
                    String id = getDcsCodeById(dcsInfo.getvDcsCode());// 效验设备code是否存在
                    if (null != id) {
                        dcsInfo.setId(id);
                        tDcsqtinfoMapper.updateByPrimaryKey(dcsInfo);
                    } else {
                        dcsInfo.setId(UuidUtils.getCode());
                        tDcsqtinfoMapper.insert(dcsInfo);
                    }
                });
                return jsonObject.get("msg").toString();
            } else {
                return jsonObject.get("msg").toString();
            }
        }
        return "同步上行数据为空";
    }

    @Override
    public List<TDcsqtinfo> getByCode(List<String> code) {
        TDcsqtinfoExample example = new TDcsqtinfoExample();
        TDcsqtinfoExample.Criteria criteria = example.createCriteria();
        if (code != null) {
            criteria.andVDcsCodeIn(code);
        }
        return tDcsqtinfoMapper.selectByExample(example);
    }

    private String getDcsCodeById(String vDcsCode) {
        TDcsqtinfoExample example = new TDcsqtinfoExample();
        TDcsqtinfoExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(vDcsCode)) {
            criteria.andVDcsCodeEqualTo(vDcsCode);
        }
        List<TDcsqtinfo> infoList = tDcsqtinfoMapper.selectByExample(example);
        if (infoList.size() > 0) {
            return infoList.get(0).getId();
        }
        return null;
    }

}
