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

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.StringUtils;
import com.xinelu.common.utils.codes.GenerateSystemCodeUtil;
import com.xinelu.common.utils.regex.RegexUtil;
import com.xinelu.manage.domain.nursestationdepartment.NurseStationDepartment;
import com.xinelu.manage.mapper.nursestationdepartment.NurseStationDepartmentMapper;
import com.xinelu.manage.service.nursestationdepartment.INurseStationDepartmentService;
import com.xinelu.manage.vo.nursestationdepartment.NurseStationDepartmentVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


/**
 * 护理站部门信息Service业务层处理
 *
 * @author xinelu
 * @date 2022-09-13
 */
@Service
public class NurseStationDepartmentServiceImpl implements INurseStationDepartmentService {

    @Resource
    private NurseStationDepartmentMapper nurseStationDepartmentMapper;

    @Resource
    private GenerateSystemCodeUtil generateSystemCodeUtil;

    @Resource
    private RegexUtil regexUtil;

    /**
     * 查询护理站部门信息
     *
     * @param id 护理站部门信息主键
     * @return 护理站部门信息
     */
    @Override
    public NurseStationDepartmentVO selectNurseStationDepartmentById(Long id) {
        return nurseStationDepartmentMapper.selectNurseStationDepartmentById(id);
    }

    /**
     * 查询护理站部门信息列表
     *
     * @param nurseStationDepartment 护理站部门信息
     * @return 护理站部门信息
     */
    @Override
    public List<NurseStationDepartmentVO> selectNurseStationDepartmentList(NurseStationDepartmentVO nurseStationDepartment) {
        return nurseStationDepartmentMapper.selectNurseStationDepartmentList(nurseStationDepartment);
    }

    /**
     * 新增护理站部门信息
     *
     * @param nurseStationDepartment 护理站部门信息
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult insertStationDepartmentList(List<NurseStationDepartment> nurseStationDepartment) {
        if (CollectionUtils.isEmpty(nurseStationDepartment)) {
            return AjaxResult.error("请添加护理站部门信息！");
        }
        //插如数据
        for (NurseStationDepartment stationDepartment : nurseStationDepartment) {
            int nurseItemTemplateIds = nurseStationDepartmentMapper.getDepartmentName(stationDepartment.getNurseStationId(), stationDepartment.getDepartmentName());
            if (nurseItemTemplateIds > 0) {
                return AjaxResult.error(stationDepartment.getDepartmentName() + "--名称重复!");
            }
            //校验手机号
            if (StringUtils.isNotBlank(stationDepartment.getPhone())) {
                boolean regexPhone = regexUtil.regexPhone(stationDepartment.getPhone());
                if (BooleanUtils.isFalse(regexPhone)) {
                    return AjaxResult.error(stationDepartment.getPhone() + "--手机号格式不正确!");
                }
                int nurseItemTemplatePhone = nurseStationDepartmentMapper.getDepartmentPhone(stationDepartment.getPhone());
                if (nurseItemTemplatePhone > 0) {
                    return AjaxResult.error(stationDepartment.getPhone() + "--手机号重复!");
                }
            }
            stationDepartment.setCreateTime(LocalDateTime.now());
            stationDepartment.setCreateBy(SecurityUtils.getUsername());
            //编号生成
            stationDepartment.setDepartmentCode(Constants.NURSE_STATION_DEPARTMENT
                    + generateSystemCodeUtil.generateDepartCode(Constants.NURSE_STATION_DEPARTMENT_PREFIX));
        }
        //批量新增
        int departmentList = nurseStationDepartmentMapper.insertNurseStationDepartmentList(nurseStationDepartment);
        if (departmentList <= 0) {
            throw new ServiceException("新增护理站部门信息失败，请联系管理员！");
        }
        return AjaxResult.success();
    }

    /**
     * 修改护理站部门信息
     *
     * @param nurseStationDepartment 护理站部门信息
     * @return 结果
     */
    @Override
    public AjaxResult updateNurseStationDepartment(NurseStationDepartment nurseStationDepartment) {
        NurseStationDepartmentVO nurseStationDepartmentVO = nurseStationDepartmentMapper.selectNurseStationDepartmentById(nurseStationDepartment.getId());
        if (!nurseStationDepartment.getDepartmentName().equals(nurseStationDepartmentVO.getDepartmentName())) {
            int nurseItemTemplateIds = nurseStationDepartmentMapper.getDepartmentName(nurseStationDepartment.getNurseStationId(), nurseStationDepartment.getDepartmentName());
            if (nurseItemTemplateIds > 0) {
                return AjaxResult.error(nurseStationDepartment.getDepartmentName() + "--名称重复!");
            }
        }
        //校验手机号
        if (StringUtils.isNotBlank(nurseStationDepartment.getPhone())) {
            boolean regexPhone = regexUtil.regexPhone(nurseStationDepartment.getPhone());
            if (BooleanUtils.isFalse(regexPhone)) {
                return AjaxResult.error(nurseStationDepartment.getPhone() + "--手机号格式不正确!");
            }
            if (!nurseStationDepartment.getPhone().equals(nurseStationDepartmentVO.getPhone())) {
                int nurseItemTemplateId = nurseStationDepartmentMapper.getDepartmentPhone(nurseStationDepartment.getPhone());
                if (nurseItemTemplateId > 0) {
                    return AjaxResult.error(nurseStationDepartment.getPhone() + "--手机号重复!");
                }
            }
        }
        nurseStationDepartment.setUpdateTime(LocalDateTime.now());
        nurseStationDepartment.setUpdateBy(SecurityUtils.getUsername());
        //修改
        int department = nurseStationDepartmentMapper.updateNurseStationDepartment(nurseStationDepartment);
        if (department <= 0) {
            throw new ServiceException("修改护理站部门信息失败，请联系管理员！");
        }
        return AjaxResult.success();
    }

    /**
     * 批量删除护理站部门信息
     *
     * @param ids 需要删除的护理站部门信息主键
     * @return 结果
     */
    @Override
    public int deleteNurseStationDepartmentByIds(Long[] ids) {
        return nurseStationDepartmentMapper.deleteNurseStationDepartmentByIds(ids);
    }
}