package com.hzs.member.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EServiceStatus;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.member.account.CuMemberAuthentication;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.member.ext.CuMemberExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.account.mapper.CuMemberAuthenticationMapper;
import com.hzs.member.account.service.ICuMemberAuthenticationService;
import com.hzs.member.account.vo.CuMemberAuthenticationVO;
import com.hzs.member.base.service.ICuMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * 实名认证 服务实现类
 */
@Service
public class CuMemberAuthenticationServiceImpl extends ServiceImpl<CuMemberAuthenticationMapper, CuMemberAuthentication> implements ICuMemberAuthenticationService {

    @Autowired
    ICuMemberService iCuMemberService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveAuthentication(CuMemberAuthenticationVO authenticationVO) {
        CuMemberAuthentication authentication = BeanUtil.copyProperties(authenticationVO, CuMemberAuthentication.class);
        authentication.setPkCreator(authenticationVO.getPkMember());
        save(authentication);
        return updateCuMemberRealName(authenticationVO);
    }

    /**
     * |更新用户表中的实名状态
     *
     * @param authenticationVO 入参
     * @return Boolean
     */
    public Boolean updateCuMemberRealName(CuMemberAuthenticationVO authenticationVO) {
        Long pkMember = authenticationVO.getPkMember();
        LambdaUpdateWrapper<CuMember> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(CuMember::getIsRealName, EYesNo.YES.getIntValue());
        updateWrapper.set(CuMember::getIdCard, authenticationVO.getIdCard());
        updateWrapper.set(CuMember::getMemberName, authenticationVO.getAccountName());
        updateWrapper.set(CuMember::getServiceAgreement, EServiceStatus.SIGNED.getValue());
        updateWrapper.set(CuMember::getModifiedTime, new Date());
        updateWrapper.set(CuMember::getPkModified, pkMember);
        updateWrapper.eq(CuMember::getPkId, pkMember);
        return iCuMemberService.update(updateWrapper);
    }

    @Override
    public Boolean memberRenewalContract(Long pkMember) {
        CuMemberExt member = iCuMemberService.getMemberById(SecurityUtils.getUserId());
        //到期日期
        LocalDate expireDate = member.getExpireDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LambdaUpdateWrapper<CuMember> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(CuMember::getServiceAgreement, EServiceStatus.SIGNED.getValue());
        updateWrapper.set(CuMember::getExpireDate, DateUtils.toDate(expireDate.plusYears(1).atStartOfDay()));
        updateWrapper.set(CuMember::getModifiedTime, new Date());
        updateWrapper.set(CuMember::getPkModified, pkMember);
        updateWrapper.eq(CuMember::getPkId, pkMember);
        return iCuMemberService.update(updateWrapper);
    }

    @Override
    public CuMemberAuthenticationVO selectCuMemberAuthenticationByPkMember(CuMemberAuthenticationVO authenticationVO) {
        CuMemberAuthentication authentication = baseMapper.selectAuthenticationByPkMember(authenticationVO);
        if (null != authentication) {
            return BeanUtil.copyProperties(authentication, CuMemberAuthenticationVO.class);
        }
        return null;

    }

    @Override
    public Boolean deleteByPkMembers(List<Long> pkMembers, Long updatePkMember) {
        return baseMapper.deleteByPkMembers(pkMembers, updatePkMember);
    }

}
