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

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 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.util.ArrayList;
import java.util.List;

/**
 * 会员伞下统计相关 前端控制器
 */
@RestController
@RequestMapping("/manager/member-brolly")
public class CuMemberBrollyStatController extends BaseController {

    @Autowired
    private IMemberService iMemberService;

    @DubboReference
    IMemberServiceApi iMemberServiceApi;

    /**
     * 查询伞下账户余额
     **/
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.UMBRELLA_BALANCE, method = EOperationMethod.SELECT)
    @GetMapping("/umbrella-list")
    public TableDataInfo umbrellalist(CuMemberUmbrellaBalanceVO umbrellaBalanceVO) {
        List<CuMemberUmbrellaBalanceVO> list = new ArrayList<>();

        if (StringUtils.isNotEmpty(umbrellaBalanceVO.getMemberCode())) {
            R<CuMember> member = iMemberServiceApi.getMember(umbrellaBalanceVO.getMemberCode());
            umbrellaBalanceVO.setPkId(member.getData().getPkId());
            umbrellaBalanceVO.setPkCountry(SecurityUtils.getPkCountry());
            startPage();
            list = iMemberService.selectUmbrellaLowerBalance(umbrellaBalanceVO);
        }
        return getDataTable(list);
    }

    /**
     * 导出 查询伞下账户余额
     **/
    @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 = new ArrayList<>();

        if (StringUtils.isNotEmpty(umbrellaBalanceVO.getMemberCode())) {
            R<CuMember> member = iMemberServiceApi.getMember(umbrellaBalanceVO.getMemberCode());
            umbrellaBalanceVO.setPkId(member.getData().getPkId());
            umbrellaBalanceVO.setPkCountry(SecurityUtils.getPkCountry());
            list = iMemberService.selectUmbrellaLowerBalance(umbrellaBalanceVO);
        }
        ExcelUtil<CuMemberUmbrellaBalanceVO> util = new ExcelUtil<>(CuMemberUmbrellaBalanceVO.class);
        util.exportExcel(response, list, "伞下账户余额");
    }
}
