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

import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.common.utils.bean.BeanUtils;
import com.xinelu.manage.domain.specialdiseasenode.SpecialDiseaseNode;
import com.xinelu.manage.domain.specialdiseaseroute.SpecialDiseaseRoute;
import com.xinelu.manage.mapper.specialdiseasenode.SpecialDiseaseNodeMapper;
import com.xinelu.manage.mapper.specialdiseaseroute.SpecialDiseaseRouteMapper;
import com.xinelu.manage.service.specialdiseasenode.ISpecialDiseaseNodeService;
import com.xinelu.manage.vo.specialdiseasenode.SpecialDiseaseNodeVO;
import com.xinelu.manage.vo.specialdiseaseroute.SpecialDiseaseRouteVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 专病路径-管理节点信息Service业务层处理
 *
 * @author xinelu
 * @date 2024-03-13
 */
@Slf4j
@Service
public class SpecialDiseaseNodeServiceImpl implements ISpecialDiseaseNodeService {
    @Resource
    private SpecialDiseaseNodeMapper specialDiseaseNodeMapper;
    @Resource
    private SpecialDiseaseRouteMapper specialDiseaseRouteMapper;


    /**
     * 查询专病路径-管理节点信息
     *
     * @param id 专病路径-管理节点信息主键
     * @return 专病路径-管理节点信息
     */
    @Override
    public SpecialDiseaseNode selectSpecialDiseaseNodeById(Long id) {
        return specialDiseaseNodeMapper.selectSpecialDiseaseNodeById(id);
    }

    /**
     * 查询专病路径-管理节点信息列表
     *
     * @param specialDiseaseNode 专病路径-管理节点信息
     * @return 专病路径-管理节点信息
     */
    @Override
    public List<SpecialDiseaseNode> selectSpecialDiseaseNodeList(SpecialDiseaseNode specialDiseaseNode) {
        return specialDiseaseNodeMapper.selectSpecialDiseaseNodeList(specialDiseaseNode);
    }

    /**
     * 新增专病路径-管理节点信息
     *
     * @param specialDiseaseNode 专病路径-管理节点信息
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult insertSpecialDiseaseNode(SpecialDiseaseRouteVO specialDiseaseNode) {
        SpecialDiseaseRoute specialDiseaseRoute = new SpecialDiseaseRoute();
        BeanUtils.copyBeanProp(specialDiseaseRoute, specialDiseaseNode);
        specialDiseaseRoute.setCreateTime(LocalDateTime.now());
        specialDiseaseRoute.setCreateBy(SecurityUtils.getUsername());
        int insertRouteCount = specialDiseaseRouteMapper.insertSpecialDiseaseRoute(specialDiseaseRoute);
        if (insertRouteCount < 0) {
            throw new ServiceException("新增专病路径管理节点信息失败");
        }
        for (SpecialDiseaseNode diseaseNode : specialDiseaseNode.getSpecialDiseaseNodeList()) {
            diseaseNode.setRouteId(specialDiseaseRoute.getId());
            diseaseNode.setCreateTime(LocalDateTime.now());
            diseaseNode.setCreateBy(SecurityUtils.getUsername());
        }
        int insertNodeCount = specialDiseaseNodeMapper.insertSpecialDiseaseNodeList(specialDiseaseNode.getSpecialDiseaseNodeList());
        if (insertNodeCount < 0) {
            throw new ServiceException("新增专病路径管理节点信息失败");
        }
        return AjaxResult.success();
    }

    /**
     * 修改专病路径-管理节点信息
     *
     * @param specialDiseaseNode 专病路径-管理节点信息
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult updateSpecialDiseaseNode(SpecialDiseaseRouteVO specialDiseaseNode) {
        SpecialDiseaseRoute specialDiseaseRoute = new SpecialDiseaseRoute();
        BeanUtils.copyBeanProp(specialDiseaseRoute, specialDiseaseNode);
        specialDiseaseRoute.setUpdateTime(LocalDateTime.now());
        specialDiseaseRoute.setUpdateBy(SecurityUtils.getUsername());
        specialDiseaseRoute.setId(specialDiseaseNode.getSpecialDiseaseRouteId());
        int updateRouteCount = specialDiseaseRouteMapper.updateSpecialDiseaseRoute(specialDiseaseRoute);
        if (updateRouteCount < 0) {
            log.info("修改专病路径失败");
            throw new ServiceException("修改专病路径管理节点信息失败");
        }
        int deleteCount = specialDiseaseNodeMapper.deleteSpecialDiseaseNodeByRouteId(specialDiseaseRoute.getId());
        if (deleteCount < 0) {
            log.info("删除管理节点信息失败");
            throw new ServiceException("修改专病路径管理节点信息失败");
        }
        for (SpecialDiseaseNodeVO diseaseNode : specialDiseaseNode.getSpecialDiseaseNodeList()) {
            diseaseNode.setRouteId(specialDiseaseRoute.getId());
            diseaseNode.setRouteName(specialDiseaseNode.getRouteName());
            diseaseNode.setUpdateTime(LocalDateTime.now());
            diseaseNode.setUpdateBy(SecurityUtils.getUsername());
        }
        int insertNodeCount = specialDiseaseNodeMapper.insertSpecialDiseaseNodeList(specialDiseaseNode.getSpecialDiseaseNodeList());
        if (insertNodeCount < 0) {
            log.info("新增管理节点信息失败");
            throw new ServiceException("修改专病路径管理节点信息失败");
        }
        return AjaxResult.success();
    }

    /**
     * 批量删除专病路径-管理节点信息
     *
     * @param ids 需要删除的专病路径-管理节点信息主键
     * @return 结果
     */
    @Override
    public int deleteSpecialDiseaseNodeByIds(Long[] ids) {
        return specialDiseaseNodeMapper.deleteSpecialDiseaseNodeByIds(ids);
    }

    /**
     * 删除专病路径-管理节点信息信息
     *
     * @param id 专病路径-管理节点信息主键
     * @return 结果
     */
    @Override
    public int deleteSpecialDiseaseNodeById(Long id) {
        return specialDiseaseNodeMapper.deleteSpecialDiseaseNodeById(id);
    }

    /**
     * 查询专病路径-管理节点信息列表
     *
     * @param specialDiseaseRouteId 路径id
     * @return AjaxResult
     */
    @Override
    public AjaxResult selectSpecialDisease(Long specialDiseaseRouteId) {
        return AjaxResult.success(specialDiseaseNodeMapper.selectSpecialDiseaseByRouteId(specialDiseaseRouteId));
    }

    /**
     * 修改节点信息状态
     *
     * @param specialDiseaseNode 节点信息
     * @return AjaxResult
     */
    @Override
    public AjaxResult updateRouteCheckStatus(SpecialDiseaseNode specialDiseaseNode) {
        if (Objects.isNull(specialDiseaseNode) || Objects.isNull(specialDiseaseNode.getId())) {
            return AjaxResult.error("请选择需要审核的信息！");
        }
        specialDiseaseNode.setRouteCheckDate(LocalDateTime.now());
        return AjaxResult.success(specialDiseaseNodeMapper.updateSpecialDiseaseNode(specialDiseaseNode));
    }
}
