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

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.system.controller.admin.equipment.vo.userExt.UsersExtRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.dataobject.equipment.DistributeRecordDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

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

@Mapper
public interface UsersExtConvert {

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

    default List<UsersExtRespVO> convertList(List<UsersExtRespVO> list, Map<Long, DeptDO> deptMap, Map<Long, DistributeRecordDO> recordDOMap) {

        return CollectionUtils.convertList(list, user -> convert(user, deptMap.get(user.getDeptId()), recordDOMap.get(user.getUserId())));
    }

    default UsersExtRespVO convert(UsersExtRespVO usersExtDO, DeptDO dept, DistributeRecordDO recordDO) {

        if (dept != null) {
            usersExtDO.setDeptName(dept.getName());
        }

        if (usersExtDO.getFaceImg() != null) {

            usersExtDO.setFaceImg(usersExtDO.getFaceImg() + "?time=" + System.currentTimeMillis());
        }

        if (usersExtDO.getAttendanceMachineNos() != null) {

           usersExtDO.setDevices(JsonUtils.parseArray(usersExtDO.getAttendanceMachineNos(), String.class));
        }

        if (recordDO != null) {

            usersExtDO.setResult(recordDO.getResult());
            usersExtDO.setCreateTime(recordDO.getCreateTime());
        }

        return usersExtDO;
    }
}
