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.consts.PowerDeptConst;
import com.docus.server.collection.dto.DeptDto;
import com.docus.server.collection.dto.DeptModifyParam;
import com.docus.server.collection.entity.PowerDept;
import com.docus.server.collection.mapper.PowerDeptMapper;
import com.docus.server.collection.service.IPowerDeptService;
import org.springframework.stereotype.Service;

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

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

    @Override
    public boolean register(DeptDto deptDto) {
        String deptCode = deptDto.getDeptCode();
        PowerDept powerDept = null;
        DeptModifyParam deptModifyParam = deptDto.transDeptAddParam();
        if (deptCode.startsWith(PowerDeptConst.WARD_DEPT_PREFIX)) {
            powerDept = powerDeptMapper.getWardDeptByDeptCode(deptCode);
        } else if (deptCode.startsWith(PowerDeptConst.MZ_DEPT_PREFIX)) {
            powerDept = powerDeptMapper.getMzDeptByDeptCode(deptCode);
        } else {
            powerDept = powerDeptMapper.getDeptByDeptCode(deptCode);
        }
        if (Objects.isNull(powerDept)) {
            long deptId = idService.getDateSeq();
            deptModifyParam.setDeptId(deptId);
            if (deptCode.startsWith(PowerDeptConst.WARD_DEPT_PREFIX)) {
                powerDeptMapper.addWardDept(deptModifyParam);
            } else if (deptCode.startsWith(PowerDeptConst.MZ_DEPT_PREFIX)) {
                powerDeptMapper.addMzDept(deptModifyParam);
            } else {
                powerDeptMapper.addDept(deptModifyParam);
            }
            return true;
        }

        deptModifyParam.setDeptId(powerDept.getDeptId());

        if (deptCode.startsWith(PowerDeptConst.WARD_DEPT_PREFIX)) {
            powerDeptMapper.updateWardDept(deptModifyParam);
        } else if (deptCode.startsWith(PowerDeptConst.MZ_DEPT_PREFIX)) {
            powerDeptMapper.updateMzDept(deptModifyParam);
        } else {
            powerDeptMapper.updateDept(deptModifyParam);
        }


        return true;
    }

    @Override
    public void delDeptByDeptCode(String deptCode) {
        if (deptCode.startsWith(PowerDeptConst.WARD_DEPT_PREFIX)) {
            powerDeptMapper.delWardDeptByDeptCode(deptCode);
        } else if (deptCode.startsWith(PowerDeptConst.MZ_DEPT_PREFIX)) {
            powerDeptMapper.delMzDeptByDeptCode(deptCode);
        } else {
            powerDeptMapper.delDeptByDeptCode(deptCode);
        }
    }
}
