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.payment.operator.domain.bo.AllocOperatorProductBo;
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.BusOperatorProductVo;
import org.dromara.payment.operator.domain.bo.BusOperatorProductBo;
import org.dromara.payment.operator.service.IBusOperatorProductService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

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

    private final IBusOperatorProductService busOperatorProductService;

    /**
     * 查询运营商产品列表
     */
    @SaCheckPermission("payment:operatorProduct:list")
    @GetMapping("/list")
    public TableDataInfo<BusOperatorProductVo> list(BusOperatorProductBo bo, PageQuery pageQuery) {
        return busOperatorProductService.queryPageList(bo, pageQuery);
    }



    /**
     * 查询运营商产品列表
     */
    @SaCheckPermission("payment:operatorProduct:list")
    @GetMapping("/listNew")
    public TableDataInfo<BusOperatorProductVo> listNew(BusOperatorProductBo bo, PageQuery pageQuery) {
        return busOperatorProductService.queryPageListNew(bo, pageQuery);
    }






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

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

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

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

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

    @Log(title = "给运营商分配产品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/allocOperatorProduct")
    public R<Void> allocOperatorProduct(@Validated() @RequestBody AllocOperatorProductBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setCreateBy(loginUser.getUserId());
        return toAjax(busOperatorProductService.allocOperatorProduct(bo));
    }
}
