package com.ruoyi.web.controller.zdsz;

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

import com.ruoyi.zdsz.domain.bo.ZEngineeringInfrastructureBo;
import com.ruoyi.zdsz.domain.bo.ZEngineeringReviewBo;
import com.ruoyi.zdsz.domain.vo.ZEngineeringInfrastructureSelectVo;
import com.ruoyi.zdsz.domain.vo.ZEngineeringInfrastructureVo;
import com.ruoyi.zdsz.service.IZEngineeringInfrastructureService;
import com.ruoyi.zdsz.service.IZEngineeringReviewService;
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.common.core.page.TableDataInfo;

/**
 * 基建工程
 *
 * @author ruoyi
 * @date 2024-01-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zdsz/engineeringInfrastructure")
public class ZEngineeringInfrastructureController extends BaseController {

    private final IZEngineeringInfrastructureService iZEngineeringInfrastructureService;

    /**
     * 查询基建工程下拉
     */
//    @SaCheckPermission("zdsz:engineeringInfrastructure:list")
    @GetMapping("/select")
    public R<List<ZEngineeringInfrastructureSelectVo>> select(ZEngineeringInfrastructureBo bo) {
        try{
            return R.ok(iZEngineeringInfrastructureService.select(bo));
        }catch (Exception e){
            return R.fail();
        }
    }

    /**
     * 查询基建工程列表
     */
//    @SaCheckPermission("zdsz:engineeringInfrastructure:list")
    @GetMapping("/list")
    public TableDataInfo<ZEngineeringInfrastructureVo> list(ZEngineeringInfrastructureBo bo, PageQuery pageQuery) {
        return iZEngineeringInfrastructureService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出基建工程列表
     */
//    @SaCheckPermission("zdsz:engineeringInfrastructure:export")
    @Log(title = "基建工程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ZEngineeringInfrastructureBo bo, HttpServletResponse response) {
        List<ZEngineeringInfrastructureVo> list = iZEngineeringInfrastructureService.queryList(bo);
        ExcelUtil.exportExcel(list, "基建工程", ZEngineeringInfrastructureVo.class, response);
    }

    /**
     * 获取基建工程详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("zdsz:engineeringInfrastructure:query")
    @GetMapping("/{id}")
    public R<ZEngineeringInfrastructureVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iZEngineeringInfrastructureService.queryById(id));
    }

    /**
     * 获取基建工程历史信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("zdsz:engineeringCivil:query")
    @GetMapping("/{id}/{type}")
    public R<ZEngineeringInfrastructureVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String id, @NotNull(message = "节点类型不能为空") @PathVariable String type) {
        return R.ok(iZEngineeringInfrastructureService.queryByIdType(id,type));
    }

    /**
     * 新增基建工程
     */
//    @SaCheckPermission("zdsz:engineeringInfrastructure:add")
    @Log(title = "基建工程", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ZEngineeringInfrastructureBo bo) {
        return toAjax(iZEngineeringInfrastructureService.insertByBo(bo));
    }
    /**
     * 新增基建工程App
     */
//    @SaCheckPermission("zdsz:engineeringInfrastructure:add")
    @Log(title = "基建工程", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/AddApp")
    public R<Void> addApp(@Validated(AddGroup.class) @RequestBody ZEngineeringInfrastructureBo bo) {
        return toAjax(iZEngineeringInfrastructureService.insertByBoApp(bo));
    }

    /**
     * 基建工程审核
     */
//    @SaCheckPermission("zdsz:engineeringInfrastructure:review")
    @Log(title = "基建工程", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/review")
    public R<Void> review(@Validated(AddGroup.class) @RequestBody ZEngineeringReviewBo bo) {
        bo.setCreateTime(new Date());
        return toAjax(iZEngineeringInfrastructureService.review(bo));
    }

    /**
     * 修改基建工程
     */
//    @SaCheckPermission("zdsz:engineeringInfrastructure:edit")
    @Log(title = "基建工程", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ZEngineeringInfrastructureBo bo) {
        return toAjax(iZEngineeringInfrastructureService.updateByBo(bo));
    }

    /**
     * 删除基建工程
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("zdsz:engineeringInfrastructure:remove")
    @Log(title = "基建工程", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iZEngineeringInfrastructureService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
