package com.hc.web.controller.browser.front;

import com.hc.business.model.UserAdvise;
import com.hc.business.service.UserAdviseService;
import com.hc.core.utils.enums.InfoEnum;
import com.hc.core.utils.result.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 用户建议表
 * (UserAdvise)表控制层
 *
 * @author java
 * @since 2020-11-16 14:20:06
 */

@RestController
@RequestMapping("/new/api/front/userAdvise")
public class FrontUserAdviseController {

    /**
     * 服务对象
     */
    private final UserAdviseService userAdviseService;

    public FrontUserAdviseController(UserAdviseService userAdviseService) {
        this.userAdviseService = userAdviseService;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "依据主键获取 UserAdvise", response = Result.class)
    public Result selectFrontDetail(@PathVariable("id") Integer id) {
        return Result.success(this.userAdviseService.queryById(id));
    }

    @PostMapping("/insert")
    @ApiOperation(value = "新增 UserAdvise", response = Result.class)
    public Result addUserAdvise(@Valid @RequestBody UserAdvise userAdvise) {
        if(userAdvise.getUserId() == null)
        {
            return Result.info(InfoEnum.MUST_LOGIN);
        }
        userAdvise.setAdviceType(2);
        return Result.success(userAdviseService.insert(userAdvise));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "依据主键删意见，删除后管理员还可以见，用户不可见", response = Result.class)
    public Result deleteUserAdvise(@PathVariable("id") Integer id) {
        UserAdvise userAdvise = this.userAdviseService.queryById(id);
        userAdvise.setUserDel(1);
        return Result.success(userAdviseService.update(userAdvise));
    }


    @GetMapping("/user/{userId}")
    @ApiOperation(value = "依据用户id获取 UserAdvise", response = Result.class)
    public Result listFrontUserAdviseById(@ApiParam(value = "用户id")
                                          @PathVariable("userId") Integer userId,
                                          @RequestParam(defaultValue = "0") Integer page,
                                          @RequestParam(defaultValue = "0") Integer size) {
        return Result.success(this.userAdviseService.queryAllByUserId(userId, page, size));
    }

}