package com.kcim.dao.repository;

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.ComputeDrugCostMapper;
import com.kcim.dao.model.ComputeDrugCost;
import com.kcim.dao.model.dto.ComputeDrugCostPageDto;
import com.kcim.vo.SessionUserVO;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

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

/**
 * @program: CostAccount
 * @description: 药材成本计算表
 * @author: Wang.YS
 * @create: 2024-01-11 20:02
 **/
@Repository
public class ComputeDrugCostRepository extends ServiceImpl<ComputeDrugCostMapper, ComputeDrugCost> {
    public void removeByComputeDate(String computeDate, SessionUserVO currentUser) {
//        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<>();

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

        this.update(updateWrapper);

    }

    public Page<ComputeDrugCostPageDto> getByPage(Integer current, Integer pageSize, String computeDate, String responsibilityCode, String filter) {
        Page<ComputeDrugCostPageDto> page = new Page<>(current,pageSize);
        List<ComputeDrugCostPageDto> byPage = this.baseMapper.getByPage(page, computeDate, responsibilityCode, filter, UserContext.getHospId());
        if(!CollectionUtils.isEmpty(byPage)){
            page.setRecords(byPage);
        }
        return page;
    }
}
