package com.ibiz.core.module2.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import com.ibiz.core.client.model.Ihealthcheck;
import com.ibiz.core.module2.domain.Healthcheck;
import com.ibiz.core.module2.filter.HealthcheckSearchContext;
import com.ibiz.core.module2.service.IHealthcheckService;
import org.springframework.beans.factory.annotation.Autowired;
import com.ibiz.util.SearchResult;

/**
 * 实体[HEALTHCHECK] 服务对象接口实现
 */
@Service
public class HealthcheckServiceImpl implements IHealthcheckService {

    @Autowired
    private com.ibiz.core.client.service.IhealthcheckClientService healthcheckClientService;

    @Override
    public Healthcheck getDraft(Healthcheck healthcheck) {
        return healthcheck;
    }

    @Override
    public boolean remove(String healthcheckid) {
        boolean bOk = false ;
        Ihealthcheck clientModel = healthcheckClientService.createModel();
        clientModel.setHEALTHCHECKID(healthcheckid);
        healthcheckClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean checkKey(Healthcheck healthcheck) {
        return true;
    }

    @Override
    public boolean update(Healthcheck healthcheck) {
        boolean bOk = false ;
        Ihealthcheck clientModel = this.convert2Model(healthcheck,null);
        healthcheckClientService.update(clientModel) ;
        this.convert2Domain(clientModel,healthcheck) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean create(Healthcheck healthcheck) {
        boolean bOk = false ;
        Ihealthcheck clientModel = this.convert2Model(healthcheck,null);
        healthcheckClientService.create(clientModel) ;
        this.convert2Domain(clientModel,healthcheck);
        bOk = true ;

        return bOk ;
    }

    @Override
    public Healthcheck test(Healthcheck healthcheck) {
        
        Ihealthcheck clientModel = this.convert2Model(healthcheck,null);
        healthcheckClientService.test(clientModel) ;
        this.convert2Domain(clientModel,null);
        return healthcheck;
    }

    @Override
    public Healthcheck get(String healthcheckid) {
        Ihealthcheck clientModel = healthcheckClientService.createModel();
        clientModel.setHEALTHCHECKID(healthcheckid);
        healthcheckClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Healthcheck get(Healthcheck healthcheck) {
        Healthcheck rethealthcheck = this.get(healthcheck.getHealthcheckid()) ;
    	BeanCopier copier=BeanCopier.create(rethealthcheck.getClass(), healthcheck.getClass(), false);
        copier.copy(rethealthcheck, healthcheck, null);
        return healthcheck ;
    }

    @Override
    public boolean save(Healthcheck healthcheck) {
        if(healthcheck.getHealthcheckid()==null){
    		return create(healthcheck);
    	}else{
            if(this.get(healthcheck.getHealthcheckid())!=null){
    			return update(healthcheck);
    		}
    		return create(healthcheck);
    	}
    }

    /**
     * 查询集合 DEFAULT
     */
    @Override
    public Page<Healthcheck> searchDefault(HealthcheckSearchContext context) {
        List<Healthcheck> list = new ArrayList<Healthcheck>();
        Page<com.ibiz.core.client.model.Ihealthcheck> clientModelList = healthcheckClientService.fetchDefault(context) ;
        for(com.ibiz.core.client.model.Ihealthcheck clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Healthcheck>(list, context.getPageable(), clientModelList.getTotalElements());
    }




    public Ihealthcheck convert2Model(Healthcheck domain , Ihealthcheck model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = healthcheckClientService.createModel() ;
        }

         if(domain.getHealthcheckidDirtyFlag())
            model.setHEALTHCHECKID(domain.getHealthcheckid());
        return model ;
    }

    public Healthcheck convert2Domain( Ihealthcheck model ,Healthcheck domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Healthcheck();
        }

        if(model.getHEALTHCHECKIDDirtyFlag())
            domain.setHealthcheckid(model.getHEALTHCHECKID());
        return domain ;
    }


}
