package com.mortals.xhx.module.datum.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.mortals.xhx.module.datum.model.DatumCategoryQuery;
import com.mortals.xhx.module.matter.model.MatterCategoryEntity;
import com.mortals.xhx.module.matter.service.MatterCategoryService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.datum.dao.DatumCategoryDao;
import com.mortals.xhx.module.datum.model.DatumCategoryEntity;
import com.mortals.xhx.module.datum.service.DatumCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

/**
 * DatumCategoryService
 * 材料分类 service实现
 *
 * @author zxfei
 * @date 2023-06-21
 */
@Service("datumCategoryService")
@Slf4j
public class DatumCategoryServiceImpl extends AbstractCRUDServiceImpl<DatumCategoryDao, DatumCategoryEntity, Long> implements DatumCategoryService {

    @Autowired
    private MatterCategoryService matterCategoryService;


    @Override
    protected void validData(DatumCategoryEntity entity, Context context) throws AppException {
        updateCategoryName(entity);
        log.info("validData:{}", JSONObject.toJSONString(entity));
    }

    @Override
    protected void saveBefore(DatumCategoryEntity entity, Context context) throws AppException {
        //判读添加的材料 是否已经存在

        super.saveBefore(entity, context);
    }

    private void updateCategoryName(DatumCategoryEntity entity) {
        if (!ObjectUtils.isEmpty(entity.getCategoryId())) {
            MatterCategoryEntity matterCategoryEntity = matterCategoryService.get(entity.getCategoryId());
            if (!ObjectUtils.isEmpty(matterCategoryEntity)) {
                entity.setCategoryName(matterCategoryEntity.getCategoryName());
            }
        }
    }

}