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.constants.NumberConstant;
import com.kcim.common.util.UserContext;
import com.kcim.dao.mapper.CostColumnMapper;
import com.kcim.dao.model.CostColumn;
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.OptionalLong;

/**
 * @program: CostAccount
 * @description: 报表列管理
 * @author: Wang.YS
 * @create: 2023-11-01 10:45
 **/
@Repository
public class CostColumnRepository extends ServiceImpl<CostColumnMapper, CostColumn> {
    public Long getCode() {
        LambdaQueryWrapper<CostColumn> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CostColumn::getHospId, UserContext.getHospId());
        List<CostColumn> list = this.list(queryWrapper);
        if(CollectionUtils.isEmpty(list)){
            return  1L;
        }else {
            OptionalLong max = list.stream().mapToLong(CostColumn::getCode).max();
            if(max.isPresent()){
                long asLong = max.getAsLong();
                return asLong + 1L;
            }else {
                return  1L;
            }
        }
    }

    public void deleteColumn(Integer id) {
        CostColumn costColumn = this.getById(id);
        if(Objects.nonNull(costColumn)){
            costColumn.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
            costColumn.setDeleteTime(new Date());
            this.updateById(costColumn);
            this.removeById(id);
        }
        //删除与此列相关的 报表-报表列对照的列

    }

    public Page<CostColumn> getPage(Integer current, Integer pageSize, String columnName, String columnType) {
        Page<CostColumn> page = new Page<>(current,pageSize);
        LambdaQueryWrapper<CostColumn> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CostColumn::getHospId,UserContext.getHospId());
        if(!StringUtils.isEmpty(columnName)){
            queryWrapper.like(CostColumn::getHeaderText,columnName);
        }
        if(!StringUtils.isEmpty(columnType)){
            queryWrapper.eq(CostColumn::getType,columnType);
        }
        queryWrapper.orderByAsc(CostColumn::getHeaderText);
        return this.page(page, queryWrapper);


    }

    public List<CostColumn> getChildColumnList(Long columnCode) {
        LambdaQueryWrapper<CostColumn> queryWrapper =  new LambdaQueryWrapper<>();
        queryWrapper.eq(CostColumn::getHospId,UserContext.getCurrentLoginHospId());
        queryWrapper.ne(CostColumn::getCode,columnCode);
        queryWrapper.orderByAsc(CostColumn::getHeaderText);
        return this.list(queryWrapper);
    }

    public List<CostColumn> getList( String columnName, String columnType) {
        LambdaQueryWrapper<CostColumn> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CostColumn::getHospId,UserContext.getHospId());
        if(!StringUtils.isEmpty(columnName)){
            queryWrapper.like(CostColumn::getHeaderText,columnName);
        }
        if(!StringUtils.isEmpty(columnType)){
            queryWrapper.like(CostColumn::getType,columnType);
        }
        queryWrapper.orderByAsc(CostColumn::getHeaderText);
        return this.list( queryWrapper);


    }

    public CostColumn getByCode(String sub) {
        LambdaQueryWrapper<CostColumn> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CostColumn::getCode,sub);
        queryWrapper.eq(CostColumn::getHospId,UserContext.getHospId());
        return this.getOne(queryWrapper);

    }

    public List<CostColumn> getColumnByColumnCode(List<Long> columnCodes) {
        LambdaQueryWrapper<CostColumn> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CostColumn::getDelFlag, NumberConstant.ZERO);
        queryWrapper.eq(CostColumn::getHospId, UserContext.getCurrentLoginHospId());
        queryWrapper.in(CostColumn::getCode,columnCodes);
        return this.list(queryWrapper);
    }
}
