package com.qqchen.deploy.backend.system.converter;

import com.qqchen.deploy.backend.system.entity.User;
import com.qqchen.deploy.backend.framework.converter.BaseConverter;
import com.qqchen.deploy.backend.system.model.UserDTO;
import com.qqchen.deploy.backend.system.model.response.LoginResponse;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.Mappings;

@Mapper(config = BaseConverter.class)
public interface UserConverter extends BaseConverter<User, UserDTO> {
    // MapStruct 会自动实现所有方法

    @Mapping(target = "token", ignore = true)
    @Mapping(target = "id", ignore = true)
    @Mapping(target = "departmentId", source = "department.id")
    @Mapping(target = "departmentName", source = "department.name")
    LoginResponse toLoginResponse(User user);

    @Mapping(target = "token", source = "token")
    @Mapping(target = "id", ignore = true)
    @Mapping(target = "departmentId", source = "user.department.id")
    @Mapping(target = "departmentName", source = "user.department.name")
    LoginResponse toLoginResponse(User user, String token);

    @Mapping(target = "token", ignore = true)
    @Mapping(target = "id", ignore = true)
    LoginResponse toLoginResponse(UserDTO userDTO);

    @Override
    @Mappings({
        @Mapping(target = "id", ignore = true),
        @Mapping(target = "password", ignore = true),
        @Mapping(target = "roles", ignore = true),
        @Mapping(target = "createTime", ignore = true),
        @Mapping(target = "createBy", ignore = true),
        @Mapping(target = "updateTime", ignore = true),
        @Mapping(target = "updateBy", ignore = true),
        @Mapping(target = "version", ignore = true),
        @Mapping(target = "deleted", ignore = true),
        @Mapping(target = "department", ignore = true)
    })
    void updateEntity(@MappingTarget User entity, UserDTO dto);

    @Override
    @Mapping(target = "departmentId", source = "department.id")
    @Mapping(target = "departmentName", source = "department.name")
    UserDTO toDto(User entity);
}