package com.dcs.hnyz.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dcs.common.enums.GeneralStatus;
import com.dcs.common.utils.DateUtils;
import com.dcs.hnyz.domain.Equipment;
import com.dcs.hnyz.service.IEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dcs.hnyz.mapper.PageControlMapper;
import com.dcs.hnyz.domain.PageControl;
import com.dcs.hnyz.service.IPageControlService;

import javax.annotation.Resource;

/**
 * 页面管理Service业务层处理
 * 
 * @author hmy
 * @date 2025-07-25
 */
@Service
public class PageControlServiceImpl implements IPageControlService 
{
    @Resource
    private PageControlMapper pageControlMapper;

    @Resource
    private IEquipmentService equipmentService;

    /**
     * 查询页面管理
     * 
     * @param pageId 页面管理主键
     * @return 页面管理
     */
    @Override
    public PageControl selectPageControlByPageId(Long pageId)
    {
        return pageControlMapper.selectById(pageId);
    }

    /**
     * 查询页面管理列表
     * 
     * @param pageControl 页面管理
     * @return 页面管理
     */
    @Override
    public List<PageControl> selectPageControlList(PageControl pageControl)
    {
        return pageControlMapper.selectPageControlList(pageControl);
    }

    /**
     * 新增页面管理
     * 
     * @param pageControl 页面管理
     * @return 结果
     */
    @Override
    public int insertPageControl(PageControl pageControl)
    {
        return pageControlMapper.insert(pageControl);
    }

    /**
     * 修改页面管理
     * 
     * @param pageControl 页面管理
     * @return 结果
     */
    @Override
    public int updatePageControl(PageControl pageControl)
    {
        return pageControlMapper.updateById(pageControl);
    }

    /**
     * 批量删除页面管理
     * 
     * @param pageIds 需要删除的页面管理主键
     * @return 结果
     */
    @Override
    public int deletePageControlByPageIds(Long[] pageIds)
    {
        return pageControlMapper.deleteBatchIds(Arrays.asList(pageIds));
    }

    /**
     * 删除页面管理信息
     * 
     * @param pageId 页面管理主键
     * @return 结果
     */
    @Override
    public int deletePageControlByPageId(Long pageId)
    {
        return pageControlMapper.deleteById(pageId);
    }

    @Override
    public Map<Long, String> getPageNameMap() {
        List<PageControl> pageControlList = pageControlMapper.selectList(null);
        return pageControlList.stream().collect(Collectors.toMap(PageControl::getPageId, PageControl::getPageName));
    }

    @Override
    public List<PageControl> getPageList(Long equipmentId) {
        LambdaQueryWrapper<PageControl> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(PageControl::getStatus, GeneralStatus.ENABLE.getCode());
        if(equipmentId==null){//不传设备id时默认查全部
            return pageControlMapper.selectList(wrapper);
        }
        Equipment e = equipmentService.selectEquipmentByEquipmentId(equipmentId);
        String pageIds = e.getPageIds();//获取ids
        List<PageControl> list=new ArrayList<>();
        for (String s : pageIds.split(",")) {
            PageControl pageControl=pageControlMapper.selectById(Long.valueOf(s));
            list.add(pageControl);
        }
        return list;
    }

    @Override
    public PageControl getPageByCode(String pageCode) {
        return pageControlMapper.selectOne(new LambdaQueryWrapper<PageControl>().eq(PageControl::getPageCode,pageCode));
    }
}
