package com.gxwebsoft.bszx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.bszx.entity.BszxPay;
import com.gxwebsoft.bszx.mapper.BszxClassMapper;
import com.gxwebsoft.bszx.service.BszxClassService;
import com.gxwebsoft.bszx.entity.BszxClass;
import com.gxwebsoft.bszx.param.BszxClassParam;
import com.gxwebsoft.bszx.service.BszxPayService;
import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.stereotype.Service;

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

/**
 * 百色中学-班级Service实现
 *
 * @author 科技小王子
 * @since 2025-03-06 22:50:25
 */
@Service
public class BszxClassServiceImpl extends ServiceImpl<BszxClassMapper, BszxClass> implements BszxClassService {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private BszxPayService bszxPayService;

    @Override
    public PageResult<BszxClass> pageRel(BszxClassParam param) {
        PageParam<BszxClass, BszxClassParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, id asc");
        List<BszxClass> list = baseMapper.selectPageRel(page, param);
        LambdaQueryWrapper<BszxPay> wrapper = new LambdaQueryWrapper<>();

        if (param.getLimit() == null) {
          list.forEach(item -> {
            wrapper.clear();
//          wrapper.eq(BszxPay::getBranchName,item.getBranchName());
            wrapper.eq(BszxPay::getGradeName,item.getGradeName());
            wrapper.eq(BszxPay::getClassName, item.getName());
            item.setTotalMoney(bszxPayService.sumMoney(wrapper));
            updateById(item);
          });
        }
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<BszxClass> listRel(BszxClassParam param) {
        List<BszxClass> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<BszxClass, BszxClassParam> page = new PageParam<>();
        page.setDefaultOrder("sort_number asc, id asc");
        return page.sortRecords(list);
    }

    @Override
    public BszxClass getByIdRel(Integer id) {
        BszxClassParam param = new BszxClassParam();
        param.setId(id);
        return param.getOne(baseMapper.selectListRel(param));
    }

}
