package com.jeequan.jeepay.agent.ctrl.account;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jeequan.jeepay.agent.ctrl.CommonCtrl;
import com.jeequan.jeepay.core.constants.ApiCodeEnum;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.InfoAccountHistory;
import com.jeequan.jeepay.service.impl.InfoAccountHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 钱包流水
 *
 * @author xiaoyu
 * @date 2022/3/23 14:20
 */
@RestController
@RequestMapping("/api/accountHistory")
public class InfoAccountHistoryController extends CommonCtrl {

    @Autowired private InfoAccountHistoryService infoAccountHistoryService;

    /**
     * @author: xiaoyu
     * @date: 2022/3/23 14:34
     * @describe: 钱包流水
     */
    @PreAuthorize("hasAuthority('ENT_ACCOUNT_HISTORY_LIST')")
    @GetMapping
    public ApiRes list() {

        InfoAccountHistory history = getObject(InfoAccountHistory.class);
        history.setInfoId(getCurrentAgentNo());
        history.setInfoType(CS.SYS_ROLE_TYPE.AGENT);
        IPage<InfoAccountHistory> pages = infoAccountHistoryService.listByPage(getIPage(), history);

        return ApiRes.page(pages);
    }

    /**
     * @author: xiaoyu
     * @date: 2022/3/23 14:34
     * @describe: 钱包流水详情
     */
    @PreAuthorize("hasAuthority('ENT_ACCOUNT_HISTORY_DETAIL')")
    @GetMapping("/{hid}")
    public ApiRes detail(@PathVariable("hid") String hid) {
        InfoAccountHistory history = infoAccountHistoryService.getById(hid);
        if (history == null) {
            return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_SEARCH);
        }
        if (!history.getInfoId().equals(getCurrentAgentNo())) {
            return ApiRes.fail(ApiCodeEnum.SYS_PERMISSION_ERROR);
        }
        return ApiRes.ok(history);
    }

}
