package org.dromara.project.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.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.project.domain.BusConstructionUserExit;
import org.dromara.project.domain.dto.constructionuserexit.BusConstructionUserExitQueryReq;
import org.dromara.project.domain.vo.constructionuserexit.BusConstructionUserExitVo;
import org.dromara.project.mapper.BusConstructionUserExitMapper;
import org.dromara.project.service.IBusConstructionUserExitService;
import org.dromara.system.domain.vo.SysOssVo;
import org.dromara.system.service.ISysOssService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 施工人员入场退场记录信息Service业务层处理
 *
 * @author lcj
 * @date 2025-03-31
 */
@Service
public class BusConstructionUserExitServiceImpl extends ServiceImpl<BusConstructionUserExitMapper, BusConstructionUserExit>
    implements IBusConstructionUserExitService {

    @Resource
    private ISysOssService ossService;

    /**
     * 查询施工人员入场退场记录信息
     *
     * @param id 主键
     * @return 施工人员入场退场记录信息
     */
    @Override
    public BusConstructionUserExitVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询施工人员入场退场记录信息列表
     *
     * @param req       查询条件
     * @param pageQuery 分页参数
     * @return 施工人员入场退场记录信息分页列表
     */
    @Override
    public TableDataInfo<BusConstructionUserExitVo> queryPageList(BusConstructionUserExitQueryReq req, PageQuery pageQuery) {
        Page<BusConstructionUserExit> result = this.page(pageQuery.build(), buildQueryWrapper(req));
        return TableDataInfo.build(this.getVoPage(result));
    }

    /**
     * 查询符合条件的施工人员入场退场记录信息列表
     *
     * @param req 查询条件
     * @return 施工人员入场退场记录信息列表
     */
    @Override
    public List<BusConstructionUserExitVo> queryList(BusConstructionUserExitQueryReq req) {
        LambdaQueryWrapper<BusConstructionUserExit> lqw = buildQueryWrapper(req);
        return this.list(lqw).stream().map(this::getVo).toList();
    }

    /**
     * 获取施工人员入场退场记录视图对象
     *
     * @param constructionUserExit 施工人员入场退场记录对象
     * @return 施工人员入场退场记录视图对象
     */
    @Override
    public BusConstructionUserExitVo getVo(BusConstructionUserExit constructionUserExit) {
        // 对象转封装类
        BusConstructionUserExitVo constructionUserExitVo = new BusConstructionUserExitVo();
        if (constructionUserExit == null) {
            return constructionUserExitVo;
        }
        BeanUtils.copyProperties(constructionUserExit, constructionUserExitVo);
        String path = constructionUserExitVo.getPath();
        if (StringUtils.isNotBlank(path)) {
            List<Long> ossIdList = Arrays.stream(path.split(",")).map(Long::parseLong).toList();
            constructionUserExitVo.setPathUrl(ossService.listByIds(ossIdList).stream().map(SysOssVo::getUrl).toList());
        }
        return constructionUserExitVo;
    }

    /**
     * 获取施工人员入场退场记录信息查询条件封装
     *
     * @param req 查询条件
     * @return 查询条件封装
     */
    @Override
    public LambdaQueryWrapper<BusConstructionUserExit> buildQueryWrapper(BusConstructionUserExitQueryReq req) {
        LambdaQueryWrapper<BusConstructionUserExit> lqw = Wrappers.lambdaQuery();
        if (req == null) {
            return lqw;
        }
        Long id = req.getId();
        String sfzNumber = req.getSfzNumber();
        Long userId = req.getUserId();
        Long projectId = req.getProjectId();
        Long teamId = req.getTeamId();
        String entryDate = req.getEntryDate();
        String leaveDate = req.getLeaveDate();
        String remark = req.getRemark();
        // 模糊查询
        lqw.like(StringUtils.isNotBlank(sfzNumber), BusConstructionUserExit::getSfzNumber, sfzNumber);
        lqw.like(StringUtils.isNotBlank(remark), BusConstructionUserExit::getRemark, remark);
        // 精确查询
        lqw.eq(ObjectUtils.isNotEmpty(id), BusConstructionUserExit::getId, id);
        lqw.eq(ObjectUtils.isNotEmpty(userId), BusConstructionUserExit::getUserId, userId);
        lqw.eq(ObjectUtils.isNotEmpty(projectId), BusConstructionUserExit::getProjectId, projectId);
        lqw.eq(ObjectUtils.isNotEmpty(teamId), BusConstructionUserExit::getTeamId, teamId);
        lqw.eq(StringUtils.isNotBlank(entryDate), BusConstructionUserExit::getEntryDate, entryDate);
        lqw.eq(StringUtils.isNotBlank(leaveDate), BusConstructionUserExit::getLeaveDate, leaveDate);
        // 排序
        lqw.orderByAsc(BusConstructionUserExit::getId);
        return lqw;
    }

    /**
     * 获取施工人员入场退场记录分页对象视图
     *
     * @param constructionUserExitPage 施工人员入场退场记录分页对象
     * @return 施工人员入场退场记录分页对象视图
     */
    @Override
    public Page<BusConstructionUserExitVo> getVoPage(Page<BusConstructionUserExit> constructionUserExitPage) {
        List<BusConstructionUserExit> constructionUserExitList = constructionUserExitPage.getRecords();
        Page<BusConstructionUserExitVo> constructionUserExitVoPage = new Page<>(
            constructionUserExitPage.getCurrent(),
            constructionUserExitPage.getSize(),
            constructionUserExitPage.getTotal());
        if (CollUtil.isEmpty(constructionUserExitList)) {
            return constructionUserExitVoPage;
        }
        List<BusConstructionUserExitVo> constructionUserExitVoList = constructionUserExitList.stream().map(this::getVo).toList();
        constructionUserExitVoPage.setRecords(constructionUserExitVoList);
        return constructionUserExitVoPage;
    }

}
