package org.dromara.payment.operator.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.satoken.utils.LoginHelper;
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.operator.domain.vo.BusOperatorVo;
import org.dromara.payment.operator.domain.bo.BusOperatorBo;
import org.dromara.payment.operator.service.IBusOperatorService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 运营商
 * 前端访问路由地址为:/payment/operator
 *
 * @author LionLi
 * @date 2024-03-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/operator")
public class BusOperatorController extends BaseController {

    private final IBusOperatorService busOperatorService;

    /**
     * 查询运营商列表
     */
    @SaCheckPermission("payment:operator:list")
    @GetMapping("/list")
    public TableDataInfo<BusOperatorVo> list(BusOperatorBo bo, PageQuery pageQuery) {
        return busOperatorService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出运营商列表
     */
    @SaCheckPermission("payment:operator:export")
    @Log(title = "运营商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusOperatorBo bo, HttpServletResponse response) {
        List<BusOperatorVo> list = busOperatorService.queryList(bo);
        ExcelUtil.exportExcel(list, "运营商", BusOperatorVo.class, response);
    }

    /**
     * 获取运营商详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("payment:operator:query")
    @GetMapping("/{id}")
    public R<BusOperatorVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(busOperatorService.queryById(id));
    }

    /**
     * 新增运营商
     */
    @SaCheckPermission("payment:operator:add")
    @Log(title = "运营商", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BusOperatorBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setCreateBy(loginUser.getUserId());
        return toAjax(busOperatorService.insertByBo(bo));
    }

    /**
     * 修改运营商
     */
    @SaCheckPermission("payment:operator:edit")
    @Log(title = "运营商", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BusOperatorBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setUpdateBy(loginUser.getUserId());
        return toAjax(busOperatorService.updateByBo(bo));
    }

    /**
     * 删除运营商
     *
     * @param ids 主键串
     */
    @SaCheckPermission("payment:operator:remove")
    @Log(title = "运营商", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(busOperatorService.deleteWithValidByIds(List.of(ids), true));
    }


    /****
     * 得到运营商的银行信息
     * @return
     */
    @GetMapping("/getBank")
    @Log(title = "获取运营商的公户银行信息", businessType = BusinessType.DELETE)
    public R<BusOperatorVo>  getOperatorBankInfo(){
        BusOperatorVo resultVo = new BusOperatorVo();
        BusOperatorVo operatorVo = this.busOperatorService.queryById(LoginHelper.getBusId());
        resultVo.setBankCard(operatorVo.getBankCard());
        resultVo.setKhd(operatorVo.getKhd());
        return R.ok(resultVo);
    }


}
