package com.ruoyi.model.service.impl;

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.h5.config.SecurityUtil;
import com.cyl.h5.service.H5CouponService;
import com.cyl.manager.marketing.pojo.SendCouponRequest;
import com.cyl.manager.marketing.service.IWechatStockService;
import com.cyl.manager.ums.domain.Member;
import com.cyl.manager.ums.service.MemberService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.model.domain.AppletTask;
import com.ruoyi.model.domain.AppletTaskUser;
import com.ruoyi.model.mapper.AppletTaskMapper;
import com.ruoyi.model.mapper.AppletTaskUserMapper;
import com.ruoyi.model.service.IAppletTaskService;
import com.ruoyi.model.service.IAppletTaskUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 任务中心Service业务层处理
 *
 * @author daixiande
 */
@Service
public class AppletTaskUserServiceImpl extends ServiceImpl<AppletTaskUserMapper, AppletTaskUser> implements IAppletTaskUserService {


    @Autowired
    private IWechatStockService wechatStockService;

    @Autowired
    private H5CouponService h5CouponService;

    @Autowired
    private MemberService memberService;

    /**
     * 查询任务中心
     *
     * @param id 任务中心主键
     * @return 任务中心
     */
    @Override
    public AppletTaskUser selectAppletTaskById(Long id) {
        AppletTaskUser task = getById(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<AppletTaskUser> selectAppletTaskList(AppletTaskUser appletTask) {

        List<AppletTaskUser> appletTasks = baseMapper.selectList(Wrappers.<AppletTaskUser>lambdaQuery()
                .eq(appletTask.getId() != null, AppletTaskUser::getId, appletTask.getId())
                .eq(appletTask.getTaskId() != null, AppletTaskUser::getTaskId, appletTask.getTaskId())
                .eq(appletTask.getMemberId() != null, AppletTaskUser::getMemberId, appletTask.getMemberId())
                .like(appletTask.getTaskName() != null, AppletTaskUser::getTaskName, appletTask.getTaskName())
                .eq(appletTask.getType() != null, AppletTaskUser::getType, appletTask.getType())
                .like(appletTask.getTitle() != null, AppletTaskUser::getTitle, appletTask.getTitle())
                .like(appletTask.getTheme() != null, AppletTaskUser::getTheme, appletTask.getTheme())
                .ge(appletTask.getStartTime() != null, AppletTaskUser::getStartTime, appletTask.getStartTime())
                .le(appletTask.getEndTime() != null, AppletTaskUser::getEndTime, appletTask.getEndTime())
                .eq(appletTask.getStatus() != null, AppletTaskUser::getStatus, appletTask.getStatus())
                .orderByDesc(AppletTaskUser::getCreateTime));
        for (AppletTaskUser 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(AppletTaskUser appletTask) {
        appletTask.setCreateTime(LocalDateTime.now());
        return baseMapper.insert(appletTask);
    }

    /**
     * 修改任务中心
     *
     * @param appletTask 任务中心
     * @return 结果
     */
    @Override
    public int updateAppletTask(AppletTaskUser appletTask) {
        appletTask.setUpdateTime(DateUtils.getNowDate());

        if (appletTask.getStatus() == 2 &&
                "0".equals(appletTask.getType()) &&
                StringUtils.isNotBlank(appletTask.getTaskCoupon())){

            Member member = memberService.selectById(appletTask.getMemberId());

            if (member == null){
                throw new ServiceException("会员不存在");
            }

            String phone = memberService.getPhoneDecrypted(member.getPhoneEncrypted());

            Arrays.stream(appletTask.getTaskCoupon().split(",")).map(Long::valueOf).forEach(c -> {
                SendCouponRequest sendCouponRequest = new SendCouponRequest();
                sendCouponRequest.setPhone(phone);
                sendCouponRequest.setStockId(c);
                h5CouponService.sendCoupon(sendCouponRequest);
            });

        }

        return baseMapper.updateById(appletTask);
    }

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

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