package org.dromara.work.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.core.domain.model.LoginUser;
import org.dromara.common.satoken.utils.LoginHelper;
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.work.domain.vo.TpDesignQuotesVo;
import org.dromara.work.domain.bo.TpDesignQuotesBo;
import org.dromara.work.service.ITpDesignQuotesService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 报价
 *
 * @author Maosw
 * @date 2025-03-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/designQuotes")
public class TpDesignQuotesController extends BaseController {

    private final ITpDesignQuotesService tpDesignQuotesService;

    /**
     * 查询报价列表
     */
    @SaCheckPermission("work:designQuotes:list")
    @GetMapping("/list")
    public TableDataInfo<TpDesignQuotesVo> list(TpDesignQuotesBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if(!LoginHelper.isSuperAdmin()){
            bo.setSid(loginUser.getUserId());
        }
        return tpDesignQuotesService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出报价列表
     */
    @SaCheckPermission("work:designQuotes:export")
    @Log(title = "报价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpDesignQuotesBo bo, HttpServletResponse response) {
        List<TpDesignQuotesVo> list = tpDesignQuotesService.queryList(bo);
        ExcelUtil.exportExcel(list, "报价", TpDesignQuotesVo.class, response);
    }

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

    /**
     * 新增报价
     */
    @SaCheckPermission("work:designQuotes:add")
    @Log(title = "报价", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpDesignQuotesBo bo) {
        return toAjax(tpDesignQuotesService.insertByBo(bo));
    }

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

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