package com.manage.service.ipml;


import com.manage.dao.Power_RoleMapper;
import com.manage.entity.Power_Role;
import com.manage.entity.Power_User;
import com.manage.service.Power_RoleService;
import com.manage.vo.Power_RoleVo;
import com.manage.vo.Power_UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @Author:hjl
 * @Date:Creatid in 1:19 2019/4/17
 * @Description:
 */
@Service
public class Power_RoleServiceImpl implements Power_RoleService {
    @Autowired
    Power_RoleMapper powerRoleMapper;
    @Override
    public int deleteByPrimaryKey(Integer roleId) {
        //将关联用户的角色id置空

        return powerRoleMapper.deleteByPrimaryKey(roleId);
    }

    @Override
    public int insert(Power_Role record) {
        return powerRoleMapper.insert(record);
    }

    @Override
    public int insertSelective(Power_Role powerRole) {
        //取得当前登录账号
        ServletRequestAttributes attr=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request =attr.getRequest();
        HttpSession session= request.getSession();
        Power_User powerUser1 =(Power_User) session.getAttribute("CURRENT_USER");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间
        powerRole.setCreater(powerUser1.getUserName());
        powerRole.setCreateDate(date);
        powerRole.setUpdater(powerUser1.getUserName());
        powerRole.setUpdateDate(date);
        return powerRoleMapper.insertSelective(powerRole);
    }

    @Override
    public Power_Role selectByPrimaryKey(Integer roleId) {
        return powerRoleMapper.selectByPrimaryKey(roleId);
    }

    @Override
    public int updateByPrimaryKeySelective(Power_Role powerRole, HttpServletRequest request) {
        //取得当前登录账号
        HttpSession session= request.getSession();
        Power_User powerUser1 =(Power_User) session.getAttribute("CURRENT_USER");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间
        powerRole.setUpdater(powerUser1.getUserName());
        powerRole.setUpdateDate(date);
        return powerRoleMapper.updateByPrimaryKeySelective(powerRole);
    }

    @Override
    public int updateByPrimaryKey(Power_Role record) {
        return powerRoleMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Power_RoleVo> findSomeByMore(Power_RoleVo powerRole, HttpServletRequest request) {
        Power_UserVo powerUser1 =(Power_UserVo) request.getSession().getAttribute("CURRENT_USER");
        List<Power_RoleVo> power_roles = powerRoleMapper.findSomeByMore(powerRole, powerUser1.getRoleId(),powerUser1.getUserId());
        Set<String> perms = powerUser1.getMenus();
        if(null != power_roles && !power_roles.isEmpty()){
            for (int i = 0; i < power_roles.size(); i++) {
                Integer isUpdate = 0;
                Integer isDelete = 0;
                for(String menu : perms){
                    if("/role/update".equals(menu)){
                        isUpdate = 1;
                    }
                    if("/role/delete".equals(menu)){
                        isDelete = 1;
                    }
                }
                power_roles.get(i).setIsUpdate(isUpdate);
                power_roles.get(i).setIsDelete(isDelete);
            }
        }
        return power_roles;
    }

    @Override
    public int getTotal(Power_RoleVo powerRole,HttpServletRequest request) {
        Power_User powerUser1 =(Power_User) request.getSession().getAttribute("CURRENT_USER");
        return powerRoleMapper.getTotal(powerRole,powerUser1.getRoleId(),powerUser1.getUserId());
    }

    @Override
    public void export(Power_RoleVo powerRole,HttpServletResponse response,HttpServletRequest request) throws Exception {
        Power_User powerUser1 =(Power_User) request.getSession().getAttribute("CURRENT_USER");
        List<Power_RoleVo> roleList = powerRoleMapper.findSomeByMore(powerRole, powerUser1.getRoleId(),powerUser1.getUserId());
        if(null != roleList && !roleList.isEmpty()){
            for (int i = 0; i < roleList.size(); i++) {
                if(roleList.get(i).getEffective() != null){
                    if(roleList.get(i).getEffective() == 1){
                        roleList.get(i).setEffectiveCn("是");
                    }else{
                        roleList.get(i).setEffectiveCn("否");
                    }
                    if(roleList.get(i).getShowRecord() == 1){
                        roleList.get(i).setShowRecordCn("是");
                    }else{
                        roleList.get(i).setShowRecordCn("否");
                    }
                    if(roleList.get(i).getDownloadRecord() == 1){
                        roleList.get(i).setDownloadRecordCn("是");
                    }else{
                        roleList.get(i).setDownloadRecordCn("否");
                    }
                }
            }
        }
        String tableThNames = "角色名称,备注,是否可预览,是否可下载,是否有效,创建时间,创建人";
        String fieldCns = "roleName,remark,showRecordCn,downloadRecordCn,effectiveCn,createDate,creater";
        //文件名
        String fileName = "角色列表(" + new SimpleDateFormat("yyyy_MM_dd HH_mm_ss").format(new Date()) + ").xls";
        //ExportExcelUtil
        com.manage.util.ExportExcelUtil exportExcelUtil = new com.manage.util.ExportExcelUtil();
        //导出excel的操作
        exportExcelUtil.expordExcel(tableThNames,fieldCns,roleList,fileName,response);
    }

    @Override
    public List<Power_RoleVo> selectList() {
        return powerRoleMapper.selectList();
    }

    @Override
    public List<Power_RoleVo> selectListByPower(HttpServletRequest request) {
        Power_User user =(Power_User) request.getSession().getAttribute("CURRENT_USER");
        List<Power_RoleVo> power_roles = powerRoleMapper.selectListByPower(user.getRoleId(),user.getUserId());
        return power_roles;
    }

    @Override
    public Power_Role checkRoleName(String roleName) {
        return powerRoleMapper.checkRoleName(roleName);
    }
}
