package org.dromara.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.convert.Convert;
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.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.CommissionTemplate;
import org.dromara.system.domain.bo.CommissionSectionBo;
import org.dromara.system.domain.bo.CommissionTemplateBo;
import org.dromara.system.domain.dto.CommissionSectionBatchAddDTO;
import org.dromara.system.domain.vo.CommissionSectionVo;
import org.dromara.system.domain.vo.CommissionTemplateVo;
import org.dromara.system.service.ICommissionSectionService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

import org.dromara.system.domain.CommissionSection;

import org.dromara.system.service.ICommissionTemplateService;
import org.dromara.system.convert.CommissionSectionConvert;


/**
 * 分成比例区间接口
 */
@Tag(name = "分成比例区间管理")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/commission-section")
public class CommissionSectionController {
    private final ICommissionSectionService commissionSectionService;
    private final CommissionSectionConvert convert;



    /** 分页查询 */
    @Operation(summary = "查询分成比例区间列表")
    @SaCheckPermission("system:commissionsection:list")
    @PostMapping("/list")
    public R<Page<CommissionSectionVo>> list(@RequestBody CommissionSectionBo bo, @RequestBody Page<CommissionSection> page) {
        Page<CommissionSection> memberPage = commissionSectionService.page(page,bo.toWrapper());
        TableDataInfo.build(memberPage);
        return R.ok(convert.toVO(memberPage));
    }

    /** 查询详情 */
    @Operation(summary = "查询分成比例区间详情")
    @GetMapping("/{id}")
    public R<CommissionSectionVo> getInfo(@Parameter(description = "主键ID") @PathVariable Integer id) {
        return R.ok(commissionSectionService.queryById(id));
    }

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

    /** 修改 */
    @Operation(summary = "修改分成比例区间")
    @Log(title = "分成比例区间", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public R<Boolean> edit(@Validated @RequestBody CommissionSectionBatchAddDTO dto) {
        List<CommissionSectionBo> list = cn.hutool.json.JSONUtil.toList(dto.getTemplateList(), CommissionSectionBo.class);
        // 给每个bo设置templateId
        for (CommissionSectionBo bo : list) {
            bo.setTemplateId(dto.getTemplateId());
            commissionSectionService.updateByBo(bo);
        }
        return R.ok(true);
    }

    /** 批量新增或修改分成比例区间 */
    @Operation(summary = "批量新增或修改分成比例区间")
    @Log(title = "分成比例区间", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public R<Boolean> batchAddOrUpdate(@RequestBody CommissionSectionBatchAddDTO dto) {
        // 反序列化字符串为List<CommissionSectionBo>
        List<CommissionSectionBo> list = cn.hutool.json.JSONUtil.toList(dto.getTemplateList(), CommissionSectionBo.class);
        // 给每个bo设置templateId
        for (CommissionSectionBo bo : list) {
            bo.setTemplateId(dto.getTemplateId());
            // 判断是否有ID，有ID则修改，没有ID则新增
            if (bo.getId() != null) {
                commissionSectionService.updateByBo(bo);
            } else {
                commissionSectionService.insertByBo(bo);
            }
        }
        return R.ok(true);
    }

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