package com.xinelu.manage.service.hospitaldepartmentinfo.impl;

import com.xinelu.common.annotation.DataScope;
import com.xinelu.common.constant.Constants;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.common.utils.codes.GenerateSystemCodeUtil;
import com.xinelu.common.utils.regex.RegexUtil;
import com.xinelu.manage.dto.hospitaldepartmentinfo.HospitalDepartmentDTO;
import com.xinelu.manage.domain.hospitaldepartmentinfo.HospitalDepartmentInfo;
import com.xinelu.manage.mapper.hospitaldepartmentinfo.HospitalDepartmentInfoMapper;
import com.xinelu.manage.service.hospitaldepartmentinfo.IHospitalDepartmentInfoService;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 科室信息管理Service业务层处理
 *
 * @author zh
 * @date 2023-02-13
 */
@Service
public class HospitalDepartmentInfoServiceImpl implements IHospitalDepartmentInfoService {
    @Resource
    private HospitalDepartmentInfoMapper hospitalDepartmentInfoMapper;
    @Resource
    private GenerateSystemCodeUtil generateSystemCodeUtil;
    @Resource
    private RegexUtil regexUtil;

    /**
     * 查询科室信息管理
     *
     * @param id 科室信息管理主键
     * @return 科室信息管理
     */
    @Override
    public HospitalDepartmentInfo selectHospitalDepartmentInfoById(Long id) {
        return hospitalDepartmentInfoMapper.selectHospitalDepartmentInfoById(id);
    }

    /**
     * 查询科室信息管理列表
     *
     * @param hospitalDepartmentInfo 科室信息管理
     * @return 科室信息管理
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<HospitalDepartmentInfo> selectHospitalDepartmentInfoList(HospitalDepartmentInfo hospitalDepartmentInfo) {
        return hospitalDepartmentInfoMapper.selectHospitalDepartmentInfoList(hospitalDepartmentInfo);
    }

    /**
     * 新增科室信息管理
     *
     * @param hospitalDepartment 科室信息管理信息集合
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult insertHospitalDepartmentInfo(HospitalDepartmentDTO hospitalDepartment) {
        //校验同一个医院下的科室名称是否重复
        List<HospitalDepartmentInfo> discountDepartmentList = hospitalDepartment.getHospitalDepartmentInfoList().stream().distinct().collect(Collectors.toList());
        List<Long> hospitalIdList = discountDepartmentList.stream().filter(item -> Objects.nonNull(item.getHospitalId())).map(HospitalDepartmentInfo::getHospitalId).collect(Collectors.toList());
        List<HospitalDepartmentInfo> departmentInfoList = hospitalDepartmentInfoMapper.getDepartmentInfoList(hospitalIdList);
        if (CollectionUtils.isNotEmpty(departmentInfoList)) {
            List<HospitalDepartmentInfo> intersectionList = new ArrayList<>(CollectionUtils.intersection(departmentInfoList, discountDepartmentList));
            if (CollectionUtils.isNotEmpty(intersectionList)) {
                HospitalDepartmentInfo departmentInfo = intersectionList.get(0);
                return AjaxResult.error("所属医院为：'" + departmentInfo.getHospitalName() + "'的'" + departmentInfo.getDepartmentName() + "'科室名称重复，请重新输入！");
            }
        }
        for (HospitalDepartmentInfo hospitalDepartmentInfo : discountDepartmentList) {
            //校验联系电话是否正确
            if (StringUtils.isNotBlank(hospitalDepartmentInfo.getDepartmentPhone())) {
                boolean phone = regexUtil.regexSeatNumber(hospitalDepartmentInfo.getDepartmentPhone());
                if (BooleanUtils.isFalse(phone)) {
                    return AjaxResult.error("您输入的科室联系电话" + hospitalDepartmentInfo.getDepartmentPhone() + "不正确，请重新输入！");
                }
            }
            //自动生成科室编号，增加创建人、时间
            hospitalDepartmentInfo.setCreateTime(LocalDateTime.now());
            hospitalDepartmentInfo.setCreateBy(SecurityUtils.getUsername());
            hospitalDepartmentInfo.setDepartmentCode(Constants.DEPARTMENT_CODE + generateSystemCodeUtil.generateSystemCode(Constants.DEPARTMENT_CODE));
        }
        int insertCount = hospitalDepartmentInfoMapper.insertHospitalDepartmentList(discountDepartmentList);
        if (insertCount <= 0) {
            throw new ServiceException("新增科室信息失败，请联系管理员！");
        }
        return AjaxResult.success();
    }

    /**
     * 修改科室信息管理
     *
     * @param hospitalDepartmentInfo 科室信息管理
     * @return 结果
     */
    @Override
    public AjaxResult updateHospitalDepartmentInfo(HospitalDepartmentInfo hospitalDepartmentInfo) {
        HospitalDepartmentInfo getHospitalDepartment = hospitalDepartmentInfoMapper.selectHospitalDepartmentInfoById(hospitalDepartmentInfo.getId());
        if (Objects.isNull(getHospitalDepartment)) {
            return AjaxResult.error("当前科室信息不存在，请联系管理员！");
        }
        //校验联系电话是否正确
        if (StringUtils.isNotBlank(hospitalDepartmentInfo.getDepartmentPhone())) {
            boolean phone = regexUtil.regexSeatNumber(hospitalDepartmentInfo.getDepartmentPhone());
            if (BooleanUtils.isFalse(phone)) {
                throw new ServiceException("您输入的科室联系电话" + hospitalDepartmentInfo.getDepartmentPhone() + "不正确，请重新输入！");
            }
        }
        //校验科室名称是否重复
        if (!hospitalDepartmentInfo.getDepartmentName().equals(getHospitalDepartment.getDepartmentName())) {
            int count = hospitalDepartmentInfoMapper.selectHospitalDepartmentCount(hospitalDepartmentInfo.getHospitalId(), hospitalDepartmentInfo.getDepartmentName());
            if (count > 0) {
                return AjaxResult.error("您输入的科室名称" + hospitalDepartmentInfo.getDepartmentName() + "重复,请重新输入");
            }
        }
        hospitalDepartmentInfo.setUpdateBy(SecurityUtils.getUsername());
        hospitalDepartmentInfo.setUpdateTime(LocalDateTime.now());
        hospitalDepartmentInfoMapper.updateHospitalDepartmentInfo(hospitalDepartmentInfo);
        return AjaxResult.success();
    }

    /**
     * 批量删除科室信息管理
     *
     * @param ids 需要删除的科室信息管理主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteHospitalDepartmentInfoByIds(Long[] ids) {
        return hospitalDepartmentInfoMapper.deleteHospitalDepartmentInfoByIds(ids);
    }

    /**
     * 删除科室信息管理信息
     *
     * @param id 科室信息管理主键
     * @return 结果
     */
    @Override
    public int deleteHospitalDepartmentInfoById(Long id) {
        return hospitalDepartmentInfoMapper.deleteHospitalDepartmentInfoById(id);
    }
}
