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

import com.alibaba.fastjson.JSONObject;
import com.chinaitop.depot.intelligent.basicdata.mapper.TJctableMapper;
import com.chinaitop.depot.intelligent.basicdata.model.TCcinfo;
import com.chinaitop.depot.intelligent.basicdata.model.TCcinfoExample;
import com.chinaitop.depot.intelligent.basicdata.model.TJctable;
import com.chinaitop.depot.intelligent.basicdata.model.TJctableExample;
import com.chinaitop.depot.intelligent.basicdata.service.QuantityService;
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.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
public class QuantityServiceImpl implements QuantityService {
    @Resource
    private TJctableMapper tJctableMapper;
    @Resource
    private UuidUtils uUidUtils;
    @Resource
    private WareHouseBasicInfoService wareHouseBasicInfoService;


    @Override
    public List<TJctable> getList(String cfCode) {
        TJctableExample example = new TJctableExample();
        TJctableExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(cfCode)) {
            criteria.andVCfCodeEqualTo(cfCode);
        }
        criteria.andDelFlagEqualTo(1);//未删除
        example.setOrderByClause("update_time DESC");
        return tJctableMapper.selectByExample(example);
    }

    @Override
    public TJctable getById(String id) {
        return tJctableMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(String grainJson) {
        if (StringUtils.isNotBlank(grainJson)) {
            TJctable tJctable = (TJctable) JsonToObjectUtils.jsonToObject(grainJson, "com.chinaitop.depot.intelligent.basicdata.model.TJctable");
            tJctable.setUpdateTime(DateUtils.getDate());
            // 推送基础数据
            JSONObject jsonObject = SendEntity.synchronizationConvert(grainJson);
            // 移除更新人
            jsonObject.remove("updatePerson");
            if (StringUtils.isNotBlank(tJctable.getId())) {
                tJctableMapper.updateByPrimaryKey(tJctable);
                // 移除不需要推送的参数
                jsonObject.remove("delFlag");
                jsonObject.remove("dataSource");
                jsonObject.remove("updateTime");
                // 推送数据并返回结果
                SendEntity.cxfFactor().saveJcTable(SendEntity.sendAndConvert(jsonObject, "U"));
            } else {
                // 获取Max+1值
                Map<String, Object> valueMax = wareHouseBasicInfoService.getCode("t_jctable", "v_jc_code");
                // 替换值
                jsonObject.put("vJcCode", valueMax.get("column1"));
                tJctable.setId(uUidUtils.getCodeId(tJctable.getOrgId(), "t_jctable"));
                tJctable.setDelFlag(1); //未删除
                tJctable.setvJcCode(valueMax.get("column1").toString());
                tJctableMapper.insert(tJctable);
                // 推送数据并返回结果
                SendEntity.cxfFactor().saveJcTable(SendEntity.sendAndConvert(jsonObject, "I"));
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteById(String id, String quantityJson) {
        TJctable tJctable = (TJctable) JsonToObjectUtils.jsonToObject(quantityJson, "com.chinaitop.depot.intelligent.basicdata.model.TJctable");
        tJctable.setDelFlag(0); //删除
        int i = tJctableMapper.updateByPrimaryKeySelective(tJctable);
        // 推送基础数据
        if (i > 0) {
            JSONObject jsonObject = SendEntity.synchronizationConvert(quantityJson);
            // 移除不需要推送的参数
            jsonObject.remove("updatePerson");
            jsonObject.remove("dataSource");
            jsonObject.remove("updateTime");
            jsonObject.remove("delFlag");
            SendEntity.cxfFactor().saveJcTable(SendEntity.sendAndConvert(jsonObject, "D"));
        }
        return i;
    }

    @Override
    public boolean getBycfCode(String vdevcode, String vdevname) {
        TJctableExample example = new TJctableExample();
        TJctableExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(vdevcode)) {
            criteria.andVJcCodeEqualTo(vdevcode);
        }
        if (StringUtils.isNotEmpty(vdevname)) {
            criteria.andVJcNameEqualTo(vdevname);
        }
        List<TJctable> tJctables = tJctableMapper.selectByExample(example);
        if (tJctables.size() > 0) {
            return true;
        }
        return false;
    }

    @Transactional
    @Override
    public void insertBatch(List<TJctable> tJctables) {
        for (TJctable t : tJctables) {
            List<TJctable> byCode = getByCode(t.getvJcCode());
            if (byCode.size() > 0) {
                tJctableMapper.updateByPrimaryKey(t);
            } else {
                tJctableMapper.insert(t);
            }
        }
//        tJctableMapper.insertBatch(tJctables);
    }

    @Override
    public List<TJctable> getByCodeAndTimes(List<String> cDcsCodes, List<Date> cTimes) {
        TJctableExample example = new TJctableExample();
        TJctableExample.Criteria criteria = example.createCriteria();
        if ((cDcsCodes != null && cDcsCodes.size() > 0) && (cTimes != null && cTimes.size() > 0)) {
            criteria.andVCfCodeIn(cDcsCodes);
            criteria.andUpdateTimeIn(cTimes);
            List<TJctable> tJctables = tJctableMapper.selectByExample(example);
            return tJctables;
        }
        return null;
    }

    @Override
    public List<TJctable> getByCode(String jcCode) {
        TJctableExample example = new TJctableExample();
        TJctableExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(jcCode)) {
            criteria.andVJcCodeEqualTo(jcCode);
            List<TJctable> tJctables = tJctableMapper.selectByExample(example);
            return tJctables;
        }
        return null;
    }

    @Transactional
    @Override
    public String synchronizationAll(String orgId) {
        //获取所有基础数据getJcTable
        String insectDevice = SendEntity.cxfFactor().getJcTable();
        //转换对象
        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, TJctable.class);
                objects.forEach(t -> {
                    TJctable tc = (TJctable) t;
                    tc.setId(uUidUtils.getCodeId(orgId, "t_jctable"));
                    tc.setOrgId(orgId);
                    tc.setDelFlag(1); //未删除
                    String vdevcode = tc.getvJcCode();
                    TJctable ccinfo1 = getCcinfo(vdevcode);
                    if (ccinfo1 != null) {
                        tc.setId(ccinfo1.getId());
                        tJctableMapper.updateByPrimaryKey(tc);
                    } else {
                        tJctableMapper.insert(tc);
                    }
                });
            }
        } else {
            return basicEntity.getMsg();
        }
        return basicEntity.getMsg();
    }

    @Override
    public List<TJctable> getByCodes(List<String> jcCodes) {
        TJctableExample example = new TJctableExample();
        TJctableExample.Criteria criteria = example.createCriteria();
        if (jcCodes.size()>0){
            criteria.andVRemarkIn(jcCodes);
        }
        return tJctableMapper.selectByExample(example);
    }

    public TJctable getCcinfo(String vdevcode) {
        TJctableExample example = new TJctableExample();
        TJctableExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(vdevcode)) {
            criteria.andVJcCodeEqualTo(vdevcode);
        }
        List<TJctable> tCcinfos = tJctableMapper.selectByExample(example);
        if (tCcinfos.size() > 0) {
            return tCcinfos.get(0);
        }
        return null;
    }
}
