package com.ruoyi.care.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.care.domain.CareApplications;
import com.ruoyi.care.domain.CareNurseRecords;
import com.ruoyi.care.mapper.CareApplicationsMapper;
import com.ruoyi.care.mapper.CareNurseRecordsMapper;
import com.ruoyi.common.enums.CareApplyStatus;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.care.mapper.CareNurseClocksMapper;
import com.ruoyi.care.domain.CareNurseClocks;
import com.ruoyi.care.service.ICareNurseClocksService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理任务打卡Service业务层处理
 * 
 * @author qy
 * @date 2024-11-26
 */
@Service
public class CareNurseClocksServiceImpl implements ICareNurseClocksService 
{
    @Autowired
    private CareNurseClocksMapper careNurseClocksMapper;
    @Autowired
    private CareApplicationsMapper careApplicationsMapper;
    @Autowired
    private CareNurseRecordsMapper careNurseRecordsMapper;
    /**
     * 查询护理任务打卡
     * 
     * @param clockId 护理任务打卡主键
     * @return 护理任务打卡
     */
    @Override
    public CareNurseClocks selectCareNurseClocksByClockId(Long clockId)
    {
        return careNurseClocksMapper.selectCareNurseClocksByClockId(clockId);
    }

    /**
     * 查询护理任务打卡列表
     * 
     * @param careNurseClocks 护理任务打卡
     * @return 护理任务打卡
     */
    @Override
    public List<CareNurseClocks> selectCareNurseClocksList(CareNurseClocks careNurseClocks)
    {
        return careNurseClocksMapper.selectCareNurseClocksList(careNurseClocks);
    }

    /**
     * 新增护理任务打卡
     * 
     * @param careNurseClocks 护理任务打卡
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCareNurseClocks(CareNurseClocks careNurseClocks)
    {
        careNurseClocks.setCreateTime(DateUtils.getNowDate());
        // 判断当天任务是否一开始
        String  date=careNurseClocks.getNursingTime().substring(0,10);
        CareNurseRecords careNurseRecords= new CareNurseRecords();
        careNurseRecords.setNursingTime(date);
        careNurseRecords.setApplyId(Long.parseLong(careNurseClocks.getApplyId()));
        List<CareNurseRecords> lst=careNurseRecordsMapper.selectCareNurseRecordsList(careNurseRecords);
        if(lst.size()>0){
           //已经开始了直接新增打卡记录
           return careNurseClocksMapper.insertCareNurseClocks(careNurseClocks);
        }else{
          //修改申请状态为正在进行，并添加护理记录级打卡记录
           CareApplications careApplications=careApplicationsMapper.selectCareApplicationsByApplyId(Long.parseLong(careNurseClocks.getApplyId()));
           careApplications.setDealStatus("in_progress");
           careApplicationsMapper.updateCareApplications(careApplications);

           careNurseRecords=new CareNurseRecords();
           careNurseRecords.setNursingTime(careNurseClocks.getNursingTime().substring(0,10));
           careNurseRecords.setApplyId(Long.parseLong(careNurseClocks.getApplyId()));
           careNurseRecordsMapper.insertCareNurseRecords(careNurseRecords);

           return careNurseClocksMapper.insertCareNurseClocks(careNurseClocks);
        }

    }

    /**
     * 修改护理任务打卡
     * 
     * @param careNurseClocks 护理任务打卡
     * @return 结果
     */
    @Override
    public int updateCareNurseClocks(CareNurseClocks careNurseClocks)
    {
        careNurseClocks.setUpdateTime(DateUtils.getNowDate());
        return careNurseClocksMapper.updateCareNurseClocks(careNurseClocks);
    }

    /**
     * 批量删除护理任务打卡
     * 
     * @param clockIds 需要删除的护理任务打卡主键
     * @return 结果
     */
    @Override
    public int deleteCareNurseClocksByClockIds(Long[] clockIds)
    {
        return careNurseClocksMapper.deleteCareNurseClocksByClockIds(clockIds);
    }

    /**
     * 删除护理任务打卡信息
     * 
     * @param clockId 护理任务打卡主键
     * @return 结果
     */
    @Override
    public int deleteCareNurseClocksByClockId(Long clockId)
    {
        return careNurseClocksMapper.deleteCareNurseClocksByClockId(clockId);
    }
}
