package com.hzs.report.member.controller.manage;


import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.report.member.service.IMemberService;
import com.hzs.report.member.vo.CuMemberUmbrellaBalanceVO;
import com.hzs.system.base.ICurrencyServiceApi;
import com.hzs.system.base.dto.CurrencyDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 会员伞下统计相关 前端控制器
 * </p>
 *
 * @author zhangjing
 * @since 2022-10-20
 */
@RestController
@RequestMapping("/manager/member-brolly")
public class CuMemberBrollyStatController extends BaseController {

    @Autowired
    private IMemberService iMemberService;
    @DubboReference
    private IMemberServiceApi iMemberServiceApi;
    @DubboReference
    private ICurrencyServiceApi currencyServiceApi;

    /**
     * @description: 查询伞下账户余额
     * @author: zhang jing
     * @date: 2023/8/25 15:53
     * @param: [umbrellaBalanceVO]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.UMBRELLA_BALANCE, method = EOperationMethod.SELECT)
    @GetMapping("/umbrella-list")
    public TableDataInfo umbrellalist(CuMemberUmbrellaBalanceVO umbrellaBalanceVO) {
        startPage();
        Integer pkCountry = SecurityUtils.getPkCountry();
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        umbrellaBalanceVO.setPkCountry(pkCountry);
        List<CuMemberUmbrellaBalanceVO> list;
        if (StringUtils.isNotEmpty(umbrellaBalanceVO.getMemberCode())) {
            R<CuMember> member = iMemberServiceApi.getMember(umbrellaBalanceVO.getMemberCode());
            umbrellaBalanceVO.setPkId(member.getData().getPkId());
            list = iMemberService.selectUmbrellaLowerBalance(umbrellaBalanceVO);
            if(CollectionUtil.isNotEmpty(list)){
                list.forEach(itm->{
                    itm.setAccount1Us(itm.getAccount1().divide(exchangeRate, 4, RoundingMode.HALF_UP));
                    itm.setAccount4Us(itm.getAccount4().divide(exchangeRate, 4, RoundingMode.HALF_UP));
                    itm.setLockAccount4Us(itm.getLockAccount4().divide(exchangeRate, 4, RoundingMode.HALF_UP));
                    itm.setAccount3Us(itm.getAccount3().divide(exchangeRate, 4, RoundingMode.HALF_UP));
                    itm.setAccount13Us(itm.getAccount13().divide(exchangeRate, 4, RoundingMode.HALF_UP));
                });
            }
        } else {
            list = new ArrayList<>();
        }
        return getDataTable(list);
    }

    /**
     * @description: 导出 查询伞下账户余额
     * @author: zhang jing
     * @date: 2023/8/25 16:10
     * @param: [response, umbrellaBalanceVO]
     * @return: void
     **/
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.UMBRELLA_BALANCE, method = EOperationMethod.EXPORT)
    @PostMapping("/export-umbrella")
    public void exportUmbrella(HttpServletResponse response, CuMemberUmbrellaBalanceVO umbrellaBalanceVO) {
        List<CuMemberUmbrellaBalanceVO> list;
        Integer pkCountry = SecurityUtils.getPkCountry();
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        umbrellaBalanceVO.setPkCountry(pkCountry);
        if (StringUtils.isNotEmpty(umbrellaBalanceVO.getMemberCode())) {
            R<CuMember> member = iMemberServiceApi.getMember(umbrellaBalanceVO.getMemberCode());
            umbrellaBalanceVO.setPkId(member.getData().getPkId());
            list = iMemberService.selectUmbrellaLowerBalance(umbrellaBalanceVO);
            if(CollectionUtil.isNotEmpty(list)){
                list.forEach(itm->{
                    itm.setAccount1Us(itm.getAccount1().divide(exchangeRate, 4, RoundingMode.HALF_UP));
                    itm.setAccount4Us(itm.getAccount4().divide(exchangeRate, 4, RoundingMode.HALF_UP));
                    itm.setLockAccount4Us(itm.getLockAccount4().divide(exchangeRate, 4, RoundingMode.HALF_UP));
                    itm.setAccount3Us(itm.getAccount3().divide(exchangeRate, 4, RoundingMode.HALF_UP));
                    itm.setAccount13Us(itm.getAccount13().divide(exchangeRate, 4, RoundingMode.HALF_UP));
                });
            }
        } else {
            list = new ArrayList<>();
        }
        ExcelUtil<CuMemberUmbrellaBalanceVO> util = new ExcelUtil<>(CuMemberUmbrellaBalanceVO.class);
        util.exportExcel(response, list, "伞下账户余额");
    }
}
