package com.kcim.dao.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcim.dao.mapper.ComputeLastProfitDateMapper;
import com.kcim.dao.model.ComputeLastProfitDate;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.Objects;

/**
 * @program: CostAccount
 * @description:
 * @author: Wang.YS
 * @create: 2024-09-04 17:59
 **/
@Repository
public class ComputeLastProfitDateRepository extends ServiceImpl<ComputeLastProfitDateMapper, ComputeLastProfitDate> {
    public ComputeLastProfitDate getLastComputeDate(Long hospId) {
        LambdaQueryWrapper<ComputeLastProfitDate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ComputeLastProfitDate::getHospId, hospId);
        return this.getOne(queryWrapper);
    }


    public void saveLastComputeDate(Long hospId,String computeDate,Long userId) {
        ComputeLastProfitDate lastComputeDate = getLastComputeDate(hospId);
        if(Objects.nonNull(lastComputeDate)){
            //当前院区已有记录 更新
            lastComputeDate.setComputeDate(computeDate);
            lastComputeDate.setUpdateTime(new Date());
            lastComputeDate.setUpdateUser(String.valueOf(userId));
            this.updateById(lastComputeDate);
        }else {
            //当前院区无记录 新增
            ComputeLastProfitDate computeLastProfitDate = new ComputeLastProfitDate();
            computeLastProfitDate.setHospId(hospId);
            computeLastProfitDate.setComputeDate(computeDate);
            computeLastProfitDate.setCreateTime(new Date());
            computeLastProfitDate.setCreateUser(String.valueOf(userId));
            this.save(computeLastProfitDate);
        }
    }
}
