package org.dromara.elevator.controller;

import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.elevator.domain.vo.EThirdPartyUserVo;
import org.dromara.elevator.domain.bo.EThirdPartyUserBo;
import org.dromara.elevator.service.IEThirdPartyUserService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 物业维保等用户信息
 *
 * @author zy
 * @date 2024-09-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/elevator/thirdPartyUser")
public class EThirdPartyUserController extends BaseController {

    private final IEThirdPartyUserService eThirdPartyUserService;

    /**
     * 查询物业维保等用户信息列表
     */
    @SaCheckPermission("elevator:thirdPartyUser:list")
    @GetMapping("/list")
    public TableDataInfo<EThirdPartyUserVo> list(EThirdPartyUserBo bo, PageQuery pageQuery) {
        return eThirdPartyUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出物业维保等用户信息列表
     */
    @SaCheckPermission("elevator:thirdPartyUser:export")
    @Log(title = "物业维保等用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EThirdPartyUserBo bo, HttpServletResponse response) {
        List<EThirdPartyUserVo> list = eThirdPartyUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "物业维保等用户信息", EThirdPartyUserVo.class, response);
    }

    /**
     * 根据电梯编号查询物业维保等人员信息
     * @param elevatorNum
     * @return
     */
    @SaCheckPermission("elevator:thirdPartyUser:list")
    @GetMapping("/getThirdPartyUser/{elevatorNum}")
    public R<List<Map<String, Object>>> getThirdPartyUser(@PathVariable String elevatorNum) {
        return R.ok(eThirdPartyUserService.getThirdPartyUser(elevatorNum));
    }

    /**
     * 获取物业维保等用户信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("elevator:thirdPartyUser:query")
    @GetMapping("/{id}")
    public R<EThirdPartyUserVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(eThirdPartyUserService.queryById(id));
    }

    /**
     * 新增物业维保等用户信息
     */
    @SaCheckPermission("elevator:thirdPartyUser:add")
    @Log(title = "物业维保等用户信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EThirdPartyUserBo bo) {
        return toAjax(eThirdPartyUserService.insertByBo(bo));
    }

    /**
     * 修改物业维保等用户信息
     */
    @SaCheckPermission("elevator:thirdPartyUser:edit")
    @Log(title = "物业维保等用户信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EThirdPartyUserBo bo) {
        return toAjax(eThirdPartyUserService.updateByBo(bo));
    }

    /**
     * 删除物业维保等用户信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("elevator:thirdPartyUser:remove")
    @Log(title = "物业维保等用户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(eThirdPartyUserService.deleteWithValidByIds(List.of(ids), true));
    }
}
