package com.lightinit.hsdataplatform.impl;

import com.lightinit.hsdataplatform.mapper.ApiregisterserviceMapper;
import com.lightinit.hsdataplatform.entity.Apiregisterservice;
import com.lightinit.hsdataplatform.entity.BaseExample;
import com.lightinit.hsdataplatform.service.IApiRegisterService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by Mr.Yao on 2017/4/21.
 */
@Service
public class ApiRegisterServiceImpl implements IApiRegisterService {
    @Autowired
    private ApiregisterserviceMapper apiregisterserviceMapper;

    public List<Apiregisterservice> QueryList(String keyword, String order, int page, int rows) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.CAMRS);
        BaseExample example=new BaseExample();
        if(StringUtils.isEmpty(keyword)==false) {
            example.setWhereClause("(INSTR(ACode,'"+keyword+"')>0 OR INSTR(AName,'"+keyword+"')>0 OR INSTR(AApp,'"+keyword+"')>0 OR INSTR(AService,'"+keyword+"')>0 OR INSTR(AMethod,'"+keyword+"')>0 OR INSTR(ADll,'"+keyword+"')>0 OR INSTR(AInterface,'"+keyword+"')>0)");
        }
        BaseExample.Page _page=new BaseExample.Page();
        _page.setPageNo(page);
        _page.setPageSize(rows);
        example.setPage(_page);
        return apiregisterserviceMapper.selectByExample(example);
    }

    public long QueryCount(String keyword) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.CAMRS);
        BaseExample example=new BaseExample();
        if(StringUtils.isEmpty(keyword)==false) {
            example.setWhereClause("(INSTR(ACode,'"+keyword+"')>0 OR INSTR(AName,'"+keyword+"')>0 OR INSTR(AApp,'"+keyword+"')>0 OR INSTR(AService,'"+keyword+"')>0 OR INSTR(AMethod,'"+keyword+"')>0 OR INSTR(ADll,'"+keyword+"')>0 OR INSTR(AInterface,'"+keyword+"')>0)");
        }
        return apiregisterserviceMapper.countByExample(example);
    }

    @Override
    public boolean Save(Apiregisterservice record) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.CAMRS);
        return apiregisterserviceMapper.insert(record)>0;
    }

    @Override
    public boolean Update(Apiregisterservice record) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.CAMRS);
        return apiregisterserviceMapper.updateByPrimaryKey(record)>0;
    }

    @Override
    public boolean Delete(long sid) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.CAMRS);
        return apiregisterserviceMapper.deleteByPrimaryKey(sid)>0;
    }

    @Override
    public Apiregisterservice QueryOne(long sid) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.CAMRS);
        return apiregisterserviceMapper.selectByPrimaryKey(sid);
    }

    @Override
    public Apiregisterservice QueryOne(String code) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.CAMRS);
        BaseExample example=new BaseExample();
        if(StringUtils.isEmpty(code)==false) {
            example.setWhereClause("ACode='"+code+"'");
            List<Apiregisterservice> list= apiregisterserviceMapper.selectByExample(example);
            if(list!=null && list.size()>0)
                return list.get(0);
            else
                return null;
        }else{
            return null;
        }
    }
}
