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.IIM;
import com.ibiz.core.module2.domain.IM;
import com.ibiz.core.module2.filter.IMSearchContext;
import com.ibiz.core.module2.service.IIMService;
import org.springframework.beans.factory.annotation.Autowired;
import com.ibiz.util.SearchResult;

/**
 * 实体[重要事项] 服务对象接口实现
 */
@Service
public class IMServiceImpl implements IIMService {

    @Autowired
    private com.ibiz.core.client.service.IIMClientService iMClientService;

    @Override
    public boolean create(IM im) {
        boolean bOk = false ;
        IIM clientModel = this.convert2Model(im,null);
        iMClientService.create(clientModel) ;
        this.convert2Domain(clientModel,im);
        bOk = true ;

        return bOk ;
    }

    @Override
    public IM get(String imid) {
        IIM clientModel = iMClientService.createModel();
        clientModel.setIMId(imid);
        iMClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public IM get(IM im) {
        IM retim = this.get(im.getIMId()) ;
    	BeanCopier copier=BeanCopier.create(retim.getClass(), im.getClass(), false);
        copier.copy(retim, im, null);
        return im ;
    }

    @Override
    public boolean checkKey(IM im) {
        return true;
    }

    @Override
    public boolean save(IM im) {
        if(im.getIMId()==null){
    		return create(im);
    	}else{
            if(this.get(im.getIMId())!=null){
    			return update(im);
    		}
    		return create(im);
    	}
    }

    @Override
    public boolean remove(String imid) {
        boolean bOk = false ;
        IIM clientModel = iMClientService.createModel();
        clientModel.setIMId(imid);
        iMClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public IM createAndStart(IM im) {
        
        IIM clientModel = this.convert2Model(im,null);
        iMClientService.createAndStart(clientModel) ;
        this.convert2Domain(clientModel,null);
        return im;
    }

    @Override
    public IM getDraft(IM im) {
        return im;
    }

    @Override
    public boolean update(IM im) {
        boolean bOk = false ;
        IIM clientModel = this.convert2Model(im,null);
        iMClientService.update(clientModel) ;
        this.convert2Domain(clientModel,im) ;
        bOk = true;
        return bOk ;
    }

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




    public IIM convert2Model(IM domain , IIM model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = iMClientService.createModel() ;
        }

         if(domain.getKgsldidsDirtyFlag())
            model.setKgsldids(domain.getKgsldids());
         if(domain.getBzDirtyFlag())
            model.setBz(domain.getBz());
         if(domain.getSblbDirtyFlag())
            model.setSblb(domain.getSblb());
         if(domain.getBmldidsDirtyFlag())
            model.setBmldids(domain.getBmldids());
         if(domain.getJjyyDirtyFlag())
            model.setJjyy(domain.getJjyy());
         if(domain.getCreateDateDirtyFlag())
            model.setCreateDate(domain.getCreateDate());
         if(domain.getHqbmDirtyFlag())
            model.setHqbm(domain.getHqbm());
         if(domain.getSdfsDirtyFlag())
            model.setSdfs(domain.getSdfs());
         if(domain.getKgsbmidsDirtyFlag())
            model.setKgsbmids(domain.getKgsbmids());
         if(domain.getSjbmDirtyFlag())
            model.setSjbm(domain.getSjbm());
         if(domain.getQttzyddxDirtyFlag())
            model.setQttzyddx(domain.getQttzyddx());
         if(domain.getLdDirtyFlag())
            model.setLd(domain.getLd());
         if(domain.getIMNameDirtyFlag())
            model.setIMName(domain.getIMName());
         if(domain.getLcbhDirtyFlag())
            model.setLcbh(domain.getLcbh());
         if(domain.getNgshidsDirtyFlag())
            model.setNgshids(domain.getNgshids());
         if(domain.getKgsldDirtyFlag())
            model.setKgsld(domain.getKgsld());
         if(domain.getShfsDirtyFlag())
            model.setShfs(domain.getShfs());
         if(domain.getUpdateDateDirtyFlag())
            model.setUpdateDate(domain.getUpdateDate());
         if(domain.getIMIdDirtyFlag())
            model.setIMId(domain.getIMId());
         if(domain.getSblxDirtyFlag())
            model.setSblx(domain.getSblx());
         if(domain.getTzydDirtyFlag())
            model.setTzyd(domain.getTzyd());
         if(domain.getWfinstanceidDirtyFlag())
            model.setWfinstanceid(domain.getWfinstanceid());
         if(domain.getBmldDirtyFlag())
            model.setBmld(domain.getBmld());
         if(domain.getTzydyyDirtyFlag())
            model.setTzydyy(domain.getTzydyy());
         if(domain.getImwfstateDirtyFlag())
            model.setImwfstate(domain.getImwfstate());
         if(domain.getCreateManDirtyFlag())
            model.setCreateMan(domain.getCreateMan());
         if(domain.getYzdxDirtyFlag())
            model.setYzdx(domain.getYzdx());
         if(domain.getNgshDirtyFlag())
            model.setNgsh(domain.getNgsh());
         if(domain.getHjcdDirtyFlag())
            model.setHjcd(domain.getHjcd());
         if(domain.getUpdateManDirtyFlag())
            model.setUpdateMan(domain.getUpdateMan());
         if(domain.getLdidsDirtyFlag())
            model.setLdids(domain.getLdids());
         if(domain.getHqbmidsDirtyFlag())
            model.setHqbmids(domain.getHqbmids());
         if(domain.getKgsbmDirtyFlag())
            model.setKgsbm(domain.getKgsbm());
         if(domain.getWfstepDirtyFlag())
            model.setWfstep(domain.getWfstep());
         if(domain.getZbbmDirtyFlag())
            model.setZbbm(domain.getZbbm());
         if(domain.getNgrqDirtyFlag())
            model.setNgrq(domain.getNgrq());
        return model ;
    }

    public IM convert2Domain( IIM model ,IM domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new IM();
        }

        if(model.getKgsldidsDirtyFlag())
            domain.setKgsldids(model.getKgsldids());
        if(model.getBzDirtyFlag())
            domain.setBz(model.getBz());
        if(model.getSblbDirtyFlag())
            domain.setSblb(model.getSblb());
        if(model.getBmldidsDirtyFlag())
            domain.setBmldids(model.getBmldids());
        if(model.getJjyyDirtyFlag())
            domain.setJjyy(model.getJjyy());
        if(model.getCreateDateDirtyFlag())
            domain.setCreateDate(model.getCreateDate());
        if(model.getHqbmDirtyFlag())
            domain.setHqbm(model.getHqbm());
        if(model.getSdfsDirtyFlag())
            domain.setSdfs(model.getSdfs());
        if(model.getKgsbmidsDirtyFlag())
            domain.setKgsbmids(model.getKgsbmids());
        if(model.getSjbmDirtyFlag())
            domain.setSjbm(model.getSjbm());
        if(model.getQttzyddxDirtyFlag())
            domain.setQttzyddx(model.getQttzyddx());
        if(model.getLdDirtyFlag())
            domain.setLd(model.getLd());
        if(model.getIMNameDirtyFlag())
            domain.setIMName(model.getIMName());
        if(model.getLcbhDirtyFlag())
            domain.setLcbh(model.getLcbh());
        if(model.getNgshidsDirtyFlag())
            domain.setNgshids(model.getNgshids());
        if(model.getKgsldDirtyFlag())
            domain.setKgsld(model.getKgsld());
        if(model.getShfsDirtyFlag())
            domain.setShfs(model.getShfs());
        if(model.getUpdateDateDirtyFlag())
            domain.setUpdateDate(model.getUpdateDate());
        if(model.getIMIdDirtyFlag())
            domain.setIMId(model.getIMId());
        if(model.getSblxDirtyFlag())
            domain.setSblx(model.getSblx());
        if(model.getTzydDirtyFlag())
            domain.setTzyd(model.getTzyd());
        if(model.getWfinstanceidDirtyFlag())
            domain.setWfinstanceid(model.getWfinstanceid());
        if(model.getBmldDirtyFlag())
            domain.setBmld(model.getBmld());
        if(model.getTzydyyDirtyFlag())
            domain.setTzydyy(model.getTzydyy());
        if(model.getImwfstateDirtyFlag())
            domain.setImwfstate(model.getImwfstate());
        if(model.getCreateManDirtyFlag())
            domain.setCreateMan(model.getCreateMan());
        if(model.getYzdxDirtyFlag())
            domain.setYzdx(model.getYzdx());
        if(model.getNgshDirtyFlag())
            domain.setNgsh(model.getNgsh());
        if(model.getHjcdDirtyFlag())
            domain.setHjcd(model.getHjcd());
        if(model.getUpdateManDirtyFlag())
            domain.setUpdateMan(model.getUpdateMan());
        if(model.getLdidsDirtyFlag())
            domain.setLdids(model.getLdids());
        if(model.getHqbmidsDirtyFlag())
            domain.setHqbmids(model.getHqbmids());
        if(model.getKgsbmDirtyFlag())
            domain.setKgsbm(model.getKgsbm());
        if(model.getWfstepDirtyFlag())
            domain.setWfstep(model.getWfstep());
        if(model.getZbbmDirtyFlag())
            domain.setZbbm(model.getZbbm());
        if(model.getNgrqDirtyFlag())
            domain.setNgrq(model.getNgrq());
        return domain ;
    }


}
