package com.zbkj.front.controller;

import com.zbkj.common.model.system.SystemHelpProblem;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.response.CommonResult;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.model.category.Category;
import com.zbkj.common.response.HelpResponse;
import com.zbkj.service.service.HelpService;
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.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController("HelpController")
@RequestMapping("api/front/help")
@Api(tags = "帮助与问题")
public class HelpController {

    @Autowired
    private HelpService helpService;

    @ApiOperation(value = "分类列表")
    @RequestMapping(value = "/category/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<Category>> categoryList() {
        return CommonResult.success(CommonPage.restPage(helpService.getCategoryList()));
    }

    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/list/{cid}", method = RequestMethod.GET)
    public CommonResult<CommonPage<HelpResponse>> getListArticle(@PathVariable(name="cid") String cid, @Validated PageParamRequest pageParamRequest) {
        List<SystemHelpProblem> list = helpService.getListHelp(cid, pageParamRequest);
        List<HelpResponse> responseList = list.stream().map(e -> {
            HelpResponse response = new HelpResponse();
            BeanUtils.copyProperties(e, response);
            return response;
        }).collect(Collectors.toList());
        return CommonResult.success(CommonPage.restPage(responseList));
    }

    @ApiOperation(value = "详情")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ApiImplicitParam(name="id", value="id")
    public CommonResult<HelpResponse> info(@RequestParam(value = "id") Integer id) {
        SystemHelpProblem detail = helpService.getDetail(id);
        HelpResponse response = new HelpResponse();
        BeanUtils.copyProperties(detail, response);
        return CommonResult.success(response);
   }

    @ApiOperation(value = "有用/无用")
    @RequestMapping(value = "/praise", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id"),
            @ApiImplicitParam(name = "type", value = "类型：1已解决，2为解决", allowableValues = "range[1,2]")
    })
    public CommonResult<Boolean> praise(@RequestParam(value = "id") Integer id, @RequestParam(value = "type") Integer type) {
        if (helpService.feedback(id, type)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
   }
}



