package org.dromara.payment.common.controller;

import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.enums.BusRole;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.payment.bill.domain.bo.MerBillBo;
import org.dromara.system.api.model.LoginUser;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.payment.common.domain.vo.BusAccountsVo;
import org.dromara.payment.common.domain.bo.BusAccountsBo;
import org.dromara.payment.common.service.IBusAccountsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 资金账户汇总
 * 前端访问路由地址为:/payment/accounts
 *
 * @author LionLi
 * @date 2024-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/busaccounts")
public class BusAccountsController extends BaseController {

    private final IBusAccountsService busAccountsService;

    /**
     * 查询资金账户汇总列表
     */
    //@SaCheckPermission("payment:accounts:list")
    @GetMapping("/list")
    @Log(title = "查询资金账户汇总列表", businessType = BusinessType.OTHER)
    public TableDataInfo<BusAccountsVo> list(BusAccountsBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if(loginUser.getBusRole() == BusRole.SERVICE.getId()){
            bo.setSydId(loginUser.getBusId().longValue());
        }else if(loginUser.getBusRole() == BusRole.MERCHANT.getId()||
        loginUser.getBusRole() == BusRole.AGENT.getId() ||
        loginUser.getBusRole() == BusRole.OPERATOR.getId()){
            return new TableDataInfo<>();
        }
        return busAccountsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出资金账户汇总列表
     */
    @SaCheckPermission("payment:accounts:export")
    @Log(title = "资金账户汇总", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusAccountsBo bo, HttpServletResponse response) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setSydId(loginUser.getBusId().longValue());
        List<BusAccountsVo> list = busAccountsService.queryList(bo);
        ExcelUtil.exportExcel(list, "资金账户汇总", BusAccountsVo.class, response);
    }

    /**
     * 获取资金账户汇总详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("payment:accounts:query")
    @GetMapping("/{id}")
    public R<BusAccountsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(busAccountsService.queryById(id));
    }

    /**
     * 新增资金账户汇总
     */
    @SaCheckPermission("payment:accounts:add")
    @Log(title = "资金账户汇总", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BusAccountsBo bo) {
        return toAjax(busAccountsService.insertByBo(bo));
    }

    /**
     * 修改资金账户汇总
     */
    @SaCheckPermission("payment:accounts:edit")
    @Log(title = "资金账户汇总", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BusAccountsBo bo) {
        return toAjax(busAccountsService.updateByBo(bo));
    }

    /**
     * 删除资金账户汇总
     *
     * @param ids 主键串
     */
    @SaCheckPermission("payment:accounts:remove")
    @Log(title = "资金账户汇总", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(busAccountsService.deleteWithValidByIds(List.of(ids), true));
    }
}
