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

import com.github.microservice.components.data.base.util.PageEntityUtil;
import com.github.microservice.net.ResultContent;
import com.github.microservice.net.ResultMessage;
import com.zhongshu.card.client.model.org.projectDict.*;
import com.zhongshu.card.client.type.DataState;
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
import com.zhongshu.card.server.core.dao.projectAbout.ProjectDictDao;
import com.zhongshu.card.server.core.dao.projectAbout.ProjectDictListDao;
import com.zhongshu.card.server.core.domain.org.Organization;
import com.zhongshu.card.server.core.domain.org.ProjectDict;
import com.zhongshu.card.server.core.domain.org.ProjectDictList;
import com.zhongshu.card.server.core.service.base.SuperService;
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.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.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目的字典管理
 *
 * @author TRX
 * @date 2024/12/19
 */
@Slf4j
@Service
public class ProjectDictService extends SuperService {

    @Autowired
    private OrganizationDao organizationDao;

    @Autowired
    private ProjectDictDao projectDictDao;

    @Autowired
    private ProjectDictListDao projectDictListDao;

    public ResultContent saveInfo(ProjectDictParam param) {
        Assert.hasText(param.getProjectOid(), "projectOid不能为空");
        if (param.getDictType() == null) {
            return ResultContent.buildFail("dictType不能为空");
        }
        ProjectDict entity = null;

        if (StringUtils.isNotEmpty(param.getId())) {
            entity = projectDictDao.findTopById(param.getId());
            if (ObjectUtils.isEmpty(entity)) {
                return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
            }
            initUpdateEntity(entity);
        } else {
            entity = new ProjectDict();
            if (param.getState() == null) {
                param.setState(DataState.Enable);
            }
            initEntityNoCheckOid(entity);
        }
        BeanUtils.copyProperties(param, entity);
        projectDictDao.save(entity);

        // 维护字典选项
        saveList(entity, param.getList());

        return ResultContent.buildSuccess();
    }

    public void saveList(ProjectDict projectDict, List<ProjectDictListParam> params) {
        // 原来的数据
        List<ProjectDictList> list = projectDictListDao.findByProjectDictOrderBySortAsc(projectDict);
        List<String> ids = new ArrayList<>();
        if (ObjectUtils.isNotEmpty(list)) {
            ids = list.stream().map(it -> it.getId()).collect(Collectors.toList());
        }
        if (ObjectUtils.isNotEmpty(params)) {
            List<ProjectDictList> saveList = new ArrayList<>();
            long i = 1;
            for (ProjectDictListParam param : params) {
                ProjectDictList projectDictList = new ProjectDictList();
                BeanUtils.copyProperties(param, projectDictList);
                projectDictList.setProjectDict(projectDict);
                projectDictList.setSort(i++);
                saveList.add(projectDictList);

                if (StringUtils.isNotEmpty(param.getId()) && ids.contains(param.getId())) {
                    ids.remove(param.getId());
                }
            }
            projectDictListDao.saveAll(saveList);
        }
        // 删除去掉的数据
        if (ObjectUtils.isNotEmpty(ids)) {
            projectDictListDao.deleteAllById(ids);
        }
    }

    public ResultContent<Page<ProjectDictModel>> page(ProjectDictSearch param, Pageable pageable) {
        String projectOid = param.getProjectOid();
        Organization organization = organizationDao.findTopByOid(projectOid);
        if (ObjectUtils.isEmpty(organization)) {
            return ResultContent.buildFail("projectOid不存在");
        }
        Page<ProjectDict> page = projectDictDao.page(pageable, param);
        return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toAboutListModel));
    }

    public ResultContent deleteInfo(String id) {
        ProjectDict entity = projectDictDao.findTopById(id);
        if (ObjectUtils.isEmpty(entity)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        projectDictDao.delete(entity);
        projectDictListDao.deleteByProjectDict(entity);
        return ResultContent.buildSuccess();
    }

    public ResultContent<ProjectDictAboutListModel> getDetailInfo(String id) {
        ProjectDict entity = projectDictDao.findTopById(id);
        if (ObjectUtils.isEmpty(entity)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        ProjectDictAboutListModel model = toAboutListModel(entity);
        List<ProjectDictList> list = projectDictListDao.findByProjectDictOrderBySortAsc(entity);

        List<ProjectDictListParam> models = new ArrayList<>();
        if (ObjectUtils.isNotEmpty(list)) {
            models = list.stream().map(this::toListModel).toList();
        }
        model.setList(models);
        return ResultContent.buildSuccess(model);
    }

    public ResultContent changeState(String id, DataState state) {
        ProjectDict entity = projectDictDao.findTopById(id);
        if (ObjectUtils.isEmpty(entity)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        entity.setState(state);
        projectDictDao.save(entity);
        return ResultContent.buildSuccess();
    }

    private ProjectDictModel toModel(ProjectDict entity) {
        ProjectDictModel model = new ProjectDictModel();
        if (ObjectUtils.isNotEmpty(entity)) {
            BeanUtils.copyProperties(entity, model);
        }
        return model;
    }

    public ProjectDictAboutListModel toAboutListModel(ProjectDict entity) {
        ProjectDictAboutListModel model = null;
        if (ObjectUtils.isNotEmpty(entity)) {
            model = new ProjectDictAboutListModel();
            BeanUtils.copyProperties(entity, model);

            // 子项
            List<ProjectDictList> list = projectDictListDao.findByProjectDictOrderBySortAsc(entity);
            List<ProjectDictListParam> models = new ArrayList<>();
            if (ObjectUtils.isNotEmpty(list)) {
                models = list.stream().map(this::toListModel).toList();
            }
            model.setList(models);
        }
        return model;
    }

    public ProjectDictListParam toListModel(ProjectDictList entity) {
        ProjectDictListParam model = null;
        if (ObjectUtils.isNotEmpty(entity)) {
            model = new ProjectDictListParam();
            BeanUtils.copyProperties(entity, model);
        }
        return model;
    }

}
