package com.zcloud.basic.info.gatewayimpl;

import cn.hutool.json.JSONUtil;
import com.alibaba.cola.dto.Response;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jjb.saas.system.client.user.facade.UserFacade;
import com.jjb.saas.system.client.user.request.RoleDeptAddCmd;
import com.jjb.saas.system.client.user.request.UserUpdatePasswordCmd;
import com.zcloud.basic.info.domain.enums.UserEmploymentFlagEnum;
import com.zcloud.basic.info.domain.gateway.UserGateway;
import com.zcloud.basic.info.domain.model.UserE;
import com.zcloud.basic.info.persistence.dataobject.UserDO;
import com.zcloud.basic.info.persistence.repository.UserRepository;
import lombok.AllArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static dm.jdbc.util.DriverUtil.log;

/**
* web-infrastructure
* @Author zhangyue
* @Date 2025-11-04 14:07:35
*/
@Service
@AllArgsConstructor
public class UserGatewayImpl implements UserGateway {
    private final UserRepository userRepository;
    @DubboReference
    private UserFacade userFacade;

    @Override
    public Boolean add(UserE userE) {
        UserDO d = new UserDO();
        BeanUtils.copyProperties(userE, d);
        userRepository.saveUser(d);
        userE.setId(d.getId());
        return true;
    }

    @Override
    public Boolean addYanshi(UserE userE) {
        UserDO d = new UserDO();
        BeanUtils.copyProperties(userE, d);
        userRepository.saveUserYanshi(d);
        userE.setId(d.getId());
        return true;
    }

    @Override
    public Boolean register(UserE userE) {
        UserDO d = new UserDO();
        BeanUtils.copyProperties(userE, d);
        userRepository.saveRegisterUser(d);
        userE.setId(d.getId());
        return true;
    }

    @Override
    public boolean addXgf(UserE userE) {
        UserDO d = new UserDO();
        // 新增用户默认在职
        userE.setEmploymentFlag(UserEmploymentFlagEnum.ON.getCode());
        BeanUtils.copyProperties(userE, d);
        userE.resetPassword(3);
        d.setPassword(userE.getPassword());
        userRepository.saveUserXgf(d);
        userE.setId(d.getId());
        return true;
    }

    @Override
    public Boolean update(UserE userE) {
        UserDO d = new UserDO();
        BeanUtils.copyProperties(userE, d);
        userRepository.updateById(d);
        return true;
    }

    @Override
    public Boolean updateByPhone(UserE userE) {
        UserDO d = new UserDO();

        d.setUserAvatarUrl(userE.getUserAvatarUrl());
        d.setName(userE.getName());
        d.setUserIdCard(userE.getUserIdCard());
        d.setNation(userE.getNation());
        d.setNationName(userE.getNationName());
        d.setCurrentAddress(userE.getCurrentAddress());
        d.setLocationAddress(userE.getLocationAddress());
        d.setCulturalLevel(userE.getCulturalLevel());
        d.setCulturalLevelName(userE.getCulturalLevelName());
        d.setPoliticalAffiliation(userE.getPoliticalAffiliation());
        d.setPoliticalAffiliationName(userE.getPoliticalAffiliationName());
        d.setMaritalStatus(userE.getMaritalStatus());
        d.setMaritalStatusName(userE.getMaritalStatusName());
        if(userE.getFlowFlag() != null){
            d.setFlowFlag(userE.getFlowFlag());
        }
        d.setEmail(userE.getEmail());

        UpdateWrapper<UserDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("phone", userE.getPhone());
        userRepository.update(d, updateWrapper);
        return true;
    }



    @Override
    public Boolean updateByIds(UserE userE, List<Long> ids) {
        UserDO d = new UserDO();

        d.setUsername(userE.getPhone());
        d.setPhone(userE.getPhone());

        d.setUserAvatarUrl(userE.getUserAvatarUrl());
        d.setName(userE.getName());
        d.setUserIdCard(userE.getUserIdCard());
        d.setNation(userE.getNation());
        d.setNationName(userE.getNationName());
        d.setCurrentAddress(userE.getCurrentAddress());
        d.setLocationAddress(userE.getLocationAddress());
        d.setCulturalLevel(userE.getCulturalLevel());
        d.setCulturalLevelName(userE.getCulturalLevelName());
        d.setPoliticalAffiliation(userE.getPoliticalAffiliation());
        d.setPoliticalAffiliationName(userE.getPoliticalAffiliationName());
        d.setMaritalStatus(userE.getMaritalStatus());
        d.setMaritalStatusName(userE.getMaritalStatusName());
        if(userE.getFlowFlag() != null){
            d.setFlowFlag(userE.getFlowFlag());
        }
        d.setEmail(userE.getEmail());

        UpdateWrapper<UserDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids);

