package com.tuoheng.admin.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tuoheng.admin.entity.domain.Question;
import com.tuoheng.admin.entity.request.*;
import com.tuoheng.admin.entity.vo.QuestionTypeCountVO;
import com.tuoheng.admin.entity.vo.QuestionVO;
import com.tuoheng.admin.service.IQuestionService;
import com.tuoheng.common.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * 巡检问题
 *
 * @author qiujinyang
 * @date 2022/8/3
 */
@RestController
@RequestMapping("/question")
@Slf4j
public class QuestionController {

    @Autowired
    private IQuestionService questionService;

    /**
     * 获取巡检问题列表(分页)
     *
     * @param questionQuery 查询条件
     * @return
     */
    @GetMapping("/page")
    public JsonResult<IPage<QuestionVO>> page(QuestionQuery questionQuery) throws ParseException {
        return JsonResult.success(questionService.queryPage(questionQuery));
    }


    /**
     * 被DSP调用，存问题
     *
     * @return
     */
    @PostMapping("/{requestId}/callback")
    public JsonResult callback(@Valid @Pattern(regexp = "^[a-zA-Z0-9]{0,36}$")
                               @NotBlank @PathVariable("requestId") String requestId,
                               @Valid @RequestBody CallbackRequest callbackRequest) {
        log.info("进入回调地址，回调参数callbackRequest为：{},requestId为：{}",callbackRequest,requestId);
        return questionService.process(requestId, callbackRequest);
    }

    /**
     * 删除巡检问题
     *
     * @param ids 巡检问题ID
     * @return
     */
    @DeleteMapping("/{ids}")
    public JsonResult delete(@PathVariable("ids") Integer[] ids) {
        return questionService.deleteByIds(ids);
    }



    /**
     * 忽略、确认、批量忽略、批量确认、修改问题类型
     *
     */
    @PutMapping("/status")
    public JsonResult check(@RequestBody QuestionStatusRequest entity) {
        return questionService.check(entity);
    }


    /**
     * 根据问题是否核实分类
     *
     */
    @GetMapping("/analyze/{missionId}")
    public JsonResult<Map<String, Integer>> analyze(@PathVariable("missionId") Integer missionId) {
        return questionService.analyzeController(missionId);
    }

    /**
     * 根据问题类型分类
     *
     */
    @GetMapping("/analyze/type/{missionId}")
    public JsonResult<List<QuestionTypeCountVO>> analyzeType(@PathVariable("missionId") Integer missionId) {
        return questionService.analyzeType(missionId);
    }

    /**
     * 问题分布
     *
     * @param questionQuery 查询条件
     * @return
     */
    @GetMapping("/distributed")
    public JsonResult distributed(QuestionQuery questionQuery) {
        return JsonResult.success(questionService.distributed(questionQuery));
    }

}
