package com.lz.modules.flow.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lz.common.utils.NumberUtil;
import com.lz.common.utils.PageUtils;
import com.lz.modules.flow.dao.RecordRoleMapper;
import com.lz.modules.flow.entity.RecordAuth;
import com.lz.modules.flow.entity.RecordRole;
import com.lz.modules.flow.service.RecordAuthService;
import com.lz.modules.flow.service.RecordRoleAuthService;
import com.lz.modules.flow.service.RecordRoleService;
import com.lz.modules.sys.entity.SysMenuEntity;
import com.lz.modules.sys.entity.SysRoleEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
* <p>
* 记录表 服务类
* </p>
*
* @author quyixiao
* @since 2020-08-18
*/

@Service
public class RecordRoleServiceImpl extends ServiceImpl<RecordRoleMapper, RecordRole> implements RecordRoleService {


    @Autowired
	private RecordRoleMapper recordRoleMapper;

    @Autowired
	private RecordRoleAuthService recordRoleAuthService;



    @Autowired
	private RecordAuthService recordAuthService;


	@Override
	public RecordRole selectRecordRoleById(Long id){
		return recordRoleMapper.selectRecordRoleById(id);
	}



	@Override
	public Long insertRecordRole(RecordRole recordRole){
		return recordRoleMapper.insertRecordRole(recordRole);
	}



	@Override
	public int updateRecordRoleById(RecordRole recordRole){
		return recordRoleMapper.updateRecordRoleById(recordRole);
	}



	@Override
	public int updateCoverRecordRoleById(RecordRole recordRole){
		return recordRoleMapper.updateCoverRecordRoleById(recordRole);
	}



	@Override
	public int deleteRecordRoleById(Long id){
		return recordRoleMapper.deleteRecordRoleById(id);
	}

    @Override
    public void deleteBatchIds(List<Long> asList) {
		for(Long id :asList){
			deleteRecordRoleById(id);
		}
    }

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		PageUtils pageUtils = PageUtils.startPage(
				NumberUtil.objToIntDefault(params.get("page"),1),
				NumberUtil.objToIntDefault(params.get("limit"),10)).doSelect(
				page -> recordRoleMapper.selectByCondition(page, params)
		);
		return  pageUtils;
	}

    @Override
    public List<RecordRole> selectAll() {
		return recordRoleMapper.selectAll();
    }

    @Override
    public SysRoleEntity getRole(Long id) {
		RecordRole lzRecordRole = selectRecordRoleById(id);
		SysRoleEntity role = new SysRoleEntity();
		role.setRoleId(lzRecordRole.getId());
		role.setRoleName(lzRecordRole.getName());
		//查询角色对应的菜单
		List<Long> menuIdList = recordRoleAuthService.queryMenuIdList(id);

		role.setMenuIdList(menuIdList);
        return role;
    }

	@Override
	public List<SysMenuEntity> selectMenuList() {
		List<SysMenuEntity> list = new ArrayList<>();
		List<RecordAuth> recordAuths = recordAuthService.selectAll();
		for (int i = 0; i < recordAuths.size(); i++) {
			RecordAuth recordAuth = recordAuths.get(i);
			Long parentId = recordAuth.getId();
			if (i + 1 < recordAuths.size()) {
				RecordAuth child = recordAuths.get(i + 1);
				if (child.getIdentity().equals(recordAuth.getIdentity())) {
					parentId = NumberUtil.objToLong(recordAuth.getId() + "000000");
				}
			}
			SysMenuEntity sysMenuEntity = buildMenuEntity(parentId, recordAuth.getName(), 0l, "");
			list.add(sysMenuEntity);
			if (i + 1 < recordAuths.size()) {
				RecordAuth child = recordAuths.get(i + 1);
				if (!child.getIdentity().equals(recordAuth.getIdentity())) {
					continue;
				}
				for (int j = i; j < recordAuths.size(); j++) {
					child = recordAuths.get(j);
					if (!child.getIdentity().equals(recordAuth.getIdentity())) {
						i = j - 1;
						break;
					}
					String name = "不可见";
					if (child.getStatus().equals(1)) {
						name = "可见";
					} else if (child.getStatus().equals(2)) {
						name = "可编辑";
					}
					SysMenuEntity childMenuEntity = buildMenuEntity(child.getId(), name, parentId, recordAuth.getName());
					list.add(childMenuEntity);
					if (j == recordAuths.size() - 1) {
						i = j;
					}
				}
			}
		}
		return list;
	}



	@Override
	public SysMenuEntity buildMenuEntity(Long id, String name, Long parentId, String parentName) {
		SysMenuEntity sysMenuEntity = new SysMenuEntity();
		sysMenuEntity.setMenuId(id);
		sysMenuEntity.setName(name);
		sysMenuEntity.setParentId(parentId);
		sysMenuEntity.setParentName(parentName);
		return sysMenuEntity;
	}


}
