package com.taais.biz.service.impl;

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

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.taais.biz.constant.BizConstant;
import com.taais.biz.domain.bo.AlgorithmBizProcessBo;
import com.taais.biz.domain.dto.TaskResultDTO;
import com.taais.biz.domain.vo.AlgorithmBizProcessVo;
import com.taais.common.core.utils.DateUtils;
import com.taais.common.core.utils.MapstructUtils;
import com.taais.common.core.utils.StringUtils;
import com.taais.common.orm.core.page.PageQuery;
import com.taais.common.core.core.page.PageResult;
import com.taais.common.orm.core.service.impl.BaseServiceImpl;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.taais.biz.mapper.AlgorithmDataProcessMapper;
import com.taais.biz.domain.AlgorithmDataProcess;
import com.taais.biz.domain.bo.AlgorithmDataProcessBo;
import com.taais.biz.domain.vo.AlgorithmDataProcessVo;
import com.taais.biz.service.IAlgorithmDataProcessService;
import static com.taais.biz.domain.table.AlgorithmDataProcessTableDef.ALGORITHM_DATA_PROCESS;

/**
 * 算法数据处理Service业务层处理
 *
 * @author 0
 * 2024-06-14
 */
@Service
public class AlgorithmDataProcessServiceImpl extends BaseServiceImpl<AlgorithmDataProcessMapper, AlgorithmDataProcess> implements IAlgorithmDataProcessService {
    private static final Logger log = LoggerFactory.getLogger(AlgorithmDataProcessServiceImpl.class);
    @Resource
    private AlgorithmDataProcessMapper algorithmDataProcessMapper;

    @Override
    public QueryWrapper query() {
        return super.query().from(ALGORITHM_DATA_PROCESS);
    }

    private QueryWrapper buildQueryWrapper(AlgorithmDataProcessBo algorithmDataProcessBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(ALGORITHM_DATA_PROCESS.SUB_TASK_ID.eq
        (algorithmDataProcessBo.getSubTaskId()));
        queryWrapper.and(ALGORITHM_DATA_PROCESS.NAME.like
        (algorithmDataProcessBo.getName()));
        queryWrapper.and(ALGORITHM_DATA_PROCESS.TYPE.eq
        (algorithmDataProcessBo.getType()));
        queryWrapper.and(ALGORITHM_DATA_PROCESS.STATUS.eq
        (algorithmDataProcessBo.getStatus()));

        return queryWrapper;
    }

    /**
     * 查询算法数据处理
     *
     * @param id 算法数据处理主键
     * @return 算法数据处理
     */
    @Override
    public AlgorithmDataProcessVo selectById(Long id) {
            return this.getOneAs(query().where(ALGORITHM_DATA_PROCESS.ID.eq(id)), AlgorithmDataProcessVo.class);

    }

    /**
     * 查询算法数据处理列表
     *
     * @param algorithmDataProcessBo 算法数据处理Bo
     * @return 算法数据处理集合
     */
    @Override
    public List<AlgorithmDataProcessVo> selectList(AlgorithmDataProcessBo algorithmDataProcessBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(algorithmDataProcessBo);
            return this.listAs(queryWrapper, AlgorithmDataProcessVo.class);
    }

    /**
     * 分页查询算法数据处理列表
     *
     * @param algorithmDataProcessBo 算法数据处理Bo
     * @return 分页算法数据处理集合
     */
    @Override
    public PageResult<AlgorithmDataProcessVo> selectPage(AlgorithmDataProcessBo algorithmDataProcessBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(algorithmDataProcessBo);
            Page<AlgorithmDataProcessVo> page = this.pageAs(PageQuery.build(), queryWrapper, AlgorithmDataProcessVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增算法数据处理
     *
     * @param algorithmDataProcessBo 算法数据处理Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(AlgorithmDataProcessBo algorithmDataProcessBo) {
    AlgorithmDataProcess algorithmDataProcess =MapstructUtils.convert(algorithmDataProcessBo, AlgorithmDataProcess. class);
        boolean save = this.save(algorithmDataProcess);
        algorithmDataProcessBo.setId(algorithmDataProcess.getId());
        algorithmDataProcessBo.setVersion(algorithmDataProcess.getVersion());
        return save;//使用全局配置的雪花算法主键生成器生成ID值
    }

    /**
     * 修改算法数据处理
     *
     * @param algorithmDataProcessBo 算法数据处理Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(AlgorithmDataProcessBo algorithmDataProcessBo) {
        AlgorithmDataProcess algorithmDataProcess =MapstructUtils.convert(algorithmDataProcessBo, AlgorithmDataProcess. class);
        if (ObjectUtil.isNotNull(algorithmDataProcess) && ObjectUtil.isNotNull(algorithmDataProcess.getId())){
            boolean updated = this.updateById(algorithmDataProcess);
                return updated;
        }
        return false;
    }

    /**
     * 批量删除算法数据处理
     *
     * @param ids 需要删除的算法数据处理主键集合
     * @return 结果:true 删除成功，false 删除失败
     */
    @Transactional
    @Override
    public boolean deleteByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

    @Override
    public List<AlgorithmDataProcessVo> getProcessBySubtaskId(Long subtaskId) {
        return mapper.getProcessBySubtaskId(subtaskId);
    }

    @Override
    public String taskResult(TaskResultDTO resultDTO) {
        Long bizId = resultDTO.getBizId();
        AlgorithmDataProcessVo dataProcessVo = this.selectById(bizId);
        if(dataProcessVo == null){
            log.error("任务数据没有找到,resultDTO:{}",resultDTO);
            return "任务数据没有找到,resultDTO:"+ resultDTO;
        }
        AlgorithmDataProcessBo algorithmDataProcessBo = MapstructUtils.convert(dataProcessVo, AlgorithmDataProcessBo.class);
        if (resultDTO.getStatus() == 1) {
            algorithmDataProcessBo.setStatus(BizConstant.TASK_STATUS_SUCCEED);

        } else if (resultDTO.getStatus() == 0) {
            algorithmDataProcessBo.setStatus(BizConstant.TASK_STATUS_FAILED);
        } else {
            log.error("不可能的分支");
        }
        algorithmDataProcessBo.setLog(resultDTO.getMsg());
        Date now = new Date();
        Date startTime = algorithmDataProcessBo.getStartTime();
        algorithmDataProcessBo.setEndTime(now);
        if (startTime != null) {
            algorithmDataProcessBo.setCostSecond(DateUtils.differentSecondByDate(now, startTime));
        }
        this.update(algorithmDataProcessBo);
        return null;
    }

}
