package com.sqx.modules.taskCenter.controller;


import cn.hutool.core.date.DateUtil;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.taskCenter.entity.TaskCenterRecord;
import com.sqx.modules.taskCenter.entity.TaskCenterReward;
import com.sqx.modules.taskCenter.service.TaskCenterRecordService;
import com.sqx.modules.taskCenter.service.TaskCenterRewardService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@Api(value = "任务中心", tags = {"任务中心"})
@RequestMapping(value = "/app/taskCenterReward")
public class TaskCenterRewardController {
    /**
     * 服务对象
     */
    @Autowired
    private TaskCenterRewardService taskCenterRewardService;
    @Autowired
    private TaskCenterRecordService recordService;

    @PostMapping("/insertTaskCenterReward")
    @ApiOperation("添加任务奖励")
    public Result insertTaskCenterReward(@RequestBody TaskCenterReward taskCenterReward) {
        taskCenterReward.setCreateTime(DateUtils.format(new Date()));
        taskCenterReward.setSurplusNumber(taskCenterReward.getTotalNumber());
        taskCenterRewardService.save(taskCenterReward);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("通过Id查询详情")
    public Result selectOne(@PathVariable Integer id) {
        return Result.success().put("data", taskCenterRewardService.getById(id));
    }

    @PostMapping("/updateTaskCenterReward")
    @ApiOperation("修改任务奖励")
    public Result updateTaskCenterReward(@RequestBody TaskCenterReward taskCenterReward) {
        TaskCenterReward reward = taskCenterRewardService.getById(taskCenterReward.getId());
        if (taskCenterReward.getTotalNumber() != null && reward.getTotalNumber() != null && !taskCenterReward.getTotalNumber().equals(reward.getTotalNumber())) {
            Integer surplusNumber = taskCenterReward.getTotalNumber() - reward.getTotalNumber();
            if (surplusNumber > 0) {
                taskCenterReward.setSurplusNumber(taskCenterReward.getSurplusNumber() == null ? surplusNumber : taskCenterReward.getSurplusNumber() + surplusNumber);
            } else {
                taskCenterReward.setSurplusNumber(taskCenterReward.getTotalNumber());
            }
        }
        taskCenterReward.setUpdateTime(DateUtils.format(new Date()));
        taskCenterRewardService.updateById(taskCenterReward);
        return Result.success();
    }

    @PostMapping("/deleteTaskCenterReward")
    @ApiOperation("删除任务奖励")
    public Result deleteTaskCenterReward(Long id) {
        taskCenterRewardService.removeById(id);
        return Result.success();
    }

    @GetMapping("/selectTaskCenterReward")
    @ApiOperation("查询任务奖励")
    public Result selectTaskCenterReward(Integer page, Integer limit, TaskCenterReward taskCenterReward) {
        return Result.success().put("data", taskCenterRewardService.page(new Page<>(page, limit), new QueryWrapper<>(taskCenterReward).orderByDesc("id")));
    }

    @Login
    @GetMapping("/selectTaskCenterUserReward")
    @ApiOperation("查询用户任务奖励列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", dataTypeClass = Long.class),
    })
    public Result selectTaskCenterUserReward(@ApiIgnore @RequestAttribute("userId") Long userId, Long taskId) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<TaskCenterReward> rewards = new QueryWrapper<TaskCenterReward>().eq("task_id", taskId);
        List<TaskCenterReward> list = taskCenterRewardService.list(rewards);
        TaskCenterRecord record = recordService.getOne(new QueryWrapper<TaskCenterRecord>()
                .eq("user_id", userId)
                .eq("task_id", taskId)
                .gt("create_time", DateUtil.beginOfMonth(new Date()) + " 00:00:00"));
        if (record != null) {
            if (record.getTargetId() != null) {
                for (TaskCenterReward reward : list) {
                    if (reward.getId().equals(record.getSourceId())) {
                        reward.setTargetId(record.getTargetId());
                    }
                }
            }
            map.put("receive", record.getSourceId());
            map.put("record", record.getId());
        }
        map.put("data", list);
        return Result.success(map);
    }
}

