package com.ruoyi.generator.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.generator.domain.Community.CommunityReport;
import com.ruoyi.generator.service.ICommunityReportService;
import com.ruoyi.generator.vo.SysDictDataVo;
import com.ruoyi.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 社区举报控制器
 *
 * @author fangzhen
 */
@Api(tags = "社区举报控制器")
@RestController
@RequestMapping("/community/report")
public class CommunityReportController extends BaseController {

    @Autowired
    private ICommunityReportService communityReportService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        List<SysDictDataVo> dictDataVos = null;
        SysDictDataVo dictDataVo = null;
        if (StringUtils.isNull(data)) {
            dictDataVos = new ArrayList<>();
        } else {
            dictDataVos = new ArrayList<>();
            for (SysDictData dictData : data) {
                dictDataVo = new SysDictDataVo();
                BeanUtils.copyProperties(dictData, dictDataVo);
                dictDataVos.add(dictDataVo);
            }
        }
        return success(dictDataVos);
    }

    /**
     * 发布举报
     */
    @ApiOperation("发布举报")
    @PostMapping()
    @Transactional
    //@Anonymous
    public AjaxResult report(@RequestBody CommunityReport communityReport) {
        if (Objects.isNull(communityReport.getReportCode()) || Objects.isNull(communityReport.getArticleId())) {
            return AjaxResult.error("参数异常!");
        }
        communityReportService.save(communityReport);
        return AjaxResult.success("您的举报已记录!");
    }
}