package cn.com.countrygarden.bi.main.service.wcx.impl;

import cn.com.countrygarden.bi.main.constant.wcx.WcxConstant;
import cn.com.countrygarden.bi.main.constant.wcx.enumeration.WcxSysRoleEnum;
import cn.com.countrygarden.bi.main.dao.sys.UserRolesMapper;
import cn.com.countrygarden.bi.main.dao.wcx.WcxRoleMapper;
import cn.com.countrygarden.bi.main.dto.wcx.WcxRoleDto;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.sys.UserRoles;
import cn.com.countrygarden.bi.main.model.wcx.WcxRole;
import cn.com.countrygarden.bi.main.service.wcx.WcxRoleService;
import cn.com.countrygarden.bi.main.util.PageUtil;
import com.github.pagehelper.PageRowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Service
public class WcxRoleServiceImpl implements WcxRoleService {

    @Resource
    private WcxRoleMapper wcxRoleMapper;
    @Autowired
    private UserRolesMapper userRolesMapper;

    @Override
    @Transactional
    public void save(WcxRoleDto dto) {
        WcxRole model = roleDtoToModel(dto);
        model.setRoleId(UUID.randomUUID().toString());
        model.setStatus(WcxConstant.STATUS_ENABLE);
        model.setCreateDate(new Date());
        model.setWeightRatio(dto.getWeightRatio());
        wcxRoleMapper.insert(model);
    }

    @Override
    @Transactional
    public boolean deleteById(String roleId) {
        WcxRole condition = new WcxRole();
        condition.setRoleId(roleId);
        WcxRole wcxRole = wcxRoleMapper.selectOne(condition);
        if (wcxRole != null) {
            if (WcxConstant.ROLE_CODE_IS_NOT_LEADER.equals(wcxRole.getRoleCode())) {
                throw new ServiceException("默认角色不能删除");
            }
        }
        String haveUser = wcxRoleMapper.haveUserUnderRole(roleId);
        if (StringUtils.hasText(haveUser)) {
            throw new ServiceException("该角色下包含人员不能删除");
        }
        wcxRoleMapper.deleteByPrimaryKey(roleId);
        return true;
    }

    @Override
    @Transactional
    public void update(WcxRoleDto dto) {
        WcxRole wcxRole = roleDtoToModel(dto);
        wcxRole.setRoleId(dto.getRoleId());
        wcxRoleMapper.update(wcxRole);
        //wcxRoleMapper.updateByPrimaryKey(wcxRole);
    }

    @Override
    public PageData selectPageList(WcxRoleDto dto) {
        int pageOffset = PageUtil.getPageOffset(dto.getPageIndex(), dto.getPageSize());
        dto.setPageOffset(pageOffset);

        List<WcxRoleDto> data = wcxRoleMapper.selectPageData(dto);
        Integer total = wcxRoleMapper.selectPageCount(dto);

        PageRowBounds pageRowBounds = new PageRowBounds(dto.getPageIndex(), dto.getPageSize());
        pageRowBounds.setTotal(total.longValue());
        pageRowBounds.setCount(true);

        PageData pageData = new PageData(data, pageRowBounds);
        return pageData;
    }

    @Override
    public boolean checkRoleDto(WcxRoleDto dto) {
        if (dto == null) {
            throw new ServiceException("参数不能为空");
        }
        if (!StringUtils.hasText(dto.getRoleType())) {
            throw new ServiceException("角色类型(是否为领导)不能为空");
        }
        if (!StringUtils.hasText(dto.getRoleName())) {
            throw new ServiceException("角色名称不能为空");
        }
        if (!StringUtils.hasText(dto.getRoleCode())) {
            throw new ServiceException("角色编码不能为空");
        }
        boolean isLeader = isLeader(dto.getRoleType());
        if (isLeader) {
            Integer goldenHammer = dto.getGoldenHammer();
            if (goldenHammer == null || goldenHammer < 0) {
                throw new ServiceException("角色类型(领导)的金锤数异常");
            }
        } else {
            Integer numberPoints = dto.getNumberPoints();
            if (numberPoints == null || numberPoints < 0) {
                throw new ServiceException("角色类型(非领导)的普通点赞数异常");
            }
        }
        return true;
    }

    @Override
    @Transactional
    public boolean updateWeightRatio(Integer thumsRole, String roleType) {
        wcxRoleMapper.updateWeightRatio(thumsRole, WcxConstant.ROLE_IS_LEADER);
        return true;
    }

    @Override
    public int selectWeightRatioByRoleType(String roleType) {
        int weightRatio = wcxRoleMapper.selectWeightRatio(WcxConstant.ROLE_IS_LEADER);
        return weightRatio;
    }

    @Override
    public List<UserRoles> selectSysRoleBySysUserCode(String sysUserCode) {
        UserRoles param = new UserRoles();
        param.setUserCode(sysUserCode);
        List<UserRoles> userRoles = userRolesMapper.select(param);
        return userRoles;
    }

    private WcxRole roleDtoToModel(WcxRoleDto dto) {
        WcxRole wcxRole = new WcxRole();
        wcxRole.setRoleName(dto.getRoleName());
        wcxRole.setRoleType(dto.getRoleType());
        wcxRole.setRoleCode(dto.getRoleCode());
        wcxRole.setWeightRatio(dto.getWeightRatio());
        if (isLeader(dto.getRoleType())) {
            wcxRole.setPointNumber(dto.getGoldenHammer());
        } else {
            wcxRole.setPointNumber(dto.getNumberPoints());
        }
        return wcxRole;
    }

    private boolean isLeader(String roleType) {
        if (WcxConstant.ROLE_IS_LEADER.equals(roleType)) {
            return true;
        } else if (WcxConstant.ROLE_IS_NOT_LEADER.equals(roleType)) {
            return false;
        } else {
            throw new ServiceException("角色类型不存在");
        }
    }

    public String getMaxRole(List<UserRoles> sysUserRoles) {
        List<String> baselineCodes = new ArrayList<>();
        for (UserRoles sysUserRole : sysUserRoles) {
            String roleCode = sysUserRole.getRoleCode();
            String baselineCode = WcxSysRoleEnum.getBaselineCode(roleCode);
            if (baselineCode != null) {
                baselineCodes.add(baselineCode);
            }
        }

        if (baselineCodes.isEmpty()) {
            throw new ServiceException("该用户没有权限查询信息");
        }

        String maxRole = null;
        if (baselineCodes.contains("Z")) {
            // 表示具有所有权限
            maxRole = "Z";
        } else {
            // 日志 如果一个人含有多个角色
            maxRole = baselineCodes.get(0);
        }
        return maxRole;
    }
}
