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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.mapper.PowerDeptMapper;
import com.docus.server.sys.service.IPowerDeptService;
import org.springframework.stereotype.Service;

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

/**
 * 科室服务实现
 *
 * @author wyb
 */
@Service
public class PowerDeptServiceImpl extends ServiceImpl<PowerDeptMapper, PowerDept> implements IPowerDeptService {
    @Resource
    private IdService idService;

    @Override
    public void batchSaveOrUpdatePowerDept(List<DeptDTO> deptDTOList) {
        deptDTOList.forEach(this::saveOrUpdatePowerDept);
    }

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

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