package com.njcn.msgpush.module.system.controller.admin.dept;

import com.njcn.msgpush.framework.common.pojo.CommonResult;
import com.njcn.msgpush.framework.common.util.object.BeanUtils;
import com.njcn.msgpush.module.system.controller.admin.dept.vo.orgleader.OrgLeaderRelationRespVO;
import com.njcn.msgpush.module.system.controller.admin.dept.vo.orgleader.OrgLeaderRelationSaveReqVO;
import com.njcn.msgpush.module.system.controller.admin.user.vo.user.UserSimpleRespVO;
import com.njcn.msgpush.module.system.convert.user.UserConvert;
import com.njcn.msgpush.module.system.dal.dataobject.dept.DeptDO;
import com.njcn.msgpush.module.system.dal.dataobject.dept.OrgLeaderRelationDO;
import com.njcn.msgpush.module.system.dal.dataobject.user.AdminUserDO;
import com.njcn.msgpush.module.system.service.dept.DeptService;
import com.njcn.msgpush.module.system.service.dept.OrgLeaderRelationService;
import com.njcn.msgpush.module.system.service.user.AdminUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

import static com.njcn.msgpush.framework.common.pojo.CommonResult.success;
import static com.njcn.msgpush.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - 组织负责人关系")
@RestController
@RequestMapping("/system/org-leader")
@Validated
public class OrgLeaderRelationController {

    @Resource
    private OrgLeaderRelationService orgLeaderRelationService;
    @Resource
    private DeptService deptService;
    @Resource
    private AdminUserService adminUserService;

    @PostMapping("/create")
    @Operation(summary = "创建组织负责人关系")
    @PreAuthorize("@ss.hasPermission('system:org-leader:create')")
    public CommonResult<Long> createOrgLeaderRelation(@Valid @RequestBody OrgLeaderRelationSaveReqVO createReqVO) {
        return success(orgLeaderRelationService.createOrgLeaderRelation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "修改组织负责人关系")
    @PreAuthorize("@ss.hasPermission('system:org-leader:update')")
    public CommonResult<Boolean> updateOrgLeaderRelation(@Valid @RequestBody OrgLeaderRelationSaveReqVO updateReqVO) {
        orgLeaderRelationService.updateOrgLeaderRelation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除组织负责人关系")
    @Parameter(name = "id", description = "负责人关系编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:org-leader:delete')")
    public CommonResult<Boolean> deleteOrgLeaderRelation(@RequestParam("id") Long id) {
        orgLeaderRelationService.deleteOrgLeaderRelation(id);
        return success(true);
    }

    @GetMapping("/list-by-dept")
    @Operation(summary = "查询组织下的负责人关系列表")
    @Parameter(name = "deptId", description = "组织节点 ID", required = true, example = "100")
    @PreAuthorize("@ss.hasPermission('system:org-leader:query')")
    public CommonResult<List<OrgLeaderRelationRespVO>> getOrgLeaderRelationListByDept(@RequestParam("deptId") Long deptId) {
        List<OrgLeaderRelationDO> relations = orgLeaderRelationService.getOrgLeaderRelationListByDeptId(deptId);
        List<OrgLeaderRelationRespVO> respList = BeanUtils.toBean(relations, OrgLeaderRelationRespVO.class);
        Map<Long, AdminUserDO> userMap = adminUserService.getUserMap(convertList(relations, OrgLeaderRelationDO::getUserId));
        respList.forEach(respVO -> {
            AdminUserDO user = userMap.get(respVO.getUserId());
            if (user != null) {
                respVO.setUserNickname(user.getNickname());
            }
        });
        return success(respList);
    }

    @GetMapping("/candidate-users")
    @Operation(summary = "查询组织负责人候选用户列表")
    @Parameter(name = "deptId", description = "组织节点 ID", required = true, example = "100")
    @PreAuthorize("@ss.hasPermission('system:org-leader:query')")
    public CommonResult<List<UserSimpleRespVO>> getCandidateUsers(@RequestParam("deptId") Long deptId) {
        List<AdminUserDO> users = orgLeaderRelationService.getCandidateUsersByDeptId(deptId);
        Map<Long, DeptDO> deptMap = deptService.getDeptMap(convertList(users, AdminUserDO::getDeptId));
        return success(UserConvert.INSTANCE.convertSimpleList(users, deptMap));
    }

}
