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.ICPXH;
import com.sinosig.core.external.domain.CPXH;
import com.sinosig.core.external.filter.CPXHSearchContext;
import com.sinosig.core.external.service.ICPXHService;
import org.springframework.beans.factory.annotation.Autowired;
import com.sinosig.util.SearchResult;

/**
 * 实体[CPXH] 服务对象接口实现
 */
@Service
public class CPXHServiceImpl implements ICPXHService {

    @Autowired
    private com.sinosig.core.client.service.ICPXHClientService cPXHClientService;




    @Override
    public boolean create(CPXH cpxh) {
        boolean bOk = false ;
        ICPXH clientModel = this.convert2Model(cpxh,null);
        cPXHClientService.create(clientModel) ;
        this.convert2Domain(clientModel,cpxh);
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean update(CPXH cpxh) {
        boolean bOk = false ;
        ICPXH clientModel = this.convert2Model(cpxh,null);
        cPXHClientService.update(clientModel) ;
        this.convert2Domain(clientModel,cpxh) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public CPXH getDraft(CPXH cpxh) {
        return cpxh;
    }

    @Override
    public boolean remove(String cpxhid) {
        boolean bOk = false ;
        ICPXH clientModel = cPXHClientService.createModel();
        clientModel.setCPXHID(cpxhid);
        cPXHClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean checkKey(CPXH cpxh) {
        return true;
    }
    @Override
    public boolean save(CPXH cpxh) {
        if(cpxh.getCPXHId()==null){
    		return create(cpxh);
    	}else{
            if(this.get(cpxh.getCPXHId())!=null){
    			return update(cpxh);
    		}
    		return create(cpxh);
    	}
    }

    @Override
    public CPXH get(String cpxhid) {
        ICPXH clientModel = cPXHClientService.createModel();
        clientModel.setCPXHID(cpxhid);
        cPXHClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public CPXH get(CPXH cpxh) {
        CPXH retcpxh = this.get(cpxh.getCPXHId()) ;
    	BeanCopier copier=BeanCopier.create(retcpxh.getClass(), cpxh.getClass(), false);
        copier.copy(retcpxh, cpxh, null);
        return cpxh ;
    }

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




    public ICPXH convert2Model(CPXH domain , ICPXH model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = cPXHClientService.createModel() ;
        }

         if(domain.getGlDirtyFlag())
            model.setGL(domain.getGl());
         if(domain.getCPXHNameDirtyFlag())
            model.setCPXHName(domain.getCPXHName());
         if(domain.getXcgzjDirtyFlag())
            model.setXCGZJ(domain.getXcgzj());
         if(domain.getHdzzlDirtyFlag())
            model.setHDZZL(domain.getHdzzl());
         if(domain.getAblDirtyFlag())
            model.setABL(domain.getAbl());
         if(domain.getCllxmsDirtyFlag())
            model.setCLLXMS(domain.getCllxms());
         if(domain.getHdzkDirtyFlag())
            model.setHDZK(domain.getHdzk());
         if(domain.getZbzlDirtyFlag())
            model.setZBZL(domain.getZbzl());
         if(domain.getDycpxhDirtyFlag())
            model.setDYCPXH(domain.getDycpxh());
         if(domain.getPlDirtyFlag())
            model.setPL(domain.getPl());
         if(domain.getCPXHIdDirtyFlag())
            model.setCPXHID(domain.getCPXHId());
         if(domain.getClsjjzDirtyFlag())
            model.setCLSJJZ(domain.getClsjjz());
        return model ;
    }

    public CPXH convert2Domain( ICPXH model ,CPXH domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new CPXH();
        }

        if(model.getGLDirtyFlag())
            domain.setGl(model.getGL());
        if(model.getCPXHNameDirtyFlag())
            domain.setCPXHName(model.getCPXHName());
        if(model.getXCGZJDirtyFlag())
            domain.setXcgzj(model.getXCGZJ());
        if(model.getHDZZLDirtyFlag())
            domain.setHdzzl(model.getHDZZL());
        if(model.getABLDirtyFlag())
            domain.setAbl(model.getABL());
        if(model.getCLLXMSDirtyFlag())
            domain.setCllxms(model.getCLLXMS());
        if(model.getHDZKDirtyFlag())
            domain.setHdzk(model.getHDZK());
        if(model.getZBZLDirtyFlag())
            domain.setZbzl(model.getZBZL());
        if(model.getDYCPXHDirtyFlag())
            domain.setDycpxh(model.getDYCPXH());
        if(model.getPLDirtyFlag())
            domain.setPl(model.getPL());
        if(model.getCPXHIDDirtyFlag())
            domain.setCPXHId(model.getCPXHID());
        if(model.getCLSJJZDirtyFlag())
            domain.setClsjjz(model.getCLSJJZ());
        return domain ;
    }

}
