package com.fdkanfang.service;

import com.fdkanfang.common.model.BaseModel;
import com.fdkanfang.dao.backend.IBaseMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Condition;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Date;
import java.util.List;

/**
 * Created by owen on 2020/2/18 0018 11:22
 */
@Transactional
public abstract class BaseMapperServiceImpl<T extends BaseModel, ID extends Serializable> implements IBaseMapperService<T, ID> {

    // 获取dao
    public abstract IBaseMapper<T, ID> getBaseMapper();


    private Class<T> entityClass;

    public BaseMapperServiceImpl(){
        ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
        entityClass = (Class<T>) pt.getActualTypeArguments()[0];
    }

    @Override
    public T findById(ID id){
        Condition condition = new Condition(entityClass);
        condition.createCriteria().andEqualTo("id", id);
        condition.and().andEqualTo("recStatus", "A");
        List<T> ts = getBaseMapper().selectByCondition(condition);
        if (ts != null && ts.size() > 0){
            return ts.get(0);
        }else{
            return null;
        }
    }

    @Override
    public T findOne(T entity){
        entity.setRecStatus("A");
        return getBaseMapper().selectOne(entity);
    }

    @Override
    public List<T> findAll(){
        Condition condition = new Condition(entityClass);
        condition.createCriteria().andEqualTo("recStatus", "A");
        return getBaseMapper().selectByCondition(condition);
    }

    /**
     * 根据主键字符串进行查询，类中只有存在一个带有@Id注解的字段
     *
     * @param ids 如 "1,2,3,4"
     * @return
     */
    @Override
    public List<T> findByIds(String ids){
        return getBaseMapper().selectByIds(ids);
    }

    @Override
    public long count(){
        List<T> all = this.findAll();
        if (all != null && all.size() > 0){
            return all.size();
        }
        return 0;
    }

    @Override
    public boolean exists(ID id){
        return getBaseMapper().existsWithPrimaryKey(id);
    }

    @Override
    public int save(T entity) {
        //migration之后要删掉判断
        if (entity.getCreateTime() == null){
            entity.setCreateTime(new Date());
        }
        if (entity.getUpdateTime() == null){
            entity.setUpdateTime(new Date());
        }
        entity.setRecStatus("A");
        return getBaseMapper().insertSelective(entity);
    }

    @Override
    public int saveAll(List<T> entitys) {
        return getBaseMapper().insertList(entitys);
    }

    @Override
    public int update(T entity) {
        //migration之后要删掉判断
        if (entity.getUpdateTime() == null){
            entity.setUpdateTime(new Date());
        }
        return getBaseMapper().updateByPrimaryKeySelective(entity);
    }

    @Override
    public int updateAll(T entity) {
        //migration之后要删掉判断
        if (entity.getUpdateTime() == null){
            entity.setUpdateTime(new Date());
        }
        return getBaseMapper().updateByPrimaryKey(entity);
    }

    @Override
    public int deleteById(ID id) {
        return getBaseMapper().deleteByPrimaryKey(id);
    }

    @Override
    public int deleteByIds(String ids){
        return getBaseMapper().deleteByIds(ids);
    }

    @Override
    public int delete(T entity){
        return getBaseMapper().delete(entity);
    }

    public List<T> findAll(Condition condition){
        condition.and().andEqualTo("recStatus", "A");
        return getBaseMapper().selectByCondition(condition);
    }

    public List<T> findAll(Condition condition, String orderBy){
        condition.and().andEqualTo("recStatus", "A");
        if (!StringUtils.isEmpty(orderBy)){
            PageHelper.orderBy(orderBy);
        }
        return getBaseMapper().selectByCondition(condition);
    }

    public PageInfo<T> findAll(int pageNum, int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.findAll());
    }

    public PageInfo<T> findAll(int pageNum, int pageSize, String orderBy){
        PageHelper.startPage(pageNum, pageSize);
        if (!StringUtils.isEmpty(orderBy)){
            PageHelper.orderBy(orderBy);
        }
        return new PageInfo<>(this.findAll());
    }

    public PageInfo<T> findAll(Condition condition, int pageNum, int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.findAll(condition));
    }

    public PageInfo<T> findAll(Condition condition, int pageNum, int pageSize, String orderBy){
        PageHelper.startPage(pageNum, pageSize);
        if (!StringUtils.isEmpty(orderBy)){
            PageHelper.orderBy(orderBy);
        }
        return new PageInfo<>(this.findAll(condition));
    }



}
