package cn.axzo.workflow.server.controller.web.manage;

import cn.axzo.framework.domain.ServiceException;
import cn.axzo.workflow.admin.repository.entity.ExtAxProcessAdmin;
import cn.axzo.workflow.admin.service.ExtAxProcessAdminService;
import cn.axzo.workflow.client.feign.manage.ProcessAdminApi;
import cn.axzo.workflow.common.model.request.admin.ProcessAdminCreateDTO;
import cn.axzo.workflow.common.model.request.admin.ProcessAdminDeleteDTO;
import cn.axzo.workflow.common.model.request.admin.ProcessAdminQueryDTO;
import cn.axzo.workflow.common.model.response.admin.ProcessAdminVo;
import cn.axzo.workflow.server.common.annotation.ErrorReporter;
import cn.azxo.framework.common.model.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
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 javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static cn.axzo.workflow.common.constant.BpmnConstants.MAX_ORG_WORKSPACE_ADMIN_COUNT;

/**
 * 流程管理员控制器
 */
@Slf4j
@RequestMapping({"/web/v1/api/process/admin", "/api/process/admin"})
@RestController
@ErrorReporter
@Validated
public class ProcessAdminController implements ProcessAdminApi {

    @Resource
    private ExtAxProcessAdminService extAxProcessAdminService;

    @PostMapping("/query")
    @Override
    public CommonResponse<List<ProcessAdminVo>> queryProcessAdmins(@RequestBody ProcessAdminQueryDTO dto) {
        List<ExtAxProcessAdmin> extAxProcessAdmins = extAxProcessAdminService.query(dto);
        if (CollectionUtils.isEmpty(extAxProcessAdmins)) {
            return CommonResponse.success();
        }
        List<ProcessAdminVo> adminVos = extAxProcessAdmins.stream()
                .map(ad -> {
                    ProcessAdminVo vo = new ProcessAdminVo();
                    vo.setProcessAdminId(ad.getId());
                    BeanUtils.copyProperties(ad, vo);
                    return vo;
                })
                .collect(Collectors.toList());
        return CommonResponse.success(adminVos);
    }

    @Override
    @PostMapping("/query/count")
    public CommonResponse<Integer> queryProcessAdminsCount(ProcessAdminQueryDTO dto) {
        return CommonResponse.success(extAxProcessAdminService.queryCount(dto));
    }

    @PostMapping("/create")
    @Override
    public CommonResponse<Long> createProcessAdmin(@RequestBody ProcessAdminCreateDTO dto) {
        List<ExtAxProcessAdmin> extAxProcessAdmins = extAxProcessAdminService.query(ProcessAdminQueryDTO
                .builder()
                .organizationalUnitId(dto.getOrganizationalUnitId())
                .workspaceId(dto.getWorkspaceId())
                .build());
        if (!CollectionUtils.isEmpty(extAxProcessAdmins) && extAxProcessAdmins.size() > MAX_ORG_WORKSPACE_ADMIN_COUNT) {
            throw new ServiceException(String.format("管理员最多不超过%d人", MAX_ORG_WORKSPACE_ADMIN_COUNT));
        }
        ExtAxProcessAdmin processAdmin = new ExtAxProcessAdmin();
        BeanUtils.copyProperties(dto, processAdmin);
        Long id = extAxProcessAdminService.insert(processAdmin);
        return CommonResponse.success(id);
    }

    @PostMapping("/batch/create")
    @Override
    public CommonResponse<Void> batchCreateProcessAdmin(@RequestBody List<ProcessAdminCreateDTO> dtos) {
        if (CollectionUtils.isEmpty(dtos)) {
            return CommonResponse.success();
        }
        List<ExtAxProcessAdmin> extAxProcessAdmins = dtos.stream()
                .map(d -> {
                    ExtAxProcessAdmin processAdmin = new ExtAxProcessAdmin();
                    BeanUtils.copyProperties(d, processAdmin);
                    return processAdmin;
                })
                .collect(Collectors.toList());
        extAxProcessAdminService.batchInsert(extAxProcessAdmins);
        return CommonResponse.success();
    }

    @DeleteMapping("/delete")
    @Override
    public CommonResponse<Integer> deleteCommonProcessAdmin(@RequestParam Long id) {
        return CommonResponse.success(extAxProcessAdminService.deleteCommonAdminsByIds(Collections.singletonList(id)));
    }

    @DeleteMapping("/delete/criteria")
    @Override
    public CommonResponse<Integer> deleteProcessAdminCriteria(@RequestBody ProcessAdminDeleteDTO dto) {
        return CommonResponse.success(extAxProcessAdminService.delete(dto));
    }

    @DeleteMapping("/batch/delete")
    @Override
    public CommonResponse<Integer> batchDeleteProcessAdmin(@RequestBody List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return CommonResponse.success();
        }
        return CommonResponse.success(extAxProcessAdminService.deleteCommonAdminsByIds(ids));
    }
}
