package org.jeecg.modules.app.controller.my;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.utils.UuidUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.app.dto.AppFeedbackFindRequestDTO;
import org.jeecg.modules.app.dto.AppSubmitFeedbackRequestDTO;
import org.jeecg.modules.system.app.dto.FindPageRequestDTO;
import org.jeecg.modules.system.app.dto.FindResponseDTO;
import org.jeecg.modules.system.app.entity.AppFeedback;
import org.jeecg.modules.system.app.service.IAppFeedbackService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 意见反馈
 */
@Slf4j
@Tag(name = "app意见反馈相关接口")
@RestController
@RequestMapping("/app/my/feedback")
public class AppFeedbackController {

    @Autowired
    IAppFeedbackService iAppFeedbackService;


    /**
     * 查询意见反馈列表接口
     * @param appFeedbackFindRequestDTO
     * @return
     */
    @GetMapping("/findByPage")
    @Operation(summary = "app意见反馈分页查询")
    public Result<IPage<FindResponseDTO>> findByList(AppFeedbackFindRequestDTO appFeedbackFindRequestDTO){
        FindPageRequestDTO findPageRequestDTO = new FindPageRequestDTO();
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        findPageRequestDTO.setUserId(loginUser.getId());
        return Result.ok(iAppFeedbackService.findPage(appFeedbackFindRequestDTO.getPageNo(),appFeedbackFindRequestDTO.getPageSize(),findPageRequestDTO));
    }


    @PostMapping("/submitFeedback")
    @Operation(summary = "提交意见反馈")
    public Result<String> submitFeedback( @Valid @RequestBody AppSubmitFeedbackRequestDTO appSubmitFeedbackRequestDTO){
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        AppFeedback appFeedback = new AppFeedback();
        BeanUtils.copyProperties(appSubmitFeedbackRequestDTO,appFeedback);
        appFeedback.setUserId(loginUser.getId());
        appFeedback.setId(UuidUtils.getUUID());
        boolean save = iAppFeedbackService.save(appFeedback);
        if (!save){
            return Result.error("提交反馈意见失败,请联系管理员");
        }
        return Result.ok("提交成功");
    }
}
