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.CUSTOMER;
import com.sinosig.core.module.filter.CUSTOMERSearchContext;
import com.sinosig.core.module.service.ICUSTOMERService;
import com.sinosig.core.module.service.logic.ICUSTOMERGetSFZXXLogic;
import org.springframework.beans.factory.annotation.Autowired;
import com.sinosig.util.SearchResult;

/**
 * 实体[CUSTOMER] 服务对象接口实现
 */
@Service
public class CUSTOMERServiceImpl implements ICUSTOMERService {

    @Autowired
    com.sinosig.core.repository.CUSTOMERRepository cUSTOMERRepository;



    @Autowired
    private ICUSTOMERGetSFZXXLogic getsfzxxLogic ;


    @Override
    public CUSTOMER getDraftTempMajor(CUSTOMER customer) {
         return customer;
    }

    @Override
    public boolean checkKey(CUSTOMER customer) {
        return true;
    }
    @Override
    public CUSTOMER getDraft(CUSTOMER customer) {
        return customer;
    }

    @Override
    public CUSTOMER removeTempMajor(CUSTOMER customer) {
         return customer;
    }

    @Override
    public CUSTOMER get(String customerid) {
        com.sinosig.core.repository.po.CUSTOMER t_CUSTOMER = cUSTOMERRepository.get(customerid);
        return cUSTOMERRepository.convert2Domain(t_CUSTOMER,null);
    }

    @Override
    public CUSTOMER get(CUSTOMER customer) {
        com.sinosig.core.repository.po.CUSTOMER t_CUSTOMER = cUSTOMERRepository.get(customer.getCUSTOMERId());
        cUSTOMERRepository.convert2Domain(t_CUSTOMER,customer);
        return customer;
    }

    @Override
    public CUSTOMER createTempMajor(CUSTOMER customer) {
         return customer;
    }

    @Override
    public CUSTOMER getTemp(CUSTOMER customer) {
         return customer;
    }

    @Override
    public CUSTOMER removeTemp(CUSTOMER customer) {
         return customer;
    }

    @Override
    public CUSTOMER getDraftTemp(CUSTOMER customer) {
         return customer;
    }

    @Override
    public CUSTOMER updateTempMajor(CUSTOMER customer) {
         return customer;
    }

    @Override
    public boolean create(CUSTOMER customer) {
        boolean bOk = false ;
        com.sinosig.core.repository.po.CUSTOMER t_CUSTOMER = cUSTOMERRepository.convert2PO(customer,null);
        bOk = cUSTOMERRepository.create(t_CUSTOMER);
        cUSTOMERRepository.convert2Domain(t_CUSTOMER,customer);

        return bOk ;
    }

    @Override
    public CUSTOMER updateTemp(CUSTOMER customer) {
         return customer;
    }

    @Override
    public boolean save(CUSTOMER customer) {
        if(customer.getCUSTOMERId()==null){
    		return create(customer);
    	}else{
            if(this.get(customer.getCUSTOMERId())!=null){
    			return update(customer);
    		}
    		return create(customer);
    	}
    }

    @Override
    public CUSTOMER getSFZXX(CUSTOMER customer) {
         getsfzxxLogic.execute(customer);
         return customer ;
    }
    @Override
    public boolean remove(String customerid) {
        boolean bOk = false ;
        bOk = cUSTOMERRepository.remove(customerid);

        return bOk ;
    }

    @Override
    public CUSTOMER createTemp(CUSTOMER customer) {
         return customer;
    }

    @Override
    public boolean update(CUSTOMER customer) {
        boolean bOk = false ;
        com.sinosig.core.repository.po.CUSTOMER t_CUSTOMER = cUSTOMERRepository.convert2PO(customer,null);
        bOk = cUSTOMERRepository.update(t_CUSTOMER);
        cUSTOMERRepository.convert2Domain(t_CUSTOMER,customer) ;

        return bOk ;
    }

    @Override
    public CUSTOMER getTempMajor(CUSTOMER customer) {
         return customer;
    }

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


    /**
     * 根据车险单查询
     */
    @Override
    public Page<CUSTOMER> selectByCID(String cidid ){
        CUSTOMERSearchContext context = new CUSTOMERSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(cidid)){
            com.sinosig.util.SearchFieldFilter fieldFilter=new com.sinosig.util.SearchFieldFilter();
            fieldFilter.setParam("cidid");
            fieldFilter.setCondition(com.sinosig.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(cidid);
            context.getCondition().add(fieldFilter);
        }
        List<CUSTOMER> list = new ArrayList<CUSTOMER>();
        Page<com.sinosig.core.repository.po.CUSTOMER> t_CUSTOMERs = cUSTOMERRepository.searchDefault(context) ;
        for(com.sinosig.core.repository.po.CUSTOMER t_CUSTOMER : t_CUSTOMERs){
            list.add(cUSTOMERRepository.convert2Domain(t_CUSTOMER,null));
        }
        return new PageImpl<CUSTOMER>(list, context.getPageable(), t_CUSTOMERs.getTotalElements());
    }

    /**
     * 根据车险单查询
     */
    @Override
    public List<CUSTOMER> selectByCID2(String cidid ){

        CUSTOMERSearchContext context = new CUSTOMERSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(cidid)){
            com.sinosig.util.SearchFieldFilter fieldFilter=new com.sinosig.util.SearchFieldFilter();
            fieldFilter.setParam("cidid");
            fieldFilter.setCondition(com.sinosig.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(cidid);
            context.getCondition().add(fieldFilter);
        }
        List<CUSTOMER> list = new ArrayList<CUSTOMER>();
        Page<com.sinosig.core.repository.po.CUSTOMER> t_CUSTOMERs = cUSTOMERRepository.searchDefault(context) ;
        for(com.sinosig.core.repository.po.CUSTOMER t_CUSTOMER : t_CUSTOMERs){
            list.add(cUSTOMERRepository.convert2Domain(t_CUSTOMER,null));
        }
        return list;
    }



}
