package ${globalConfig.parentPack}.service;

import java.util.List;
import java.util.Arrays;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.util.StrUtil;
import ${globalConfig.parentPack}.common.base.BaseService;
import ${globalConfig.parentPack}.common.support.ConvertUtil;
import ${globalConfig.parentPack}.mapper.auto.${TsysTables.tableModel}Mapper;
import ${globalConfig.parentPack}.model.auto.${TsysTables.tableModel};
import ${globalConfig.parentPack}.model.auto.${TsysTables.tableModel}Example;
import ${globalConfig.parentPack}.model.custom.Tablepar;
import ${globalConfig.parentPack}.util.SnowflakeIdWorker;
import StringUtils;

/**
 * ${TsysTables.tableComment} ${TsysTables.tableModel}Service
 * @Title: ${TsysTables.tableModel}Service.java 
 * @Package ${globalConfig.parentPack}.service 
 * @author ${globalConfig.author}_自动生成
 * @email ${globalConfig.email}
 * @date ${datetime}  
 **/
@Service
public class ${TsysTables.tableModel}Service implements BaseService<${TsysTables.tableModel}, ${TsysTables.tableModel}Example>{
	@Resource
	private ${TsysTables.tableModel}Mapper ${TsysTables.tableModel_a}Mapper;
	
   #set( $pklag = false)
   #set( $pkfield = '' )
   #set( $pk_column_name = '' )
   #set( $pk_attrType = '')
   #foreach ($column in $beanColumns)
	   #if(${column.column_key} == 'PRI')
	   	#set( $pklag = true )
	   	#set( $pkfield = $column.beanName_A )
	   	#set( $pk_column_name=$column.column_name)
	   	#set( $pk_attrType=$column.attrType)
	   #end
   #end
	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<${TsysTables.tableModel}> list(Tablepar tablepar,${TsysTables.tableModel} record){
	        ${TsysTables.tableModel}Example testExample=new ${TsysTables.tableModel}Example();
			//搜索
			if(StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
	        	testExample.createCriteria().andLikeQuery2(tablepar.getSearchText());
	        }else {//大搜索
	        	testExample.createCriteria().andLikeQuery(record);
	        }
			//表格排序
			if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
	        	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
	        }else{
	        	testExample.setOrderByClause("${pk_column_name} ASC");
	        }
	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<${TsysTables.tableModel}> list= ${TsysTables.tableModel_a}Mapper.selectByExample(testExample);
	        PageInfo<${TsysTables.tableModel}> pageInfo = new PageInfo<${TsysTables.tableModel}>(list);
	        return  pageInfo;
	 }
#if(${pklag} == true)

	@Override
	public int deleteByPrimaryKey(String ids) {
		#if(${pk_attrType} == 'String')
		
			List<String> lista=ConvertUtil.toListStrArray(ids);
			${TsysTables.tableModel}Example example=new ${TsysTables.tableModel}Example();
			example.createCriteria().and${pkfield}In(lista);
			return ${TsysTables.tableModel_a}Mapper.deleteByExample(example);
			
		#elseif(${pk_attrType} == 'Long')
		
			Long[] integers = ConvertUtil.toLongArray(",", ids);
			List<Long> stringB = Arrays.asList(integers);
			${TsysTables.tableModel}Example example=new ${TsysTables.tableModel}Example();
			example.createCriteria().and${pkfield}In(stringB);
			return ${TsysTables.tableModel_a}Mapper.deleteByExample(example);
		
		#else
			
			Integer[] integers = ConvertUtil.toIntArray(",", ids);
			List<Integer> stringB = Arrays.asList(integers);
			${TsysTables.tableModel}Example example=new ${TsysTables.tableModel}Example();
			example.createCriteria().and${pkfield}In(stringB);
			return ${TsysTables.tableModel_a}Mapper.deleteByExample(example);
			
		#end
		
	}
	
	
	@Override
	public ${TsysTables.tableModel} selectByPrimaryKey(String id) {
		#if(${pk_attrType} == 'String')
		
			return ${TsysTables.tableModel_a}Mapper.selectByPrimaryKey(id);
		#elseif(${pk_attrType} == 'Long')
		
			Long id1 = Long.valueOf(id);
			return ${TsysTables.tableModel_a}Mapper.selectByPrimaryKey(id1);
			
		#else
		
			Integer id1 = Integer.valueOf(id);
			return ${TsysTables.tableModel_a}Mapper.selectByPrimaryKey(id1);
		#end
		
	}

	
	@Override
	public int updateByPrimaryKeySelective(${TsysTables.tableModel} record) {
		return ${TsysTables.tableModel_a}Mapper.updateByPrimaryKeySelective(record);
	}
	
#end	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(${TsysTables.tableModel} record) {
		#if(${pk_attrType} == 'String')
		
		//添加雪花主键id
		record.set${pkfield}(SnowflakeIdWorker.getUUID());
			
		#else
		
		record.set${pkfield}(null);
		
		#end
		
		return ${TsysTables.tableModel_a}Mapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(${TsysTables.tableModel} record, ${TsysTables.tableModel}Example example) {
		
		return ${TsysTables.tableModel_a}Mapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(${TsysTables.tableModel} record, ${TsysTables.tableModel}Example example) {
		
		return ${TsysTables.tableModel_a}Mapper.updateByExample(record, example);
	}

	@Override
	public List<${TsysTables.tableModel}> selectByExample(${TsysTables.tableModel}Example example) {
		
		return ${TsysTables.tableModel_a}Mapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(${TsysTables.tableModel}Example example) {
		
		return ${TsysTables.tableModel_a}Mapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(${TsysTables.tableModel}Example example) {
		
		return ${TsysTables.tableModel_a}Mapper.deleteByExample(example);
	}
	
	/**
	 * 检查name
	 * @param ${TsysTables.tableModel_a}
	 * @return
	 */
	public int checkNameUnique(${TsysTables.tableModel} ${TsysTables.tableModel_a}){
		${TsysTables.tableModel}Example example=new ${TsysTables.tableModel}Example();
		example.createCriteria().and${conditionQueryField}EqualTo(${TsysTables.tableModel_a}.get${conditionQueryField}());
		List<${TsysTables.tableModel}> list=${TsysTables.tableModel_a}Mapper.selectByExample(example);
		return list.size();
	}


}
