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

import com.docus.core.util.Func;
import com.docus.infrastructure.redis.service.IdService;
import com.docus.server.collection.dto.DeptDto;
import com.docus.server.collection.dto.DeptModifyParam;
import com.docus.server.collection.dto.SzyDeptDto;
import com.docus.server.collection.infrastructure.dao.entity.PowerDept;
import com.docus.server.collection.infrastructure.dao.mapper.PowerDeptMapper;
import com.docus.server.collection.service.IPowerDeptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

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

    @Override
    public boolean register(DeptDto deptDto) {
        PowerDept powerDept = powerDeptMapper.getDeptByDeptCode(deptDto.getDeptCode());
        DeptModifyParam deptModifyParam = deptDto.transDeptAddParam();
        if (Func.isEmpty(powerDept)) {
            long deptId = idService.getDateSeq();
            deptModifyParam.setDeptId(deptId);
            powerDeptMapper.addDept(deptModifyParam);
            return true;
        }
        deptModifyParam.setDeptId(powerDept.getDeptId());
        powerDeptMapper.updateDept(deptModifyParam);
        return true;
    }

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

    @Override
    public void add(SzyDeptDto deptDto) {
        PowerDept powerDept = powerDeptMapper.getDeptByDeptCode(deptDto.getDeptCode());
        DeptModifyParam deptModifyParam =new DeptModifyParam();
        deptModifyParam.setAuthorId(deptDto.getAuthorId());
        deptModifyParam.setAuthorName(deptDto.getAuthorName());
        deptModifyParam.setDeptCode(deptDto.getDeptCode());
        deptModifyParam.setDeptName(deptDto.getDeptName());
        if (Func.isEmpty(powerDept)) {
            long deptId = idService.getDateSeq();
            deptModifyParam.setDeptId(deptId);
            powerDeptMapper.addDept(deptModifyParam);
            return;
        }
        deptModifyParam.setDeptId(powerDept.getDeptId());
        powerDeptMapper.updateDept(deptModifyParam);
    }
}
