package cn.iocoder.yudao.module.system.convert.user;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.smartfactory.api.factoryInfo.dto.FactoryInfoDTO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.factoryUser.FactoryUserRespVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictDataDO;
import cn.iocoder.yudao.module.system.dal.dataobject.equipment.UsersExtDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;

@Mapper
public interface FactoryUserConvert {

    FactoryUserConvert INSTANCE = Mappers.getMapper(FactoryUserConvert.class);

    default List<FactoryUserRespVO> convertList(List<AdminUserDO> list,
                                                Map<Long, FactoryInfoDTO> factoryInfoMap,
                                                Map<String, DictDataDO> dictDataMap,
                                                Map<Long, UsersExtDO> usersExtDOMap) {

        return CollectionUtils.convertList(list, user -> convert(user, factoryInfoMap.get(user.getDeptId()), dictDataMap, usersExtDOMap.get(user.getId())));
    }

    default FactoryUserRespVO convert(AdminUserDO user, FactoryInfoDTO factoryInfo, Map<String, DictDataDO> dict, UsersExtDO usersExt) {

        FactoryUserRespVO userVO = BeanUtils.toBean(user, FactoryUserRespVO.class);
        if (factoryInfo != null) {

            userVO.setFactoryId(factoryInfo.getId());
            userVO.setShortName(factoryInfo.getShortName());
        }

        if (usersExt != null) {

            userVO.setFaceImg(usersExt.getFaceImg());
            userVO.setWorkType(usersExt.getWorkType());
            userVO.setWorkTypeName(dict.get(usersExt.getWorkType().toString()).getLabel());
        }

        return userVO;
    }

    default FactoryUserRespVO convert(AdminUserDO user, FactoryInfoDTO factoryInfoDTO, DictDataDO dict, UsersExtDO usersExt) {

        FactoryUserRespVO userVO = BeanUtils.toBean(user, FactoryUserRespVO.class);
        if (factoryInfoDTO != null) {

            userVO.setFactoryId(factoryInfoDTO.getId());
            userVO.setShortName(factoryInfoDTO.getShortName());
        }

        if (usersExt != null) {

            userVO.setWorkType(usersExt.getWorkType());
            userVO.setFaceImg(usersExt.getFaceImg());
        }

        if (dict != null) {

            userVO.setWorkTypeName(dict.getLabel());
        }

        return userVO;
    }
}
