package com.liquidnet.client.admin.zhengzai.adam.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.liquidnet.client.admin.zhengzai.adam.service.IAdamCaomeiBadgeApplyAuditAdminService;
import com.liquidnet.service.adam.dto.AdamCaomeiBadgeApplyAuditDetailDto;
import com.liquidnet.service.adam.dto.AdamCaomeiBadgeApplyAuditDto;
import com.liquidnet.service.adam.dto.param.AdamCaomeiBadgeApplyAuditSearchParam;
import com.liquidnet.service.adam.dto.vo.AdamCaomeiBadgeApplyAuditDetailVo;
import com.liquidnet.service.adam.dto.vo.AdamCaomeiBadgeApplyAuditVo;
import com.liquidnet.service.adam.entity.AdamCaomeiBadgeApplyRecord;
import com.liquidnet.service.adam.mapper.AdamCaomeiBadgeApplyRecordMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdamCaomeiBadgeApplyAuditAdminServiceImpl implements IAdamCaomeiBadgeApplyAuditAdminService {

    @Autowired
    private AdamCaomeiBadgeApplyRecordMapper badgeApplyRecordMapper;

    @Override
    public PageInfo<AdamCaomeiBadgeApplyAuditVo> listApplyAudits(AdamCaomeiBadgeApplyAuditSearchParam param) {
        if (param == null) {
            param = new AdamCaomeiBadgeApplyAuditSearchParam();
        }
        List<AdamCaomeiBadgeApplyAuditDto> rows = badgeApplyRecordMapper.selectAuditList(
                StringUtils.trimToEmpty(param.getUserName()),
                param.getAuditStatus()
        );
        PageInfo<AdamCaomeiBadgeApplyAuditDto> dtoPage = new PageInfo<>(rows);
        List<AdamCaomeiBadgeApplyAuditVo> voList = dtoPage.getList().stream().map(item -> {
            AdamCaomeiBadgeApplyAuditVo vo = new AdamCaomeiBadgeApplyAuditVo();
            BeanUtils.copyProperties(item, vo);
            vo.setIdCard(maskIdCard(item.getIdCard()));
            return vo;
        }).collect(Collectors.toList());
        PageInfo<AdamCaomeiBadgeApplyAuditVo> voPage = new PageInfo<>(voList);
        voPage.setTotal(dtoPage.getTotal());
        return voPage;
    }

    @Override
    public AdamCaomeiBadgeApplyAuditDetailVo getApplyAuditDetail(String applyRecordId) {
        if (StringUtils.isBlank(applyRecordId)) {
            return null;
        }
        AdamCaomeiBadgeApplyAuditDetailDto detail = badgeApplyRecordMapper.selectAuditDetailByApplyRecordId(applyRecordId.trim());
        if (detail == null) {
            return null;
        }
        AdamCaomeiBadgeApplyAuditDetailVo vo = new AdamCaomeiBadgeApplyAuditDetailVo();
        BeanUtils.copyProperties(detail, vo);
        vo.setIdCard(maskIdCard(detail.getIdCard()));
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean passApplyAudit(String applyRecordId) {
        if (StringUtils.isBlank(applyRecordId)) {
            return false;
        }
        AdamCaomeiBadgeApplyRecord record = badgeApplyRecordMapper.selectOne(
                Wrappers.lambdaQuery(AdamCaomeiBadgeApplyRecord.class)
                        .eq(AdamCaomeiBadgeApplyRecord::getApplyRecordId, applyRecordId.trim())
                        .last("limit 1")
        );
        if (record == null || record.getAuditStatus() == null || record.getAuditStatus() != 0) {
            return false;
        }
        if (StringUtils.isBlank(record.getUserId()) || StringUtils.isBlank(record.getBadgeId())) {
            return false;
        }
        int updated = badgeApplyRecordMapper.passAudit(applyRecordId.trim());
        if (updated <= 0) {
            return false;
        }
        badgeApplyRecordMapper.issueBadgeIfAbsent(record.getUserId(), record.getBadgeId());
        return true;
    }

    @Override
    public boolean rejectApplyAudit(String applyRecordId, String rejectReason) {
        if (StringUtils.isBlank(applyRecordId) || StringUtils.isBlank(rejectReason)) {
            return false;
        }
        return badgeApplyRecordMapper.rejectAudit(applyRecordId.trim(), rejectReason.trim()) > 0;
    }

    private static String maskIdCard(String idCard) {
        if (StringUtils.isBlank(idCard)) {
            // 未实名或无证件信息
            return "";
        }
        if (idCard.length() > 8) {
            return idCard.substring(0, 4) + "****" + idCard.substring(idCard.length() - 4);
        }
        return "****";
    }
}
