package com.yonge.cooleshow.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.mybatis.support.PageUtil;

import com.yonge.toolset.base.util.StringUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import com.yonge.cooleshow.biz.dal.vo.PianoRoomSettingsVo;
import com.yonge.cooleshow.biz.dal.dto.search.PianoRoomSettingsSearch;
import com.yonge.cooleshow.biz.dal.entity.PianoRoomSettings;
import com.yonge.cooleshow.biz.dal.service.PianoRoomSettingsService;

import java.util.Date;

@RestController
@RequestMapping("/pianoRoomSettings")
@Api(value = "琴房时长价格配置", tags = "")
public class PianoRoomSettingsController extends BaseController {

    @Autowired
    private PianoRoomSettingsService pianoRoomSettingsService;

    @Autowired
    private SysUserFeignService sysUserFeignService;

    /**
     * 查询单条
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "详情", notes = "传入id")
    public HttpResponseResult<PianoRoomSettingsVo> detail(@PathVariable("id") Long id) {
        return succeed(pianoRoomSettingsService.detail(id));
    }

    /**
     * 查询分页
     */
    @PostMapping("/page")
    @ApiOperation(value = "查询分页", notes = "传入pianoRoomSettingsSearch")
    public HttpResponseResult<PageInfo<PianoRoomSettingsVo>> page(@RequestBody PianoRoomSettingsSearch query) {
        IPage<PianoRoomSettingsVo> pages = pianoRoomSettingsService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }
    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增", notes = "传入pianoRoomSettings")
    public HttpResponseResult save(@Valid @RequestBody PianoRoomSettings pianoRoomSettings) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || user.getId() == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        pianoRoomSettings.setCreateBy(user.getId());
        pianoRoomSettings.setUpdateBy(user.getId());
        return status(pianoRoomSettingsService.save(pianoRoomSettings));
    }
    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "传入pianoRoomSettings")
    public HttpResponseResult update(@Valid @RequestBody PianoRoomSettings pianoRoomSettings) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || user.getId() == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        pianoRoomSettings.setUpdateBy(user.getId());
        pianoRoomSettings.setUpdateTime(new Date());
        return status(pianoRoomSettingsService.updateById(pianoRoomSettings));
    }


    /**
     * 启用/冻结
     */
    @GetMapping(value = "/changeStatus/{id}/{status}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", dataType = "long", required = true),
            @ApiImplicitParam(name = "status", value = "状态 0-正常，1-冻结", paramType = "path", dataType = "int", required = true)
    })
    @ApiOperation(value = "启用/冻结")
    public HttpResponseResult<Boolean> changeStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        if (null == id || (status != 0 && status != 1)) {
            return HttpResponseResult.failed("参数异常");
        }
        return HttpResponseResult.status(pianoRoomSettingsService.changeStatus(id,status) > 0);
    }


    /**
     * 删除
     */
    @PostMapping("/remove")
    @ApiOperation(value = "删除", notes = "传入ids")
    public HttpResponseResult remove(@ApiParam(value = "主键集合(多个id,分割)", required = true) @RequestParam String ids) {
        if (StringUtil.isEmpty(ids)) {
            return failed("参数不能为空");
        }
        return status(pianoRoomSettingsService.removeByIds(StringUtil.toLongList(ids)));
    }

}