package org.dromara.payment.operator.controller;

import java.util.ArrayList;
import java.util.Arrays;
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.core.utils.StringUtils;
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.BusOperatorFlowsVo;
import org.dromara.payment.operator.domain.bo.BusOperatorFlowsBo;
import org.dromara.payment.operator.service.IBusOperatorFlowsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

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

    private final IBusOperatorFlowsService busOperatorFlowsService;

    /**
     * 查询运营商流水列表
     */
    @SaCheckPermission("payment:operatorFlows:list")
    @GetMapping("/list")
    public TableDataInfo<BusOperatorFlowsVo> list(BusOperatorFlowsBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setOpId(loginUser.getBusId());
        return busOperatorFlowsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出运营商流水列表
     */
    @SaCheckPermission("payment:operatorFlows:export")
    @Log(title = "运营商流水", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusOperatorFlowsBo bo, HttpServletResponse response,String fields) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setOpId(loginUser.getBusId());
        List fieldList = new ArrayList();
        if(StringUtils.isNotBlank(fields)){
            fieldList = Arrays.asList(fields.split(","));
        }
        List<BusOperatorFlowsVo> list = busOperatorFlowsService.queryList(bo);
        ExcelUtil.exportExcelInclude(list, "运营商流水", BusOperatorFlowsVo.class, response,fieldList);
    }

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

    /**
     * 新增运营商流水
     */
    @SaCheckPermission("payment:operatorFlows:add")
    @Log(title = "运营商流水", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BusOperatorFlowsBo bo) {
        return toAjax(busOperatorFlowsService.insertByBo(bo));
    }

    /**
     * 修改运营商流水
     */
    @SaCheckPermission("payment:operatorFlows:edit")
    @Log(title = "运营商流水", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BusOperatorFlowsBo bo) {
        return toAjax(busOperatorFlowsService.updateByBo(bo));
    }

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