package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.common.core.utils.DateUtils;
import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.domain.BMainExceptionTaskCheck;
import cn.cecep.talroad.domain.BMainExceptionTaskCheckStep;
import cn.cecep.talroad.domain.PcOutlet;
import cn.cecep.talroad.mapper.BMainExceptionTaskCheckMapper;
import cn.cecep.talroad.mapper.BMainExceptionTaskCheckStepMapper;
import cn.cecep.talroad.mapper.PcOutletMapper;
import cn.cecep.talroad.service.IBMainExceptionTaskCheckService;
import cn.cecep.talroad.vo.BMainExceptionTaskCheckVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * 移动执法信息-异常任务核查Service业务层处理
 */
@Service
public class BMainExceptionTaskCheckServiceImpl extends ServiceImpl<BMainExceptionTaskCheckMapper,BMainExceptionTaskCheck> implements IBMainExceptionTaskCheckService
{
    @Autowired
    private PcOutletMapper outletMapper;

    @Autowired
    private BMainExceptionTaskCheckStepMapper exceptionTaskCheckStepMapper;

    @Override
    public BMainExceptionTaskCheckVo.DetailVo getInfo(String id) {
        BMainExceptionTaskCheck taskCheck = getById(id);
        BMainExceptionTaskCheckVo.DetailVo detailVo = new BMainExceptionTaskCheckVo.DetailVo();
        BeanUtils.copyProperties(taskCheck, detailVo);

        List<PcOutlet> outlets = new ArrayList<>();
        if (StringUtils.isNotEmpty(detailVo.getOutletIds())) {
            outlets = outletMapper.selectBatchIds(Arrays.asList(detailVo.getOutletIds().split(",")));
        }
        detailVo.setOutlets(outlets);

        if (StringUtils.isNotEmpty(detailVo.getExceptionTime())) {
            Date date = DateUtils.parseDate(detailVo.getExceptionTime());

            detailVo.setExceptionTimeStart(DateUtils.parseDateToStr("yyyy-MM-dd HH", DateUtils.addHours(date, -12)));
            detailVo.setExceptionTimeEnd(DateUtils.parseDateToStr("yyyy-MM-dd HH", DateUtils.addHours(date, 11)));
        }

        // 固定的三个步骤
        List<BMainExceptionTaskCheckStep> steps = new ArrayList<>();
        steps.add(new BMainExceptionTaskCheckStep("生成"));
        steps.add(new BMainExceptionTaskCheckStep("签收"));
        steps.add(new BMainExceptionTaskCheckStep("办理"));

        // 查询已经过的步骤
        List<BMainExceptionTaskCheckStep> checkSteps = exceptionTaskCheckStepMapper.selectList(
                new QueryWrapper<BMainExceptionTaskCheckStep>()
                        .eq("exception_task_check_id", detailVo.getId())
        );

        // 步骤号从0开始（表示一步都没完成）
        int stepNum = 0;

        for (int i = 0; i < steps.size(); i++) {
            BMainExceptionTaskCheckStep step = steps.get(i);
            checkSteps.stream().filter(o -> step.getStepName().equals(o.getStepName())).findFirst()
                    .ifPresent(temp -> BeanUtils.copyProperties(temp, step));

            // 若操作时间不为空，表示该步骤已完成
            if (StringUtils.isNotEmpty(step.getOperateTime())) {
                stepNum = i + 1; // 步骤号=下标加1
            }
        }
        detailVo.setSteps(steps);
        detailVo.setStepNum(stepNum);
        return detailVo;
    }
}
