package com.crm.custom.controller;

import com.crm.custom.service.GoldenRewardQuestionService;
import com.crm.rely.backend.core.constant.FeignClientAnnotation;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.model.dto.golden.question.GoldenRewardQuestionListDto;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
import com.crm.rely.backend.model.entity.golden.question.*;
import com.crm.rely.backend.util.DateUtil;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Log4j
@RestController
@RequestMapping("/reward/question")
public class GoldenRewardQuestionController {

    @Autowired
    private GoldenRewardQuestionService goldenRewardQuestionService;

    @PostMapping("/get")
    public BaseResultDto getQuestion(CustomInfoEntity infoEntity) throws Exception {
        return BaseResultDto.success(goldenRewardQuestionService.getQuestion(infoEntity.getId()));
    }

    @PostMapping("/custom/add")
    public BaseResultDto customAddQuestion(@RequestBody @Validated GoldenQuestionCustomAddEntity entity, CustomInfoEntity infoEntity) throws Exception {
        entity.setCustomId(infoEntity.getId());
        goldenRewardQuestionService.customAddQuestion(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/manager/add")
    @FeignClientAnnotation
    public BaseResultDto add(@RequestBody @Validated GoldenRewardQuestionAddEntity entity) throws Exception {
        goldenRewardQuestionService.add(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/manager/delete")
    @FeignClientAnnotation
    public BaseResultDto delete(@RequestBody @Validated GoldenRewardQuestionDeleteEntitys entitys) throws Exception {

        goldenRewardQuestionService.delete(entitys);
        return BaseResultDto.success();
    }

    @PostMapping("/manager/update")
    @FeignClientAnnotation
    public BaseResultDto update(@RequestBody @Validated GoldenRewardQuestionUpdateEntity entity) throws Exception {
        goldenRewardQuestionService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/manager/search/list")
    @FeignClientAnnotation
    public ResultWithPagerDto<GoldenRewardQuestionListDto> pageList(@RequestBody GoldenRewardQuestionSearchEntity entity) throws Exception {
        return goldenRewardQuestionService.searchPageList(entity);
    }

    @PostMapping("/search/single")
    @FeignClientAnnotation
    public BaseResultDto searchSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        return BaseResultDto.success(goldenRewardQuestionService.searchSingle(entity));
    }

    @Transactional(rollbackFor = Exception.class)
    @Scheduled(cron = "0 20 0 * * ?")
    public void rewardQuestionTask() throws Exception {
        try {
            log.info("有奖问答统计任务开始 ---------------");
            goldenRewardQuestionService.rewardQuestionTask();
            log.info("有奖问答统计任务结束 ---------------");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
    }

    @PostMapping("/rewardQuestionTask")
    public BaseResultDto rewardQuestionTask(@RequestBody SingleLongEntity entity) throws Exception {
        goldenRewardQuestionService.rewardQuestionTask();
        return BaseResultDto.success();
    }

}
