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

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.impl.AbstractCRUDCacheServiceImpl;
import com.mortals.xhx.module.dept.dao.DeptDao;
import com.mortals.xhx.module.dept.model.DeptEntity;
import com.mortals.xhx.module.dept.service.DeptService;
import com.mortals.xhx.module.site.service.SiteMatterService;
import com.mortals.xhx.module.site.service.SiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * DeptService
 * 部门 service实现
 *
 * @author zxfei
 * @date 2022-01-12
 */
@Service("deptService")
public class DeptServiceImpl extends AbstractCRUDCacheServiceImpl<DeptDao, DeptEntity, Long> implements DeptService {

    @Autowired
    private SiteService siteService;

    @Autowired
    private SiteMatterService siteMatterService;


    @Override
    protected String getExtKey(DeptEntity data) {
        return data.getDeptNumber();
    }

    /**
     * @param entity
     * @param context
     * @throws AppException
     */
    @Override
    protected void saveBefore(DeptEntity entity, Context context) throws AppException {
        super.saveBefore(entity, context);
        //新增校验部门编码是否重复
        DeptEntity extCache = this.getExtCache(entity.getDeptNumber());
        if (!ObjectUtils.isEmpty(extCache)) {
            throw new AppException("部门编码重复!deptCode:" + extCache.getDeptNumber());
        }
    }

    /**
     * @param entity
     * @param context
     * @throws AppException
     */
    @Override
    protected void updateBefore(DeptEntity entity, Context context) throws AppException {
        super.updateBefore(entity, context);
        DeptEntity beforeEntity = this.get(entity.getId(), context);
        if (!beforeEntity.getDeptNumber().equals(entity.getDeptNumber())) {
            DeptEntity extCache = this.getExtCache(entity.getDeptNumber());
            if (!ObjectUtils.isEmpty(extCache)) {
                throw new AppException("部门编码重复!deptCode:" + extCache.getDeptNumber());
            }
        }
    }

}