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

import cn.hutool.core.collection.CollUtil;
import com.hzs.common.core.annotation.ColumnAuthority;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.FinanceMsgConstants;
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.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.member.account.CuMemberTransferExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.member.account.service.ICuMemberBaseService;
import com.hzs.member.account.service.ICuMemberTransferService;
import com.hzs.member.account.vo.CuMemberTransferVO;
import com.hzs.system.sys.IMenuColumnServiceApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 会员转账表 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2022-09-06
 */
@RestController
@RequestMapping("/manager/transfer")
public class CuMemberTransferController extends BaseController {

    @Autowired
    ICuMemberTransferService cuMemberTransferService;

    @Autowired
    ITransactionCommonService iTransactionCommonService;

    @Autowired
    ICuMemberBaseService baseService;

    @DubboReference
    IMenuColumnServiceApi menuColumnServiceApi;

    /**
     * 查询列表
     *
     * @param memberTransferVO 会员转账信息
     * @return TableDataInfo
     */
    @Log(module = EOperationModule.TRANSFER_DETAIL, business = EOperationBusiness.TRANSFER_DETAIL, method = EOperationMethod.SELECT)
    @PostMapping("/list")
    @ColumnAuthority(perms = "TransferDetails")
    public TableDataInfo findTransferList(@RequestBody CuMemberTransferVO memberTransferVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        memberTransferVO.setPkCountry(pkCountry);
        startPage();
        Object[] objects = cuMemberTransferService.selectMemberTransferList(memberTransferVO);
        //处理分页
        TableDataInfo tableDataInfo = getDataTable((List<CuMemberTransferExt>) objects[0]);
        tableDataInfo.setRows((List<CuMemberTransferVO>) objects[1]);
        return tableDataInfo;
    }


    /**
     * 充值明细列表导出
     *
     * @param response         入参
     * @param memberTransferVO 查询入参
     */
    @Log(module = EOperationModule.TRANSFER_DETAIL, business = EOperationBusiness.TRANSFER_DETAIL, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CuMemberTransferVO memberTransferVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        memberTransferVO.setPkCountry(pkCountry);
        List<CuMemberTransferVO> memberTransferVOS = (List<CuMemberTransferVO>) cuMemberTransferService.selectMemberTransferList(memberTransferVO)[1];
        if (CollUtil.isNotEmpty(memberTransferVOS)) {
            for (CuMemberTransferVO CuMemberTransfer : memberTransferVOS) {
                Integer pkAccount = CuMemberTransfer.getPkAccount();
                Integer pkToAccount = CuMemberTransfer.getPkToAccount();
                CuMemberTransfer.setPkAccountVal(baseService.getAccountTranslateFromDataBase(pkAccount));
                CuMemberTransfer.setPkToAccountVal(baseService.getAccountTranslateFromDataBase(pkToAccount));
            }
        }
        ExcelUtil<CuMemberTransferVO> util = new ExcelUtil<>(CuMemberTransferVO.class,menuColumnServiceApi.queryMenuColumn("TransferDetails",SecurityUtils.getUserId()).getData());
        util.exportExcel(response, memberTransferVOS, FinanceMsgConstants.TRANSFER_DETAILS_EXPORT);
    }

}
