package com.kcim.dao.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.CostReportColumnMapper;
import com.kcim.dao.model.CostReportColumn;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

/**
 * @program: CostAccount
 * @description: 报表-报表列对照管理数据库交互
 * @author: Wang.YS
 * @create: 2023-11-01 10:49
 **/
@Repository
public class CostReportColumnRepository extends ServiceImpl<CostReportColumnMapper, CostReportColumn> {
    public Page<CostReportColumn> getPage(Integer current, Integer pageSize, Long reportCode, String headText) {
        Page<CostReportColumn> page = new Page<>(current,pageSize);
        LambdaQueryWrapper<CostReportColumn> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CostReportColumn::getHospId, UserContext.getHospId());
        queryWrapper.eq(CostReportColumn::getReportCode,reportCode);
        if(!StringUtils.isEmpty(headText)){
            queryWrapper.like(CostReportColumn::getColumnHeaderText,headText);
        }
        queryWrapper.orderByAsc(CostReportColumn::getColumnHeaderText);
        return this.page(page,queryWrapper);
    }

    public List<CostReportColumn> getList(Long reportCode) {
        LambdaQueryWrapper<CostReportColumn> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CostReportColumn::getHospId, UserContext.getHospId());
        queryWrapper.eq(CostReportColumn::getReportCode,reportCode);
        queryWrapper.orderByAsc(CostReportColumn::getColumnHeaderText);
        return this.list(queryWrapper);
    }

    public void deleteReportColumnByReportCode(Long reportCode) {
        LambdaQueryWrapper<CostReportColumn> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CostReportColumn::getHospId,UserContext.getCurrentLoginHospId());
        queryWrapper.eq(CostReportColumn::getReportCode,reportCode);
        List<CostReportColumn> list = this.list(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            for( CostReportColumn reportColumn:list){
                reportColumn.setDeleteTime(new Date());
                reportColumn.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
            }
            List<Integer> ids = list.stream().map(CostReportColumn::getId).collect(Collectors.toList());
            this.updateBatchById(list);
            this.removeByIds(ids);
        }
    }

    public void deleteReportColumn(Integer id) {
        CostReportColumn reportColumn = this.getById(id);
        if(Objects.nonNull(reportColumn)){
            reportColumn.setDeleteTime(new Date());
            reportColumn.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
            this.updateById(reportColumn);
            this.removeById(id);
        }
    }

    public void batchDeleteReportColumn(List<Integer> id) {
        LambdaQueryWrapper<CostReportColumn> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(CostReportColumn::getId,id);
        List<CostReportColumn> list = this.list(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            for (CostReportColumn costReportColumn : list) {
                costReportColumn.setDeleteTime(new Date());
                costReportColumn.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
            }
            this.updateBatchById(list);
            this.removeBatchByIds(id);
        }
    }

    public List<CostReportColumn> getReportColumnByReportCode(Long reportCode) {
        LambdaQueryWrapper<CostReportColumn> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CostReportColumn::getHospId, UserContext.getCurrentLoginHospId());
        queryWrapper.eq(CostReportColumn::getReportCode,reportCode);

        return this.list(queryWrapper);
    }
}
