package org.dromara.payment.channel.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.payment.channel.domain.vo.BaseCompInfo;
import org.dromara.system.api.domain.vo.RemoteDictDataVo;
import org.redisson.api.RList;
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.channel.domain.vo.BusChannelVo;
import org.dromara.payment.channel.domain.bo.BusChannelBo;
import org.dromara.payment.channel.service.IBusChannelService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 代理商
 * 前端访问路由地址为:/payment/channel
 *
 * @author LionLi
 * @date 2024-04-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/channel")
public class BusChannelController extends BaseController {

    private final IBusChannelService busChannelService;




    /**
     * 查询代理商列表
     */
    @SaCheckPermission("payment:channel:list")
    @GetMapping("/list")
    public TableDataInfo<BusChannelVo> list(BusChannelBo bo, PageQuery pageQuery) {
        return busChannelService.queryPageList(bo, pageQuery);
    }


    /**
     * 查询代理商列表
     */
    @SaCheckPermission("payment:channel:listConfig")
    @GetMapping("/listConfig")
    public TableDataInfo<BusChannelVo> listConfig(BusChannelBo bo, PageQuery pageQuery) {
        return busChannelService.queryPageListProConfig(bo, pageQuery);
    }

    /**
     * 导出代理商列表
     */
    @SaCheckPermission("payment:channel:export")
    @Log(title = "代理商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusChannelBo bo, HttpServletResponse response) {
        List<BusChannelVo> list = busChannelService.queryList(bo);
        ExcelUtil.exportExcel(list, "代理商", BusChannelVo.class, response);
    }

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



    /**
     * 获取代理商详细信息
     *
     */
    @GetMapping("/getAgInfo")
    @SaCheckPermission("payment:channel:getAgentInfo")
    @Log(title = "根据登录用户获取代理商详情", businessType = BusinessType.SELECT)
    public R<BaseCompInfo> getAgentInfo() {
        return R.ok(busChannelService.getAgentInfo());
    }

    /**
     * 新增代理商
     */
    @SaCheckPermission("payment:channel:add")
    @Log(title = "代理商", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BusChannelBo bo) {
        return toAjax(busChannelService.insertByBo(bo));
    }

    /**
     * 修改代理商
     */
    @SaCheckPermission("payment:channel:edit")
    @Log(title = "代理商", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BusChannelBo bo) {
        return toAjax(busChannelService.updateByBo(bo));
    }

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

    /**
     * 审核代理商
     */
    @Log(title = "审核代理商", businessType = BusinessType.UPDATE)
    @PostMapping("/chk")
    @SaCheckPermission("payment:channel:check")
    public R<Boolean> chk(Long chId,Long checkType,String reason,String domain) {
        return R.ok(this.busChannelService.checkChannel(chId,checkType,reason,domain));
    }


    @Log(title = "获取代理商的级别", businessType = BusinessType.UPDATE)
    @GetMapping("/getDwLevel")
    public R<List<RemoteDictDataVo>> getDwLevel(){
        return R.ok(this.busChannelService.getDwLevel());
    }


}
