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

import com.xinelu.common.constant.Constants;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.enums.ReleaseStatusEnum;
import com.xinelu.common.enums.RouteCheckStatusEnum;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.common.utils.bean.BeanUtils;
import com.xinelu.common.utils.codes.GenerateSystemCodeUtil;
import com.xinelu.manage.domain.specialdiseaseroute.SpecialDiseaseRoute;
import com.xinelu.manage.domain.specialdiseaseroutepackage.SpecialDiseaseRoutePackage;
import com.xinelu.manage.mapper.specialdiseasenode.SpecialDiseaseNodeMapper;
import com.xinelu.manage.mapper.specialdiseaseroute.SpecialDiseaseRouteMapper;
import com.xinelu.manage.mapper.specialdiseaseroutepackage.SpecialDiseaseRoutePackageMapper;
import com.xinelu.manage.mapper.specialdiseasetriggercondition.SpecialDiseaseTriggerConditionMapper;
import com.xinelu.manage.service.specialdiseaseroute.ISpecialDiseaseRouteService;
import com.xinelu.manage.vo.department.DepartmentVO;
import com.xinelu.manage.vo.specialdiseaseroute.SpecialDiseaseRouteVO;
import com.xinelu.manage.vo.specialdiseaseroutepackage.SpecialDiseaseRoutePackageVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 专病路径信息Service业务层处理
 *
 * @author xinelu
 * @date 2024-03-13
 */
@Service
public class SpecialDiseaseRouteServiceImpl implements ISpecialDiseaseRouteService {
    @Resource
    private SpecialDiseaseRouteMapper specialDiseaseRouteMapper;
    @Resource
    private SpecialDiseaseNodeMapper specialDiseaseNodeMapper;
    @Resource
    private SpecialDiseaseTriggerConditionMapper triggerConditionMapper;
    @Resource
    private GenerateSystemCodeUtil generateSystemCodeUtil;
    @Resource
    private SpecialDiseaseRoutePackageMapper specialDiseaseRoutePackageMapper;


    /**
     * 查询专病路径信息
     *
     * @param id 专病路径信息主键
     * @return 专病路径信息
     */
    @Override
    public SpecialDiseaseRouteVO selectSpecialDiseaseRouteById(Long id) {
        return specialDiseaseRouteMapper.selectSpecialDiseaseRouteAndTriggerById(id);
    }

    /**
     * 查询专病路径信息列表
     *
     * @param specialDiseaseRoute 专病路径信息
     * @return 专病路径信息
     */
    @Override
    public List<SpecialDiseaseRouteVO> selectSpecialDiseaseRouteList(SpecialDiseaseRoute specialDiseaseRoute) {
        return specialDiseaseRouteMapper.selectDiseaseRouteList(specialDiseaseRoute);
    }

