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

import com.alibaba.fastjson.JSONObject;
import com.chinaitop.depot.intelligent.grainsituation.mapper.TGasConfigMapper;
import com.chinaitop.depot.intelligent.grainsituation.model.TGasConfig;
import com.chinaitop.depot.intelligent.grainsituation.model.TGasConfigExample;
import com.chinaitop.depot.intelligent.grainsituation.service.GasConfigService;
import com.chinaitop.depot.intelligent.utils.UuidUtils;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author qingsong.han
 * @description:
 * @create 2021-10-15 14:27
 */
@Service
public class GasConfigServiceImpl implements GasConfigService {
    private TGasConfigMapper gasConfigMapper;

    @Autowired
    public void setGasConfigImpl(TGasConfigMapper gasConfigMapper) {
        this.gasConfigMapper = gasConfigMapper;
    }

    @Override
    public PageInfo<TGasConfig> getList(String storeCode, String ip) {
        TGasConfigExample example = new TGasConfigExample();
        TGasConfigExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(storeCode)) {
            criteria.andStoreCodeEqualTo(storeCode);
        }
        if (StringUtils.isNotBlank(ip)) {
            criteria.andRangeIpEqualTo(ip);
        }
        return new PageInfo<>(gasConfigMapper.selectByExample(example));
    }

    @Override
    public TGasConfig getById(String id) {
        return gasConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    public int save(String gasConfigJson) {
        TGasConfig gasConfig = JSONObject.parseObject(gasConfigJson, TGasConfig.class);
        int insertResult;
        if (StringUtils.isBlank(gasConfig.getId())) {
            gasConfig.setId(UuidUtils.getCode());
            insertResult = gasConfigMapper.insert(gasConfig);
        } else {
            insertResult = gasConfigMapper.updateByPrimaryKey(gasConfig);
        }
        return insertResult;
    }

    @Override
    public int deleteById(String id) {
        return gasConfigMapper.deleteByPrimaryKey(id);
    }
}
