package org.dromara.facility.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.dromara.common.core.constant.HttpStatus;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.ObjectUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.facility.domain.FacPercentageFacility;
import org.dromara.facility.domain.dto.percentagefacility.FacPercentageFacilityQueryReq;
import org.dromara.facility.domain.vo.percentagefacility.FacPercentageFacilityVo;
import org.dromara.facility.mapper.FacPercentageFacilityMapper;
import org.dromara.facility.service.IFacPercentageFacilityService;
import org.dromara.project.service.IBusProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 设施-百分比设施Service业务层处理
 *
 * @author lcj
 * @date 2025-05-26
 */
@Service
public class FacPercentageFacilityServiceImpl extends ServiceImpl<FacPercentageFacilityMapper, FacPercentageFacility>
    implements IFacPercentageFacilityService {

    @Resource
    private IBusProjectService projectService;

    /**
     * 查询设施-百分比设施
     *
     * @param id 主键
     * @return 设施-百分比设施
     */
    @Override
    public FacPercentageFacilityVo queryById(Long id) {
        FacPercentageFacility percentageFacility = this.getById(id);
        if (percentageFacility == null) {
            throw new ServiceException("查询设施失败，数据不存在", HttpStatus.NOT_FOUND);
        }
        return this.getVo(percentageFacility);
    }

    /**
     * 分页查询设施-百分比设施列表
     *
     * @param req       查询条件
     * @param pageQuery 分页参数
     * @return 设施-百分比设施分页列表
     */
    @Override
    public TableDataInfo<FacPercentageFacilityVo> queryPageList(FacPercentageFacilityQueryReq req, PageQuery pageQuery) {
        Page<FacPercentageFacility> result = this.page(pageQuery.build(), this.buildQueryWrapper(req));
        return TableDataInfo.build(this.getVoPage(result));
    }

    /**
     * 查询符合条件的设施-百分比设施列表
     *
     * @param req 查询条件
     * @return 设施-百分比设施列表
     */
    @Override
    public List<FacPercentageFacilityVo> queryList(FacPercentageFacilityQueryReq req) {
        List<FacPercentageFacility> list = this.list(this.buildQueryWrapper(req));
        return list.stream().map(this::getVo).toList();
    }

    /**
     * 校验并批量删除设施-百分比设施信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        Long userId = LoginHelper.getUserId();
        List<FacPercentageFacility> percentageFacilityList = this.listByIds(ids);
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
            List<Long> projectId = percentageFacilityList.stream().map(FacPercentageFacility::getProjectId).toList();
            projectService.validAuth(projectId, userId);
        }
        return this.removeBatchByIds(ids);
    }

    /**
     * 获取设施-百分比设施视图对象
     *
     * @param percentageFacility 设施-百分比设施对象
     * @return 设施-百分比设施视图对象
     */
    @Override
    public FacPercentageFacilityVo getVo(FacPercentageFacility percentageFacility) {
        // 对象转封装类
        FacPercentageFacilityVo percentageFacilityVo = new FacPercentageFacilityVo();
        if (percentageFacility == null) {
            return percentageFacilityVo;
        }
        BeanUtils.copyProperties(percentageFacility, percentageFacilityVo);
        return percentageFacilityVo;
    }

    /**
     * 获取设施-百分比设施查询条件封装
     *
     * @param req 查询条件
     * @return 查询条件封装
     */
    @Override
    public LambdaQueryWrapper<FacPercentageFacility> buildQueryWrapper(FacPercentageFacilityQueryReq req) {
        LambdaQueryWrapper<FacPercentageFacility> lqw = new LambdaQueryWrapper<>();
        if (req == null) {
            return lqw;
        }
        Long projectId = req.getProjectId();
        Long matrixId = req.getMatrixId();
        Long progressCategoryId = req.getProgressCategoryId();
        String progressCategoryName = req.getProgressCategoryName();
        lqw.eq(ObjectUtils.isNotEmpty(projectId), FacPercentageFacility::getProjectId, projectId);
        lqw.eq(ObjectUtils.isNotEmpty(matrixId), FacPercentageFacility::getMatrixId, matrixId);
        lqw.eq(ObjectUtils.isNotEmpty(progressCategoryId), FacPercentageFacility::getProgressCategoryId, progressCategoryId);
        lqw.eq(StringUtils.isNotBlank(progressCategoryName), FacPercentageFacility::getProgressCategoryName, progressCategoryName);
        return lqw;
    }

    /**
     * 获取设施-百分比设施分页对象视图
     *
     * @param percentageFacilityPage 设施-百分比设施分页对象
     * @return 设施-百分比设施分页对象视图
     */
    @Override
    public Page<FacPercentageFacilityVo> getVoPage(Page<FacPercentageFacility> percentageFacilityPage) {
        List<FacPercentageFacility> percentageFacilityList = percentageFacilityPage.getRecords();
        Page<FacPercentageFacilityVo> percentageFacilityVoPage = new Page<>(
            percentageFacilityPage.getCurrent(),
            percentageFacilityPage.getSize(),
            percentageFacilityPage.getTotal());
        if (CollUtil.isEmpty(percentageFacilityList)) {
            return percentageFacilityVoPage;
        }
        List<FacPercentageFacilityVo> percentageFacilityVoList = percentageFacilityList.stream().map(this::getVo).toList();
        percentageFacilityVoPage.setRecords(percentageFacilityVoList);
        return percentageFacilityVoPage;
    }
}
