package com.docus.server.sys.service.impl;

import com.docus.core.util.Func;
import com.docus.infrastructure.redis.service.IdService;
import com.docus.server.sys.common.pojo.dto.DeptDTO;
import com.docus.server.sys.common.pojo.dto.DeptModifyParam;
import com.docus.server.sys.common.pojo.entity.PowerDept;
import com.docus.server.sys.infrastructure.dao.IPowerDeptDao;
import com.docus.server.sys.service.IPowerDeptService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 科室服务实现
 *
 * @author wyb
 */
@Service
public class PowerDeptServiceImpl implements IPowerDeptService {
    @Resource
    private IdService idService;
    @Resource
    private IPowerDeptDao powerDeptDao;

    @Transactional
    @Override
    public void batchInsertOrUpdatePowerDept(List<DeptDTO> deptDTOList) {
        deptDTOList.forEach(this::insertOrUpdatePowerDept);
    }

    @Override
    public PowerDept findByDeptCode(String deptId) {
        return powerDeptDao.findOneBy("deptCode", deptId);
    }

    @Transactional
    @Override
    public boolean insertOrUpdatePowerDept(DeptDTO deptDTO) {
        PowerDept powerDept = powerDeptDao.getDeptByDeptCode(deptDTO.getDeptCode());
        DeptModifyParam deptModifyParam = deptDTO.transDeptAddParam();
        if (Func.isEmpty(powerDept)) {
            long deptId = idService.getDateSeq();
            deptModifyParam.setDeptId(deptId);
            powerDeptDao.addDept(deptModifyParam);
            return true;
        }
        deptModifyParam.setDeptId(powerDept.getDeptId());
        powerDeptDao.updateDept(deptModifyParam);
        return true;
    }

    @Transactional
    @Override
    public boolean delDeptByDeptCode(String deptCode) {
        PowerDept powerDept = powerDeptDao.getDeptByDeptCode(deptCode);
        if (Func.isEmpty(powerDept)) {
            return true;
        }
        powerDeptDao.delDeptByDeptCode(deptCode);
        return true;
    }
}
