package com.sinosig.core.module.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.module.domain.CID;
import com.sinosig.core.module.filter.CIDSearchContext;
import com.sinosig.core.module.service.ICIDService;
import com.sinosig.core.module.service.logic.ICIDGetOrgNameByUserLogic;
import com.sinosig.core.module.service.logic.ICIDGetCPXHXXLogic;
import org.springframework.beans.factory.annotation.Autowired;
import com.sinosig.util.SearchResult;

/**
 * 实体[CID] 服务对象接口实现
 */
@Service
public class CIDServiceImpl implements ICIDService {

    @Autowired
    com.sinosig.core.repository.CIDRepository cIDRepository;



    @Autowired
    private ICIDGetOrgNameByUserLogic getorgnamebyuserLogic ;

    @Autowired
    private ICIDGetCPXHXXLogic getcpxhxxLogic ;


    @Override
    public boolean checkKey(CID cid) {
        return true;
    }
    @Override
    public CID createTemp(CID cid) {
         return cid;
    }

    @Override
    public CID removeTemp(CID cid) {
         return cid;
    }

    @Override
    public CID getMRSYXXX(CID cid) {
         return cid;
    }
    @Override
    public boolean create(CID cid) {
        boolean bOk = false ;
        com.sinosig.core.repository.po.CID t_CID = cIDRepository.convert2PO(cid,null);
        bOk = cIDRepository.create(t_CID);
        cIDRepository.convert2Domain(t_CID,cid);

        return bOk ;
    }

    @Override
    public CID getOrgNameByUser(CID cid) {
         getorgnamebyuserLogic.execute(cid);
         return cid ;
    }
    @Override
    public CID removeTempMajor(CID cid) {
         return cid;
    }

    @Override
    public CID getTempMajor(CID cid) {
         return cid;
    }

    @Override
    public CID get(String cidid) {
        com.sinosig.core.repository.po.CID t_CID = cIDRepository.get(cidid);
        return cIDRepository.convert2Domain(t_CID,null);
    }

    @Override
    public CID get(CID cid) {
        com.sinosig.core.repository.po.CID t_CID = cIDRepository.get(cid.getCIDId());
        cIDRepository.convert2Domain(t_CID,cid);
        return cid;
    }

    @Override
    public CID getDraftTemp(CID cid) {
         return cid;
    }

    @Override
    public CID updateTempMajor(CID cid) {
         return cid;
    }

    @Override
    public CID updateTemp(CID cid) {
         return cid;
    }

    @Override
    public CID getDraft(CID cid) {
        return cid;
    }

    @Override
    public CID jSBF(CID cid) {
         return cid;
    }
    @Override
    public CID getMRJQXXX(CID cid) {
         return cid;
    }
    @Override
    public boolean update(CID cid) {
        boolean bOk = false ;
        com.sinosig.core.repository.po.CID t_CID = cIDRepository.convert2PO(cid,null);
        bOk = cIDRepository.update(t_CID);
        cIDRepository.convert2Domain(t_CID,cid) ;

        return bOk ;
    }

    @Override
    public CID getTemp(CID cid) {
         return cid;
    }

    @Override
    public boolean save(CID cid) {
        if(cid.getCIDId()==null){
    		return create(cid);
    	}else{
            if(this.get(cid.getCIDId())!=null){
    			return update(cid);
    		}
    		return create(cid);
    	}
    }

    @Override
    public CID getDraftTempMajor(CID cid) {
         return cid;
    }

    @Override
    public boolean remove(String cidid) {
        boolean bOk = false ;
        bOk = cIDRepository.remove(cidid);

        return bOk ;
    }

    @Override
    public CID getCPXHXX(CID cid) {
         getcpxhxxLogic.execute(cid);
         return cid ;
    }
    @Override
    public CID createTempMajor(CID cid) {
         return cid;
    }

    /**
     * 查询集合 DEFAULT
     */
    @Override
    public Page<CID> searchDefault(CIDSearchContext context) {
        List<CID> list = new ArrayList<CID>();
        Page<com.sinosig.core.repository.po.CID> t_CIDs = cIDRepository.searchDefault(context) ;
        for(com.sinosig.core.repository.po.CID t_CID : t_CIDs){
            list.add(cIDRepository.convert2Domain(t_CID,null));
        }
        return new PageImpl<CID>(list, context.getPageable(), t_CIDs.getTotalElements());
    }


