package com.kcim.dao.repository;

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

import java.util.List;

/**
 * @program: CostAccount
 * @description:
 * @author: Wang.YS
 * @create: 2023-11-14 14:28
 **/
@Repository
public class AccountingRepository extends ServiceImpl<AccountingMapper, Accounting> {

    /**
     * 获取会计科目收入字典
     */
    public List<Accounting> getAccountIncome(String name){
        LambdaQueryWrapper<Accounting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Accounting::getHospId, UserContext.getHospId());
        queryWrapper.eq(Accounting::getAccountingType, NumberConstant.ONE);
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like(Accounting::getAccountingName, name);
        }
        return this.list(queryWrapper);
    }

    public List<Accounting> getAllIncome() {
        LambdaQueryWrapper<Accounting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Accounting::getHospId, UserContext.getHospId());
        queryWrapper.eq(Accounting::getAccountingType, NumberConstant.ONE);

        return this.list(queryWrapper);
    }

    public List<Accounting> getAllCostAccounting() {
        LambdaQueryWrapper<Accounting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Accounting::getHospId, UserContext.getHospId());
        queryWrapper.eq(Accounting::getAccountingType, NumberConstant.TWO);
        return this.list(queryWrapper);
    }

    public List<Accounting> getList(Integer accountType) {
        LambdaQueryWrapper<Accounting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Accounting::getHospId, UserContext.getHospId());
        queryWrapper.eq(Accounting::getAccountingType, accountType);
        queryWrapper.orderByDesc(Accounting::getCreateTime);
        return this.list(queryWrapper);

    }

    public List<Accounting> getBaseCostList(String filter) {
        LambdaQueryWrapper<Accounting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Accounting::getHospId, UserContext.getHospId());
        queryWrapper.eq(Accounting::getAccountingType, NumberConstant.TWO);
        queryWrapper.eq(Accounting::getIsBaseCost,NumberConstant.ONE);
        if(!StringUtils.isEmpty(filter)){
            queryWrapper.like(Accounting::getAccountingName,filter);
        }
        queryWrapper.orderByDesc(Accounting::getCreateTime);
        return this.list(queryWrapper);

    }

    public List<Accounting> getList(Integer accountType, String filter) {
        LambdaQueryWrapper<Accounting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Accounting::getHospId, UserContext.getHospId());
        queryWrapper.eq(Accounting::getAccountingType, accountType);
        queryWrapper.and(q->q.like(Accounting::getAccountingCode,filter).or().like(Accounting::getAccountingName,filter));
        queryWrapper.orderByDesc(Accounting::getCreateTime);
        return this.list(queryWrapper);
    }
}
