package com.cpxt.biz.service.impl;

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

import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cpxt.biz.domain.BizCar;
import com.cpxt.biz.domain.BizOrderTask;
import com.cpxt.biz.mapper.BizOrderTaskMapper;
import com.cpxt.biz.service.IBizOrderTaskService;
import com.cpxt.common.utils.DateUtils;
import com.cpxt.quartz.domain.SysJob;
import com.cpxt.quartz.mapper.SysJobMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单任务Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-20
 */
@Service
public class BizOrderTaskServiceImpl implements IBizOrderTaskService
{
    @Autowired
    private BizOrderTaskMapper bizOrderTaskMapper;

    @Autowired
    private SysJobMapper sysJobMapper;

    /**
     * 查询订单任务
     * 
     * @param id 订单任务主键
     * @return 订单任务
     */
    @Override
    public BizOrderTask selectBizOrderTaskById(Integer id)
    {
        return bizOrderTaskMapper.selectById(id);
    }

    /**
     * 查询订单任务列表
     * 
     * @param bizOrderTask 订单任务
     * @return 订单任务
     */
    @Override
    public List<BizOrderTask> selectBizOrderTaskList(BizOrderTask bizOrderTask)
    {
        LambdaQueryWrapper<BizOrderTask> queryWrapper = new LambdaQueryWrapper<>();
        return bizOrderTaskMapper.selectList(queryWrapper);
    }

    /**
     * 新增订单任务
     * 
     * @param bizOrderTask 订单任务
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBizOrderTask(BizOrderTask bizOrderTask)
    {
        String uuid = UUID.randomUUID().toString();

        // 根据execTimes构建cron表达式
        String execTimes = bizOrderTask.getExecTimes(); // 16:24:30
        String[] split = execTimes.split(":");
        String cron = split[2] + " " + split[1] + " " + split[0] + " * * ?";
        bizOrderTask.setCronExpression(cron);

        SysJob sysJob = new SysJob();
        sysJob.setJobName(bizOrderTask.getName());
        sysJob.setInvokeTarget("orderTask.generateOrder('" + uuid +"')");
        sysJob.setCronExpression(bizOrderTask.getCronExpression());
        sysJob.setMisfirePolicy(bizOrderTask.getMisfirePolicy());
        sysJob.setConcurrent(bizOrderTask.getConcurrent());
        sysJob.setStatus(bizOrderTask.getStatus());
        sysJob.setCreateBy(bizOrderTask.getCreateBy());
        sysJob.setCreateTime(bizOrderTask.getCreateTime());
        sysJob.setUpdateBy(bizOrderTask.getUpdateBy());
        sysJob.setUpdateTime(bizOrderTask.getUpdateTime());
        sysJob.setRemark(bizOrderTask.getRemark());
        sysJobMapper.insertJob(sysJob);

        bizOrderTask.setCode(uuid);
        bizOrderTask.setJobId(sysJob.getJobId());

        return bizOrderTaskMapper.insert(bizOrderTask);
    }

    /**
     * 修改订单任务
     * 
     * @param bizOrderTask 订单任务
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBizOrderTask(BizOrderTask bizOrderTask)
    {
        // 根据execTimes构建cron表达式
        String execTimes = bizOrderTask.getExecTimes(); // 16:24:30
        String[] split = execTimes.split(":");
        String cron = split[2] + " " + split[1] + " " + split[0] + " * * ?";
        bizOrderTask.setCronExpression(cron);

        // 同步修改订单任务
        SysJob sysJob = sysJobMapper.selectJobById(bizOrderTask.getJobId());
        sysJob.setJobName(bizOrderTask.getName());
        sysJob.setCronExpression(bizOrderTask.getCronExpression());
        sysJob.setMisfirePolicy(bizOrderTask.getMisfirePolicy());
        sysJob.setConcurrent(bizOrderTask.getConcurrent());
        sysJob.setStatus(bizOrderTask.getStatus());
        sysJob.setCreateBy(bizOrderTask.getCreateBy());
        sysJob.setCreateTime(bizOrderTask.getCreateTime());
        sysJob.setUpdateBy(bizOrderTask.getUpdateBy());
        sysJob.setUpdateTime(bizOrderTask.getUpdateTime());
        sysJob.setRemark(bizOrderTask.getRemark());
        sysJobMapper.updateJob(sysJob);
        return bizOrderTaskMapper.updateById(bizOrderTask);
    }

    /**
     * 批量删除订单任务
     * 
     * @param ids 需要删除的订单任务主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBizOrderTaskByIds(Integer[] ids)
    {
        // 同步删除job表中的任务
        for (Integer id : ids) {
            Long jobId = bizOrderTaskMapper.selectById(id).getJobId();
            sysJobMapper.deleteJobById(jobId);
        }
        return bizOrderTaskMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除订单任务信息
     * 
     * @param id 订单任务主键
     * @return 结果
     */
    @Override
    public int deleteBizOrderTaskById(Integer id)
    {
        return bizOrderTaskMapper.deleteById(id);
    }

    @Override
    public void updateBizOrderTaskStatus(Long jobId,String status) {
        bizOrderTaskMapper.updateBizOrderTaskStatus(jobId,status);
    }
}
