package com.youlai.boot.system.service.impl;

import com.youlai.boot.core.security.util.SecurityUtils;
import com.youlai.boot.system.model.entity.SysFolder;
import com.youlai.boot.system.model.vo.UserInfoVO;
import com.youlai.boot.system.service.SysFolderService;
import com.youlai.boot.system.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.mapper.MineDemandMapper;
import com.youlai.boot.system.service.MineDemandService;
import com.youlai.boot.system.model.entity.MineDemand;
import com.youlai.boot.system.model.form.MineDemandForm;
import com.youlai.boot.system.model.query.MineDemandQuery;
import com.youlai.boot.system.model.vo.MineDemandVO;
import com.youlai.boot.system.converter.MineDemandConverter;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 煤矿需求管理服务实现类
 *
 * @author youlaitech
 * @since 2025-07-23 14:40
 */
@Service
@RequiredArgsConstructor

public class MineDemandServiceImpl extends ServiceImpl<MineDemandMapper, MineDemand> implements MineDemandService {

    private final MineDemandConverter mineDemandConverter;

    private final SysFolderService sysFolderService;

    private final UserService userService;

    /**
    * 获取煤矿需求管理分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<MineDemandVO>} 煤矿需求管理分页列表
    */
    @Override

    public IPage<MineDemandVO> getMineDemandPage(MineDemandQuery queryParams) {

        List<Integer> subIds = sysFolderService.getSubFolderIdsRecursively(queryParams.getFolderId());
        queryParams.setSubIds(subIds);

        UserInfoVO userInfo = userService.getCurrentUserInfo();

        String nickname = userInfo.getNickname();

        queryParams.setCurUserName(nickname);

        Page<MineDemandVO> pageVO = this.baseMapper.getMineDemandPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        pageVO.getRecords().forEach(mineDemandVO -> {
            int type = mineDemandVO.getType();
            if (type == 1) {
                mineDemandVO.setTypeName("新功能需求");
            }
            if (type == 2) {
                mineDemandVO.setTypeName("设备接入或页面升级");
            }
            if (type == 3) {
                mineDemandVO.setTypeName("功能优化或问题整改");
            }
        });
        return pageVO;
    }
    
    /**
     * 获取煤矿需求管理表单数据
     *
     * @param id 煤矿需求管理ID
     * @return
     */
    @Override
    public MineDemandForm getMineDemandFormData(Long id) {
        MineDemand entity = this.getById(id);
        return mineDemandConverter.toForm(entity);
    }
    
    /**
     * 新增煤矿需求管理
     *
     * @param formData 煤矿需求管理表单对象
     * @return
     */
    @Override
    public boolean saveMineDemand(MineDemandForm formData) {
        MineDemand entity = mineDemandConverter.toEntity(formData);
        entity.setCreateTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());
        return this.save(entity);
    }
    
    /**
     * 更新煤矿需求管理
     *
     * @param id   煤矿需求管理ID
     * @param formData 煤矿需求管理表单对象
     * @return
     */
    @Override
    public boolean updateMineDemand(Long id,MineDemandForm formData) {
        MineDemand entity = mineDemandConverter.toEntity(formData);
        entity.setUpdateTime(LocalDateTime.now());
        return this.updateById(entity);
    }
    
    /**
     * 删除煤矿需求管理
     *
     * @param ids 煤矿需求管理ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteMineDemands(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的煤矿需求管理数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
