package com.corewing.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corewing.app.common.page.PageContext;
import com.corewing.app.entity.PrivacyPolicy;
import com.corewing.app.mapper.PrivacyPolicyMapper;
import com.corewing.app.service.PrivacyPolicyService;
import com.corewing.app.util.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class PrivacyPolicyServiceImpl extends ServiceImpl<PrivacyPolicyMapper, PrivacyPolicy> implements PrivacyPolicyService {

    @Override
    public Page<PrivacyPolicy> page(PrivacyPolicy privacyPolicy) {
        Page<PrivacyPolicy> page = PageContext.getDefaultPage(PrivacyPolicy.class);
        LambdaQueryWrapper<PrivacyPolicy> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(privacyPolicy.getTitle()), PrivacyPolicy::getTitle, privacyPolicy.getTitle());
        queryWrapper.eq(privacyPolicy.getVisible() != null, PrivacyPolicy::getVisible, privacyPolicy.getVisible());
        return page(page, queryWrapper);
    }

    @Override
    public String getLastUpdateTime() {
        LambdaQueryWrapper<PrivacyPolicy> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(PrivacyPolicy::getUpdateTime, PrivacyPolicy::getCreateTime);
        PrivacyPolicy privacyPolicy = list(queryWrapper).get(0);
        return DateUtils.format(privacyPolicy.getUpdateTime() == null ? privacyPolicy.getCreateTime() : privacyPolicy.getUpdateTime(), "yyyy年MM月dd日");
    }
}
