package com.gree.mall.manager.logic.admin;

import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.OperationLog;
import com.gree.mall.manager.plus.service.OperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OperationLogLogic {

    @Autowired
    OperationLogService operationLogService;
    @Autowired
    CommonLogic commonLogic;

    /**
     * 操作日志列表分页
     */
    public IPage<OperationLog> listPage(HttpServletRequest request,String moduleName,String companyWechatName,String nickName, String startTime, String endTime, Integer pageNo, Integer pageSize){
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        List<String> companyWechatIds = adminUser.getCompanyWechatIds();
        if(endTime.length() <= 10){
            endTime += " 23:59:59";
        }
        return operationLogService.lambdaQuery()
                .in(CollectionUtils.isNotEmpty(companyWechatIds),OperationLog::getCompanyWechatId,companyWechatIds)
                .likeRight(StringUtils.isNotBlank(companyWechatName),OperationLog::getCompanyName,companyWechatName)
                .likeRight(StringUtils.isNotBlank(nickName),OperationLog::getNickName,nickName)
                .like(OperationLog::getModuleName,moduleName)
                .between(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime),
                        OperationLog::getCreateTime,startTime,endTime)
                .orderByDesc(OperationLog::getCreateTime,OperationLog::getOperationLogId)
                .page(new Page<>(pageNo,pageSize));
    }

    /**
     * 所属模块
     */
    public List<String> moduleList(HttpServletRequest request){
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        List<String> companyWechatIds = adminUser.getCompanyWechatIds();

        List<OperationLog> distinctModuleName = operationLogService.lambdaQuery()
                .select(OperationLog::getModuleName)
                .in(CollectionUtils.isNotEmpty(companyWechatIds),OperationLog::getCompanyWechatId,companyWechatIds)
                .groupBy(OperationLog::getModuleName)
                .list();
        if(distinctModuleName.size() == 0){
            return new ArrayList<>();
        }
        List<String> collect = distinctModuleName.stream().map(OperationLog::getModuleName).distinct().collect(Collectors.toList());
        return collect;
    }

    /**
     * 详情
     */
    public OperationLog detail(String operationLogId){
        return operationLogService.getById(operationLogId);
    }


}
