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.ComputeStandPatientProjectCostMapper;
import com.kcim.dao.model.ComputeStandPatientProjectCost;
import com.kcim.vo.ComputeProjectCostPageDto;
import com.kcim.vo.SessionUserVO;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

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

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

        this.update(updateWrapper);
    }

    public Page<ComputeStandPatientProjectCost> getPage(Integer current, Integer pageSize, String computeDate, String departmentFilter, String patientFilter) {
        Page<ComputeStandPatientProjectCost> page = new Page<>(current, pageSize);
        LambdaQueryWrapper<ComputeStandPatientProjectCost> queryWrapper = new LambdaQueryWrapper<>();

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

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

    public List<ComputeProjectCostPageDto> getByProjectCostId(String computeDate, List<Integer> collect1) {
        return this.baseMapper.getByProjectCostId( computeDate, UserContext.getHospId(),collect1);

    }
}
