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

import com.alibaba.fastjson.JSONObject;
import com.chinaitop.depot.intelligent.basicdata.mapper.TCcinfoMapper;
import com.chinaitop.depot.intelligent.basicdata.model.TCcinfo;
import com.chinaitop.depot.intelligent.basicdata.model.TCcinfoExample;
import com.chinaitop.depot.intelligent.basicdata.service.InsectService;
import com.chinaitop.depot.intelligent.basicdata.service.WareHouseBasicInfoService;
import com.chinaitop.depot.intelligent.grainsituation.model.TCcdata;
import com.chinaitop.depot.intelligent.utils.JsonToObjectUtils;
import com.chinaitop.depot.intelligent.utils.UuidUtils;
import com.google.common.collect.Lists;
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 InsectServiceImpl implements InsectService {
    @Resource
    private TCcinfoMapper tCcinfoMapper;
    @Resource
    private WareHouseBasicInfoService wareHouseBasicInfoService;
    @Resource
    private UuidUtils uUidUtils;


    @Override
    public List<TCcinfo> getList(String cfCode) {
        TCcinfoExample example = new TCcinfoExample();
        TCcinfoExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(cfCode)) {
            criteria.andVcfcodeEqualTo(cfCode);
        }
        criteria.andDelFlagEqualTo(1);//未删除
        return tCcinfoMapper.selectByExample(example);
    }

    @Override
    public TCcinfo getById(String id) {
        return tCcinfoMapper.selectByPrimaryKey(id);
    }

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

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

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

    @Transactional
    @Override
    public String synchronizationAll(String orgId) {
        //获取所有虫害基础数据
        String insectDevice = SendEntity.cxfFactor().getInsectDevice();
        //转换对象
        BasicEntity basicEntity = (BasicEntity) JsonToObjectUtils.jsonToObject(insectDevice, BasicEntity.class);
        if (basicEntity.getState().equals("0")) {
            List<Object> data = basicEntity.getData();
            if (data.size() > 0) {
                String tcs = JSONObject.toJSONString(data);
                ArrayList<Object> objects = JsonToObjectUtils.jsonToObjects(tcs, TCcinfo.class);
                objects.forEach(t -> {
                    TCcinfo tc = (TCcinfo) t;
                    tc.setId(uUidUtils.getCodeId(orgId, "t_ccinfo"));
                    tc.setOrgId(orgId);
                    tc.setDelFlag(1); //未删除
                    String vdevcode = tc.getVdevcode();
                    TCcinfo ccinfo1 = getCcinfo(vdevcode);
                    if (ccinfo1 != null) {
                        tc.setId(ccinfo1.getId());
                        tCcinfoMapper.updateByPrimaryKey(tc);
                    } else {
                        tCcinfoMapper.insert(tc);
                    }
                });
            }
        } else {
            return basicEntity.getMsg();
        }
        return basicEntity.getMsg();
    }

    public TCcinfo getCcinfo(String vdevcode) {
        TCcinfoExample example = new TCcinfoExample();
        TCcinfoExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(vdevcode)) {
            criteria.andVdevcodeEqualTo(vdevcode);
        }
        List<TCcinfo> tCcinfos = tCcinfoMapper.selectByExample(example);
        if (tCcinfos.size() > 0) {
            return tCcinfos.get(0);
        }
        return null;
    }
}
