package com.liang.service.impl;

import com.liang.dao.BaseProjectbudgetDao;
import com.liang.entity.BaseProjectbudget;
import com.liang.entity.StaBudget;
import com.liang.dao.StaBudgetDao;
import com.liang.service.StaBudgetService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * 经费预算(StaBudget)表服务实现类
 *
 * @author makejava
 * @since 2023-04-09 22:07:01
 */
@Service("staBudgetService")
public class StaBudgetServiceImpl implements StaBudgetService {
    @Resource
    private StaBudgetDao staBudgetDao;
    @Resource
    private BaseProjectbudgetDao baseProjectbudgetDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public StaBudget queryById(Integer id) {
        return this.staBudgetDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param staBudget   筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<StaBudget> queryByPage(StaBudget staBudget, PageRequest pageRequest) {
        long total = this.staBudgetDao.count(staBudget);
        return new PageImpl<>(this.staBudgetDao.queryAllByLimit(staBudget, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param staBudget 实例对象
     * @return 实例对象
     */
    @Override
    public StaBudget insert(StaBudget staBudget) {
        this.staBudgetDao.insert(staBudget);
        return staBudget;
    }

    /**
     * 修改数据
     *
     * @param staBudget 实例对象
     * @return 实例对象
     */
    @Override
    public StaBudget update(StaBudget staBudget) {
        this.staBudgetDao.update(staBudget);
        return this.queryById(staBudget.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.staBudgetDao.deleteById(id) > 0;
    }

    @Override
    public boolean deleteByXm(Integer xm) {
        return this.staBudgetDao.deleteByXm(xm) > 0;
    }

    @Override
    public int updateBatch(List<StaBudget> staBudgets) {
        int count = 0;
        for (StaBudget budget : staBudgets) {
            count += this.staBudgetDao.update(budget);
        }
        return count;
    }

    @Override
    public int insertBatchByXmlx(String xmlx, Integer xm) {
        staBudgetDao.deleteByXm(xm);
        List<BaseProjectbudget> budgetList = baseProjectbudgetDao.getBudgetListByXmlx(xmlx);
        if (budgetList.size() == 0)
            return 0;
        else {
            for (int i = 0; i < budgetList.size(); i++) {
                budgetList.get(i).setXm(xm);
            }
            return this.staBudgetDao.insertBatch(budgetList);
        }
    }

    @Override
    public List<StaBudget> getBudgetList(Integer xm) {
        return this.staBudgetDao.getBudgetList(xm);
    }
}
