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

import com.github.microservice.components.data.base.util.PageEntityUtil;
import com.zhongshu.card.client.model.org.OidAboutInfo;
import com.zhongshu.card.client.model.school.*;
import com.github.microservice.net.ResultContent;
import com.github.microservice.net.ResultMessage;
import com.zhongshu.card.client.service.school.DictInfoService;
import com.zhongshu.card.client.type.DictInfoType;
import com.zhongshu.card.server.core.dao.school.DictInfoDao;
import com.zhongshu.card.server.core.domain.school.DictInfo;
import com.zhongshu.card.server.core.service.base.SuperService;
import com.zhongshu.card.server.core.service.orgManager.OrganizationManagerServiceImpl;
import com.zhongshu.card.server.core.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

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

/**
 * 字典管理服务
 *
 * @author TRX
 * @date 2024/6/12
 */
@Slf4j
@Service
public class DictInfoServiceImpl extends SuperService implements DictInfoService {

    @Autowired
    DictInfoDao dictInfoDao;

    @Autowired
    OrganizationManagerServiceImpl organizationManagerService;

    /**
     * 添加-编辑字典
     *
     * @param param
     * @return
     */
    @Override
    public ResultContent addDict(DictInfoParam param) {
        Assert.hasText(param.getName(), "name不能为空");
        if (param.getDictInfoType() == null) {
            return ResultContent.buildFail(String.format("dictInfoType不能为空"));
        }
        initDefaultUserAndOidCheck(param);

        if (param.getSort() == null) {
            param.setSort(1L);
        }
        DictInfoType dictInfoType = param.getDictInfoType();

        DictInfo dictInfo = new DictInfo();
        if (StringUtils.isNotEmpty(param.getId())) {
            dictInfo = dictInfoDao.findTopById(param.getId());
            if (ObjectUtils.isEmpty(dictInfo)) {
                return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
            }
            DictInfo temp = dictInfoDao.findTopByNameAndOidAndDictInfoType(param.getName(), param.getOid(), dictInfoType);
            if (ObjectUtils.isNotEmpty(temp) && !temp.getId().equals(dictInfo.getId())) {
                return ResultContent.buildFail(String.format("name已存在：%s", param.getName()));
            }
        } else {
            DictInfo temp = dictInfoDao.findTopByNameAndOidAndDictInfoType(param.getName(), param.getOid(), dictInfoType);
            if (ObjectUtils.isNotEmpty(temp)) {
                return ResultContent.buildFail(String.format("name已存在：%s", param.getName()));
            }
        }

        BeanUtils.copyProperties(param, dictInfo, "id");
        OidAboutInfo oidAboutInfo = organizationManagerService.getOidAboutInfo(param.getOid());
        dictInfo.setProjectOid(oidAboutInfo.getProjectOid());
        dictInfo.setAboutAuthType(oidAboutInfo.getAuthType());
        dictInfo.setAboutOid(oidAboutInfo.getOid());
        dictInfoDao.save(dictInfo);
        return ResultContent.buildSuccess();
    }

    /**
     * 删除字典
     *
     * @param id
     * @return
     */
    @Override
    public ResultContent deleteDict(String id) {
        DictInfo dictInfo = dictInfoDao.findTopById(id);
        if (ObjectUtils.isEmpty(dictInfo)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        dictInfoDao.delete(dictInfo);
        return ResultContent.buildSuccess();
    }

    /**
     * @param id
     * @return
     */
    @Override
    public ResultContent<DictInfoParam> getDictInfo(String id) {
        DictInfo dictInfo = dictInfoDao.findTopById(id);
        if (ObjectUtils.isEmpty(dictInfo)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        DictInfoParam model = toModel(dictInfo);
        return ResultContent.buildSuccess(model);
    }

    public DictInfo getEntity(String id) {
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        return dictInfoDao.findTopById(id);
    }

    /**
     * 分页查询
     *
     * @param param
     * @param pageable
     * @return
     */
    @Override
    public ResultContent<Page<DictInfoParam>> page(DictInfoSearch param, Pageable pageable) {
        initSearchParamCheckOid(param);

        Page<DictInfo> page = dictInfoDao.page(pageable, param);
        return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toModel));
    }

    /**
     * 查询字典所有数据
     *
     * @param param
     * @return
     */
    @Override
    public ResultContent<List<DictInfoParam>> findDictInfosByOid(DictInfoSearch param) {
        initSearchParamCheckOid(param);
        Assert.isNull(param.getDictInfoType(), "dictInfoType不能为空");

        List<DictInfo> list = dictInfoDao.findByOidAndDictInfoTypeOrderBySortAsc(param.getOid(), param.getDictInfoType());
        List<DictInfoParam> models = list.stream().map(this::toModel).collect(Collectors.toList());
        return ResultContent.buildSuccess(models);
    }

    public DictInfo getProjectDictByName(String projectOid, DictInfoType dictInfoType, String name) {
        DictInfo dictInfo = null;
        if (StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(projectOid)) {
            dictInfo = dictInfoDao.findTopByNameAndProjectOidAndDictInfoType(name, projectOid, dictInfoType);
        }
        return dictInfo;
    }


    public DictInfoParam toModel(DictInfo dictInfo) {
        DictInfoParam model = null;
        if (ObjectUtils.isNotEmpty(dictInfo)) {
            model = new DictInfoParam();
            BeanUtils.copyProperties(dictInfo, model);
        }
        return model;
    }

    public DictInfoSimpleModel toSimpleModel(DictInfo dictInfo) {
        DictInfoSimpleModel model = null;
        if (ObjectUtils.isNotEmpty(dictInfo)) {
            model = new DictInfoSimpleModel();
            BeanUtils.copyProperties(dictInfo, model);
        }
        return model;
    }

}
