package com.xjrsoft.module.room.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.module.room.dto.AddRoomRuleDto;
import com.xjrsoft.module.room.dto.AddRoomRuleItemDto;
import com.xjrsoft.module.room.dto.RoomRulePageDto;
import com.xjrsoft.module.room.dto.UpdateRoomRuleDto;
import com.xjrsoft.module.room.entity.RoomRule;
import com.xjrsoft.module.room.service.IRoomRuleService;
import com.xjrsoft.module.room.vo.RoomRulePageVo;
import com.xjrsoft.module.room.vo.RoomRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
* @title: 寝室生成规则
* @Author dzx
* @Date: 2024-01-02
* @Version 1.0
*/
@RestController
@RequestMapping("/room" + "/roomRule")
@Api(value = "/room"  + "/roomRule",tags = "寝室生成规则代码")
@AllArgsConstructor
public class RoomRuleController {


    private final IRoomRuleService roomRuleService;

    @GetMapping(value = "/page")
    @ApiOperation(value="寝室生成规则列表(分页)")
    @SaCheckPermission("roomrule:detail")
    public RT<PageOutput<RoomRulePageVo>> page(@Valid RoomRulePageDto dto){
        Page<RoomRulePageVo> page = roomRuleService.getPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<RoomRulePageVo> pageOutput = ConventPage.getPageOutput(page, RoomRulePageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询寝室生成规则信息")
    @SaCheckPermission("roomrule:detail")
    public RT<RoomRuleVo> info(@RequestParam Long id){
        RoomRule roomRule = roomRuleService.getByIdDeep(id);
        if (roomRule == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(roomRule, RoomRuleVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增寝室生成规则")
    @SaCheckPermission("roomrule:add")
    public RT<Boolean> add(@Valid @RequestBody AddRoomRuleDto dto){
        RoomRule roomRule = BeanUtil.toBean(dto, RoomRule.class);
        boolean isSuccess = roomRuleService.add(roomRule);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改寝室生成规则")
    @SaCheckPermission("roomrule:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateRoomRuleDto dto){

        RoomRule roomRule = BeanUtil.toBean(dto, RoomRule.class);
        return RT.ok(roomRuleService.update(roomRule));
    }

    @DeleteMapping
    @ApiOperation(value = "删除寝室生成规则")
    @SaCheckPermission("roomrule:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(roomRuleService.delete(ids));
    }

    @PostMapping(value="/bind-builds")
    @ApiOperation(value = "绑定楼栋")
    @SaCheckPermission("roomrule:edit")
    public RT<Boolean> bindBuilds(@Valid @RequestBody AddRoomRuleItemDto dto){
        return RT.ok(roomRuleService.bindBuilds(dto));
    }

}