package com.ruoyi.web.controller.zdsz;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.zdsz.domain.vo.ZEnginSpecificationsVo;
import com.ruoyi.zdsz.domain.bo.ZEnginSpecificationsBo;
import com.ruoyi.zdsz.service.IZEnginSpecificationsService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工程材质规格
 *
 * @author ruoyi
 * @date 2023-12-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zdsz/enginSpecifications")
public class ZEnginSpecificationsController extends BaseController {

    private final IZEnginSpecificationsService iZEnginSpecificationsService;

    /**
     * 查询工程材质规格列表
     */
//    @SaCheckPermission("zdsz:enginSpecifications:list")
    @GetMapping("/list")
    public TableDataInfo<ZEnginSpecificationsVo> list(ZEnginSpecificationsBo bo, PageQuery pageQuery) {
        return iZEnginSpecificationsService.queryPageList(bo, pageQuery);
    }
    /**
     * 材质规格下拉
     */
    @GetMapping("/getEnginSpecificationsList")
    public R getEnginSpecifications(String materId) {
        return R.ok(iZEnginSpecificationsService.getEnginSpecifications(materId));
    }
    /**
     * 导出工程材质规格列表
     */
//    @SaCheckPermission("zdsz:enginSpecifications:export")
    @Log(title = "工程材质规格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ZEnginSpecificationsBo bo, HttpServletResponse response) {
        List<ZEnginSpecificationsVo> list = iZEnginSpecificationsService.queryList(bo);
        ExcelUtil.exportExcel(list, "工程材质规格", ZEnginSpecificationsVo.class, response);
    }

    /**
     * 获取工程材质规格详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("zdsz:enginSpecifications:query")
    @GetMapping("/{id}")
    public R<ZEnginSpecificationsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iZEnginSpecificationsService.queryById(id));
    }

    /**
     * 新增工程材质规格
     */
//    @SaCheckPermission("zdsz:enginSpecifications:add")
    @Log(title = "工程材质规格", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ZEnginSpecificationsBo bo) {
        return toAjax(iZEnginSpecificationsService.insertByBo(bo));
    }

    /**
     * 修改工程材质规格
     */
//    @SaCheckPermission("zdsz:enginSpecifications:edit")
    @Log(title = "工程材质规格", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ZEnginSpecificationsBo bo) {
        return toAjax(iZEnginSpecificationsService.updateByBo(bo));
    }

    /**
     * 删除工程材质规格
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("zdsz:enginSpecifications:remove")
    @Log(title = "工程材质规格", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        Boolean b = iZEnginSpecificationsService.deleteWithValidByIds(Arrays.asList(ids), true);
        if (!b){
            return R.fail("规格已绑定用料预设,不能删除");
        }
        return R.ok("删除成功");
    }
}
