package com.kcim.dao.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcim.common.util.UserContext;
import com.kcim.dao.mapper.ComputePatientProjectCostMapper;
import com.kcim.dao.model.ComputePatientProjectCost;
import com.kcim.vo.SessionUserVO;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @program: CostAccount
 * @description:
 * @author: Wang.YS
 * @create: 2024-06-25 17:05
 **/
@Repository
public class ComputePatientProjectCostRepository extends ServiceImpl<ComputePatientProjectCostMapper, ComputePatientProjectCost> {
    public void removeByComputeDate(String computeDate, SessionUserVO currentUser) {
        UpdateWrapper<ComputePatientProjectCost> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(ComputePatientProjectCost::getDelFlag, 0)
                .eq(ComputePatientProjectCost::getHospId, currentUser.getHospId())
                .eq(ComputePatientProjectCost::getComputeDate, computeDate)
                .set(ComputePatientProjectCost::getDeleteTime,new Date())
                .set(ComputePatientProjectCost::getDeleteUser,String.valueOf(currentUser.getId()))
                .set(ComputePatientProjectCost::getDelFlag, 1);

        this.update(updateWrapper);
    }

    public Page<ComputePatientProjectCost> getPage(Integer current, Integer pageSize, String computeDate, String departmentFilter, String patientFilter) {

        Page<ComputePatientProjectCost> page = new Page<>(current, pageSize);
        LambdaQueryWrapper<ComputePatientProjectCost> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(ComputePatientProjectCost::getComputeDate,computeDate);
        queryWrapper.eq(ComputePatientProjectCost::getHospId, UserContext.getHospId());

        if(!StringUtils.isEmpty(departmentFilter)){
            queryWrapper.and(q->q.like(ComputePatientProjectCost::getDepartmentCode,departmentFilter).or().like(ComputePatientProjectCost::getDepartmentName,departmentFilter));
        }
        if(!StringUtils.isEmpty(patientFilter)){
            queryWrapper.and(q->q.like(ComputePatientProjectCost::getPatientNo,patientFilter).or().like(ComputePatientProjectCost::getPatientName,patientFilter));
        }
        return this.page(page, queryWrapper);

    }
}
