package com.ruoyi.care.service.impl;

import java.util.List;

import com.ruoyi.care.domain.CareAssignments;
import com.ruoyi.care.domain.CareRecords;
import com.ruoyi.care.mapper.CareAssignmentsMapper;
import com.ruoyi.care.mapper.CareRecordsMapper;
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;

    @Autowired
    private CareRecordsMapper careRecordsMapper;
    /**
     * 查询护理申请
     * 
     * @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, Long userId) {
        int result=0;
        CareAssignments careAssignments=new CareAssignments();
        careApplicationsMapper.updateCareApplications(careApplications);
        //获取当前节点记录
        CareRecords curr=new CareRecords();
        curr.setApplyId(careApplications.getApplyId());
        curr.setState("0");
        List<CareRecords> lst=careRecordsMapper.selectCareRecordsList(curr);
        if(lst.size()>0){
            curr=lst.get(0);
            curr.setState("1");
            curr.setIsAgree("1");
            curr.setOperate(userId.toString());
            curr.setUpdateTime(DateUtils.getNowDate());
            curr.setUpdateBy(userId.toString());
            if(careApplications.getStatus().equals("wait_assign_nurse")){//如果下一步为指派人员，当前则为指派公司
                curr.setRemark(careApplications.getAssignRemark());
            }else if(careApplications.getStatus().equals("assigned")){//如果下一步为已完成，当前则为指派人员
                curr.setRemark(careApplications.getAssignNurseRemark());
            }
            careRecordsMapper.updateCareRecords(curr);
        }

        //保存下一个记录
        CareRecords careRecords2=new CareRecords();
        careRecords2.setState("0");//待完成
        careRecords2.setParentId(curr.getRecordId());
        careRecords2.setCreateTime(DateUtils.getNowDate());
        careRecords2.setCreateBy(userId.toString());
        careRecords2.setApplyId(careApplications.getApplyId());


        if(careApplications.getStatus().equals("wait_assign_nurse")){//如果下一步为指派人员，当前则为指派公司
            careRecords2.setNodeTitle("指派护理人员");
        }else if(careApplications.getStatus().equals("assigned")){//如果下一步为已完成，当前则为指派人员
            careRecords2.setNodeTitle("护理打卡");
        }
        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);
        }

        careRecordsMapper.insertCareRecords(careRecords2);
        return result;
    }

    @Override
    @Transactional
    public int start(CareApplications careApplications,Long userId) {
        //保存流程记录
        CareRecords careRecords=new CareRecords();
        careRecords.setNodeTitle("资料提交");
        careRecords.setOperate(userId.toString());
        careRecords.setState("1");//已完成
        careRecords.setIsAgree("1");//同意
        careRecords.setCreateTime(DateUtils.getNowDate());
        careRecords.setCreateBy(userId.toString());
        careRecords.setRemark("提交成功");
        careRecords.setApplyId(careApplications.getApplyId());
        careRecordsMapper.insertCareRecords(careRecords);

        //保存下一个节点信息
        CareRecords careRecords2=new CareRecords();
        careRecords2.setNodeTitle("资料审核");
        careRecords2.setState("0");//待完成
        careRecords2.setParentId(careRecords.getRecordId());
        careRecords2.setCreateTime(DateUtils.getNowDate());
        careRecords2.setCreateBy(userId.toString());
        careRecords2.setApplyId(careApplications.getApplyId());
        careRecordsMapper.insertCareRecords(careRecords2);

        careApplications.setStatus("wait_check");
        careApplications.setUpdateTime(DateUtils.getNowDate());
        return careApplicationsMapper.updateCareApplications(careApplications);
    }

    @Override
    public int check(CareApplications careApplications, Long userId) {
        //获取当前节点记录
        CareRecords curr=new CareRecords();
        curr.setApplyId(careApplications.getApplyId());
        curr.setState("0");
        List<CareRecords> lst=careRecordsMapper.selectCareRecordsList(curr);
        if(lst.size()>0){
            curr=lst.get(0);
            curr.setOperate(userId.toString());

            if(careApplications.getStatus().equals("disagree")){
                curr.setIsAgree("0");
                curr.setRemark(careApplications.getAuditReason());
                curr.setState("2");//不同意 退回
            }else{
                curr.setRemark("同意");
                curr.setIsAgree("1");
                curr.setState("1");

                //保存下一个记录
                CareRecords careRecords2=new CareRecords();
                careRecords2.setNodeTitle("指派护理公司");
                careRecords2.setState("0");//待完成
                careRecords2.setParentId(curr.getRecordId());
                careRecords2.setCreateTime(DateUtils.getNowDate());
                careRecords2.setCreateBy(userId.toString());
                careRecords2.setApplyId(careApplications.getApplyId());
                careRecordsMapper.insertCareRecords(careRecords2);
            }
            curr.setUpdateTime(DateUtils.getNowDate());
            curr.setUpdateBy(userId.toString());
            careRecordsMapper.updateCareRecords(curr);
        }

        careApplications.setUpdateTime(DateUtils.getNowDate());
        return careApplicationsMapper.updateCareApplications(careApplications);
    }
}
