package com.neusoft.smart.pos.device.persistent.repository;

import com.neusoft.smart.pos.device.persistent.domain.Device;
import com.neusoft.smart.pos.device.persistent.domain.DeviceExample;
import com.neusoft.smart.pos.device.persistent.ext.domain.DeviceQuery;
import com.neusoft.smart.pos.device.persistent.mapper.DeviceMapper;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;

@Component
public class DeviceRepository {
	
	@Autowired
	private DeviceMapper deviceMapper;
	
	
	/**
	 * @param device
	 * @return
	 */
	public Device create(Device device) {
		deviceMapper.insertSelective(device);
		return device;
	}
	
	/**
	 * @param deviceList
	 */
	public void batchCreate(List<Device> deviceList) {
		deviceMapper.batchInsert(deviceList);
	}
	
	/**
	 * @param deviceId
	 * @return
	 */
	public Device get(Integer deviceId) {
		return deviceMapper.selectByPrimaryKey(deviceId);
	}
	
	/**
	 * @param device
	 */
	public void update(Device device) {
		deviceMapper.updateByPrimaryKeySelective(device);
	}
	
	public void batchUpdate(Device device, DeviceExample deviceExample) {
		deviceMapper.updateByExampleSelective(device, deviceExample);
	}
	
	/**
	 * @param device
	 */
	public void updateWithNull(Device device) {
		deviceMapper.updateByPrimaryKey(device);
	}
	
	/**
	 * @param deviceQuery
	 * @return
	 */
	public List<Device> getList(DeviceQuery deviceQuery) {
		DeviceExample deviceExample = this.exampleProvider(deviceQuery);
		if (deviceQuery.getIsPage()) {
			deviceExample.setLimit(deviceQuery.getLimit());
			deviceExample.setOffset(deviceQuery.getOffset());
		}
		return deviceMapper.selectByExample(deviceExample);
	}
	
	/**
	 * @param deviceQuery
	 * @return
	 */
	public Integer getCount(DeviceQuery deviceQuery) {
		DeviceExample deviceExample = this.exampleProvider(deviceQuery);
		return deviceMapper.countByExample(deviceExample);
	}
	
	/**
	 *
	 * @param deviceExample
	 * @return
	 */
	public Integer queryCountByExample(DeviceExample deviceExample) {
		return deviceMapper.countByExample(deviceExample);
	}
	
	/**
	 * @param deviceIds
	 * @param storeId
	 */
	public void setStoreId(List<Integer> deviceIds, Integer storeId) {
		DeviceExample deviceExample = new DeviceExample();
		deviceExample.createCriteria().andIdIn(deviceIds);
		Device device = new Device();
		device.setStoreId(storeId);
		deviceMapper.updateByExampleSelective(device, deviceExample);
	}
	
	private DeviceExample exampleProvider(DeviceQuery deviceQuery) {
		DeviceExample deviceExample = new DeviceExample();
		deviceExample.setOrderByClause("created_date desc");
		DeviceExample.Criteria criteria = deviceExample.createCriteria();
		if (!StringUtils.isEmpty(deviceQuery.getMacAddr())) {
			criteria.andMacAddrEqualTo(deviceQuery.getMacAddr());
		}
		if (!StringUtils.isEmpty(deviceQuery.getSerialNo())) {
			criteria.andSerialNoEqualTo(deviceQuery.getSerialNo());
		}
		if (!StringUtils.isEmpty(deviceQuery.getCurVersion())) {
			criteria.andCurVersionEqualTo(deviceQuery.getCurVersion());
		}
		if (!StringUtils.isEmpty(deviceQuery.getManufacturer())) {
			criteria.andManufacturerLike("%" + deviceQuery.getManufacturer() + "%");
		}
		if (!StringUtils.isEmpty(deviceQuery.getRegion())) {
			criteria.andRegionLike("%" + deviceQuery.getRegion() + "%");
		}
		if (!StringUtils.isEmpty(deviceQuery.getDepartment())) {
			criteria.andDepartmentLike("%" + deviceQuery.getDepartment() + "%");
		}
		if (!StringUtils.isEmpty(deviceQuery.getCustomer())) {
			criteria.andCustomerLike("%" + deviceQuery.getCustomer() + "%");
		}
		if (!StringUtils.isEmpty(deviceQuery.getDeviceType())) {
			criteria.andDeviceTypeLike("%" + deviceQuery.getDeviceType() + "%");
		}
		if (!StringUtils.isEmpty(deviceQuery.getDeviceModel())) {
			criteria.andDeviceModelLike("%" + deviceQuery.getDeviceModel() + "%");
		}
		if (!StringUtils.isEmpty(deviceQuery.getAlias())) {
			criteria.andAliasLike("%" + deviceQuery.getAlias() + "%");
		}
		
		if (null != deviceQuery.getStatus()) {
			criteria.andStatusEqualTo(deviceQuery.getStatus());
		}
		
		if (null != deviceQuery.getOrganizationId()) {
			criteria.andOrganizationIdEqualTo(deviceQuery.getOrganizationId());
		}
		
		if (null != deviceQuery.getStoreIds() && !deviceQuery.getStoreIds().isEmpty()) {
			criteria.andStoreIdIn(deviceQuery.getStoreIds());
		}
		
		if (null != deviceQuery.getIds() && !deviceQuery.getIds().isEmpty()) {
			criteria.andIdIn(deviceQuery.getIds());
		}
		
		if (deviceQuery.getExcludeDeleted()) {
			criteria.andStatusNotEqualTo(1);//过滤删除的
		}
		switch (RequestHeaderUtil.getRole()) {
			case "admin":
				criteria.andOrganizationIdEqualTo(RequestHeaderUtil.getOrgId());
				break;
			case "user":
				if(!deviceQuery.getIsStoreUserQueryWithoutStoreId()) {
					criteria.andStoreIdIn(RequestHeaderUtil.getStoreIds());
				}
				criteria.andOrganizationIdEqualTo(RequestHeaderUtil.getOrgId());
				break;
			default:
		}
		
		return deviceExample;
	}
	
}
