package com.sqx.modules.pay.controller.app;


import com.sqx.common.utils.Result;
import com.sqx.modules.coupon.entity.Coupon;
import com.sqx.modules.coupon.service.CouponService;
import com.sqx.modules.pay.entity.PayClassify;
import com.sqx.modules.pay.entity.PayClassifyDetail;
import com.sqx.modules.pay.service.PayClassifyDetailService;
import com.sqx.modules.pay.service.PayClassifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author fang
 * @date 2022/4/16
 */
@Slf4j
@RestController
@Api(value = "充值分类", tags = {"充值分类"})
@RequestMapping(value = "/app/payClassify")
public class AppPayClassifyController {

    @Autowired
    private PayClassifyService payClassifyService;
    @Autowired
    private PayClassifyDetailService payClassifyDetailService;

    @GetMapping("/selectPayClassifyList")
    @ApiOperation("查询充值分类")
    public Result selectPayClassifyList(){
        List<PayClassify> list = payClassifyService.list();
        for(PayClassify payClassify : list){
            BigDecimal sumMoney = BigDecimal.ZERO;
            StringBuffer sb = new StringBuffer();
            BigDecimal price = payClassify.getPrice();
            BigDecimal money = payClassify.getMoney();
            sumMoney = sumMoney.add(money);
            if(price.compareTo(money) != 0){
                sb.append(money);
                sb.append("余额");
                sb.append(" ");
            }
            List<PayClassifyDetail> detailList = payClassifyDetailService.findList(payClassify.getPayClassifyId());
            payClassify.setDetailList(detailList);
            for(PayClassifyDetail classifyDetail : detailList){
                Integer giveNum = classifyDetail.getGiveNum();
                sb.append(giveNum);
                sb.append("张");
                Coupon coupon = classifyDetail.getCoupon();
                BigDecimal couponMoney = coupon.getMoney();
                sb.append(couponMoney);
                sb.append("元券");
                sb.append(" ");
                BigDecimal sumCouponMoney = couponMoney.multiply(new BigDecimal(giveNum));
                sumMoney = sumMoney.add(sumCouponMoney);
            }
            payClassify.setSumMoney(sumMoney);
            payClassify.setRemarks(sb.toString());
        }
        return Result.success().put("data",list);
    }

}