package com.ho.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.util.PageResult;
import com.ho.common.tools.util.PageUtils;
import com.ho.common.tools.util.RedisCommon;
import com.ho.common.tools.entity.SimpleUser;
import com.ho.user.api.entity.SysDept;
import com.ho.user.api.entity.SysLog;
import com.ho.user.mapper.SysLogMapper;
import com.ho.user.service.DeptService;
import com.ho.user.service.LogService;
import com.ho.user.service.PermissionService;
import com.ho.user.vo.req.SysLogPageReqVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @ClassName: LogServiceImpl
 * @Author:
 * @CreateDate: 2019/9/23 16:18
 * @UpdateUser:
 * @UpdateDate: 2019/9/23 16:18
 * @Version: 0.0.1
 */
@Service
@Slf4j
public class LogServiceImpl implements LogService{
    @Autowired
    private SysLogMapper sysLogMapper;

    @Autowired
    private DeptService deptService;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RedisCommon redisCommon;

    @Override
    public PageResult pageInfo(SysLogPageReqVO vo, SimpleUser simpleUser) {
        //判断用户是否为超管
        if(CommonConstant.TOP_PARENT_ID.equals(simpleUser.getGroupId())){
            if(CommonConstant.TOP_PARENT_ID.equals(simpleUser.getDeptId())){
                //超管可以查询所有
                String endTime = vo.getEndTime();
                String startTime = vo.getStartTime();
                String operation = vo.getOperation();
                String username = vo.getUsername();
                PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
                List<SysLog> sysLogs = sysLogMapper.selectAllLogs(endTime,startTime,operation,username);
                return PageUtils.getPageResult(new PageInfo<>(sysLogs));
        }
           }
        Integer deptId = simpleUser.getDeptId();
        //如果没传入部门,仅查询用户所属部门下的数据
        if (deptId == null) {
            SimpleUser currentSimpleUser = permissionService.getCurrentSimpleUser();
            deptId = currentSimpleUser.getDeptId();
        }
        SysDept sysDept = deptService.detailInfo(deptId);
        if (null == sysDept) {
            log.error("传入 的 id:{}不合法", deptId);
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        List<SysDept> sysDeptList =new ArrayList<>();
        sysDeptList.add(sysDept);
        List<SysDept> sysDepts = deptService.getSubsidiaryDepts(sysDeptList,CommonConstant.DEPT_KEY);
        vo.setDepts(sysDepts);
        vo.setGroupId(simpleUser.getGroupId());
        vo.setUserId(null);
        vo.setUsername(vo.getUsername());
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());

        List<SysLog> sysLogs = sysLogMapper.selectAll(vo);
        return PageUtils.getPageResult(new PageInfo<>(sysLogs));
    }


    @Override
    public void deleted(List<Long> logIds) {
        sysLogMapper.batchDeletedLog(logIds);
    }
}
