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.ISYXBJ;
import com.sinosig.core.external.domain.SYXBJ;
import com.sinosig.core.external.filter.SYXBJSearchContext;
import com.sinosig.core.external.service.ISYXBJService;
import org.springframework.beans.factory.annotation.Autowired;
import com.sinosig.util.SearchResult;

/**
 * 实体[SYXBJ] 服务对象接口实现
 */
@Service
public class SYXBJServiceImpl implements ISYXBJService {

    @Autowired
    private com.sinosig.core.client.service.ISYXBJClientService sYXBJClientService;




    @Override
    public boolean save(SYXBJ syxbj) {
        if(syxbj.getSYXBJId()==null){
    		return create(syxbj);
    	}else{
            if(this.get(syxbj.getSYXBJId())!=null){
    			return update(syxbj);
    		}
    		return create(syxbj);
    	}
    }

    @Override
    public boolean remove(String syxbjid) {
        boolean bOk = false ;
        ISYXBJ clientModel = sYXBJClientService.createModel();
        clientModel.setSYXBJId(syxbjid);
        sYXBJClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public SYXBJ getDraft(SYXBJ syxbj) {
        return syxbj;
    }

    @Override
    public boolean checkKey(SYXBJ syxbj) {
        return true;
    }
    @Override
    public boolean update(SYXBJ syxbj) {
        boolean bOk = false ;
        ISYXBJ clientModel = this.convert2Model(syxbj,null);
        sYXBJClientService.update(clientModel) ;
        this.convert2Domain(clientModel,syxbj) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean create(SYXBJ syxbj) {
        boolean bOk = false ;
        ISYXBJ clientModel = this.convert2Model(syxbj,null);
        sYXBJClientService.create(clientModel) ;
        this.convert2Domain(clientModel,syxbj);
        bOk = true ;
        return bOk ;
    }

    @Override
    public SYXBJ get(String syxbjid) {
        ISYXBJ clientModel = sYXBJClientService.createModel();
        clientModel.setSYXBJId(syxbjid);
        sYXBJClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public SYXBJ get(SYXBJ syxbj) {
        SYXBJ retsyxbj = this.get(syxbj.getSYXBJId()) ;
    	BeanCopier copier=BeanCopier.create(retsyxbj.getClass(), syxbj.getClass(), false);
        copier.copy(retsyxbj, syxbj, null);
        return syxbj ;
    }

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




    public ISYXBJ convert2Model(SYXBJ domain , ISYXBJ model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = sYXBJClientService.createModel() ;
        }

         if(domain.getSYXBJNameDirtyFlag())
            model.setSYXBJName(domain.getSYXBJName());
         if(domain.getFlDirtyFlag())
            model.setFl(domain.getFl());
         if(domain.getPtfhxsDirtyFlag())
            model.setPtfhxs(domain.getPtfhxs());
         if(domain.getJzbfDirtyFlag())
            model.setJzbf(domain.getJzbf());
         if(domain.getTkmcDirtyFlag())
            model.setTkmc(domain.getTkmc());
         if(domain.getMdDirtyFlag())
            model.setMd(domain.getMd());
         if(domain.getTkdmDirtyFlag())
            model.setTkdm(domain.getTkdm());
         if(domain.getSjbfDirtyFlag())
            model.setSjbf(domain.getSjbf());
         if(domain.getSYXBJIdDirtyFlag())
            model.setSYXBJId(domain.getSYXBJId());
        return model ;
    }

    public SYXBJ convert2Domain( ISYXBJ model ,SYXBJ domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new SYXBJ();
        }

        if(model.getSYXBJNameDirtyFlag())
            domain.setSYXBJName(model.getSYXBJName());
        if(model.getFlDirtyFlag())
            domain.setFl(model.getFl());
        if(model.getPtfhxsDirtyFlag())
            domain.setPtfhxs(model.getPtfhxs());
        if(model.getJzbfDirtyFlag())
            domain.setJzbf(model.getJzbf());
        if(model.getTkmcDirtyFlag())
            domain.setTkmc(model.getTkmc());
        if(model.getMdDirtyFlag())
            domain.setMd(model.getMd());
        if(model.getTkdmDirtyFlag())
            domain.setTkdm(model.getTkdm());
        if(model.getSjbfDirtyFlag())
            domain.setSjbf(model.getSjbf());
        if(model.getSYXBJIdDirtyFlag())
            domain.setSYXBJId(model.getSYXBJId());
        return domain ;
    }

}
