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.ComputeShareParamMapper;
import com.kcim.dao.model.ComputeShareParam;
import com.kcim.vo.ComputeShareParamDetailVo;
import com.kcim.vo.ComputeShareParamSumVo;
import com.kcim.vo.SessionUserVO;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
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-01-15 20:33
 **/
@Repository
public class ComputeShareParamRepository extends ServiceImpl<ComputeShareParamMapper, ComputeShareParam> {
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void removeByComputeDate(String computeDate, SessionUserVO currentUser) {
        UpdateWrapper<ComputeShareParam> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(ComputeShareParam::getDelFlag, 0)
                .eq(ComputeShareParam::getHospId, currentUser.getHospId())
                .eq(ComputeShareParam::getComputeDate, computeDate)
                .set(ComputeShareParam::getDeleteTime,new Date())
                .set(ComputeShareParam::getDeleteUser,String.valueOf(currentUser.getId()))
                .set(ComputeShareParam::getDelFlag, 1);

        this.update(updateWrapper);
    }

//    public Page<ComputeDateShareParamVo> getPage(Integer current, Integer pageSize, String computeDate, String itemType, String responsibility, String itemFilter) {
//        Page<ComputeDateShareParamVo> page = new Page<>(current,pageSize);
//        List<ComputeDateShareParamVo> byPage = this.baseMapper.getByPage(page, computeDate, itemType, responsibility,itemFilter, UserContext.getHospId());
//        if(!CollectionUtils.isEmpty(byPage)){
//            page.setRecords(byPage);
//        }
//        return page;
//    }

    public Page<ComputeShareParam> getPage(Integer current, Integer pageSize, String computeDate, String itemType, String responsibility, String itemFilter) {
        Page<ComputeShareParam> page = new Page<>(current, pageSize);
        LambdaQueryWrapper<ComputeShareParam> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(ComputeShareParam::getComputeDate,computeDate);
        queryWrapper.eq(ComputeShareParam::getHospId,UserContext.getHospId());
        if(!StringUtils.isEmpty(itemType)){
            queryWrapper.eq(ComputeShareParam::getItemType,itemType);
        }
        if(!StringUtils.isEmpty(responsibility)){
            queryWrapper.eq(ComputeShareParam::getResponsibilityCode,responsibility);
        }
        if(!StringUtils.isEmpty(itemFilter)){
            queryWrapper.and(q->q.like(ComputeShareParam::getCode,itemFilter).or().like(ComputeShareParam::getName,itemFilter));
        }
        return this.page(page, queryWrapper);
    }
    public long getComputeShareParamCount(String computeDate) {
        LambdaQueryWrapper<ComputeShareParam> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ComputeShareParam::getHospId,UserContext.getHospId());
        queryWrapper.eq(ComputeShareParam::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());

    }

    /**
     * 获取责任中心的分摊参数汇总数据
     * @param computeDate
     * @return
     */
    public List<ComputeShareParamDetailVo> getComputeShareParamDetailGroup(String computeDate) {
        List<ComputeShareParamDetailVo> computeShareParamDetail = this.baseMapper.getComputeShareParamDetailGroup(computeDate, UserContext.getHospId());
        return computeShareParamDetail.stream().filter(f -> !StringUtils.isEmpty(f.getResponsibilityCode())).collect(Collectors.toList());
    }

    /**
     * 获取要计算的科室收费项目
     * @param computeDate
     * @return
     */
    public List<ComputeShareParamDetailVo> getComputeResponsibilityItem(String computeDate) {
        List<ComputeShareParamDetailVo> computeShareParamDetail = this.baseMapper.getComputeResponsibilityItem(computeDate, UserContext.getHospId());
        return computeShareParamDetail.stream().filter(f -> !StringUtils.isEmpty(f.getResponsibilityCode())).collect(Collectors.toList());
    }
}
