package com.ruoyi.model.service.impl;

import java.util.Arrays;
import java.util.List;
import java.time.LocalDateTime;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyl.manager.marketing.service.IWechatStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.model.mapper.AppletTaskMapper;
import com.ruoyi.model.domain.AppletTask;
import com.ruoyi.model.service.IAppletTaskService;

/**
 * 任务中心Service业务层处理
 *
 * @author daixiande
 */
@Service
public class AppletTaskServiceImpl extends ServiceImpl<AppletTaskMapper, AppletTask> implements IAppletTaskService {


    @Autowired
    private AppletTaskMapper appletTaskMapper;

    @Autowired
    private IWechatStockService wechatStockService;

    /**
     * 查询任务中心
     *
     * @param id 任务中心主键
     * @return 任务中心
     */
    @Override
    public AppletTask selectAppletTaskById(Long id) {

        AppletTask task = appletTaskMapper.selectById(id);

        if (StringUtils.isNotBlank(task.getTaskCoupon())){

            List<String> ids = Arrays.asList(task.getTaskCoupon().split(","));

            task.setTaskCouponTitle(wechatStockService.getStockTitleByIds(ids));
        }
        return task;
    }

    /**
     * 查询任务中心列表
     *
     * @param appletTask 任务中心
     * @return 任务中心
     */
    @Override
    public List<AppletTask> selectAppletTaskList(AppletTask appletTask) {

        List<AppletTask> appletTasks = appletTaskMapper.selectList(Wrappers.<AppletTask>lambdaQuery()
                .eq(appletTask.getId() != null, AppletTask::getId, appletTask.getId())
                .like(appletTask.getTaskName() != null, AppletTask::getTaskName, appletTask.getTaskName())
                .eq(appletTask.getType() != null, AppletTask::getType, appletTask.getType())
                .like(appletTask.getTitle() != null, AppletTask::getTitle, appletTask.getTitle())
                .like(appletTask.getTheme() != null, AppletTask::getTheme, appletTask.getTheme())
                .like(appletTask.getTaskCoupon() != null, AppletTask::getTaskCoupon, appletTask.getTaskCoupon())
                .ge(appletTask.getStartTime() != null, AppletTask::getStartTime, appletTask.getStartTime())
                .le(appletTask.getEndTime() != null, AppletTask::getEndTime, appletTask.getEndTime())
                .eq(appletTask.getMaxGetNum() != null, AppletTask::getMaxGetNum, appletTask.getMaxGetNum())
                .eq(appletTask.getGiveNum() != null, AppletTask::getGiveNum, appletTask.getGiveNum())
                .orderByDesc(AppletTask::getCreateTime));

        for (AppletTask task : appletTasks) {
            if (StringUtils.isNotBlank(task.getTaskCoupon())){

                List<String> ids = Arrays.asList(task.getTaskCoupon().split(","));

                task.setTaskCouponTitle(wechatStockService.getStockTitleByIds(ids));
            }
        }

        return appletTasks;
    }

    /**
     * 新增任务中心
     *
     * @param appletTask 任务中心
     * @return 结果
     */
    @Override
    public int insertAppletTask(AppletTask appletTask) {
        appletTask.setCreateTime(LocalDateTime.now());
        return appletTaskMapper.insert(appletTask);
    }

    /**
     * 修改任务中心
     *
     * @param appletTask 任务中心
     * @return 结果
     */
    @Override
    public int updateAppletTask(AppletTask appletTask) {
        appletTask.setUpdateTime(LocalDateTime.now());
        return appletTaskMapper.updateById(appletTask);
    }

    /**
     * 批量删除任务中心
     *
     * @param ids 需要删除的任务中心主键
     * @return 结果
     */
    @Override
    public int deleteAppletTaskByIds(Long[] ids) {
        return appletTaskMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除任务中心信息
     *
     * @param id 任务中心主键
     * @return 结果
     */
    @Override
    public int deleteAppletTaskById(Long id) {
        return appletTaskMapper.deleteById(id);
    }
}
