package cn.hanghui.outapi.platform.tenant.service.impl;

import cn.hanghui.outapi.platform.common.controller.BaseController;
import cn.hanghui.outapi.platform.common.entity.po.Account;
import cn.hanghui.outapi.platform.common.entity.rest.RestResult;
import cn.hanghui.outapi.platform.tenant.entity.dto.SysOperLogSearch;
import cn.hanghui.outapi.platform.tenant.entity.dto.SysOperLogVo;
import cn.hanghui.outapi.platform.tenant.entity.dto.account.AccountVo;
import cn.hanghui.outapi.platform.tenant.entity.dto.blacklist.UserBlacklistTaskVo;
import cn.hanghui.outapi.platform.tenant.entity.enums.AccountTypeEnum;
import cn.hanghui.outapi.platform.tenant.entity.enums.BusinessTypeEnum;
import cn.hanghui.outapi.platform.tenant.entity.po.Purchaser;
import cn.hanghui.outapi.platform.tenant.entity.po.SysOperLog;
import cn.hanghui.outapi.platform.tenant.entity.po.UserBlacklistTask;
import cn.hanghui.outapi.platform.tenant.mapper.SysOperLogMapper;
import cn.hanghui.outapi.platform.tenant.service.AccountService;
import cn.hanghui.outapi.platform.tenant.service.SysOperLogService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysOperLogServiceImpl extends BaseController implements SysOperLogService {
    private final SysOperLogMapper sysOperLogMapper;
    private final AccountService accountService;

    @Override
    public RestResult<IPage<SysOperLogVo>> page(SysOperLogSearch dto) {
        LambdaQueryWrapper<SysOperLog> wrapper = this.getWrapper(dto);
        Page<SysOperLog> page = new Page<>(dto.getPageNumber(), dto.getPageSize());
        IPage<SysOperLog> iPage = sysOperLogMapper.selectPage(page, wrapper);
        IPage<SysOperLogVo> convert = iPage.convert(t -> {
            SysOperLogVo item = BeanUtil.copyProperties(t, SysOperLogVo.class);
            BusinessTypeEnum code = BusinessTypeEnum.getByCode(t.getBusinessType());
            item.setBusinessTypeName(code.getDesc());
            return item;
        });

        return RestResult.ok(convert);
    }

    private LambdaQueryWrapper<SysOperLog> getWrapper(SysOperLogSearch dto) {
        LambdaQueryWrapper<SysOperLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(dto.getTag()), SysOperLog::getTag, dto.getTag());
        wrapper.eq(ObjectUtil.isNotEmpty(dto.getBusinessType()), SysOperLog::getBusinessType, dto.getBusinessType());
        wrapper.like(StrUtil.isNotBlank(dto.getTitle()), SysOperLog::getTitle, dto.getTitle());
        wrapper.eq(StrUtil.isNotBlank(dto.getOperName()), SysOperLog::getOperName, dto.getOperName());
        wrapper.eq(StrUtil.isNotBlank(dto.getOperPhone()), SysOperLog::getOperPhone, dto.getOperPhone());
        wrapper.ge(ObjectUtil.isNotNull(dto.getStartTime()), SysOperLog::getOperTime, dto.getStartTime());
        wrapper.le(ObjectUtil.isNotNull(dto.getEndTime()), SysOperLog::getOperTime, dto.getEndTime());
        wrapper.eq(ObjectUtil.isNotNull(dto.getAccountId()), SysOperLog::getAccountId, dto.getAccountId());

        Account account = getAccount();
        RestResult<AccountVo> query = accountService.query(account);
        AccountVo data = query.getData();
        if(AccountTypeEnum.AUTH.getCode().equals(data.getAccountType())){
            wrapper.eq(SysOperLog::getAccountId, data.getId());
        }

        return wrapper;
    }
}
