package com.ruoyi.web.controller.zdsz;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.utils.file.FolderToZipUtil;
import com.ruoyi.zdsz.service.IZEngineeringNodeService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.ZEngineeringPipeJackingVo;
import com.ruoyi.zdsz.domain.bo.ZEngineeringPipeJackingBo;
import com.ruoyi.zdsz.service.IZEngineeringPipeJackingService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 顶管工程
 *
 * @author ruoyi
 * @date 2024-01-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zdsz/engineeringPipeJacking")
public class ZEngineeringPipeJackingController extends BaseController {

    private final IZEngineeringPipeJackingService iZEngineeringPipeJackingService;
    @Autowired
    private IZEngineeringNodeService nodeService;
    /**
     * 查询顶管工程列表
     */
    @SaCheckPermission("zdsz:engineeringPipeJacking:list")
    @GetMapping("/list")
    public TableDataInfo<ZEngineeringPipeJackingVo> list(ZEngineeringPipeJackingBo bo, PageQuery pageQuery) {
        return iZEngineeringPipeJackingService.queryPageList(bo, pageQuery);
    }

    /**
     *
     * @param bo
     * @return
     */
    @GetMapping("/queryListNew")
    public R<List<Map<String,Object>>> nameList(ZEngineeringPipeJackingBo bo) {
        return R.ok(iZEngineeringPipeJackingService.nameList(bo));
    }

    /**
     * 导出顶管工程列表
     */
//    @SaCheckPermission("zdsz:engineeringPipeJacking:export")
    @Log(title = "顶管工程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ZEngineeringPipeJackingBo bo, HttpServletResponse response) {
        List<ZEngineeringPipeJackingVo> list = iZEngineeringPipeJackingService.queryList(bo);
        ExcelUtil.exportExcel(list, "顶管工程", ZEngineeringPipeJackingVo.class, response);
    }

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

    /**
     * 获取顶管工程详细信息
     *
     * @param id 主键
     * @param type 节点类型
     */
//    @SaCheckPermission("zdsz:engineeringPipeJacking:query")
    @GetMapping("/{id}/{type}")
    public R<ZEngineeringPipeJackingVo> getInfo(@NotNull(message = "主键不能为空")
                                                @PathVariable("id") String id,@NotNull(message = "节点类型不能为空")
    @PathVariable("type") String type) {
        return R.ok(iZEngineeringPipeJackingService.queryByIdType(id,type));
    }


    /**
     * 新增顶管工程
     */
//    @SaCheckPermission("zdsz:engineeringPipeJacking:add")
    @Log(title = "顶管工程", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ZEngineeringPipeJackingBo bo) {
        return toAjax(iZEngineeringPipeJackingService.insertByBo(bo));
    }
    @Value("${filePath}")
    private String filePath;

    @GetMapping(value = "/downloadZip/{type}/{name}")
    public void downloadZip(@PathVariable String type,@PathVariable String name,HttpServletResponse response) {
//        File file = new File(zipPath);//创建指定目录和文件名称的文件对象
        try {
            // 创建最外层文件夹
            byte[] fileNameBytes = filePath.getBytes("UTF-8");
            // 使用UTF-8编码创建文件
            File file = new File(new String(fileNameBytes, "UTF-8"));
            if (!file.exists()) {
                file.mkdirs();
            }
            byte[] typeFileNameBytes = (file.getPath() + "/" + type).getBytes("UTF-8");
            // 使用UTF-8编码创建文件
            File typeFile = new File(new String(typeFileNameBytes, "UTF-8"));
            if (!typeFile.exists()) {
                typeFile.mkdirs();
            }
            byte[] enginNameFileNameBytes = (typeFile.getPath() + "/" + name).getBytes("UTF-8");
            // 使用UTF-8编码创建文件
            File enginNameFile = new File(new String(enginNameFileNameBytes, "UTF-8"));
            if (!enginNameFile.exists()) {
                enginNameFile.mkdirs();
            }
            System.out.println(enginNameFile.getPath());
            FolderToZipUtil.toZip(filePath+"/"+type+"/"+name,response.getOutputStream(),true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 新增顶管工程App
     */
//    @SaCheckPermission("zdsz:engineeringPipeJacking:add")
    @Log(title = "顶管工程", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/AppAdd")
    public R<Void> addApp(@Validated(AddGroup.class) @RequestBody ZEngineeringPipeJackingBo bo) {
        bo.setCreateTime(new Date());
        return toAjax(iZEngineeringPipeJackingService.insertByBoApp(bo));
    }

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

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