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.ComputeStandProjectCostMapper;
import com.kcim.dao.model.ComputeStandProjectCost;
import com.kcim.vo.ProjectCostVo;
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-25 11:02
 **/
@Repository
public class ComputeStandProjectCostRepository extends ServiceImpl<ComputeStandProjectCostMapper, ComputeStandProjectCost> {
    public long itemCount(String computeDate, SessionUserVO currentUser) {
        LambdaQueryWrapper<ComputeStandProjectCost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ComputeStandProjectCost::getComputeDate,computeDate);
        queryWrapper.eq(ComputeStandProjectCost::getHospId,currentUser.getHospId());
        return this.count(queryWrapper);
    }

    public void removeByComputeDate(String computeDate, SessionUserVO currentUser) {
        UpdateWrapper<ComputeStandProjectCost> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(ComputeStandProjectCost::getDelFlag, 0)
                .eq(ComputeStandProjectCost::getHospId, currentUser.getHospId())
                .eq(ComputeStandProjectCost::getComputeDate, computeDate)
                .set(ComputeStandProjectCost::getDeleteTime,new Date())
                .set(ComputeStandProjectCost::getDeleteUser,String.valueOf(currentUser.getId()))
                .set(ComputeStandProjectCost::getDelFlag, 1);
        this.update(updateWrapper);
    }

    public Page<ComputeStandProjectCost> getPage(Integer current, Integer pageSize, String computeDate, String itemType, String responsibility, String itemFilter) {

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

        queryWrapper.eq(ComputeStandProjectCost::getComputeDate,computeDate);
        queryWrapper.eq(ComputeStandProjectCost::getHospId, UserContext.getHospId());
        if(!StringUtils.isEmpty(itemType)){
            queryWrapper.eq(ComputeStandProjectCost::getItemType,itemType);
        }
        if(!StringUtils.isEmpty(responsibility)){
            queryWrapper.eq(ComputeStandProjectCost::getResponsibilityCode,responsibility);
        }
        if(!StringUtils.isEmpty(itemFilter)){
            queryWrapper.and(q->q.like(ComputeStandProjectCost::getCode,itemFilter).or().like(ComputeStandProjectCost::getName,itemFilter));
        }
        return this.page(page, queryWrapper);

    }

    public List<ProjectCostVo> getProjectCost(String computeDate, SessionUserVO currentUser) {
        return this.baseMapper.getProjectCost(computeDate,currentUser.getHospId());
    }
}
