
package com.tmerclub.cloud.platform.service.impl;

import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.tmerclub.cloud.api.auth.bo.UidInfoBO;
import com.tmerclub.cloud.api.auth.constant.SysTypeEnum;
import com.tmerclub.cloud.api.auth.dto.AuthAccountDTO;
import com.tmerclub.cloud.api.auth.feign.AccountFeignClient;
import com.tmerclub.cloud.api.auth.vo.AuthAccountVO;
import com.tmerclub.cloud.common.constant.IsPassShopEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.IpHelper;
import com.tmerclub.cloud.platform.dto.ChangeAccountDTO;
import com.tmerclub.cloud.platform.mapper.SysUserMapper;
import com.tmerclub.cloud.platform.model.SysUser;
import com.tmerclub.cloud.platform.service.SysUserAccountService;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author lhd
 * @date 2020/12/22
 */
@Service
public class SysUserAccountServiceImpl implements SysUserAccountService {

    @Resource
    private SysUserMapper sysUserMapper;
    @DubboReference
    private AccountFeignClient accountFeignClient;

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    @Transactional(rollbackFor = Exception.class)
    public ServerResponseEntity<Void> save(ChangeAccountDTO changeAccountDTO) {
        AuthAccountDTO authAccountDTO = getAuthAccountDTO(changeAccountDTO);
        authAccountDTO.setCreateIp(IpHelper.getIpAddr());
        authAccountDTO.setSysType(SysTypeEnum.PLATFORM.value());
        authAccountDTO.setIsAdmin(0);
        authAccountDTO.setIsPassShop(IsPassShopEnum.YES.value());
        // 保存
        ServerResponseEntity<Long> serverResponseEntity = accountFeignClient.save(authAccountDTO);
        if (!serverResponseEntity.isSuccess()) {
            return ServerResponseEntity.transform(serverResponseEntity);
        }
        SysUser sysUser = new SysUser();
        sysUser.setSysUserId(changeAccountDTO.getUserId());
        sysUser.setHasAccount(1);
        sysUser.setPhoneNum(changeAccountDTO.getPhone());
        sysUserMapper.update(sysUser);
        return ServerResponseEntity.success();
    }

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    @Transactional(rollbackFor = Exception.class)
    public ServerResponseEntity<Void> update(ChangeAccountDTO changeAccountDTO) {

        AuthAccountDTO authAccountDTO = getAuthAccountDTO(changeAccountDTO);
        // 更新，不涉及分布式事务
        ServerResponseEntity<Void> serverResponseEntity = accountFeignClient.update(authAccountDTO);
        if (!serverResponseEntity.isSuccess()) {
            return serverResponseEntity;
        }
        sysUserMapper.updatePhoneNum(changeAccountDTO.getUserId(), changeAccountDTO.getPhone());
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<AuthAccountVO> getByUserIdAndSysType(Long userId, Integer sysType) {
        ServerResponseEntity<AuthAccountVO> response = accountFeignClient.getByUserIdAndSysType(userId, sysType);
        AuthAccountVO authAccountVO = response.getData();
        if (Objects.nonNull(authAccountVO) && StrUtil.isNotBlank(authAccountVO.getPhone())) {
            authAccountVO.setPhone(PhoneUtil.hideBetween(authAccountVO.getPhone()).toString());
        }
        return response;
    }

    private AuthAccountDTO getAuthAccountDTO(ChangeAccountDTO changeAccountDTO) {
        AuthAccountDTO authAccountDTO = new AuthAccountDTO();
        UidInfoBO uidInfoBO = AuthUserContext.get();
        authAccountDTO.setTenantId(uidInfoBO.getTenantId());
        authAccountDTO.setPassword(changeAccountDTO.getPassword());
        authAccountDTO.setUsername(changeAccountDTO.getUsername());
        authAccountDTO.setStatus(changeAccountDTO.getStatus());
        authAccountDTO.setSysType(uidInfoBO.getSysType());
        authAccountDTO.setUserId(changeAccountDTO.getUserId());
        authAccountDTO.setEmail(changeAccountDTO.getEmail());
        authAccountDTO.setPhone(changeAccountDTO.getPhone());
        return authAccountDTO;
    }

}
