package com.rj.controller;

import com.rj.entity.QweiUser;
import com.rj.service.IQweiUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/qweiUser")
@Tag(name = "企微用户", description = "qwei_user增删改查与分页")
public class QweiUserController {

    @Autowired
    private IQweiUserService qweiUserService;

    @PostMapping("/add")
    @Operation(summary = "新增")
    public ResponseEntity<Map<String, Object>> add(
            @Parameter(description = "实体", required = true) @RequestBody QweiUser entity) {
        Map<String, Object> result = qweiUserService.add(entity);
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.badRequest().body(result);
    }

    @PutMapping("/update")
    @Operation(summary = "更新")
    public ResponseEntity<Map<String, Object>> update(
            @Parameter(description = "实体", required = true) @RequestBody QweiUser entity) {
        Map<String, Object> result = qweiUserService.update(entity);
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.badRequest().body(result);
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除")
    public ResponseEntity<Map<String, Object>> delete(
            @Parameter(description = "主键ID(UUID)", required = true) @PathVariable String id) {
        Map<String, Object> result = qweiUserService.deleteById(id);
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.badRequest().body(result);
    }

    @GetMapping("/list")
    @Operation(summary = "分页查询")
    public ResponseEntity<Map<String, Object>> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String userid,
            @RequestParam(required = false) String userName,
            @RequestParam(required = false) Long mainDepartmentId,
            @RequestParam(required = false) Integer status) {
        Map<String, Object> result = qweiUserService.pageQuery(current, size, userid, userName, mainDepartmentId, status);
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.internalServerError().body(result);
    }

    @PostMapping("/sync")
    @Operation(summary = "同步企微用户并入库")
    public ResponseEntity<Map<String, Object>> sync(
            @RequestParam(required = false) String tenantId) {
        Map<String, Object> result = qweiUserService.syncFromQiWei(tenantId);
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.badRequest().body(result);
    }

    @PostMapping("/syncCustomer")
    @Operation(summary = "同步企微客户并入库")
    public ResponseEntity<Map<String, Object>> syncCustomer(
            @RequestParam(required = false) String tenantId) {
        Map<String, Object> result = qweiUserService.syncCustomerFromQiWei(tenantId);
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.badRequest().body(result);
    }
}

