package cn.newfeifan.mall.module.distri.service.ptprofit;

import cn.newfeifan.mall.module.distri.controller.app.ptprofitlog.vo.PtProfitLogSaveReqVO;
import cn.newfeifan.mall.module.distri.dal.dataobject.ptprofit.PtProfitDO;
import cn.newfeifan.mall.module.distri.dal.mysql.ptprofit.PtProfitMapper;
import cn.newfeifan.mall.module.distri.enums.IntegralEnum;
import cn.newfeifan.mall.module.distri.enums.ProfitTypeEnum;
import cn.newfeifan.mall.module.distri.service.ptprofitlog.PtProfitLogService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

/**
 * 平台利润 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class PtProfitServiceImpl implements PtProfitService {

    @Resource
    private PtProfitMapper ptProfitMapper;

    @Resource
    private PtProfitLogService ptProfitLogService;


    @Override
    public void updatePtProfit(Long ptProfit, Long serviceCharge, Long profit, Long settlementPrice, IntegralEnum integralEnum,
                               Long orderId, String orderNO, Long userId) {
        // 增加收益
        PtProfitDO ptProfitDO = ptProfitMapper.selectList().get(0);
        ptProfit -= serviceCharge;

        ptProfitDO.setPtGrossAdd(ptProfitDO.getPtGrossAdd() + ptProfit);
        ptProfitDO.setPtAdd(ptProfitDO.getPtAdd() + serviceCharge);
        ptProfitDO.setProfit(ptProfitDO.getProfit() + profit);
        ptProfitDO.setSettlementPrice(ptProfitDO.getSettlementPrice() + settlementPrice);
        ptProfitDO.setPtTotalAdd(ptProfitDO.getPtTotalAdd() + ptProfit + serviceCharge + profit + settlementPrice);
        ptProfitMapper.updateById(ptProfitDO);

        // 添加平台收益记录
        PtProfitLogSaveReqVO ptProfitLog = PtProfitLogSaveReqVO.builder()
                .profitStatus(integralEnum.getType())
                .profitType(ProfitTypeEnum.PT_PROFIT.getType())
                .amount(ptProfit)
                .afterAmount(ptProfitDO.getPtGrossAdd())
                .orderId(orderId)
                .orderNo(orderNO)
                .generateUserId(userId)
                .build();
        // 添加平台服务费记录
        PtProfitLogSaveReqVO serviceChargeLog = PtProfitLogSaveReqVO.builder()
                .profitStatus(integralEnum.getType())
                .profitType(ProfitTypeEnum.SERVICE_CHARGE.getType())
                .amount(serviceCharge)
                .afterAmount(ptProfitDO.getPtAdd())
                .orderId(orderId)
                .orderNo(orderNO)
                .generateUserId(userId)
                .build();
        // 添加平台利润记录
        PtProfitLogSaveReqVO profitLog = PtProfitLogSaveReqVO.builder()
                .profitStatus(integralEnum.getType())
                .profitType(ProfitTypeEnum.PROFIT.getType())
                .amount(profit)
                .afterAmount(ptProfitDO.getProfit())
                .orderId(orderId)
                .orderNo(orderNO)
                .generateUserId(userId).build();
        // 添加平台结算价记录
        PtProfitLogSaveReqVO settlementPriceLog = PtProfitLogSaveReqVO.builder()
                .profitStatus(integralEnum.getType())
                .profitType(ProfitTypeEnum.SETTLEMENT_PRICE.getType())
                .amount(settlementPrice)
                .afterAmount(ptProfitDO.getSettlementPrice())
                .orderId(orderId)
                .orderNo(orderNO)
                .generateUserId(userId).build();

        ptProfitLogService.saveBatch(Arrays.asList(ptProfitLog, serviceChargeLog, profitLog, settlementPriceLog));
    }

    @Override
    public void subtractIntegral(Long settlementPrice, IntegralEnum integralEnum, Long orderId, String orderNO, Long userId) {
        // 增加收益
        PtProfitDO ptProfitDO = ptProfitMapper.selectList().get(0);

        ptProfitDO.setSettlementPrice(ptProfitDO.getSettlementPrice() - settlementPrice);
        ptProfitDO.setPtTotalAdd(ptProfitDO.getPtTotalAdd() - settlementPrice);

        ptProfitMapper.updateById(ptProfitDO);

        // 记录日志
        PtProfitLogSaveReqVO settlementPriceLog = PtProfitLogSaveReqVO.builder()
                .profitStatus(integralEnum.getType())
                .profitType(ProfitTypeEnum.SETTLEMENT_PRICE.getType())
                .amount(-settlementPrice)
                .afterAmount(ptProfitDO.getSettlementPrice())
                .orderId(orderId)
                .orderNo(orderNO)
                .generateUserId(userId).build();

        ptProfitLogService.createPtProfitLog(settlementPriceLog);
    }
}