package com.ruoyi.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.page.PageResult;
import com.ruoyi.controller.req.AnswerPageReq;
import com.ruoyi.frequency.platformanswer.entity.PlatformAnswer;
import com.ruoyi.frequency.platformanswer.service.PlatformAnswerService;
import com.ruoyi.frequency.platformanswertype.entity.PlatformAnswerType;
import com.ruoyi.frequency.platformanswertype.service.PlatformAnswerTypeService;
import com.ruoyi.response.ResponseData;
import com.ruoyi.vo.BodyIdReq;
import com.ruoyi.vo.RyController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 有问必答
 */
@CrossOrigin
@RestController
@RequestMapping("/api/answer")
@Api(tags = "有问必答")
public class ApiPlatformAnswerController extends RyController {


    @Autowired
    private PlatformAnswerService platformAnswerService;

    @Autowired
    private PlatformAnswerTypeService platformAnswerTypeService;

    @PostMapping("/answerTypeList/{userType}")
    @ApiOperation(value = "有问必答分类", notes = "有问必答分类")
    public ResponseData<List<PlatformAnswerType>> answerTypeList(@PathVariable Integer userType) {
        List<PlatformAnswerType> list = platformAnswerTypeService.list(new QueryWrapper<PlatformAnswerType>().lambda()
                .eq(PlatformAnswerType::getDelFlag, PublicCommon.启用)
                        .eq(PlatformAnswerType::getUserType,userType)
                .orderByAsc(PlatformAnswerType::getSort));
        return ResponseData.success(list);
    }

    @PostMapping("/answerPage")
    @ApiOperation(value = "有问必答", notes = "有问必答")
    public ResponseData<PageResult<PlatformAnswer>> answerPage(@RequestBody AnswerPageReq req) {
        Page<PlatformAnswer> page = platformAnswerService.page(new Page<>(req.getPageNo(),req.getPageSize()),new QueryWrapper<PlatformAnswer>().lambda()
                .eq(PlatformAnswer::getDelFlag, PublicCommon.启用)
                .eq(PlatformAnswer::getUserType,req.getUserType())
                .eq(ObjectUtil.isNotEmpty(req.getPlatformAnswerTypeId()),PlatformAnswer::getPlatformAnswerTypeId,req.getPlatformAnswerTypeId())
                .orderByAsc(PlatformAnswer::getSort));
        return ResponseData.success(new PageResult<>(page));
    }


    @PostMapping("/detail")
    @ApiOperation(value = "有问必答详情", notes = "有问必答详情")
    public ResponseData<PlatformAnswer> detail(@Valid @NotNull(message = "id不能为空") @RequestBody BodyIdReq req) {
        PlatformAnswer platformAnswer = this.platformAnswerService.getById(req.getId());
        PlatformAnswerType platformAnswerType = platformAnswerTypeService.getById(platformAnswer.getPlatformAnswerTypeId());
        platformAnswer.setPlatformAnswerTypeName(platformAnswerType.getName());
        //修改浏览数
        platformAnswerService.lambdaUpdate().setSql("browse_num = browse_num + 1")
                .eq(PlatformAnswer::getId,req.getId()).update();

        return ResponseData.success(platformAnswer);
    }

}
