package co.yixiang.yshop.module.distributor.service.distributorwageslog;

import co.yixiang.yshop.framework.mybatis.core.query.LambdaQueryWrapperX;
import co.yixiang.yshop.framework.mybatis.core.query.QueryWrapperX;
import co.yixiang.yshop.module.distributor.dal.dataobject.distributororder.DistributorOrderDO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.sql.Wrapper;
import java.util.*;

import co.yixiang.yshop.module.distributor.controller.admin.distributorwageslog.vo.*;
import co.yixiang.yshop.module.distributor.dal.dataobject.distributorwageslog.DistributorWagesLogDO;
import co.yixiang.yshop.framework.common.pojo.PageResult;

import co.yixiang.yshop.module.distributor.convert.distributorwageslog.DistributorWagesLogConvert;
import co.yixiang.yshop.module.distributor.dal.mysql.distributorwageslog.DistributorWagesLogMapper;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.distributor.enums.ErrorCodeConstants.*;

/**
 * 分销商佣金记录 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class DistributorWagesLogServiceImpl implements DistributorWagesLogService {

    @Resource
    private DistributorWagesLogMapper wagesLogMapper;

    @Override
    public Long createWagesLog(DistributorWagesLogCreateReqVO createReqVO) {
        // 插入
        DistributorWagesLogDO wagesLog = DistributorWagesLogConvert.INSTANCE.convert(createReqVO);
        wagesLogMapper.insert(wagesLog);
        // 返回
        return wagesLog.getId();
    }

    @Override
    public void updateWagesLog(DistributorWagesLogUpdateReqVO updateReqVO) {
        // 校验存在
        validateWagesLogExists(updateReqVO.getId());
        // 更新
        DistributorWagesLogDO updateObj = DistributorWagesLogConvert.INSTANCE.convert(updateReqVO);
        wagesLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteWagesLog(Long id) {
        // 校验存在
        validateWagesLogExists(id);
        // 删除
        wagesLogMapper.deleteById(id);
    }

    private void validateWagesLogExists(Long id) {
        if (wagesLogMapper.selectById(id) == null) {
            throw exception(WAGES_LOG_NOT_EXISTS);
        }
    }

    @Override
    public DistributorWagesLogDO getWagesLog(Long id) {
        return wagesLogMapper.selectById(id);
    }

    @Override
    public List<DistributorWagesLogDO> getWagesLogList(Collection<Long> ids) {
        return wagesLogMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DistributorWagesLogDO> getWagesLogPage(DistributorWagesLogPageReqVO pageReqVO) {
        return wagesLogMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DistributorWagesLogDO> getWagesLogList(DistributorWagesLogExportReqVO exportReqVO) {
        return wagesLogMapper.selectList(exportReqVO);
    }

    @Override
    public DistributorWagesLogDO sumAmount(DistributorWagesLogExportReqVO exportReqVO) {
        QueryWrapperX<DistributorWagesLogDO> queryWrapperX = new QueryWrapperX<>();
        queryWrapperX.select("sum(amount) as amount");
        queryWrapperX.betweenIfPresent("create_time", exportReqVO.getCreateTime());
        queryWrapperX.eqIfPresent("distributor_id", exportReqVO.getDistributorId());
        queryWrapperX.eqIfPresent("type", exportReqVO.getType());
        return wagesLogMapper.selectOne(queryWrapperX);
    }

    @Override
    public void updateWagesLogType(Long distributorId, Long distributorOrderId, Integer type) {
        wagesLogMapper.update(new DistributorWagesLogDO()
                .setType(type), new LambdaQueryWrapperX<DistributorWagesLogDO>()
                .eq(DistributorWagesLogDO::getDistributorId, distributorId)
                .eq(DistributorWagesLogDO::getDistributorOrderId, distributorOrderId)
        );
    }

}
