package com.cyksj.web.controller.manage.market;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.market.task.TaskFollowPlatVerifyMapper;
import com.cyksj.mapper.market.task.TaskMapper;
import com.cyksj.mapper.market.task.TaskTypeMapper;
import com.cyksj.mapper.market.task.TaskUserCompleteRecordMapper;
import com.cyksj.model.entity.Task;
import com.cyksj.model.entity.TaskFollowPlatVerify;
import com.cyksj.model.entity.TaskType;
import com.cyksj.model.views.TaskFollowVerifyView;
import com.cyksj.model.views.TaskView;
import com.cyksj.service.market.task.TaskService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/*
 *项目名: netflix
 *文件名: GoodsDonSkuFrontView
 *创建者: JavaZou
 *创建时间:2022/12/27 13:34
 */
@RestController
@RequestMapping("/manage/market/task")
@RequiredArgsConstructor
public class TaskController {
	private final TaskTypeMapper taskTypeMapper;

	private final TaskMapper taskMapper;

	private final TaskService taskService;

	private final BeanSearcher beanSearcher;

	private final TaskUserCompleteRecordMapper taskUserCompleteRecordMapper;

	private final TaskFollowPlatVerifyMapper taskFollowPlatVerifyMapper;

	/**
	 * 任务分类列表
	 */
	@GetMapping("/get/type")
	public Result<List<TaskType>> getType() {
		return GatewayResponse.SUCCESS.newBuilder().toResult(taskTypeMapper.selectList(null));
	}

	/**
	 * 新增任务
	 */
	@PostMapping("/post")
	public Result<String> saveTask(@RequestBody @Validated Task task) {
		taskService.saveTask(task);
		return GatewayResponse.SUCCESS.newBuilder().toResult("新增任务成功");
	}

	/**
	 * 删除任务
	 */
	@DeleteMapping("/delete/{id}")
	public Result<String> deleteTask(@PathVariable Long id) {
		Task task = taskMapper.selectById(id);
		if (task == null || !task.getDeleted()) {
			throw BusinessRuntimeException.getInstance("该任务已不存在");
		}
		task.setDeleted(!task.getDeleted());
		taskMapper.updateById(task);
		return GatewayResponse.SUCCESS.newBuilder().toResult("删除任务成功");
	}

	/**
	 * 修改任务
	 */
	@PutMapping("/put")
	public Result<String> updateTask(@RequestBody Task task) {
		if (task.getId() == null || task.getId() < 1) {
			throw BusinessRuntimeException.getInstance("任务id错误");
		}
		taskService.updateTask(task);
		return GatewayResponse.SUCCESS.newBuilder().toResult("编辑任务成功");
	}

	/**
	 * 任务上下架
	 */
	@PutMapping("/put/{id}")
	public Result<String> updateStatusById(@PathVariable Long id) {
		Task task = taskMapper.selectById(id);
		if (task == null || !task.getDeleted()) {
			throw BusinessRuntimeException.getInstance("该任务已不存在");
		}
		task.setStatus(!task.getStatus());
		taskMapper.updateById(task);
		return GatewayResponse.SUCCESS.newBuilder().toResult("任务上下架成功");
	}

	/**
	 * 任务列表
	 */
	@GetMapping("/get")
	@SaCheckPermission("sign:view")
	public Result<SearchResult<TaskView>> getTask(HttpServletRequest request) {
		DateTime now = DateTime.now();
		DateTime beginOfDay = DateUtil.beginOfDay(now);
		DateTime endOfDay = DateUtil.endOfDay(now);
		SearchResult<TaskView> search = beanSearcher.search(TaskView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
		search.getDataList().forEach(data -> {
			Integer todayCompleteNum = taskUserCompleteRecordMapper.getTodayCompleteNum(data.getId(),beginOfDay,endOfDay);
			data.setTodayCompleteNum(todayCompleteNum);
			Integer sumCompleteNum = taskUserCompleteRecordMapper.getSumCompleteNum(data.getId());
			data.setSumCompleteNum(sumCompleteNum);
			Integer todayPoints = taskUserCompleteRecordMapper.sumTodayPoints(data.getId(), beginOfDay, endOfDay);
			data.setTodayPoints(todayPoints);
			Integer sumPoints = taskUserCompleteRecordMapper.sumPoints(data.getId());
			data.setSumPoints(sumPoints);
			BigDecimal todayBalance = taskUserCompleteRecordMapper.sumTodayBalance(data.getId(), beginOfDay, endOfDay);
			data.setTodayBalance(todayBalance);
			BigDecimal sumBalance = taskUserCompleteRecordMapper.sumBalance(data.getId());
			data.setSumBalance(sumBalance);
			//关注任务待审核数量
			data.setVerifyNum(taskFollowPlatVerifyMapper.selectCount(Wrappers.lambdaQuery(TaskFollowPlatVerify.class)
					.eq(TaskFollowPlatVerify::getTaskId, data.getId())
					.eq(TaskFollowPlatVerify::getStatus, TaskFollowPlatVerify.Status.verifying)));
		});
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 关注任务 待审核详情
	 */
	@GetMapping("/get/follow/detail")
	public Result<SearchResult<TaskFollowVerifyView>> getFollowDetail(HttpServletRequest request) {
		SearchResult<TaskFollowVerifyView> search = beanSearcher.search(TaskFollowVerifyView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 批量审核 关注任务
	 */
	@PutMapping("/batch/verify/follow")
	public Result<String> batchVerifyFollow(@RequestBody List<TaskFollowPlatVerify> taskFollowPlatVerifyList) {
		taskService.batchVerifyFollow(taskFollowPlatVerifyList);
		return GatewayResponse.SUCCESS.newBuilder().toResult("审核成功");
	}
}

