/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.distribution.comment.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.distribution.comment.dao.DistributionAuditingMapper;
import com.yami.shop.distribution.comment.dao.DistributionUserMapper;
import com.yami.shop.distribution.comment.dto.DistributionAuditingDto;
import com.yami.shop.distribution.comment.model.DistributionAuditing;
import com.yami.shop.distribution.comment.model.DistributionUser;
import com.yami.shop.distribution.comment.param.RangeTimeParam;
import com.yami.shop.distribution.comment.service.DistributionAuditingService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author lgh on 2019/04/01.
 */
@Service
@AllArgsConstructor
public class DistributionAuditingServiceImpl extends ServiceImpl<DistributionAuditingMapper, DistributionAuditing> implements DistributionAuditingService {

    private final DistributionAuditingMapper distributionAuditingMapper;

    private final DistributionUserMapper distributionUserMapper;

    @Override
    public Page<DistributionAuditingDto> distributionAuditingPage(Page page, DistributionAuditing distributionAuditing, RangeTimeParam rangeTimeParam, Integer startExpenseNumber, Integer endExpenseNumber, Double startPayAmount, Double endPayAmount, String mobile) {
        return distributionAuditingMapper.distributionAuditingPage(page, distributionAuditing, rangeTimeParam, startExpenseNumber, endExpenseNumber, startPayAmount, endPayAmount, mobile);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void examine(DistributionAuditing distributionAuditing) {
        DistributionUser distributionUser = new DistributionUser();
        distributionUser.setDistributionUserId(distributionAuditing.getDistributionUserId());

        distributionUser.setBindTime(new Date());
        //判断是否通过
        if (distributionAuditing.getState() == 1) {
            //通过审核,修改分销员状态
            distributionUser.setState(1);
            distributionUserMapper.updateById(distributionUser);
        } else if (distributionAuditing.getState() == -1) {
            //未通过审核,修改分销员状态
            distributionUser.setState(3);
            distributionUserMapper.updateById(distributionUser);
        }
        distributionAuditingMapper.updateById(distributionAuditing);
    }
}
