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.projectAbout.ProjectExcelTemplateModel;
import com.zhongshu.card.client.model.projectAbout.ProjectExcelTemplateParam;
import com.zhongshu.card.client.model.projectAbout.ProjectExcelTemplateQuery;
import com.zhongshu.card.client.model.projectAbout.ProjectExcelTemplateSearch;
import com.zhongshu.card.client.model.school.CardInfoPoolModel;
import com.zhongshu.card.client.model.school.CardInfoPoolSearch;
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
import com.zhongshu.card.server.core.dao.projectAbout.ProjectExcelTemplateDao;
import com.zhongshu.card.server.core.domain.org.Organization;
import com.zhongshu.card.server.core.domain.projectAbout.ProjectExcelTemplate;
import com.zhongshu.card.server.core.domain.school.CardInfoPool;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

/**
 * 项目的 关联的excel模型
 *
 * @author TRX
 * @date 2024/9/24
 */
@Slf4j
@Service
public class ProjectExcelTemplateService extends SuperService {

    @Autowired
    OrganizationDao organizationDao;

    @Autowired
    ProjectExcelTemplateDao projectExcelTemplateDao;

    /**
     * 保存信息
     *
     * @param param
     * @return
     */
    public ResultContent saveExcelTemplate(ProjectExcelTemplateParam param) {
        String mark = param.getMark();
        ProjectExcelTemplate entity = projectExcelTemplateDao.findTopByMark(mark);
        if (ObjectUtils.isEmpty(entity)) {
            entity = new ProjectExcelTemplate();
            entity.setIsDelete(Boolean.FALSE);
            initEntityNoOid(entity);
        }
        BeanUtils.copyProperties(param, entity);
        projectExcelTemplateDao.save(entity);
        return ResultContent.buildSuccess();
    }

    /**
     * 分页列表
     *
     * @param param
     * @param pageable
     * @return
     */
    public ResultContent<Page<ProjectExcelTemplateModel>> page(ProjectExcelTemplateSearch param, Pageable pageable) {
        Page<ProjectExcelTemplate> page = projectExcelTemplateDao.page(pageable, param);
        return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toModel));
    }

    public ResultContent deleteData(String id) {
        ProjectExcelTemplate entity = projectExcelTemplateDao.findTopById(id);
        if (ObjectUtils.isEmpty(entity)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        projectExcelTemplateDao.delete(entity);
        return ResultContent.buildSuccess();
    }

    public ResultContent getDetail(String id) {
        ProjectExcelTemplate entity = projectExcelTemplateDao.findTopById(id);
        if (ObjectUtils.isEmpty(entity)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        return ResultContent.buildSuccess(toModel(entity));
    }

    public ResultContent<ProjectExcelTemplateModel> getDetailByMark(ProjectExcelTemplateQuery param) {
        ProjectExcelTemplate entity = projectExcelTemplateDao.findTopByMark(param.getMark());
        if (ObjectUtils.isEmpty(entity)) {
            if (param.getIsInit() != null && param.getIsInit()) {
                ProjectExcelTemplateParam param1 = new ProjectExcelTemplateParam();
                param1.setMark(param.getMark());
                saveExcelTemplate(param1);
            }
            return ResultContent.buildFail(String.format("数据不存在：%s", param.getMark()));
        }
        return ResultContent.buildSuccess(toModel(entity));
    }

    public ProjectExcelTemplateModel toModel(ProjectExcelTemplate entity) {
        ProjectExcelTemplateModel model = null;
        if (ObjectUtils.isNotEmpty(entity)) {
            model = new ProjectExcelTemplateModel();
            BeanUtils.copyProperties(entity, model);
        }
        return model;
    }

}
