package org.dromara.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;

import org.dromara.system.domain.bo.CommissionTemplateBo;
import org.dromara.system.domain.vo.CommissionTemplateVo;
import org.dromara.system.service.ICommissionTemplateService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

import org.dromara.system.domain.CommissionTemplate;
import org.dromara.system.convert.CommissionTemplateConvert;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.service.ICommissionSectionService;
import org.dromara.system.domain.vo.CommissionSectionVo;


/**
 * 分成比例模板接口
 */
@Tag(name = "分成比例模板管理")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/commission-template")
public class CommissionTemplateController {
    private final ICommissionTemplateService commissionTemplateService;
    private final CommissionTemplateConvert convert;
    private final ICommissionSectionService commissionSectionService;


    /** 分页查询 */
    @Operation(summary = "分页查询分成比例模板")
    @Tag(name = "查询分成比例模板列表")
    @PostMapping("/list")
    public R<Page<CommissionTemplateVo>> list(@RequestBody CommissionTemplateBo bo, @RequestBody Page<CommissionTemplate> page) {
        Page<CommissionTemplate> memberPage = commissionTemplateService.page(page,bo.toWrapper());
        TableDataInfo.build(memberPage);
        return R.ok(convert.toVO(memberPage));
    }

    @Operation(summary = "查询全部分成比例模板")
    @Tag(name = "查询分成比例模板列表")
    @GetMapping("/all")
    public R<List<CommissionTemplateVo>> listAll() {
        // 查询全部，不分页
        List<CommissionTemplate> list = commissionTemplateService.list();
        List<CommissionTemplateVo> voList = convert.toVO(list);
        return R.ok(voList);
    }
    /** 查询详情 */
    @Operation(summary = "查询分成比例模板详情")
    @GetMapping("/{id}")
    public R<CommissionTemplateVo> getInfo(@Parameter(description = "主键ID") @PathVariable Integer id) {
        return R.ok(commissionTemplateService.queryById(id));
    }

    /** 新增 */
    @Operation(summary = "新增分成比例模板")
    @Log(title = "分成比例模板", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Boolean> add(@Validated @RequestBody CommissionTemplateBo bo) {
        return R.ok(commissionTemplateService.insertByBo(bo));
    }

    /** 修改 */
    @Operation(summary = "修改分成比例模板")
    @Log(title = "分成比例模板", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public R<Boolean> edit(@Validated @RequestBody CommissionTemplateBo bo) {
        return R.ok(commissionTemplateService.updateByBo(bo));
    }

    /** 删除 */
    @Operation(summary = "批量删除分成比例模板")
    @Log(title = "分成比例模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Boolean> remove(@Parameter(description = "主键ID集合") @PathVariable List<Integer> ids) {
        return R.ok(commissionTemplateService.deleteWithValidByIds(ids, true));
    }

    /** 根据模板ID查询区间比例列表（POST风格） */
    @Operation(summary = "根据模板ID查询区间比例列表")
    @PostMapping("/sections")
    public R<List<CommissionSectionVo>> getSectionsByTemplateId(@RequestBody TemplateIdRequest req) {
        List<CommissionSectionVo> list = commissionSectionService.queryListByTemplateId(req.getTemplateId());
        return R.ok(list);
    }

    // DTO
    public static class TemplateIdRequest {
        private Integer templateId;
        public Integer getTemplateId() { return templateId; }
        public void setTemplateId(Integer templateId) { this.templateId = templateId; }
    }
}
