package com.ibiz.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 org.springframework.transaction.annotation.Transactional;
import com.ibiz.core.module.domain.PS;
import com.ibiz.core.module.filter.PSSearchContext;
import com.ibiz.core.module.service.IPSService;
import com.ibiz.core.module.service.logic.IPSXDLogic;
import com.ibiz.core.module.service.logic.IPSCreateAndStartIMLogic;
import org.springframework.beans.factory.annotation.Autowired;
import com.ibiz.util.SearchResult;

/**
 * 实体[采购单] 服务对象接口实现
 */
@Service
public class PSServiceImpl implements IPSService {

    @Autowired
    com.ibiz.core.repository.PSRepository pSRepository;

    @Autowired
    private IPSXDLogic xdLogic ;

    @Autowired
    private IPSCreateAndStartIMLogic createandstartimLogic ;

    @Override
    @Transactional
    public boolean remove(String psid) {
        boolean bOk = false ;

        PS _pS = this.get(psid);
        bOk = pSRepository.remove(psid);

        return bOk ;
    }

    @Override
    @Transactional
    public boolean create(PS ps) {
        boolean bOk = false ;
        com.ibiz.core.repository.po.PS t_PS = pSRepository.convert2PO(ps,null);
        bOk = pSRepository.create(t_PS);
        pSRepository.convert2Domain(t_PS,ps);


        return bOk ;
    }

    @Override
    @Transactional
    public PS get(String psid) {
        com.ibiz.core.repository.po.PS _t_PS = pSRepository.get(psid);
        if(_t_PS == null)
            return null ;

        PS _pS = pSRepository.convert2Domain(_t_PS,null) ;

        return _pS;
    }

    @Override
    @Transactional
    public PS get(PS ps) {
        com.ibiz.core.repository.po.PS t_PS = pSRepository.get(ps.getPSId());
        pSRepository.convert2Domain(t_PS,ps);
        return ps;
    }

    @Override
    @Transactional
    public PS xD(PS ps) {
         xdLogic.execute(ps);
         return ps ;
    }

    @Override
    public PS getDraft(PS ps) {
        return ps;
    }

    @Override
    @Transactional
    public boolean save(PS ps) {
        if(ps.getPSId()==null){
    		return create(ps);
    	}else{
            if(this.get(ps.getPSId())!=null){
    			return update(ps);
    		}
    		return create(ps);
    	}
    }

    @Override
    @Transactional
    public boolean update(PS ps) {
        boolean bOk = false ;
        com.ibiz.core.repository.po.PS t_PS = pSRepository.convert2PO(ps,null);
        bOk = pSRepository.update(t_PS);
        pSRepository.convert2Domain(t_PS,ps) ;
        return bOk ;
    }

    @Override
    @Transactional
    public PS createAndStartIM(PS ps) {
         createandstartimLogic.execute(ps);
         return ps ;
    }

    @Override
    public boolean checkKey(PS ps) {
        return true;
    }

    /**
     * 查询集合 DEFAULT
     */
    @Override
    public Page<PS> searchDefault(PSSearchContext context) {
        List<PS> list = new ArrayList<PS>();
        Page<com.ibiz.core.repository.po.PS> t_PSs = pSRepository.searchDefault(context) ;
        for(com.ibiz.core.repository.po.PS t_PS : t_PSs){
            list.add(pSRepository.convert2Domain(t_PS,null));
        }
        return new PageImpl<PS>(list, context.getPageable(), t_PSs.getTotalElements());
    }






}
