package com.xy.controller;

import cn.hutool.core.util.BooleanUtil;
import com.xy.annotate.RestMappingController;
import com.xy.dto.MercFeeCountMonthDto;
import com.xy.dto.MercFeeSettleDto;
import com.xy.dto.MercFeeStatisticDto;
import com.xy.entity.MercFeeStatistic;
import com.xy.error.CommRuntimeException;
import com.xy.service.MercFeeCountMonthServiceImpl;
import com.xy.service.MercFeeSettleServiceImpl;
import com.xy.service.MercFeeStatisticServiceImpl;
import com.xy.utils.MercAuthUtils;
import com.xy.utils.PageBean;
import com.xy.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@AllArgsConstructor
@Api(tags = "商户-收益概览")
@RestMappingController("merc-api/fee")
public class MercFeeCountController {

    private MercFeeCountMonthServiceImpl mercFeeCountMonthService;
    private MercFeeStatisticServiceImpl mercFeeStatisticService;
    private MercFeeSettleServiceImpl mercFeeSettleService;


    @PostMapping("page")
    @ApiOperation("收益概览-分页")
    public R<PageBean<MercFeeCountMonthDto.Vo>> page(@RequestBody MercFeeCountMonthDto.Page page) {
        page.setParentMercId(MercAuthUtils.getMercId());
        return mercFeeCountMonthService.page(page);
    }

    @PostMapping("confirm")
    @ApiOperation("确认佣金")
    public R confirm(@RequestBody @Validated MercFeeCountMonthDto.ConfirmDTO dto) {
        dto.setParentMercId(MercAuthUtils.getMercId());
        return mercFeeCountMonthService.confirm(dto);
    }

    @PostMapping("info")
    @ApiOperation("总览")
    public R<MercFeeStatisticDto.InFoVO> info() {
        return mercFeeStatisticService.info(MercAuthUtils.getMercId());
    }


    @PostMapping("applySettle")
    @ApiOperation("结算申请")
    @Transactional(rollbackFor = Exception.class)
    public R applySettle(@RequestBody @Validated MercFeeSettleDto.ApplyDTO dto) {
        Long mercId = MercAuthUtils.getMercId();
        dto.setMercId(mercId);
        // 验证结算金额是否在可结算金额范围内
        Integer settleMoney = dto.getSettleMoney();
        if (settleMoney.intValue() <= 0) {
            throw new CommRuntimeException("申请结算金额填写有误，必须大于0");
        }
        // 可结算金额查看
        MercFeeStatisticDto.InFoVO inFoVO = R.feignCheckData(mercFeeStatisticService.info(mercId));
        Integer unSettleMoney = inFoVO.getUnSettleMoney();
        if (settleMoney > unSettleMoney) {
            throw new CommRuntimeException("申请结算金额填写有误，不可大于可结算金额：" + unSettleMoney);
        }

        Boolean b = R.feignCheckData(mercFeeSettleService.applySettle(dto));
        if (BooleanUtil.isTrue(b)) {
            // 成功申请，统计表中的可结算佣金扣除申请结算金额
            unSettleMoney = unSettleMoney - settleMoney;
            MercFeeStatistic mercFeeStatistic = new MercFeeStatistic();
            mercFeeStatistic.setMercId(mercId);
            mercFeeStatistic.setUnSettleMoney(unSettleMoney);
            mercFeeStatisticService.updateById(mercFeeStatistic);
        }
        return R.ok();
    }

    @PostMapping("settlePage")
    @ApiOperation("收益概览-结算记录")
    public R<PageBean<MercFeeSettleDto.Vo>> settlePage(@RequestBody MercFeeSettleDto.Page page) {
        page.setMercId(MercAuthUtils.getMercId());
        return mercFeeSettleService.page(page);
    }


}
