package com.sinosig.core.external.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.sinosig.core.client.model.IJQXBJ;
import com.sinosig.core.external.domain.JQXBJ;
import com.sinosig.core.external.filter.JQXBJSearchContext;
import com.sinosig.core.external.service.IJQXBJService;
import org.springframework.beans.factory.annotation.Autowired;
import com.sinosig.util.SearchResult;

/**
 * 实体[JQXBJ] 服务对象接口实现
 */
@Service
public class JQXBJServiceImpl implements IJQXBJService {

    @Autowired
    private com.sinosig.core.client.service.IJQXBJClientService jQXBJClientService;




    @Override
    public boolean update(JQXBJ jqxbj) {
        boolean bOk = false ;
        IJQXBJ clientModel = this.convert2Model(jqxbj,null);
        jQXBJClientService.update(clientModel) ;
        this.convert2Domain(clientModel,jqxbj) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public JQXBJ get(String jqxbjid) {
        IJQXBJ clientModel = jQXBJClientService.createModel();
        clientModel.setJQXBJId(jqxbjid);
        jQXBJClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public JQXBJ get(JQXBJ jqxbj) {
        JQXBJ retjqxbj = this.get(jqxbj.getJQXBJId()) ;
    	BeanCopier copier=BeanCopier.create(retjqxbj.getClass(), jqxbj.getClass(), false);
        copier.copy(retjqxbj, jqxbj, null);
        return jqxbj ;
    }

    @Override
    public boolean remove(String jqxbjid) {
        boolean bOk = false ;
        IJQXBJ clientModel = jQXBJClientService.createModel();
        clientModel.setJQXBJId(jqxbjid);
        jQXBJClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean save(JQXBJ jqxbj) {
        if(jqxbj.getJQXBJId()==null){
    		return create(jqxbj);
    	}else{
            if(this.get(jqxbj.getJQXBJId())!=null){
    			return update(jqxbj);
    		}
    		return create(jqxbj);
    	}
    }

    @Override
    public boolean create(JQXBJ jqxbj) {
        boolean bOk = false ;
        IJQXBJ clientModel = this.convert2Model(jqxbj,null);
        jQXBJClientService.create(clientModel) ;
        this.convert2Domain(clientModel,jqxbj);
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean checkKey(JQXBJ jqxbj) {
        return true;
    }
    @Override
    public JQXBJ getDraft(JQXBJ jqxbj) {
        return jqxbj;
    }

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




    public IJQXBJ convert2Model(JQXBJ domain , IJQXBJ model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = jQXBJClientService.createModel() ;
        }

         if(domain.getFlxsDirtyFlag())
            model.setFlxs(domain.getFlxs());
         if(domain.getJQXBJNameDirtyFlag())
            model.setJQXBJName(domain.getJQXBJName());
         if(domain.getSjbfDirtyFlag())
            model.setSjbf(domain.getSjbf());
         if(domain.getZrxeDirtyFlag())
            model.setZrxe(domain.getZrxe());
         if(domain.getJQXBJIdDirtyFlag())
            model.setJQXBJId(domain.getJQXBJId());
         if(domain.getTkdmDirtyFlag())
            model.setTkdm(domain.getTkdm());
         if(domain.getTkmcDirtyFlag())
            model.setTkmc(domain.getTkmc());
         if(domain.getJzbfDirtyFlag())
            model.setJzbf(domain.getJzbf());
        return model ;
    }

    public JQXBJ convert2Domain( IJQXBJ model ,JQXBJ domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new JQXBJ();
        }

        if(model.getFlxsDirtyFlag())
            domain.setFlxs(model.getFlxs());
        if(model.getJQXBJNameDirtyFlag())
            domain.setJQXBJName(model.getJQXBJName());
        if(model.getSjbfDirtyFlag())
            domain.setSjbf(model.getSjbf());
        if(model.getZrxeDirtyFlag())
            domain.setZrxe(model.getZrxe());
        if(model.getJQXBJIdDirtyFlag())
            domain.setJQXBJId(model.getJQXBJId());
        if(model.getTkdmDirtyFlag())
            domain.setTkdm(model.getTkdm());
        if(model.getTkmcDirtyFlag())
            domain.setTkmc(model.getTkmc());
        if(model.getJzbfDirtyFlag())
            domain.setJzbf(model.getJzbf());
        return domain ;
    }

}
