package org.dromara.app.merchant;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzj.soopin.member.convert.AccountBillConvert;
import com.wzj.soopin.member.domain.bo.AccountBillBO;
import com.wzj.soopin.member.domain.po.AccountBill;
import com.wzj.soopin.member.domain.vo.AccountBillVO;
import com.wzj.soopin.member.domain.vo.AccountStatisticVO;
import com.wzj.soopin.member.enums.AccountTypeEnum;
import com.wzj.soopin.transaction.service.IAccountBillService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.SysTenantAccount;
import org.dromara.system.service.ISysTenantAccountService;
import org.dromara.system.service.ISysTenantService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商户端,账单接口
 *
 * @author Chopper
 * @since 2020-12-18 16:59
 */
@Slf4j
@Api(tags = "商户端,账单接口")
@RequestMapping("/app/merchant/account/")
@RestController
@RequiredArgsConstructor
public class AppMerchantBillController {

    private final IAccountBillService service;
    private final AccountBillConvert convert;

    private final ISysTenantAccountService sysTenantAccountService;
    private final ISysTenantService sysTenantService;

    private final IAccountBillService accountBillService;

    @Tag(name = "分页列表")
    @PostMapping("/bill/page")
    public R<IPage<AccountBillVO>> page(@RequestBody AccountBillBO bo) {

        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            return R.fail("用户未登录");
        }
        if(loginUser.getTenantId() == null){
            return R.fail("用户不是商户");
        }
        //取member对应的商户
        SysTenantAccount sysTenantAccount = sysTenantAccountService.getByTenantId(loginUser.getTenantId());
//        bo.setSource(AccountTypeEnum.MERCHANT.getCode());
        bo.setAccountId(sysTenantAccount.getId());
        IPage<AccountBillVO> voPage = service.selectVoPage(bo);
        return R.ok(voPage);
    }

    @Tag(name = "账户统计")
    @PostMapping("/statistic")
    public R<AccountStatisticVO> statistic() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            return R.fail("用户未登录");
        }
        if(loginUser.getTenantId() == null){
            return R.fail("用户不是商户");
        }
        //根据当前登录的member获取用户信息
        return R.ok(accountBillService.getStatistic(loginUser.getTenantId()));
    }


}
