package com.yuchen.saas.park.service.controller.door;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.dto.door.DoorUserGroupDTO;
import com.yuchen.saas.park.api.entity.door.*;
import com.yuchen.saas.park.api.vo.door.DoorUserVO;
import com.yuchen.saas.park.service.service.door.DoorUserGroupItemService;
import com.yuchen.saas.park.service.service.door.DoorUserGroupService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 人员分组 前端控制器
 * </p>
 *
 * @author miaoqs
 * @since 2023-12-21
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/doorUserGroup")
public class DoorUserGroupController {

    private final DoorUserGroupService doorUserGroupService;

    private final DoorUserGroupItemService doorUserGroupItemService;

    /**
     * 人员分组 新增/修改
     */
    @PostMapping("/saveOrUpdate")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "人员分组 新增/修改", notes = "人员分组 新增/修改")
    public R saveOrUpdate(@Valid @RequestBody DoorUserGroupDTO doorUserGroupDTO) {
        if(Func.isEmpty(doorUserGroupDTO)) {
            return R.error("人员分组不能为空");
        }
        return R.data(doorUserGroupService.saveOrUpdateByParam(doorUserGroupDTO));
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "人员分组详情", notes = "人员分组详情")
    public R detail(Long id) {
        return R.data(doorUserGroupService.detailById(id));
    }

    /**
     * 根据主表id删除人员分组
     * @param id
     * @return
     */
    @PostMapping("/removeId")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据主表id人员分组删除", notes = "根据主表id人员分组删除")
    public R removeId(Long id) throws Exception {
        String msg = doorUserGroupService.deleteById(id);
        if(Func.isNotEmpty(msg)){
            return R.fail(msg);
        }else{
            return R.success();
        }
    }

    /**
     * 批量删除-根据主表id删除人员分组
     * @param idList
     * @return
     */
    @PostMapping("/batchRemoveId")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "批量删除-根据主表id删除人员分组", notes = "批量删除-根据主表id删除人员分组")
    public R batchRemoveId(@RequestBody List<Long> idList) throws Exception {
        String msg = doorUserGroupService.batchRemoveId(idList);
        if(Func.isNotEmpty(msg)){
            return R.fail(msg);
        }else{
            return R.success();
        }
    }

    /**
     * 人员分组 分页
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "人员分组 分页", notes = "人员分组 分页")
    public R<IPage<DoorUserGroup>> page(DoorUserGroup doorUserGroup, Query query) {
        IPage<DoorUserGroup> pages = doorUserGroupService.page(Condition.getPage(query),
                new LambdaQueryWrapper<DoorUserGroup>()
                        .like(Func.isNotEmpty(doorUserGroup.getGroupName()), DoorUserGroup::getGroupName,
                                doorUserGroup.getGroupName())
                        .orderByDesc(DoorUserGroup::getCreateTime)
        );
        return  R.data(pages);
    }

    /**
     * 详情 人员列表 分页
     */
    @PostMapping("/infoUserPage")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "详情 人员列表 分页", notes = "详情 人员列表 分页")
    public R<NestPage<DoorUserVO>> infoUserPage(@Valid @RequestBody PageUserGroupReqDTO req) {
        return  R.data(doorUserGroupService.infoUserPage(req));
    }

    /**
     * 详情 人员列表 批量删除
     * @param idList
     * @return
     */
    @PostMapping("/infoBatchRemoveId")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "详情 人员列表 批量删除", notes = "详情 人员列表 批量删除")
    public R infoBatchRemoveId(@RequestBody List<Long> idList) {
        doorUserGroupService.infoBatchRemoveId(idList);
        return R.success();
    }

    /**
     * 设置人员数量
     */
    @PostMapping("/setUserNumber")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "设置人员数量", notes = "设置人员数量")
    public R setUserNumber(@RequestBody DoorUserGroup doorUserGroup) {
        if(Func.isEmpty(doorUserGroup.getId())){
            return R.error("id不能为空");
        }
        if(Func.isEmpty(doorUserGroup.getNumber())){
            return R.error("人员数量不能为空");
        }
        doorUserGroupService.updateById(doorUserGroup);
        return  R.success();
    }

    /**
     * 人员分组 基础信息新增/修改
     */
    @PostMapping("/basicsSaveOrUpdate")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "人员分组 基础信息新增/修改", notes = "人员分组 基础信息新增/修改")
    public R basicsSaveOrUpdate(@Valid @RequestBody DoorUserGroup doorUserGroup) {
        if(Func.isEmpty(doorUserGroup)) {
            return R.error("人员分组不能为空");
        }
        return R.data(doorUserGroupService.saveOrUpdate(doorUserGroup));
    }

    /**
     * 人员分组 详情 人员列表新增子项
     */
    @PostMapping("/infoSaveItem")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "人员分组 详情 人员列表新增子项", notes = "人员分组 详情 人员列表新增子项")
    public R infoSaveItem(@Valid @RequestBody List<DoorUserGroupItem> doorUserGroupItemList) {
        if(Func.isEmpty(doorUserGroupItemList)) {
            return R.error("人员不能为空");
        }
        return R.data(doorUserGroupItemService.saveBatch(doorUserGroupItemList));
    }

    /**
     * 获取人员分组 列表
     * @param doorUserGroup
     * @return
     */
    @GetMapping("/getListByparam")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "获取人员分组 列表", notes = "获取人员分组 列表")
    public R getListByparam(DoorUserGroup doorUserGroup) {
        return R.data(doorUserGroupService.listByparam(doorUserGroup));
    }

}
