package com.dxhy.auth.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxhy.auth.dao.ClientDao;
import com.dxhy.auth.dao.ClientServiceDao;
import com.dxhy.auth.entity.ClientEntity;
import com.dxhy.auth.entity.ClientServiceEntity;
import com.dxhy.auth.service.ClientBizService;
import com.dxhy.auth.service.ClientServiceBizService;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.utils.Constant;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.Query;

import lombok.extern.slf4j.Slf4j;

/**
 *
 * @author jiaohongyang
 * @date 2019年3月26日 下午5:53:28
 *
 */
@Service("clientBizService")
@Slf4j
public class ClientBizServiceImpl extends ServiceImpl<ClientDao, ClientEntity> implements ClientBizService {
    @Resource
    private ClientServiceDao clientServiceMapper;
    @Resource
    private ClientServiceBizService clientServiceBizService;

    @Override
    public List<ClientEntity> getClientServices(Long id) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return baseMapper.selectAuthorityServiceInfo(id);
    }

    @Override
    public void modifyClientServices(Map<String, Object> params) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        String id = (String)params.get("id");
        String clients = (String)params.get("clients");
        clientServiceMapper.deleteByServiceId(Long.valueOf(id));
        List<ClientServiceEntity> clientList = new ArrayList<>();
        if (!StringUtils.isEmpty(clients)) {
            String[] mem = clients.split(",");
            for (String m : mem) {
                ClientServiceEntity clientService = new ClientServiceEntity();
                clientService.setServiceId(m);
                clientService.setClientId(id);
                clientList.add(clientService);
            }
        }
        if (clientList.size() > 0) {
            clientServiceBizService.saveOrUpdateBatch(clientList);
        }
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        IPage<ClientEntity> page = this.page(new Query<ClientEntity>().getPage(params), new QueryWrapper<ClientEntity>()
            .apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER)));
        return new PageUtils(page);
    }

    @Override
    public void saveClient(ClientEntity clientEntity) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        baseMapper.insert(clientEntity);
    }

    @Override
    public ClientEntity queryCleint(String clientCode) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return baseMapper.selectOne(new QueryWrapper<ClientEntity>().eq("code", clientCode));
    }

    @Override
    public void updateClient(ClientEntity clientEntity) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        baseMapper.updateById(clientEntity);
    }

    @Override
    public void deleteClient(Long id) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        baseMapper.deleteById(id);
    }

    @Override
    public ClientEntity queryClientEntity(Long id) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return baseMapper.selectById(id);
    }
}
