package org.dromara.work.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
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.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
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.common.web.core.BaseController;
import org.dromara.work.domain.bo.TpChangePriceBo;
import org.dromara.work.domain.vo.TpChangePriceVo;
import org.dromara.work.service.ITpChangePriceService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 改价配置
 *
 * @author Maosw
 * @date 2024-08-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/changePrice")
public class TpChangePriceController extends BaseController {

    private final ITpChangePriceService tpChangePriceService;

    /**
     * 查询改价配置列表
     */
    @SaCheckPermission("work:changePrice:list")
    @GetMapping("/list")
    public TableDataInfo<TpChangePriceVo> list(TpChangePriceBo bo, PageQuery pageQuery) {
        return tpChangePriceService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出改价配置列表
     */
    @SaCheckPermission("work:changePrice:export")
    @Log(title = "改价配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpChangePriceBo bo, HttpServletResponse response) {
        List<TpChangePriceVo> list = tpChangePriceService.queryList(bo);
        ExcelUtil.exportExcel(list, "改价配置", TpChangePriceVo.class, response);
    }

    /**
     * 获取改价配置详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:changePrice:query")
    @GetMapping("/{id}")
    public R<TpChangePriceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tpChangePriceService.queryById(id));
    }

    /**
     * 新增改价配置
     */
    @SaCheckPermission("work:changePrice:add")
    @Log(title = "改价配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpChangePriceBo bo) {
        return toAjax(tpChangePriceService.insertByBo(bo));
    }

    /**
     * 修改改价配置
     */
    @SaCheckPermission("work:changePrice:edit")
    @Log(title = "改价配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpChangePriceBo bo) {
        return toAjax(tpChangePriceService.updateByBo(bo));
    }

    /**
     * 删除改价配置
     *
     * @param ids 主键串
     */
    @SaCheckPermission("work:changePrice:remove")
    @Log(title = "改价配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tpChangePriceService.deleteWithValidByIds(List.of(ids), true));
    }
}
