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

import com.tmerclub.cloud.api.auth.bo.UidInfoBO;
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.multishop.dto.ChangeAccountDTO;
import com.tmerclub.cloud.multishop.mapper.ShopUserMapper;
import com.tmerclub.cloud.multishop.model.ShopUser;
import com.tmerclub.cloud.multishop.service.ShopUserAccountService;
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;

/**
 * @author tmerclub
 * @date 2020/09/03
 */
@Service
public class ShopUserAccountServiceImpl implements ShopUserAccountService {

    @Resource
    private ShopUserMapper shopUserMapper;
    @DubboReference
    private AccountFeignClient accountFeignClient;

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    @Transactional(rollbackFor = Exception.class)
    public ServerResponseEntity<Void> save(ChangeAccountDTO changeAccountDTO) {
        AuthAccountDTO authAccountDTO = getAuthAccountDTO(changeAccountDTO);
        authAccountDTO.setIsPassShop(IsPassShopEnum.YES.value());
        authAccountDTO.setCreateIp(IpHelper.getIpAddr());
        authAccountDTO.setIsAdmin(0);
        // 保存
        ServerResponseEntity<Long> serverResponseEntity = accountFeignClient.save(authAccountDTO);
        if (!serverResponseEntity.isSuccess()) {
            return ServerResponseEntity.transform(serverResponseEntity);
        }
        ShopUser shopUser = new ShopUser();
        shopUser.setShopUserId(changeAccountDTO.getUserId());
        shopUser.setHasAccount(1);
        shopUser.setShopId(AuthUserContext.get().getTenantId());
        shopUserMapper.update(shopUser);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> update(ChangeAccountDTO changeAccountDTO) {

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

        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<AuthAccountVO> getByUserIdAndSysType(Long userId, Integer sysType) {
        return accountFeignClient.getByUserIdAndSysType(userId, sysType);
    }

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

}
