package com.lightinit.hsdatashow.impl;

import com.lightinit.hsdatashow.mapper.UserModuleMapper;
import com.lightinit.hsdatashow.entity.UserModule;
import com.lightinit.hsdatashow.entity.UserModuleExample;
import com.lightinit.hsdatashow.service.IUserModuleService;
import com.lightinit.hsdatashow.spring.DynamicDataContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Mr.Yao on 2017/4/26.
 */
@Service
public class UserModuleServiceImpl implements IUserModuleService{
    @Autowired
    private UserModuleMapper userModuleMapper;

    public List<UserModule> QueryUserModuleList(long uid) {
        UserModuleExample example=new UserModuleExample();
        UserModuleExample.Criteria criteria=example.createCriteria();
        criteria.andUseridEqualTo(uid);
        return userModuleMapper.selectByExample(example);
    }

    public boolean SaveUserModule(long uid, List<Long> ids) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        UserModuleExample example=new UserModuleExample();
        UserModuleExample.Criteria criteria=example.createCriteria();
        criteria.andUseridEqualTo(uid);
        userModuleMapper.deleteByExample(example);
        int resultCount=0;
        for (Long id:ids) {
            UserModule record=new UserModule();
            record.setModuleid(id);
            record.setUserid(uid);
            resultCount+=userModuleMapper.insert(record);
        }
        return resultCount==ids.size();
    }

    public boolean CheckUserHasModules(long uid) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        UserModuleExample example=new UserModuleExample();
        UserModuleExample.Criteria criteria=example.createCriteria();
        criteria.andUseridEqualTo(uid);
        return userModuleMapper.countByExample(example)>0;
    }
}
