package com.hai.service.impl;

import com.hai.dao.HighDeviceMapper;
import com.hai.entity.HighDevice;
import com.hai.entity.HighDeviceExample;
import com.hai.service.HighDeviceService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("deviceService")
public class HighDeviceServiceImpl implements HighDeviceService {

    @Resource
    private HighDeviceMapper deviceMapper;

    @Override
    public void editDevice(HighDevice device) {
        if (device.getId() == null) {
            device.setCreateTime(new Date());
            device.setUpdateTime(new Date());
            device.setStatus(1);
            deviceMapper.insert(device);
        } else {
            device.setUpdateTime(new Date());
            deviceMapper.updateByPrimaryKey(device);
        }
    }

    @Override
    public HighDevice getDetailById(Long deviceId) {
        return deviceMapper.selectByPrimaryKey(deviceId);
    }

    @Override
    public List<HighDevice> getDeviceList(Map<String, Object> param) {
        HighDeviceExample example = new HighDeviceExample();
        HighDeviceExample.Criteria criteria = example.createCriteria().andStatusNotEqualTo(0);

        if (MapUtils.getInteger(param, "type") != null) {
            criteria.andTypeEqualTo(MapUtils.getInteger(param, "type"));
        }

        if (MapUtils.getLong(param, "companyId") != null) {
            criteria.andCompanyIdEqualTo(MapUtils.getLong(param, "companyId"));
        }

        if (MapUtils.getLong(param, "merId") != null) {
            criteria.andMerIdEqualTo(MapUtils.getLong(param, "merId"));
        }

        if (MapUtils.getLong(param, "storeId") != null) {
            criteria.andMerStoreIdEqualTo(MapUtils.getLong(param, "storeId"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "deviceName"))) {
            criteria.andDeviceNameLike("%" + MapUtils.getString(param, "deviceName") + "%");
        }

        example.setOrderByClause("create_time desc");
        return deviceMapper.selectByExample(example);
    }

    @Override
    public List<HighDevice> getDeviceListByStoreId(Long storeId) {
        HighDeviceExample example = new HighDeviceExample();
        example.createCriteria()
        .andMerStoreIdEqualTo(storeId)
        .andStatusNotEqualTo(0);
        return deviceMapper.selectByExample(example);
    }
}