        userRepository.update(d, updateWrapper);
        return true;
    }

    @Override
    public Boolean updateByGBS(UserE userE) {
        com.jjb.saas.system.client.user.request.UserUpdateCmd updateCmd = new com.jjb.saas.system.client.user.request.UserUpdateCmd();
        updateCmd.setId(userE.getId());
        updateCmd.setAccount(userE.getPhone());
        updateCmd.setMobile(userE.getPhone());
        updateCmd.setName(userE.getName());
        updateCmd.setEmail(userE.getEmail());
        RoleDeptAddCmd roleDeptAddCmd = new RoleDeptAddCmd();
        roleDeptAddCmd.setRoleId(userE.getRoleId());
        roleDeptAddCmd.setDeptId(userE.getDepartmentId());
        List<RoleDeptAddCmd> roleDeptAddCmdList = Collections.singletonList(roleDeptAddCmd);
        updateCmd.setRoleDepts(roleDeptAddCmdList);
        log.info("GBS修改用户开始,用户信息:{}", JSONUtil.toJsonStr(updateCmd));
        Response response = userFacade.update(updateCmd);
        log.info("GBS修改用户结束,用户id:{},结果:{}", JSONUtil.toJsonStr(response));
        return true;
    }


    @Override
    public Boolean updateInfoAndPasswordByGBS(UserE userE) {
        com.jjb.saas.system.client.user.request.UserUpdateCmd updateCmd = new com.jjb.saas.system.client.user.request.UserUpdateCmd();
        updateCmd.setId(userE.getId());
        updateCmd.setAccount(userE.getPhone());
        updateCmd.setMobile(userE.getPhone());
        updateCmd.setName(userE.getName());
        updateCmd.setEmail(userE.getEmail());
        RoleDeptAddCmd roleDeptAddCmd = new RoleDeptAddCmd();
        roleDeptAddCmd.setRoleId(userE.getRoleId());
        roleDeptAddCmd.setDeptId(userE.getDepartmentId());
        List<RoleDeptAddCmd> roleDeptAddCmdList = Collections.singletonList(roleDeptAddCmd);
        updateCmd.setRoleDepts(roleDeptAddCmdList);
        log.info("GBS修改用户开始,用户信息:{}", JSONUtil.toJsonStr(updateCmd));
        Response response = userFacade.update(updateCmd);
        log.info("GBS修改用户结束,用户id:{},结果:{}", JSONUtil.toJsonStr(response));
//        UserUpdatePasswordCmd updatePasswordCmd = new UserUpdatePasswordCmd();
//        updatePasswordCmd.setId(userE.getId());
//        updatePasswordCmd.setPassword(userE.getPassword());
//        updatePasswordCmd.setVersion(1);
//        userFacade.updatePassword(updatePasswordCmd);
        return true;
    }

    @Override
    public Boolean updateByAppGBS(UserE userE) {
        com.jjb.saas.system.client.user.request.UserUpdateCmd updateCmd = new com.jjb.saas.system.client.user.request.UserUpdateCmd();
        updateCmd.setId(userE.getId());
        updateCmd.setAccount(userE.getPhone());
        updateCmd.setName(userE.getName());
        updateCmd.setEmail(userE.getEmail());
        log.info("GBS修改用户app信息开始,用户信息:{}", JSONUtil.toJsonStr(updateCmd));
        Response response = userFacade.update(updateCmd);
        log.info("GBS修改用户app信息结束,用户id:{},结果:{}", JSONUtil.toJsonStr(response));
        return true;
    }

    @Override
    public Boolean updateInfo(UserE userE) {
        UserDO d = new UserDO();
        BeanUtils.copyProperties(userE, d);
        userRepository.updateInfoById(d);
        return true;
    }

    @Override
    public void deletedUserById(Long id) {
        userRepository.deleteUserById(id);
    }

    @Override
    public Boolean deletedUserByIds(Long[] ids) {
        return userRepository.removeByIds(Arrays.asList(ids));
    }

    @Override
    public Response countUser(Map<String,Object> params) {
        return userRepository.countUser(params);
    }

    @Override
    public Response updatePassword(UserE userE) {
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(userE, userDO);
        return userRepository.updatePassword(userDO);
    }

    @Override
    @Transactional
    public void updateXgf(UserE userE, Long id) {
        if (StringUtils.isEmpty(userE.getUsername())) {
            userE.setUsername(userE.getPhone());
        }
        Integer flowFlag = userE.getFlowFlag();
        //修改所有手机号的信息
//        userE.resetPassword();
        userE.setEmploymentFlag(null);
        userE.setFlowFlag(null);
        this.updateByPhone(userE);
        //修改.状态为在职
        if(id!= null){
            userE.setId(id);
        }

        userE.setEmploymentFlag(UserEmploymentFlagEnum.ON.getCode());
        userE.setFlowFlag(flowFlag);
        this.update(userE);
        userRepository.updateXgf(userE);
    }

    @Override
    public void deletedUserByPhone(String phone) {

        userRepository.deletedUserByPhone(phone);
    }
}

