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

import com.chinaitop.depot.intelligent.utils.JsonToObjectUtils;
import com.chinaitop.depot.intelligent.utils.UuidUtils;
import com.chinaitop.depot.intelligent.ventilation.mapper.TCtldevinfoMapper;
import com.chinaitop.depot.intelligent.ventilation.mapper.TJobMapper;
import com.chinaitop.depot.intelligent.ventilation.model.TCtldevinfo;
import com.chinaitop.depot.intelligent.ventilation.model.TCtldevinfoExample;
import com.chinaitop.depot.intelligent.ventilation.model.TJob;
import com.chinaitop.depot.intelligent.ventilation.model.TJobApplication;
import com.chinaitop.depot.intelligent.ventilation.model.TJobExample;
import com.chinaitop.depot.intelligent.ventilation.service.AerationJobService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class AerationJobServiceImpl implements AerationJobService {
    @Autowired
    private TJobMapper tJobMapper;

	@Override
	public void save(String applyJobJson,String taskNumber,String creator,Integer orgId) {
		// TODO Auto-generated method stub
		if (StringUtils.isNotBlank(applyJobJson)) {
			TJob tJob = new TJob();
			TJobApplication tJobApplication = (TJobApplication) JsonToObjectUtils.jsonToObject(applyJobJson, "com.chinaitop.depot.intelligent.ventilation.model.TJobApplication");
			tJob.setTaskNumber(taskNumber);
			tJob.setAerationPlanNumber(tJobApplication.getAreationPlanNumber());
			tJob.setTaskState(0);//未开始状态
			tJob.setCreationTime(new Date());
			tJob.setCreator(creator);
			tJob.setOrgId(orgId);
			if (StringUtils.isNotBlank(tJob.getId())) {
				//tJobMapper.updat
            } else {
            	tJob.setId(UuidUtils.getCode());
            	tJobMapper.insert(tJob);
            }
        }
	}

	@Override
	public List<TJob> getList(TJob tJob) {
		// TODO Auto-generated method stub
		TJobExample tJobExample = new TJobExample();
		TJobExample.Criteria criteria = tJobExample.createCriteria();
		
        
        if (StringUtils.isNotBlank(tJob.getTaskNumber())) {
        	criteria.andTaskNumberLike("%" + tJob.getTaskNumber() + "%");
        }
        
        tJobExample.setOrderByClause(" creation_time desc"); //创建时间
        
        //tCtldevinfoExample.setOrderByClause("v_update_time desc");
        return tJobMapper.selectByExample(tJobExample);
        
	}

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

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

   

   /* @Override
    public List<TCtldevinfo> getList(String vcfcode) {
    	TCtldevinfoExample tCtldevinfoExample = new TCtldevinfoExample();
    	tCtldevinfoExample.setDistinct(true);
    	TCtldevinfoExample.Criteria criteria = tCtldevinfoExample.createCriteria();
        if (StringUtils.isNotEmpty(vcfcode)) {
            criteria.andVcfcodeEqualTo(vcfcode);
        }
        
        //tCtldevinfoExample.setOrderByClause("v_update_time desc");
        return tCtldevinfoMapper.selectByExample(tCtldevinfoExample);
    }*/



    //保存
    /*@Transactional(rollbackFor = Exception.class)
	@Override
	public void save(String aerationParameterJson) {
		// TODO Auto-generated method stub
		if (StringUtils.isNotBlank(aerationParameterJson)) {
			TCtldevinfo tCtldevinfo = (TCtldevinfo) JsonToObjectUtils.jsonToObject(aerationParameterJson, "com.chinaitop.depot.intelligent.ventilation.model.TCtldevinfo");
			String vdevkindcode = tCtldevinfo.getVdevkindcode().toString();
        	tCtldevinfo.setVdevkindcode(vdevkindcode);
        	
			if (StringUtils.isNotBlank(tCtldevinfo.getId())) {
				tJobMapper.updateByPrimaryKey(tCtldevinfo);
            } else {
            	tCtldevinfo.setId(UuidUtils.getCode());
            	tJobMapper.insert(tCtldevinfo);
            }
        }
	}*/


    /*//删除
	@Transactional(rollbackFor = Exception.class)
	@Override
	public int deleteById(String id) {
		// TODO Auto-generated method stub
		return tCtldevinfoMapper.deleteByPrimaryKey(id);
	}



	//根据id查找
	@Override
	public TCtldevinfo getById(String id) {
		// TODO Auto-generated method stub
		return tCtldevinfoMapper.selectByPrimaryKey(id);
	}



	//根据仓房查找设备信息
	@Override
	public List<TCtldevinfo> getBycf(String vcfcode) {
		// TODO Auto-generated method stub
		return tCtldevinfoMapper.selectBycf(vcfcode);
	}
*/

}
