package com.ruoyi.applet.contoller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.cyl.h5.config.SecurityUtil;
import com.cyl.manager.marketing.service.IWechatStockService;
import com.cyl.manager.ums.domain.Member;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.model.domain.AppletTask;
import com.ruoyi.model.domain.AppletTaskUser;
import com.ruoyi.model.service.IAppletTaskService;
import com.ruoyi.model.service.IAppletTaskUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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


@Slf4j
@Api(description = "下单小程序端 - 任务中心相关接口")
@RestController
@RequestMapping("/h5/mall/task")
public class ApiMallTaskController extends BaseController {

    @Autowired
    private IAppletTaskService appletTaskService;


    @Autowired
    private IAppletTaskUserService appletTaskUserService;

    @Autowired
    private IWechatStockService wechatStockService;


    // 任务中心待领取列表
    @ApiOperation("mall端-任务中心待领取列表")
    @GetMapping("/list")
    public TableDataInfo list(AppletTask appletTask) {
        Member member = SecurityUtil.getLocalMember();

        List<Long> notIds = appletTaskUserService.lambdaQuery()
                .eq(AppletTaskUser::getMemberId, member.getId())
                .select(AppletTaskUser::getTaskId)
                .list().stream().map(n -> n.getTaskId())
                .collect(Collectors.toList());

        startPage();


        List<AppletTask> list = appletTaskService.lambdaQuery()
                .notIn(notIds.size() > 0, AppletTask::getId, notIds)
                .list();

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

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

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

//        for (AppletTask task : list) {
//            task.setStatus(appletTaskUserService.lambdaQuery()
//                    .eq(AppletTaskUser::getMemberId, member.getId())
//                    .eq(AppletTaskUser::getTaskId, task.getId())
//                    .count() > 0);
//        }

        return getDataTable(list);
    }

    // 任务中心已领取列表
    @ApiOperation("mall端-任务中心已领取列表")
    @GetMapping("/listByUser")
    public TableDataInfo listByUser(AppletTaskUser appletTask) {
        startPage();

        Member member = SecurityUtil.getLocalMember();

        appletTask.setMemberId(member.getId());

        List<AppletTaskUser> list = appletTaskUserService.selectAppletTaskList(appletTask);

        for (AppletTaskUser task : list) {
            if (StringUtils.isNotBlank(task.getTaskCoupon())){

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

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

        return getDataTable(list);
    }


    //根据标识查询待领取任务详情
    @ApiOperation("mall端-根据标识查询待领取任务详情")
    @GetMapping("/taskDetail/{id}")
    public AjaxResult taskDetail(AppletTask appletTask){

        Member member = SecurityUtil.getLocalMember();

        AppletTask task = appletTaskService.selectAppletTaskById(appletTask.getId());

//        task.setStatus(appletTaskUserService.lambdaQuery()
//                .eq(AppletTaskUser::getMemberId, member.getId())
//                .eq(AppletTaskUser::getTaskId, task.getId())
//                .count() > 0);

        return AjaxResult.success("任务详情",task);
    }

    //根据标识查询已领取任务详情
    @ApiOperation("mall端-根据标识查询已领取任务详情")
    @GetMapping("/taskUserDetail/{id}")
    public AjaxResult taskUserDetail(AppletTaskUser appletTask){



        AppletTaskUser task = appletTaskUserService.selectAppletTaskById(appletTask.getId());

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

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

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

        return AjaxResult.success("任务详情",task);
    }


    //接受任务
    @ApiOperation("mall端-接受任务")
    @GetMapping("/acceptTask")
    public AjaxResult acceptTask(AppletTask appletTask){

        Member member = SecurityUtil.getLocalMember();

        AppletTask task = appletTaskService.selectAppletTaskById(appletTask.getId());

        AppletTaskUser taskUser = new AppletTaskUser();
        BeanUtils.copyProperties(task, taskUser);

        taskUser.setMemberId(member.getId());
        taskUser.setId(null);
        taskUser.setTaskId(task.getId());

        appletTaskUserService.save(taskUser);

        task.setGiveNum(task.getGiveNum() + 1);
        appletTaskService.updateById(task);

        return AjaxResult.success("接受成功");
    }


    //提交任务信息
    @ApiOperation("mall端-提交任务信息")
    @PostMapping("/submitTask")
    public AjaxResult submitTask(@RequestBody AppletTaskUser appletTask){
        appletTaskUserService.lambdaUpdate()
                .eq(AppletTaskUser::getId, appletTask.getId())
                .set(AppletTaskUser::getProveTxt, appletTask.getProveTxt())
                .set(AppletTaskUser::getProveImage, appletTask.getProveImage())
                .set(AppletTaskUser::getStatus, 1)
                .set(AppletTaskUser::getSubmitTime, LocalDateTime.now())
                .update();
        return AjaxResult.success("提交成功");
    }


}
