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.TJobApplicationMapper;
import com.chinaitop.depot.intelligent.ventilation.model.TJobApplication;
import com.chinaitop.depot.intelligent.ventilation.model.TJobApplicationAudit;
import com.chinaitop.depot.intelligent.ventilation.model.TJobApplicationExample;
import com.chinaitop.depot.intelligent.ventilation.service.AerationTaskService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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


@Service
public class AerationTaskServiceImpl implements AerationTaskService {
    @Autowired
    private TJobApplicationMapper tJobApplicationMapper;

    
    /**
	 * 列表
	 */
	@Override
	public List<TJobApplication> getList(TJobApplication tJobApplication, Integer approvalState,Integer userId) {
		// TODO Auto-generated method stub
		TJobApplicationExample tJobApplicationExample = new TJobApplicationExample();
		tJobApplicationExample.setDistinct(true);
		TJobApplicationExample.Criteria criteria = tJobApplicationExample.createCriteria();
        if (tJobApplication.getState()!=null) {
        	criteria.andStateEqualTo(tJobApplication.getState());
        }
        if(tJobApplication.getvCfCode()!=null){
        	criteria.andVCfCodeEqualTo(tJobApplication.getvCfCode());
        }
        if(tJobApplication.getAreationPlanNumber()!=null){ //通风记录新增时 ，只通过计划编号获取仓房编号，不做其他操作
        	criteria.andAreationPlanNumberEqualTo(tJobApplication.getAreationPlanNumber());
        }else{
        	if(userId!=null){
        		criteria.andAidEqualTo(String.valueOf(userId));
        	}
        	if(null != approvalState && approvalState==4){//审批结束：4
        		criteria.andStateEqualTo(4);
        	}else{
        		criteria.andStateNotEqualTo(4);
        	}
        }
        tJobApplicationExample.setOrderByClause("apply_time desc");
        return tJobApplicationMapper.selectByExample(tJobApplicationExample);
	}
	
	
    
    /**
     * 新增
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
    @Override
    public Map<String, String> save(String taskApplyJson, Integer orgId) {
		Map<String, String> returnMap = new HashMap<String, String>();
        if (StringUtils.isNotBlank(taskApplyJson)) {
        	TJobApplication tJobApplication = (TJobApplication) JsonToObjectUtils.jsonToObject(taskApplyJson, "com.chinaitop.depot.intelligent.ventilation.model.TJobApplication");
        	tJobApplication.setOrgId(orgId);
        	if (StringUtils.isNotBlank(tJobApplication.getId())) {
            	tJobApplicationMapper.updateByPrimaryKey(tJobApplication);
            } else {
            	tJobApplication.setId(UuidUtils.getCode());
                tJobApplicationMapper.insert(tJobApplication);
				returnMap.put("id", tJobApplication.getId());
            }
        }
		return returnMap;
	}



	/**
	 * 提交
	 */
	@Override
	public void submit(String id, Integer approvalStatus) {
		// TODO Auto-generated method stub
		TJobApplication tJobApplication = tJobApplicationMapper.selectByPrimaryKey(id);
        
        if (tJobApplication == null) {
            throw new RuntimeException("没有找到相应的数据");
        }
 
        // 设置审批状态为，保管科长审批中.
        tJobApplication.setState(approvalStatus);
        /*if (approvalStatus == 4) {
        	tJobApplication.setEndTime(new Date());
        }*/
        // 修改.
        tJobApplicationMapper.updateByPrimaryKey(tJobApplication);
	}



	/**
	 * 根据id删除数据
	 */
	@Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteById(String id) {
        return tJobApplicationMapper.deleteByPrimaryKey(id);
    }


	/**
	 * 审批列表
	 */
	@Override
	public List<TJobApplicationAudit> getApprovalList(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return tJobApplicationMapper.getApprovalList(paramMap);
	}



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



	@Override
	public List<TJobApplication> getJobCtrlList(Map<String, String> map) {
		// TODO Auto-generated method stub
		return tJobApplicationMapper.getJobCtrlList(map);
	}



	//根据计划编号获取数据
	@Override
	public TJobApplication getHouseByNumber(String aerationPlanNumber) {
		// TODO Auto-generated method stub
		return tJobApplicationMapper.getHouseByNumber(aerationPlanNumber);
	}

}
