package com.enterprise.manage.modules.system.controller;

import com.enterprise.manage.common.base.BaseController;
import com.enterprise.manage.common.constant.ControllerConstant;
import com.enterprise.manage.common.constant.emuns.InstitutionNature;
import com.enterprise.manage.common.constant.emuns.OperationType;
import com.enterprise.manage.common.constant.emuns.StatusType;
import com.enterprise.manage.common.web.response.Result;
import com.enterprise.manage.common.web.response.module.ResultTable;
import com.enterprise.manage.core.annotation.Log;
import com.enterprise.manage.modules.system.domain.po.Org;
import com.enterprise.manage.modules.system.service.IOrgService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 组织机构
 *
 * @author PeiZhi Guo
 * 2022-7-11 11:42
 */
@RestController
@RequestMapping(ControllerConstant.SYSTEM + "org/v1")
public class OrgController extends BaseController {

    private IOrgService orgService;

    @Autowired
    public OrgController(IOrgService orgService) {
        this.orgService = orgService;
    }

    @GetMapping
    @PreAuthorize("hasPermission('', 'sys:org:main')")
    public ResultTable main(Org param) {
        return treeTable(orgService.list(param));
    }

    @GetMapping("{id}")
    @PreAuthorize("hasPermission('','sys:org:main')")
    public Result get(@PathVariable Long id) {
        return success(orgService.queryById(id));
    }


    @GetMapping("selectTree")
    @PreAuthorize("hasPermission('','sys:org:main')")
    public Result selectTree() {
        return success(orgService.querySelectTree());
    }

    @PostMapping
    @PreAuthorize("hasPermission('','sys:org:add')")
    @Log(operationType = OperationType.INSERT, title = "组织机构")
    public Result save(@RequestBody Org org) {
        if (org.getNature().equals(InstitutionNature.CITY_NATURE)){
            org.setArea(org.getCity());
        }

        String msg = checkParams(org);
        if (StringUtils.isNotBlank(msg)) {
            return failure(msg);
        }
        if (orgService.orgNameUniqueCheck(org.getId(),org.getCity().getId(), org.getArea().getId(), org.getName())) {
            return failure("该组织机构已存在");
        }
        org.setStatus(StatusType.ENABLE.ordinal());
        if (org.getParentId().equals(0L)) {
            org.setLevel(1);
        } else {
            Org parentOrg = orgService.queryById(org.getParentId());
            org.setLevel(parentOrg.getLevel() + 1);
        }
        return decide(orgService.save(org));
    }

    @PutMapping
    @PreAuthorize("hasPermission('','sys:org:edit')")
    @Log(operationType = OperationType.UPDATE, title = "组织机构")
    public Result edit(@RequestBody Org org) {
        if (org.getNature().equals(InstitutionNature.CITY_NATURE)){
            org.setArea(org.getCity());
        }
        String msg = checkEditParams(org);
        if (StringUtils.isNotBlank(msg)) {
            return failure(msg);
        }
        if (orgService.orgNameUniqueCheck(org.getId(),org.getCity().getId(), org.getArea().getId(), org.getName())) {
            return failure("该组织机构已存在");
        }
        return decide(orgService.update(org));
    }

    @DeleteMapping("{id}")
    @PreAuthorize("hasPermission('','sys:org:del')")
    @Log(operationType = OperationType.DELETE, title = "组织机构")
    public Result remove(@PathVariable Long id) {
        if (id == null) {
            return failure("organization id is not null");
        }
        if (orgService.queryChildren(id)) {
            return failure("部门下存在子集");
        }
        return decide(orgService.removeById(id));
    }

    public String checkParams(Org org) {
        if (StringUtils.isBlank(org.getName())) {
            return "organization name is not null";
        }
        if (org.getCity() == null) {
            return "organization city is not null";
        }
        if (org.getNature() == null) {
            return "organization nature is not null";
        }
        return null;
    }

    public String checkEditParams(Org org) {
        if (org.getId() == null) {
            return "organization id is not null";
        }
        return checkParams(org);
    }
}
