package com.zhongshu.card.server.core.service.school;

import com.zhongshu.card.client.model.school.CollegeModel;
import com.zhongshu.card.client.model.school.CollegeParam;
import com.github.microservice.net.ResultContent;
import com.github.microservice.net.ResultMessage;
import com.zhongshu.card.client.service.school.CollegeService;
import com.zhongshu.card.client.utils.ITree;
import com.zhongshu.card.client.utils.TreeUtil;
import com.zhongshu.card.client.type.DataState;
import com.zhongshu.card.server.core.dao.school.CollegeDao;
import com.zhongshu.card.server.core.domain.school.College;
import com.zhongshu.card.server.core.service.base.SuperService;
import com.zhongshu.card.server.core.util.BeanUtils;
import com.zhongshu.card.server.core.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author TRX
 * @date 2024/6/3
 */
@Slf4j
@Service
public class CollegeServiceImpl extends SuperService implements CollegeService {

    @Autowired
    CollegeDao collegeDao;

    /**
     * 添加院级
     *
     * @param param
     * @return
     */
    @Override
    public ResultContent<College> addCollege(CollegeParam param) {
        initDefaultUserAndOid(param);

        if (CommonUtil.longIsEmpty(param.getSort())) {
            param.setSort(1L);
        }
        if (StringUtils.isEmpty(param.getParentId())) {
            param.setParentId(ITree.ROOT_ID);
        }
        if (!param.getParentId().equals(ITree.ROOT_ID)) {
            College temp = collegeDao.findTopById(param.getParentId());
            if (ObjectUtils.isEmpty(temp)) {
                return ResultContent.buildFail(String.format("上级数据ID不存在：%s", param.getParentId()));
            }
        }
        if (param.getState() == null) {
            param.setState(DataState.Enable);
        }
        College college = null;
        if (StringUtils.isNotEmpty(param.getId())) {
            college = collegeDao.findTopById(param.getId());
            if (ObjectUtils.isEmpty(college)) {
                return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
            }
            if (StringUtils.isNotEmpty(param.getCode())) {
                college = collegeDao.findTopByCodeAndOid(param.getCode(), param.getOid());
                if (ObjectUtils.isNotEmpty(college) && !college.getId().equals(param.getId())) {
                    return ResultContent.buildFail(String.format("%s code已存在", param.getCode()));
                }
            }
        } else {
            if (StringUtils.isNotEmpty(param.getCode())) {
                college = collegeDao.findTopByCodeAndOid(param.getCode(), college.getOid());
                if (ObjectUtils.isNotEmpty(college)) {
                    return ResultContent.buildFail(String.format("%s code已存在", param.getCode()));
                }
            }
            college = new College();
        }
        BeanUtils.copyProperties(param, college);
        collegeDao.save(college);
        return ResultContent.buildSuccess(college);
    }

    /**
     * 删除院级
     *
     * @param id
     * @return
     */
    @Override
    public ResultContent deleteCollege(String id) {
        String oid = getCurrentOid();
        College college = collegeDao.findTopById(id);
        if (ObjectUtils.isEmpty(college)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        List<College> childrens = collegeDao.findByParentIdAndOid(id, oid);
        if (ObjectUtils.isNotEmpty(childrens)) {
            return ResultContent.buildFail(String.format("%s 有下级院级，不能删除", college.getName()));
        }
        collegeDao.delete(college);
        return ResultContent.buildSuccess();
    }

    /**
     * 得到结构所有部门信息 (树形结构)
     *
     * @param oid
     * @return
     */
    @Override
    public ResultContent<List<ITree>> getCollegeTree(String oid) {
        if (StringUtils.isEmpty(oid)) {
            oid = getCurrentOid();
        }
        List<College> list = collegeDao.findByOidOrderBySortAsc(oid);
        List<ITree> models = list.stream().map(this::toModel).collect(Collectors.toList());
        List<ITree> iTrees = TreeUtil.buildTree(models, ITree.ROOT_ID);
        return ResultContent.buildSuccess(iTrees);
    }

    public CollegeModel toModel(College entity) {
        CollegeModel model = new CollegeModel();
        if (ObjectUtils.isNotEmpty(entity)) {
            BeanUtils.copyProperties(entity, model);
        }
        return model;
    }
}
