package com.ruoyi.care.service.impl;

import java.util.List;

import com.ruoyi.care.domain.CareAssignments;
import com.ruoyi.care.mapper.CareAssignmentsMapper;
import com.ruoyi.common.enums.CareApplyStatus;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.care.mapper.CareApplicationsMapper;
import com.ruoyi.care.domain.CareApplications;
import com.ruoyi.care.service.ICareApplicationsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理申请Service业务层处理
 * 
 * @author qy
 * @date 2024-11-25
 */
@Service
public class CareApplicationsServiceImpl implements ICareApplicationsService 
{
    @Autowired
    private CareApplicationsMapper careApplicationsMapper;

    @Autowired
    private CareAssignmentsMapper careAssignmentsMapper;
    /**
     * 查询护理申请
     * 
     * @param applyId 护理申请主键
     * @return 护理申请
     */
    @Override
    public CareApplications selectCareApplicationsByApplyId(Long applyId)
    {
        return careApplicationsMapper.selectCareApplicationsByApplyId(applyId);
    }

    /**
     * 查询护理申请列表
     * 
     * @param careApplications 护理申请
     * @return 护理申请
     */
    @Override
    public List<CareApplications> selectCareApplicationsList(CareApplications careApplications)
    {
        return careApplicationsMapper.selectCareApplicationsList(careApplications);
    }

    /**
     * 新增护理申请
     * 
     * @param careApplications 护理申请
     * @return 结果
     */
    @Override
    public int insertCareApplications(CareApplications careApplications)
    {
        careApplications.setCreateTime(DateUtils.getNowDate());
        return careApplicationsMapper.insertCareApplications(careApplications);
    }

    /**
     * 修改护理申请
     * 
     * @param careApplications 护理申请
     * @return 结果
     */
    @Override
    public int updateCareApplications(CareApplications careApplications)
    {
        careApplications.setUpdateTime(DateUtils.getNowDate());
        return careApplicationsMapper.updateCareApplications(careApplications);
    }

    /**
     * 批量删除护理申请
     * 
     * @param applyIds 需要删除的护理申请主键
     * @return 结果
     */
    @Override
    public int deleteCareApplicationsByApplyIds(Long[] applyIds)
    {
        return careApplicationsMapper.deleteCareApplicationsByApplyIds(applyIds);
    }

    /**
     * 删除护理申请信息
     * 
     * @param applyId 护理申请主键
     * @return 结果
     */
    @Override
    public int deleteCareApplicationsByApplyId(Long applyId)
    {
        return careApplicationsMapper.deleteCareApplicationsByApplyId(applyId);
    }

    @Override
    @Transactional
    public int assign(CareApplications careApplications) {
        int result=0;
        CareAssignments careAssignments=new CareAssignments();
        careApplicationsMapper.updateCareApplications(careApplications);

        if(careApplications.getAssignId()==null){
            careAssignments.setApplyId(careApplications.getApplyId());
            careAssignments.setCompanyId(careApplications.getCompanyId());
            careAssignments.setNurseId(careApplications.getNurseId());
            careAssignments.setRemark(careApplications.getAssignRemark());
            careAssignments.setCreateTime(DateUtils.getNowDate());

            result=careAssignmentsMapper.insertCareAssignments(careAssignments);
        }else{
            careAssignments=careAssignmentsMapper.selectCareAssignmentsByAssignId(careApplications.getAssignId());
            careAssignments.setCompanyId(careApplications.getCompanyId());
            careAssignments.setNurseId(careApplications.getNurseId());
            careAssignments.setRemark(careApplications.getAssignRemark());
            careAssignments.setUpdateTime(DateUtils.getNowDate());

            result=careAssignmentsMapper.updateCareAssignments(careAssignments);
        }

        return result;
    }
}
