package org.dromara.payment.invoice.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.satoken.utils.LoginHelper;
import org.dromara.payment.operator.domain.bo.ReProductInvoiceBo;
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.invoice.domain.vo.MerInvoiceTypeVo;
import org.dromara.payment.invoice.domain.bo.MerInvoiceTypeBo;
import org.dromara.payment.invoice.service.IMerInvoiceTypeService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 发票类目
 * 前端访问路由地址为:/payment/invoiceType
 *
 * @author LionLi
 * @date 2024-05-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/invoiceType")
public class MerInvoiceTypeController extends BaseController {

    private final IMerInvoiceTypeService merInvoiceTypeService;

    /**
     * 查询发票类目列表(分页)
     */
    @SaCheckPermission("payment:invoiceType:list")
    @GetMapping("/list")
    public TableDataInfo<MerInvoiceTypeVo> list(MerInvoiceTypeBo bo, PageQuery pageQuery) {
        return merInvoiceTypeService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询发票类目列表
     */
    @GetMapping("/getList")
    public R<List<MerInvoiceTypeVo>> getList(MerInvoiceTypeBo bo) {
        return R.ok(merInvoiceTypeService.queryList(bo));
    }


    /**
     * 查询发票类目列表
     */
    @GetMapping("/getListInvoice")
    public R<List<Map>>  getListInvoice(MerInvoiceTypeBo bo){
        return R.ok(this.merInvoiceTypeService.getListInvoice(bo));
    }


    /**
     * 查询发票类目列表
     */
    @GetMapping("/getListKpInvoice")
    public R<List<Map>>  getListInvoice(Integer proId){
        return R.ok(this.merInvoiceTypeService.kp(proId));
    }


    /**
     * 查询商户指定产品的开票类目
     * @param proId 产品id
     * @param billIds 代发批次id(多个逗号分隔)
     * @return
     */
    @GetMapping("/types")
    public R<List<MerInvoiceTypeVo>> types(int proId,String billIds) {
        return R.ok(merInvoiceTypeService.selectList(11,billIds,proId));
    }

    /**
     * 导出发票类目列表
     */
    @SaCheckPermission("payment:invoiceType:export")
    @Log(title = "发票类目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MerInvoiceTypeBo bo, HttpServletResponse response) {
        List<MerInvoiceTypeVo> list = merInvoiceTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "发票类目", MerInvoiceTypeVo.class, response);
    }

    /**
     * 获取发票类目详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("payment:invoiceType:query")
    @GetMapping("/{id}")
    public R<MerInvoiceTypeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(merInvoiceTypeService.queryById(id));
    }

    /**
     * 新增发票类目
     */
    @SaCheckPermission("payment:invoiceType:add")
    @Log(title = "发票类目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MerInvoiceTypeBo bo) {
        return toAjax(merInvoiceTypeService.insertByBo(bo));
    }

    /**
     * 修改发票类目
     */
    @SaCheckPermission("payment:invoiceType:edit")
    @Log(title = "发票类目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MerInvoiceTypeBo bo) {
        return toAjax(merInvoiceTypeService.updateByBo(bo));
    }

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