    /**
     * 新增专病路径信息
     *
     * @param specialDiseaseRoute 专病路径信息
     * @return 结果
     */
    @Override
    public AjaxResult insertSpecialDiseaseRoute(SpecialDiseaseRouteVO specialDiseaseRoute) {
        specialDiseaseRoute.setCreateTime(LocalDateTime.now());
        specialDiseaseRoute.setCreateBy(SecurityUtils.getUsername());
        specialDiseaseRoute.setRouteCode(Constants.ROUTE_CODE + generateSystemCodeUtil.generateSystemCode(Constants.ROUTE_CODE));
        int insertRouteCount = specialDiseaseRouteMapper.insertSpecialDiseaseRoute(specialDiseaseRoute);
        if (insertRouteCount < 0) {
            return AjaxResult.error("新增专病路径信息失败，请联系管理员！");
        }
        List<SpecialDiseaseRoutePackage> specialDiseaseRoutePackages = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(specialDiseaseRoute.getRoutePackageList())) {
            for (SpecialDiseaseRoutePackage specialDiseaseRoutePackage : specialDiseaseRoute.getRoutePackageList()) {
                SpecialDiseaseRoutePackage routePackage = new SpecialDiseaseRoutePackage();
                specialDiseaseRoutePackage.setRouteId(specialDiseaseRoute.getId());
                specialDiseaseRoutePackage.setRouteName(specialDiseaseRoute.getRouteName());
                specialDiseaseRoutePackage.setCreateTime(LocalDateTime.now());
                specialDiseaseRoutePackage.setCreateBy(SecurityUtils.getUsername());
                BeanUtils.copyBeanProp(routePackage, specialDiseaseRoutePackage);
                specialDiseaseRoutePackages.add(routePackage);
            }
            int insertRoutePackageCount = specialDiseaseRoutePackageMapper.insertSpecialDiseaseRoutePackageList(specialDiseaseRoutePackages);
            if (insertRoutePackageCount < 0) {
                return AjaxResult.error("新增专病路径信息失败，请联系管理员！");
            }
        }
        return AjaxResult.success();
    }

    /**
     * 修改专病路径信息
     *
     * @param specialDiseaseRoute 专病路径信息
     * @return 结果
     */
    @Override
    public AjaxResult updateSpecialDiseaseRoute(SpecialDiseaseRouteVO specialDiseaseRoute) {
        int deleteRoutePackageCount = specialDiseaseRoutePackageMapper.deleteSpecialDiseaseRoutePackageByRouteId(specialDiseaseRoute.getId());
        if (deleteRoutePackageCount < 0) {
            return AjaxResult.error("修改专病路径信息失败，请联系管理员！");
        }
        specialDiseaseRoute.setUpdateTime(LocalDateTime.now());
        specialDiseaseRoute.setUpdateBy(SecurityUtils.getUsername());
        int updateRouteCount = specialDiseaseRouteMapper.updateSpecialDiseaseRoute(specialDiseaseRoute);
        if (updateRouteCount < 0) {
            return AjaxResult.error("修改专病路径信息失败，请联系管理员！");
        }
        List<SpecialDiseaseRoutePackage> specialDiseaseRoutePackages = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(specialDiseaseRoute.getRoutePackageList())) {
            for (SpecialDiseaseRoutePackageVO specialDiseaseRoutePackage : specialDiseaseRoute.getRoutePackageList()) {
                SpecialDiseaseRoutePackage routePackage = new SpecialDiseaseRoutePackage();
                specialDiseaseRoutePackage.setRouteId(specialDiseaseRoute.getId());
                specialDiseaseRoutePackage.setRouteName(specialDiseaseRoute.getRouteName());
                specialDiseaseRoutePackage.setUpdateTime(LocalDateTime.now());
                specialDiseaseRoutePackage.setUpdateBy(SecurityUtils.getUsername());
                BeanUtils.copyBeanProp(routePackage, specialDiseaseRoutePackage);
                specialDiseaseRoutePackages.add(routePackage);
            }
            int insertRoutePackageCount = specialDiseaseRoutePackageMapper.insertSpecialDiseaseRoutePackageList(specialDiseaseRoutePackages);
            if (insertRoutePackageCount < 0) {
                return AjaxResult.error("修改专病路径信息失败，请联系管理员！");
            }
        }
        return AjaxResult.success();
    }

    /**
     * 批量删除专病路径信息
     *
     * @param ids 需要删除的专病路径信息主键
     * @return 结果
     */
    @Override
    public int deleteSpecialDiseaseRouteByIds(Long[] ids) {
        return specialDiseaseRouteMapper.deleteSpecialDiseaseRouteByIds(ids);
    }

    /**
     * 删除专病路径信息信息
     *
     * @param id 专病路径信息主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult deleteSpecialDiseaseRouteById(Long id) {
        int deleteRouteCount = specialDiseaseRouteMapper.deleteSpecialDiseaseRouteById(id);
        if (deleteRouteCount < 0) {
            throw new ServiceException("删除专病路径管理信息失败，请联系管理员！");
        }
        int deleteNodeCount = specialDiseaseNodeMapper.deleteSpecialDiseaseNodeByRouteId(id);
        if (deleteNodeCount < 0) {
            throw new ServiceException("删除专病路径管理信息失败，请联系管理员！");
        }
        int deleteTriggerConditionCount = triggerConditionMapper.deleteSpecialDiseaseTriggerConditionByRouteId(id);
        if (deleteTriggerConditionCount < 0) {
            throw new ServiceException("删除专病路径管理信息失败，请联系管理员！");
        }
        int deleteRoutePackageCount = specialDiseaseRoutePackageMapper.deleteSpecialDiseaseRoutePackageByRouteId(id);
        if (deleteRoutePackageCount < 0) {
            return AjaxResult.error("删除专病路径管理信息失败，请联系管理员！");
        }
        return AjaxResult.success();
    }

    /**
     * 科室路径数量
     *
     * @param departmentName 科室名称
     * @return AjaxResult
     */
    @Override
    public AjaxResult departmentRouteCount(String departmentName) {
        DepartmentVO departmentVO = new DepartmentVO();
        List<DepartmentVO> department = new ArrayList<>();
        departmentVO.setDepartmentName("全部");
        departmentVO.setCountNum(0);
        List<DepartmentVO> departmentVOS = specialDiseaseRouteMapper.departmentRouteByDepartmentName(departmentName);
        if (CollectionUtils.isNotEmpty(departmentVOS)) {
            Integer result = departmentVOS.stream().mapToInt(DepartmentVO::getCountNum).sum();
            departmentVO.setCountNum(result);
            department.add(departmentVO);
            department.addAll(departmentVOS);
        }
        return AjaxResult.success(department);
    }

    /**
     * 专病路径发布
     *
     * @param specialDiseaseRoute 路径信息
     * @return AjaxResult
     */
    @Override
    public AjaxResult editReleaseStatus(SpecialDiseaseRoute specialDiseaseRoute) {
        if (Objects.isNull(specialDiseaseRoute) || StringUtils.isBlank(specialDiseaseRoute.getReleaseStatus())) {
            return AjaxResult.error("请选择需要发布专病路径的信息！");
        }
        if (ReleaseStatusEnum.PUBLISHED.getInfo().equals(specialDiseaseRoute.getReleaseStatus())) {
            int count = specialDiseaseRouteMapper.selectRouteCheckStatus(specialDiseaseRoute.getId(), RouteCheckStatusEnum.AGREE.getInfo());
            if (count > 0) {
                return AjaxResult.error("该路径存在未审核的节点，请全部审核后发布！");
            }
        }
        specialDiseaseRoute.setUpdateTime(LocalDateTime.now());
        specialDiseaseRoute.setUpdateBy(SecurityUtils.getUsername());
        return AjaxResult.success(specialDiseaseRouteMapper.updateSpecialDiseaseRoute(specialDiseaseRoute));
    }
}