    /**
     * 根据厂牌型号查询
     */
    @Override
    public Page<CID> selectByCPXH(String cpxhid ){
        CIDSearchContext context = new CIDSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(cpxhid)){
            com.sinosig.util.SearchFieldFilter fieldFilter=new com.sinosig.util.SearchFieldFilter();
            fieldFilter.setParam("cpxhid");
            fieldFilter.setCondition(com.sinosig.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(cpxhid);
            context.getCondition().add(fieldFilter);
        }
        List<CID> list = new ArrayList<CID>();
        Page<com.sinosig.core.repository.po.CID> t_CIDs = cIDRepository.searchDefault(context) ;
        for(com.sinosig.core.repository.po.CID t_CID : t_CIDs){
            list.add(cIDRepository.convert2Domain(t_CID,null));
        }
        return new PageImpl<CID>(list, context.getPageable(), t_CIDs.getTotalElements());
    }

    /**
     * 根据厂牌型号查询
     */
    @Override
    public List<CID> selectByCPXH2(String cpxhid ){

        CIDSearchContext context = new CIDSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(cpxhid)){
            com.sinosig.util.SearchFieldFilter fieldFilter=new com.sinosig.util.SearchFieldFilter();
            fieldFilter.setParam("cpxhid");
            fieldFilter.setCondition(com.sinosig.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(cpxhid);
            context.getCondition().add(fieldFilter);
        }
        List<CID> list = new ArrayList<CID>();
        Page<com.sinosig.core.repository.po.CID> t_CIDs = cIDRepository.searchDefault(context) ;
        for(com.sinosig.core.repository.po.CID t_CID : t_CIDs){
            list.add(cIDRepository.convert2Domain(t_CID,null));
        }
        return list;
    }
    /**
     * 根据人员查询
     */
    @Override
    public Page<CID> selectByORGUSER(String orguserid ){
        CIDSearchContext context = new CIDSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(orguserid)){
            com.sinosig.util.SearchFieldFilter fieldFilter=new com.sinosig.util.SearchFieldFilter();
            fieldFilter.setParam("orguserid");
            fieldFilter.setCondition(com.sinosig.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(orguserid);
            context.getCondition().add(fieldFilter);
        }
        List<CID> list = new ArrayList<CID>();
        Page<com.sinosig.core.repository.po.CID> t_CIDs = cIDRepository.searchDefault(context) ;
        for(com.sinosig.core.repository.po.CID t_CID : t_CIDs){
            list.add(cIDRepository.convert2Domain(t_CID,null));
        }
        return new PageImpl<CID>(list, context.getPageable(), t_CIDs.getTotalElements());
    }

    /**
     * 根据人员查询
     */
    @Override
    public List<CID> selectByORGUSER2(String orguserid ){

        CIDSearchContext context = new CIDSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(orguserid)){
            com.sinosig.util.SearchFieldFilter fieldFilter=new com.sinosig.util.SearchFieldFilter();
            fieldFilter.setParam("orguserid");
            fieldFilter.setCondition(com.sinosig.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(orguserid);
            context.getCondition().add(fieldFilter);
        }
        List<CID> list = new ArrayList<CID>();
        Page<com.sinosig.core.repository.po.CID> t_CIDs = cIDRepository.searchDefault(context) ;
        for(com.sinosig.core.repository.po.CID t_CID : t_CIDs){
            list.add(cIDRepository.convert2Domain(t_CID,null));
        }
        return list;
    }
    /**
     * 根据机构查询
     */
    @Override
    public Page<CID> selectByORG(String orgid ){
        CIDSearchContext context = new CIDSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(orgid)){
            com.sinosig.util.SearchFieldFilter fieldFilter=new com.sinosig.util.SearchFieldFilter();
            fieldFilter.setParam("orgid");
            fieldFilter.setCondition(com.sinosig.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(orgid);
            context.getCondition().add(fieldFilter);
        }
        List<CID> list = new ArrayList<CID>();
        Page<com.sinosig.core.repository.po.CID> t_CIDs = cIDRepository.searchDefault(context) ;
        for(com.sinosig.core.repository.po.CID t_CID : t_CIDs){
            list.add(cIDRepository.convert2Domain(t_CID,null));
        }
        return new PageImpl<CID>(list, context.getPageable(), t_CIDs.getTotalElements());
    }

    /**
     * 根据机构查询
     */
    @Override
    public List<CID> selectByORG2(String orgid ){

        CIDSearchContext context = new CIDSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(orgid)){
            com.sinosig.util.SearchFieldFilter fieldFilter=new com.sinosig.util.SearchFieldFilter();
            fieldFilter.setParam("orgid");
            fieldFilter.setCondition(com.sinosig.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(orgid);
            context.getCondition().add(fieldFilter);
        }
        List<CID> list = new ArrayList<CID>();
        Page<com.sinosig.core.repository.po.CID> t_CIDs = cIDRepository.searchDefault(context) ;
        for(com.sinosig.core.repository.po.CID t_CID : t_CIDs){
            list.add(cIDRepository.convert2Domain(t_CID,null));
        }
        return list;
    }



}
