package cn.iocoder.yudao.module.system.dal.mysql.customersettlement;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.system.api.customersettlement.dto.SettlementItemDTO;
import cn.iocoder.yudao.module.system.controller.admin.customersettlement.vo.CustomerSettlementPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.customersettlement.vo.StatisticsVO;
import cn.iocoder.yudao.module.system.dal.dataobject.customersettlement.CustomerSettlementDO;
import cn.iocoder.yudao.module.system.dal.dataobject.customersettlement.SettlementItemDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 客户结算信息 Mapper
 *
 * @author 陈宾顺
 */
@Mapper
public interface CustomerSettlementMapper extends BaseMapperX<CustomerSettlementDO> {

    default PageResult<CustomerSettlementDO> selectPage(CustomerSettlementPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CustomerSettlementDO>()
                .eqIfPresent(CustomerSettlementDO::getCustomerId, reqVO.getCustomerId())
                .betweenIfPresent(CustomerSettlementDO::getSettlementMonth, reqVO.getSettlementMonth())
                .orderByDesc(CustomerSettlementDO::getId));
    }

    StatisticsVO selectTotal(@Param("reqVO") CustomerSettlementPageReqVO pageReqVO);

    default List<SettlementItemDTO> selectDeductionItems(Long factoryId, String month) {

        MPJLambdaWrapperX<CustomerSettlementDO> queryWrapper = new MPJLambdaWrapperX<CustomerSettlementDO>();
        queryWrapper.selectAs(CustomerSettlementDO::getCustomerId, SettlementItemDTO::getFactoryId);
        queryWrapper.selectAs(CustomerSettlementDO::getSettlementMonth, SettlementItemDTO::getMonth);
        queryWrapper.selectAs(SettlementItemDO::getBusinessType, SettlementItemDTO::getBusinessType);
        queryWrapper.selectAs(SettlementItemDO::getDeductionItems, SettlementItemDTO::getDeductionItems);
        queryWrapper.leftJoin(SettlementItemDO.class, SettlementItemDO::getSettlementId, CustomerSettlementDO::getId);
        queryWrapper.eqIfPresent(CustomerSettlementDO::getCustomerId, factoryId);
        queryWrapper.eqIfPresent(CustomerSettlementDO::getSettlementMonth, month);

        return selectJoinList(SettlementItemDTO.class, queryWrapper);
    }
}