package com.rj.controller;

import com.rj.entity.LbDepartmentUser;
import com.rj.service.ILbDepartmentUserService;
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.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Map;

@RestController
@RequestMapping("/api/lbDepartmentUser")
@Tag(name = "部门用户", description = "lb_department_user增删改查与分页")
public class LbDepartmentUserController {

    @Autowired
    private ILbDepartmentUserService lbDepartmentUserService;

    @PostMapping("/add")
    @Operation(summary = "新增")
    public ResponseEntity<Map<String, Object>> add(
            @Parameter(description = "实体", required = true) @RequestBody LbDepartmentUser entity) {
        Map<String, Object> result = lbDepartmentUserService.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 LbDepartmentUser entity) {
        Map<String, Object> result = lbDepartmentUserService.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 = lbDepartmentUserService.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 tenantId,
            @RequestParam(required = false) String userId,
            @RequestParam(required = false) String parentId,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String phone,
            @RequestParam(required = false) String industry) {
        Map<String, Object> result = lbDepartmentUserService.pageQuery(
                current, size, tenantId, userId, parentId, name, phone, industry
        );
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.internalServerError().body(result);
    }

    @PostMapping("/bindParentUser")
    @Operation(summary = "绑定父子用户关系")
    public ResponseEntity<Map<String, Object>> bindParentUser(
            @RequestParam String parentUserId,
            @RequestParam(required = false) String parentUserName,
            @RequestParam String childUserId,
            @RequestParam(required = false) String childUserName,
            @RequestParam String tenantId) {
        Map<String, Object> result = lbDepartmentUserService.bindParentUser(
                parentUserId, parentUserName, childUserId, childUserName, tenantId
        );
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.badRequest().body(result);
    }

    @PostMapping("/syncFromDailyUserTrade")
    @Operation(summary = "从lb_daily_user_trade增量同步到部门用户")
    public ResponseEntity<Map<String, Object>> syncFromDailyUserTrade(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        Map<String, Object> result = lbDepartmentUserService.syncFromDailyUserTrade(startDate, endDate);
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.badRequest().body(result);
    }
}
