package com.wzj.soopin.member.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzj.soopin.member.domain.po.Member;
import com.wzj.soopin.member.domain.po.OrgMember;
import com.wzj.soopin.member.mapper.MemberMapper;
import com.wzj.soopin.member.mapper.OrgMemberMapper;
import com.wzj.soopin.member.service.IMemberService;
import com.wzj.soopin.member.service.IMemberSyncService;

import jodd.typeconverter.impl.LocalDateConverter;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.dromara.common.core.utils.LocalDataUtil;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * 会员同步服务实现类
 *
 * @author 王智杰
 */
@Slf4j
@Service
@AllArgsConstructor
public class MemberSyncServiceImpl implements IMemberSyncService {

    private final OrgMemberMapper orgMemberMapper;

    private final MemberMapper memberMapper;

    private final IMemberService memberService;
    @Override
    public void syncMember(Long memberId) throws InvocationTargetException, IllegalAccessException {
        //访问生产数据库
        //生产数据库为mysql

        //使用orgMember表为用户表
        //获取总条数
        //分包计算
        //每次获取1000条
        //循环处理，转成Member表，
        //检查是否存在，不存在则新增，存在则更新
        //更新时，只更新会员用户名、会员密码、昵称、手机号、邮箱、头像、性别、年龄、生日、会员等级、积分、余额、状态、备注、创建时间、更新时间、删除标志
        //访问生产数据库
        //生产数据库为mysql

        //使用orgMember表为用户表
        //获取总条数 n
        int totalCount = orgMemberMapper.selectCount(null).intValue();
        log.info("开始同步会员数据，总条数：{}", totalCount);

        //分包计算
        int pageSize = 1000;
        int totalPages = (totalCount + pageSize - 1) / pageSize;
        LocalDateConverter localDateConverter = new LocalDateConverter();

        //每次获取1000条
        for (int pageNum = 1; pageNum <= totalPages; pageNum++) {
            int offset = (pageNum - 1) * pageSize;
            Page<OrgMember> page = new Page<>(pageNum, pageSize);
            Page<OrgMember> orgMembers = orgMemberMapper.selectPage(page, null);

            //循环处理，转成Member表
            for (OrgMember orgMember : orgMembers.getRecords()) {
                Member member = new Member();
                BeanUtils.copyProperties(orgMember, member);

                //检查是否存在，不存在则新增，存在则更新
                Member curMember = memberService.findByMobile(orgMember.getMobile());
                if (curMember == null) {
                    curMember=new Member();
                    BeanUtils.copyProperties(orgMember, curMember);
                    curMember.setGender(orgMember.getSex());
                    curMember.setNickname(orgMember.getNickName());
                    curMember.setBirthday(localDateConverter.convert(orgMember.getBirthday()));
                    curMember.setPhoneHidden(orgMember.getMobile());
                    curMember.setPhoneEncrypted(orgMember.getMobile());
                    curMember.setUserSig(orgMember.getUserSig());
                    //新增会员
                    memberService.saveOrUpdate(member);
                    log.info("新增会员：{}", orgMember.getUsername());
                } else {
                    //更新时，只更新会员用户名、会员密码、昵称、手机号、邮箱、头像、性别、年龄、生日、会员等级、积分、余额、状态、备注、创建时间、更新时间、删除标志
                    BeanUtils.copyProperties(orgMember, curMember);
                    curMember.setGender(orgMember.getSex());
                    curMember.setNickname(orgMember.getNickName());
                    curMember.setBirthday(localDateConverter.convert(orgMember.getBirthday()));
                    curMember.setPhoneHidden(orgMember.getMobile());
                    curMember.setPhoneEncrypted(orgMember.getMobile());
                    curMember.setUserSig(orgMember.getUserSig());

                    memberMapper.updateById(curMember);
                    log.info("更新会员：{}", orgMember.getUsername());
                }
            }
            log.info("完成第{}页会员同步，共{}条数据", pageNum, orgMembers.getRecords().size());
        }
        log.info("会员同步完成，总处理条数：{}", totalCount);

    }
}
