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

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

/**
 * @program: CostAccount
 * @description:
 * @author: Wang.YS
 * @create: 2024-01-15 20:31
 **/
@Repository
public class ComputeProjectCostRepository extends ServiceImpl<ComputeProjectCostMapper, ComputeProjectCost> {

    /**
     * 获取项目成本计算分页查询
     * @param current  当前页
     * @param pageSize 页容量
     * @param computeDate 核算年月
     * @param itemType 项目类型
     * @param responsibility
     * @param itemFilter
     * @return
     */
    public Page<ComputeProjectCostPageDto> getByPage(Integer current, Integer pageSize, String computeDate, String itemType, String responsibility, String itemFilter) {
        Page<ComputeProjectCostPageDto> page = new Page<>(current,pageSize);
        List<ComputeProjectCostPageDto> byPage = this.baseMapper.getByPage(page, computeDate, itemType, responsibility,itemFilter, UserContext.getHospId());
        if(!CollectionUtils.isEmpty(byPage)){
            page.setRecords(byPage);
        }
        return page;
    }

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

        this.update(updateWrapper);
//        LambdaQueryWrapper<ComputeDrugCost> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(ComputeDrugCost::getHospId, currentUser.getHospId());
//        queryWrapper.eq(ComputeDrugCost::getComputeDate,computeDate);
//        List<ComputeDrugCost> list = this.list(queryWrapper);
//        if(!CollectionUtils.isEmpty(list)){
//            List<Integer> collect = list.stream().map(ComputeDrugCost::getId).collect(Collectors.toList());
//            this.removeBatchByIds(collect,500);
//            return collect;
//        }
//        return new ArrayList<>();
    }

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

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

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

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

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


    }

    public long itemCount(String computeDate, SessionUserVO currentUser) {
        LambdaQueryWrapper<ComputeProjectCost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ComputeProjectCost::getComputeDate,computeDate);
        queryWrapper.eq(ComputeProjectCost::getHospId,currentUser.getHospId());
        return this.count(queryWrapper);
    }

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


    }
}
