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

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chinaitop.depot.intelligent.utils.UuidUtils;
import com.chinaitop.depot.intelligent.ventilation.mapper.TVentilationOperationMapper;
import com.chinaitop.depot.intelligent.ventilation.model.TVentilationOperation;
import com.chinaitop.depot.intelligent.ventilation.model.TVentilationOperationExample;
import com.chinaitop.depot.intelligent.ventilation.service.VentilationOperationService;
import com.github.pagehelper.PageHelper;

@Service
public class VentilationOperationServiceImpl implements VentilationOperationService {
    @Autowired
    private TVentilationOperationMapper tVentilationOperationMapper;

	@Override
	public List<TVentilationOperation> getList(Integer pageNum, Integer pageSize,String orgId, String cfbh,String cfmc) {
		TVentilationOperationExample tVentilationOperationExample = new TVentilationOperationExample();
		tVentilationOperationExample.setDistinct(true);
		TVentilationOperationExample.Criteria criteria = tVentilationOperationExample.createCriteria();
        if (StringUtils.isNotEmpty(cfbh)) {
            criteria.andCfbhEqualTo(cfbh);
        }
        if (StringUtils.isNotEmpty(cfmc)) {
        	criteria.andCfmcEqualTo(cfmc);
        }
        if (StringUtils.isNotEmpty(orgId)) {
        	criteria.andOrgIdEqualTo(Integer.parseInt(orgId));
        }
        tVentilationOperationExample.setOrderByClause("djrq desc");
        if (null != pageNum && null != pageSize) {
            PageHelper.startPage(pageNum, pageSize);
        }
        return tVentilationOperationMapper.selectByExample(tVentilationOperationExample);
	}

	@Override
	public TVentilationOperation getById(String id) {
		// TODO Auto-generated method stub
		return tVentilationOperationMapper.selectByPrimaryKey(id);
	}

	@Override
	public void save(String ventilationOperationJson) {
		// TODO Auto-generated method stub
		if (StringUtils.isNotBlank(ventilationOperationJson)) {
			
			TVentilationOperation tVentilationOperation;
			
			try {
				JSONObject jsonObject = JSON.parseObject(ventilationOperationJson);
				 tVentilationOperation = JSONObject.toJavaObject(jsonObject , TVentilationOperation.class);
				if (StringUtils.isNotBlank(tVentilationOperation.getId())) {
					tVentilationOperationMapper.updateByPrimaryKeySelective(tVentilationOperation);
	            } else {
	            	tVentilationOperation.setId(UuidUtils.getCode());
	            	tVentilationOperationMapper.insert(tVentilationOperation);
	            }
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
        }
	}

	@Override
	public int deleteById(String id) {
		// TODO Auto-generated method stub
		return tVentilationOperationMapper.deleteByPrimaryKey(id);
	}

	

}
