package com.ruoyi.bgt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
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.ruoyi.bgt.bo.BgtProjectTaskProgressQueryBo;
import com.ruoyi.bgt.domain.BgtProjectTaskProgress;
import com.ruoyi.bgt.domain.dto.BgtProjectTaskProgressQueryDTO;
import com.ruoyi.bgt.domain.vo.BgtProjectTaskProgressDetailVO;
import com.ruoyi.bgt.domain.vo.BgtProjectTaskProgressVO;
import com.ruoyi.bgt.mapper.BgtProjectTaskProgressMapper;
import com.ruoyi.bgt.service.IBgtProjectTaskProgressService;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.fbs.domain.FbsProjectTask;
import com.ruoyi.fbs.service.IFbsProjectTaskService;
import com.ruoyi.zbf.domain.ZbfProject;
import com.ruoyi.zbf.service.IZbfProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 包工头项目任务进度Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-18
 */
@Service
public class BgtProjectTaskProgressServiceImpl extends ServicePlusImpl<BgtProjectTaskProgressMapper, BgtProjectTaskProgress> implements IBgtProjectTaskProgressService {

	@Autowired
	private IZbfProjectService zbfProjectService;

	@Autowired
	private IFbsProjectTaskService fbsProjectTaskService;



    @Override
    public BgtProjectTaskProgress queryById(Long id){
        return getById(id);
    }

    @Override
    public TableDataInfo<BgtProjectTaskProgress> queryPageList(BgtProjectTaskProgressQueryBo bo) {
        Page<BgtProjectTaskProgress> result = page(PageUtils.buildPage(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<BgtProjectTaskProgress> queryList(BgtProjectTaskProgressQueryBo bo) {
        return list(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<BgtProjectTaskProgress> buildQueryWrapper(BgtProjectTaskProgressQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BgtProjectTaskProgress> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProjectId() != null, BgtProjectTaskProgress::getProjectId, bo.getProjectId());
        lqw.eq(bo.getSectionId() != null, BgtProjectTaskProgress::getSectionId, bo.getSectionId());
        lqw.eq(bo.getSubId() != null, BgtProjectTaskProgress::getSubId, bo.getSubId());
        lqw.eq(bo.getTaskId() != null, BgtProjectTaskProgress::getTaskId, bo.getTaskId());
        lqw.like(StrUtil.isNotBlank(bo.getProgressName()), BgtProjectTaskProgress::getProgressName, bo.getProgressName());
        lqw.eq(StrUtil.isNotBlank(bo.getDirector()), BgtProjectTaskProgress::getDirector, bo.getDirector());
        lqw.eq(StrUtil.isNotBlank(bo.getUploader()), BgtProjectTaskProgress::getUploader, bo.getUploader());
        lqw.eq(bo.getProgress() != null, BgtProjectTaskProgress::getProgress, bo.getProgress());
        lqw.eq(StrUtil.isNotBlank(bo.getProgressDescribe()), BgtProjectTaskProgress::getProgressDescribe, bo.getProgressDescribe());
        lqw.eq(StrUtil.isNotBlank(bo.getProgressAnnex()), BgtProjectTaskProgress::getProgressAnnex, bo.getProgressAnnex());
        lqw.eq(StrUtil.isNotBlank(bo.getAuditStatus()), BgtProjectTaskProgress::getAuditStatus, bo.getAuditStatus());
        lqw.eq(bo.getReviewerId() != null, BgtProjectTaskProgress::getReviewerId, bo.getReviewerId());
        return lqw;
    }

    @Override
    public Boolean insert(BgtProjectTaskProgress bo) {
        BgtProjectTaskProgress add = BeanUtil.toBean(bo, BgtProjectTaskProgress.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean update(BgtProjectTaskProgress bo) {
        BgtProjectTaskProgress update = BeanUtil.toBean(bo, BgtProjectTaskProgress.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(BgtProjectTaskProgress entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

	@Override
	public TableDataInfo<BgtProjectTaskProgressVO> appQueryPageList(BgtProjectTaskProgressQueryDTO dto) {
		Page<BgtProjectTaskProgressQueryDTO> queryDTOPage = new Page<>();
		queryDTOPage.setCurrent(dto.getPageNum());
		queryDTOPage.setSize(dto.getPageSize());
		Page<BgtProjectTaskProgressVO> queryVOPage = baseMapper.appQueryPageList(queryDTOPage, dto);
		return PageUtils.buildDataInfo(queryVOPage);
	}

	@Override
	public BgtProjectTaskProgressDetailVO appQueryById(Long id) {
		BgtProjectTaskProgress progress = getById(id);
		BgtProjectTaskProgressDetailVO bean = BeanUtil.toBean(progress, BgtProjectTaskProgressDetailVO.class);
		ZbfProject project = zbfProjectService.getById(progress.getProjectId());
		bean.setProjectName(project.getProjectName());
		FbsProjectTask task = fbsProjectTaskService.getById(progress.getTaskId());
		bean.setTaskName(task.getTaskName());
		bean.setTaskAddress(task.getTaskAddress());
		bean.setTaskStaffNum(task.getTaskStaffNum());
		return bean;
	}

	@Override
	public Integer getLastProgress(Long taskId) {
		LambdaQueryWrapper<BgtProjectTaskProgress> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(BgtProjectTaskProgress::getTaskId, taskId);
		wrapper.orderByDesc(BgtProjectTaskProgress::getProgress);
		List<BgtProjectTaskProgress> list = baseMapper.selectList(wrapper);
		return CollectionUtil.isEmpty(list)?0:list.get(0).getProgress();
	}
}
