package com.multictrl.modules.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.multictrl.common.constant.Constant;
import com.multictrl.common.page.PageData;
import com.multictrl.common.utils.ConvertUtils;
import com.multictrl.common.utils.HmsUtils;
import com.multictrl.modules.business.dao.HmsDao;
import com.multictrl.modules.business.dto.HmsDTO;
import com.multictrl.modules.business.entity.HmsEntity;
import com.multictrl.modules.business.service.MiscService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * 杂项接口
 *
 * @author Sdy
 * @since 1.0.0 2026/5/10
 */
@Service
@RequiredArgsConstructor
public class MiscServiceImpl implements MiscService {
    private final HmsDao hmsDao;

    @Override
    public PageData<HmsDTO> pageHmsLog(Map<String, Object> params) {
        long curPage = params.get(Constant.PAGE) != null ? Long.parseLong((String) params.get(Constant.PAGE)) : 1;
        long limit = params.get(Constant.LIMIT) != null ? Long.parseLong((String) params.get(Constant.LIMIT)) : 10;
        Integer level = params.get("level") != null ? Integer.parseInt((String) params.get("level")) : null;
        String dockSn = (String) params.get("dockSn");
        Page<HmsEntity> page = hmsDao.selectPage(new Page<>(curPage, limit),
                new QueryWrapper<HmsEntity>().eq(StringUtils.isNotBlank(dockSn), "dock_sn", dockSn)
                        .eq(level != null, "level", level).orderByDesc("report_date"));
        List<HmsDTO> list = ConvertUtils.sourceToTarget(page.getRecords(), HmsDTO.class);
        for (HmsDTO dto : list) {
            dto.setDescription(HmsUtils.getHmsText(dto.getCode()));
        }

        return new PageData<>(list, page.getTotal());
    }
}
