package cn.superdesk.uniorder.service.sys.role.impl;

import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.sys.role.SysRole;
import cn.superdesk.uniorder.dao.entity.sys.role.SysRoleMenuKey;
import cn.superdesk.uniorder.dao.mapper.sys.role.SysRoleMapper;
import cn.superdesk.uniorder.dao.mapper.sys.role.SysRoleMenuMapperEx;
import cn.superdesk.uniorder.service.sys.role.SysRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper       sysRoleMapper;
    @Autowired
    private SysRoleMenuMapperEx sysRoleMenuMapper;


    @Override
    public int insertSelective(SysRole t) {

        return sysRoleMapper.insertSelective(t);
    }

    @Override
    public int updateByPrimaryKeySelective(SysRole t) {

        return sysRoleMapper.updateByPrimaryKeySelective(t);
    }

    @Override
    public void deleteObj(Long id) {

        sysRoleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SysRole findById(Long roleId) {

        return sysRoleMapper.selectByPrimaryKey(roleId);
    }

    @Deprecated
    @Override
    public PageInfo<SysRole> findByPaging(Map<String, Object> params, Integer pageNo, Integer pageSize) {

        //PageHelper.startPage(pageNo, pageSize);
        return null;
    }


    @Transactional
    @Override
    public void deleteTran(Long roleId) {

        sysRoleMenuMapper.deleteByRoleId(roleId);
        sysRoleMapper.deleteByPrimaryKey(roleId);
    }

    @Override
    public SysRole findByAvicCode(String avicCode) {
        return sysRoleMapper.findByAvicCode(avicCode);
    }


    @Override
    public List<SysRole> findAll(Map<String, Object> queryMap) {

        return null;
    }

    @Transactional
    @Override
    public SysRole addObjTran(SysRole role, List<SysRoleMenuKey> ls) {

        role.setIssystem((byte) 0);
        sysRoleMapper.insert(role);
        if (ls.size() > 0) {
            for (SysRoleMenuKey item : ls) {
                item.setRoleId(role.getId());
                sysRoleMenuMapper.insert(item);
            }
        }
        return role;
    }

    @Transactional
    @Override
    public void updateObjTran(SysRole role, List<SysRoleMenuKey> ls) {

        sysRoleMapper.updateByPrimaryKeySelective(role);
        if (ls.size() > 0) {
            sysRoleMenuMapper.deleteByRoleId(role.getId());
            for (SysRoleMenuKey item : ls) {
                item.setRoleId(role.getId());
                sysRoleMenuMapper.insert(item);
            }
        } else {
            sysRoleMenuMapper.deleteByRoleId(role.getId());
        }
    }

    @Override
    public R<List<SysRole>> findAllPage(Map<String, Object> params, int pageNo, int pageSize) {

        PageHelper.startPage(pageNo, pageSize);

        List<SysRole> ls = sysRoleMapper.findAll(params);

        PageInfo<SysRole> page = new PageInfo<SysRole>(ls);
        PageHelper.clearPage();

        return new R<List<SysRole>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), ls);
    }


}
