package com.jy.log.dao;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.framework.model.base.BaseDao;
import com.jy.log.mapper.LogOperateMapper;
import com.jy.log.model.dto.LogOperateSelectDto;
import com.jy.log.model.entity.LogOperate;
import com.jy.log.model.table.LogOperateTable;
import com.jy.log.model.vo.LogOperateVo;
import com.jy.system.model.table.SysUserTable;
import org.springframework.stereotype.Service;

@Service
public class LogOperateDao extends BaseDao<LogOperateMapper, LogOperate> {

    /**
     * 操作日志分页
     */
    public Page<LogOperateVo> getPage(LogOperateSelectDto dto) {
        LogOperateTable lo = LogOperateTable.lo;
        SysUserTable su = SysUserTable.su;

        return sql(LogOperateVo.class)
                .select(
                        lo.id,
                        lo.title,
                        lo.businessType,
                        lo.costTime,
                        lo.status,
                        lo.createTime,
                        su.nickname,
                        su.username
                )
                .from(lo)
                .leftJoin(su).on(lo.createUser.eq(su.id))
                .where(
                        lo.title.like(dto.getTitle()),
                        lo.businessType.eq(dto.getBusinessType()),
                        lo.status.eq(dto.getStatus()),
                        su.username.like(dto.getUsername()),
                        lo.createUser.eq(dto.getCreateUser()),
                        lo.createTime.between(dto.getCreateTime())
                )
                .orderBy(lo.id.desc())
                .page(dto.getPage());
    }

    /**
     * 操作日志明细
     */
    public LogOperateVo getDetail(Long id) {
        LogOperateTable lo = LogOperateTable.lo;

        return sql(LogOperateVo.class)
                .select(
                        lo.id,
                        lo.method,
                        lo.bestMatching,
                        lo.requestAttribute,
                        lo.requestParam,
                        lo.requestBody,
                        lo.errorMsg,
                        lo.errorDetail
                )
                .from(lo)
                .where(lo.id.eq(id))
                .one();
    }

}
