package com.jeesharp.business.modules.setting.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeesharp.business.modules.setting.entity.PrivacyPolicy;
import com.jeesharp.business.modules.setting.enums.PrivacyPolicyStatusEnum;
import com.jeesharp.business.modules.setting.mapper.PrivacyPolicyMapper;
import com.jeesharp.business.modules.setting.service.PrivacyPolicyService;
import com.jeesharp.business.modules.setting.vo.PrivacyPolicyQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 隐私政策领域服务实现。
 *
 * @author jeesharp
 * @since 2026-05-20
 */
@Service
@Transactional(readOnly = true)
public class PrivacyPolicyServiceImpl extends CrudService<PrivacyPolicyMapper, PrivacyPolicy, PrivacyPolicyQueryVO>
        implements PrivacyPolicyService {

    @Override
    public PrivacyPolicy getEnabledByDocCode(String docCode) {
        if (StrUtil.isBlank(docCode)) {
            return null;
        }
        LambdaQueryWrapper<PrivacyPolicy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrivacyPolicy::getDelFlag, "0");
        wrapper.eq(PrivacyPolicy::getDocCode, docCode.trim().toUpperCase());
        wrapper.eq(PrivacyPolicy::getStatus, PrivacyPolicyStatusEnum.ENABLED.getValue());
        wrapper.last("LIMIT 1");
        return getOne(wrapper);
    }

    @Override
    public long countByDocCodeExcludeId(String docCode, String excludeId) {
        if (StrUtil.isBlank(docCode)) {
            return 0L;
        }
        LambdaQueryWrapper<PrivacyPolicy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrivacyPolicy::getDelFlag, "0");
        wrapper.eq(PrivacyPolicy::getDocCode, docCode.trim().toUpperCase());
        if (StrUtil.isNotBlank(excludeId)) {
            wrapper.ne(PrivacyPolicy::getId, excludeId.trim());
        }
        return getBaseMapper().selectCount(wrapper);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateStatus(String id, Integer status) {
        PrivacyPolicy entity = get(id);
        if (entity != null) {
            entity.setStatus(status);
            update(entity);
        }
    }
}
