package com.ibiz.mybatis.external;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.math.BigInteger;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import com.ibiz.core.repository.ORGRepository;
import com.ibiz.core.repository.po.ORG;
import com.ibiz.core.external.filter.ORGSearchContext;
import com.ibiz.mybatis.external.mapper.T_ORGMapper;
import com.ibiz.mybatis.external.po.T_ORGImpl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ibiz.mybatis.util.helper.QueryBuilder;


/**
 * 关系型数据实体表[T_ORG] 存储对象
 */
@Repository
@ConditionalOnProperty(prefix = "org", value="store" , havingValue = "sql", matchIfMissing = true)
public class T_ORGRepositoryImpl extends ServiceImpl<T_ORGMapper, T_ORGImpl> implements ORGRepository {

    @Autowired
    QueryBuilder queryBuilder;

    private boolean isLogicValid = false ;

    @Autowired
	T_ORGMapper t_ORGMapper;

    public ORG createPO(){
        return new T_ORGImpl();
    }

    public ORG get(String key){
        return this.getById(key);
    }


    public boolean create(ORG po){
        return this.save((T_ORGImpl)po);
    }

    public boolean update(ORG po){
        return this.updateById((T_ORGImpl)po);
    }

    public boolean remove(String key){
        return this.removeById(key);
    }

    public Page<ORG> searchDefault(ORGSearchContext context){
        QueryWrapper qw=queryBuilder.buildQueryWrapper(context);
        com.baomidou.mybatisplus.extension.plugins.pagination.Page page=queryBuilder.buildPage(context);
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<ORG> page_datas=t_ORGMapper.searchDefault(page,context,qw);
        List<ORG> list_datas=queryBuilder.page2List(page_datas);
        return new PageImpl<ORG>(list_datas, context.getPageable(), page_datas.getTotal());
    }

    public Page<ORG> searchTopOrg(ORGSearchContext context){
        QueryWrapper qw=queryBuilder.buildQueryWrapper(context);
        com.baomidou.mybatisplus.extension.plugins.pagination.Page page=queryBuilder.buildPage(context);
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<ORG> page_datas=t_ORGMapper.searchTopOrg(page,context,qw);
        List<ORG> list_datas=queryBuilder.page2List(page_datas);
        return new PageImpl<ORG>(list_datas, context.getPageable(), page_datas.getTotal());
    }

    public Page<ORG> searchCurOrg(ORGSearchContext context){
        QueryWrapper qw=queryBuilder.buildQueryWrapper(context);
        com.baomidou.mybatisplus.extension.plugins.pagination.Page page=queryBuilder.buildPage(context);
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<ORG> page_datas=t_ORGMapper.searchCurOrg(page,context,qw);
        List<ORG> list_datas=queryBuilder.page2List(page_datas);
        return new PageImpl<ORG>(list_datas, context.getPageable(), page_datas.getTotal());
    }


    public com.ibiz.core.external.domain.ORG convert2Domain( ORG po ,com.ibiz.core.external.domain.ORG domain){
              if(po == null)
                     return domain ;
              if(domain == null){
                     domain = new com.ibiz.core.external.domain.ORG();
              }
            //if(po.getORGIdDirtyFlag())
                domain.setORGId(po.getORGId());
            //if(po.getUpdateDateDirtyFlag())
                domain.setUpdateDate(po.getUpdateDate());
            //if(po.getCreateManDirtyFlag())
                domain.setCreateMan(po.getCreateMan());
            //if(po.getORGNameDirtyFlag())
                domain.setORGName(po.getORGName());
            //if(po.getUpdateManDirtyFlag())
                domain.setUpdateMan(po.getUpdateMan());
            //if(po.getCreateDateDirtyFlag())
                domain.setCreateDate(po.getCreateDate());
            //if(po.getPorgnameDirtyFlag())
                domain.setPorgname(po.getPorgname());
            //if(po.getPorgidDirtyFlag())
                domain.setPorgid(po.getPorgid());
              return domain ;
    }

    public ORG convert2PO(com.ibiz.core.external.domain.ORG domain , ORG po){
        if(domain == null)
            return po ;
        if(po==null){
            po = createPO() ;
        }
        if(domain.getORGIdDirtyFlag())
            po.setORGId(domain.getORGId());
        if(domain.getUpdateDateDirtyFlag())
            po.setUpdateDate(domain.getUpdateDate());
        if(domain.getCreateManDirtyFlag())
            po.setCreateMan(domain.getCreateMan());
        if(domain.getORGNameDirtyFlag())
            po.setORGName(domain.getORGName());
        if(domain.getUpdateManDirtyFlag())
            po.setUpdateMan(domain.getUpdateMan());
        if(domain.getCreateDateDirtyFlag())
            po.setCreateDate(domain.getCreateDate());
        if(domain.getPorgnameDirtyFlag())
            po.setPorgname(domain.getPorgname());
        if(domain.getPorgidDirtyFlag())
            po.setPorgid(domain.getPorgid());

        return po ;
    }
}
