package shop.alien.store.controller;

import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeSysDept;
import shop.alien.entity.store.dto.LifeSysDeptAddDto;
import shop.alien.entity.store.dto.LifeSysDeptSortDto;
import shop.alien.entity.store.dto.LifeSysDeptUpdateDto;
import shop.alien.entity.store.vo.LifeSysDeptVo;
import shop.alien.entity.store.vo.LifeSysVo;
import shop.alien.store.service.LifeSysDeptService;

import java.util.List;

/**
 * 部门表 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"数据中台-部门管理"})
@ApiSort(2)
@CrossOrigin
@RestController
@RequestMapping("/sys/dept")
@RequiredArgsConstructor
public class LifeSysDeptController {

    private final LifeSysDeptService lifeSysDeptService;

    @ApiOperation("新增部门")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父部门ID（0表示根部门）", dataType = "Long", paramType = "body"),
            @ApiImplicitParam(name = "deptName", value = "部门名称", dataType = "String", paramType = "body", required = true),
            @ApiImplicitParam(name = "deptSort", value = "显示顺序", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "leader", value = "部门负责人", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "phone", value = "联系电话", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "email", value = "邮箱", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "status", value = "部门状态（0正常 1停用）", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "description", value = "部门描述", dataType = "String", paramType = "body"),
    })
    @PostMapping(value = "/addDept")
    public R<LifeSysDept> addDept(@RequestBody LifeSysDeptAddDto addDto) {
        log.info("LifeSysDeptController.addDept => deptName={}, parentId={}, deptSort={}, leader={}, phone={}, email={}, status={}, description={}",
                addDto.getDeptName(), addDto.getParentId(), addDto.getDeptSort(),
                addDto.getLeader(), addDto.getPhone(), addDto.getEmail(), addDto.getStatus(), addDto.getDescription());
        return lifeSysDeptService.addDept(addDto);
    }

    @ApiOperation("更新一级部门排序")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptId", value = "部门ID", dataType = "Long", paramType = "body", required = true),
            @ApiImplicitParam(name = "newSort", value = "目标排序位置（新排序值）", dataType = "Integer", paramType = "body", required = true),
    })
    @PostMapping(value = "/updateFirstLevelDeptSort")
    public R<Boolean> updateFirstLevelDeptSort(@RequestBody LifeSysDeptSortDto sortDto) {
        log.info("LifeSysDeptController.updateFirstLevelDeptSort => deptId={}, newSort={}", sortDto.getDeptId(), sortDto.getNewSort());
        return lifeSysDeptService.updateFirstLevelDeptSort(sortDto);
    }

    @ApiOperation("编辑部门")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptId", value = "部门ID", dataType = "Long", paramType = "body", required = true),
            @ApiImplicitParam(name = "deptName", value = "部门名称", dataType = "String", paramType = "body", required = true),
            @ApiImplicitParam(name = "description", value = "部门描述", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "leader", value = "部门负责人", dataType = "Integer", paramType = "body"),
    })
    @PostMapping(value = "/updateDept")
    public R<LifeSysDept> updateDept(@RequestBody LifeSysDeptUpdateDto updateDto) {
        log.info("LifeSysDeptController.updateDept => deptId={}, deptName={}, description={}, leader={}",
                updateDto.getDeptId(), updateDto.getDeptName(), updateDto.getDescription(), updateDto.getLeader());
        return lifeSysDeptService.updateDept(updateDto);
    }

    @ApiOperation("搜索部门（根据部门名称模糊查询）")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptName", value = "部门名称（支持模糊查询，为空时返回所有未删除的部门）", dataType = "String", paramType = "query", required = false),
    })
    @GetMapping(value = "/searchDept")
    public R<List<LifeSysDeptVo>> searchDept(@RequestParam(required = false) String deptName) {
        log.info("LifeSysDeptController.searchDept => deptName={}", deptName);
        return lifeSysDeptService.searchDeptByName(deptName);
    }

    @ApiOperation("删除部门")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptId", value = "部门ID", dataType = "Long", paramType = "query", required = true),
    })
    @DeleteMapping(value = "/deleteDept")
    public R<Boolean> deleteDept(@RequestParam Long deptId) {
        log.info("LifeSysDeptController.deleteDept => deptId={}", deptId);
        return lifeSysDeptService.deleteDept(deptId);
    }

    @ApiOperation("查看部门成员")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", dataType = "Integer", paramType = "query", required = false, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", dataType = "Integer", paramType = "query", required = false, defaultValue = "10"),
            @ApiImplicitParam(name = "deptId", value = "部门ID", dataType = "Long", paramType = "query", required = true),
    })
    @GetMapping(value = "/viewMembers")
    public R<List<LifeSysVo>> viewMembers(@RequestParam(defaultValue = "1", required = false) Integer pageNum, @RequestParam(defaultValue = "10", required = false) Integer pageSize, @RequestParam Long deptId) {
        log.info("LifeSysDeptController.viewMembers => pageNum={}, pageSize={}, deptId={}", pageNum, pageSize, deptId);
        return lifeSysDeptService.viewMembers(pageNum, pageSize, deptId);
    }
}

