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.ComputeStandShareParamMapper;
import com.kcim.dao.model.ComputeStandShareParam;
import com.kcim.vo.ComputeShareParamDetailVo;
import com.kcim.vo.ComputeShareParamSumVo;
import com.kcim.vo.SessionUserVO;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: CostAccount
 * @description:
 * @author: Wang.YS
 * @create: 2024-06-27 15:14
 **/
@Repository
public class ComputeStandShareParamRepository extends ServiceImpl<ComputeStandShareParamMapper, ComputeStandShareParam> {
    public Page<ComputeStandShareParam> getPage(Integer current, Integer pageSize, String computeDate, String itemType, String responsibility, String itemFilter) {
        Page<ComputeStandShareParam> page = new Page<>(current, pageSize);
        LambdaQueryWrapper<ComputeStandShareParam> queryWrapper = new LambdaQueryWrapper<>();

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

    public long getComputeShareParamCount(String computeDate) {
        LambdaQueryWrapper<ComputeStandShareParam> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ComputeStandShareParam::getHospId,UserContext.getHospId());
        queryWrapper.eq(ComputeStandShareParam::getComputeDate,computeDate);
        return this.count(queryWrapper);

    }

    public List<ComputeShareParamSumVo> getComputeShareParamSum(String computeDate) {
        List<ComputeShareParamSumVo> computeShareParamSum = this.baseMapper.getComputeShareParamSum(computeDate, UserContext.getHospId());
        return computeShareParamSum.stream().filter(f -> !StringUtils.isEmpty(f.getResponsibilityCode())).collect(Collectors.toList());
    }

    public List<ComputeShareParamDetailVo> getComputeShareParamDetail(String computeDate) {
        List<ComputeShareParamDetailVo> computeShareParamDetail = this.baseMapper.getComputeShareParamDetail(computeDate, UserContext.getHospId());
        return computeShareParamDetail.stream().filter(f -> !StringUtils.isEmpty(f.getResponsibilityCode())).collect(Collectors.toList());
    }

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

        this.update(updateWrapper);
    }
}
