package com.czg.service.system.service.impl;


import cn.hutool.core.util.StrUtil;
import com.czg.sa.StpKit;
import com.czg.service.system.mapper.OperationLogMapper;
import com.czg.system.dto.OperationLogDTO;
import com.czg.system.entity.OperationLog;
import com.czg.system.service.OperationLogService;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 操作日志
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0
 */
@Service
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements OperationLogService {

    private QueryWrapper buildQueryWrapper(OperationLogDTO param) {
        QueryWrapper queryWrapper = PageUtil.buildSortQueryWrapper();
        if (StrUtil.isNotEmpty(param.getOperation())) {
            queryWrapper.like(OperationLog::getOperation, param.getOperation());
        }
        if (StrUtil.isNotBlank(param.getStartTime())) {
            queryWrapper.ge(OperationLog::getCreateTime, param.getStartTime());
        }
        if (StrUtil.isNotBlank(param.getEndTime())) {
            queryWrapper.le(OperationLog::getCreateTime, param.getEndTime());
        }
        Long shopId = StpKit.USER.getShopId();
        queryWrapper.eq(OperationLog::getShopId, shopId);
        queryWrapper.orderBy(OperationLog::getId, false);
        return queryWrapper;
    }

    @Override
    public Page<OperationLogDTO> page(OperationLogDTO param) {
        QueryWrapper queryWrapper = buildQueryWrapper(param);
        return super.pageAs(PageUtil.buildPage(), queryWrapper, OperationLogDTO.class);
    }

    @Override
    public List<OperationLogDTO> list(OperationLogDTO param) {
        QueryWrapper queryWrapper = buildQueryWrapper(param);
        return super.listAs(queryWrapper, OperationLogDTO.class);
    }

}
