package com.huashe.park.application.web.controller.cons;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.huashe.common.domain.AjaxResult;
import com.huashe.common.exception.ServiceException;
import com.huashe.park.common.i18n.MessageUtils;
import com.huashe.park.core.service.IConsUnitInfoService;
import com.huashe.park.core.service.IPileHoleIndexRealtimeService;
import com.huashe.park.domain.dto.cons.ConsUnitMachine;
import com.huashe.park.domain.entity.ConsUnitInfo;
import com.huashe.park.domain.entity.PileHoleIndexRealtime;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringMatchUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 施工单元Controller
 * 
 * @author ruoyi
 * @date 2025-02-14
 */
@RestController
@RequestMapping("/cons/consUnitInfo")
public class ConsUnitInfoController extends BaseController {
    @Autowired
    private IConsUnitInfoService consUnitInfoService;

    @Autowired
    private IPileHoleIndexRealtimeService pileHoleIndexRealtimeService;

    /**
     * 查询施工单元列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ConsUnitInfo consUnitInfo) {
        List<ConsUnitInfo> list = consUnitInfoService.selectConsUnitInfoList(consUnitInfo);
        return getDataTable(list);
    }

    /**
     * 导出施工单元列表
     */
    @Log(title = "施工单元", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ConsUnitInfo consUnitInfo) {
        List<ConsUnitInfo> list = consUnitInfoService.selectConsUnitInfoList(consUnitInfo);
        ExcelUtil<ConsUnitInfo> util = new ExcelUtil<ConsUnitInfo>(ConsUnitInfo.class);
        util.exportExcel(response, list, "施工单元数据");
    }

    @Log(title = "施工单元", businessType = BusinessType.EXPORT)
    @PostMapping("/import/cad")
    public void importCad(@RequestBody ConsUnitInfo consUnitInfo) {
        if (StringUtils.isEmpty(consUnitInfo.getFileId())) {
            throw new ServiceException(MessageUtils.message("common.not.null", "file id"));
        }
        consUnitInfoService.importConsUnitHole(consUnitInfo);
    }

    @Log(title = "施工单元", businessType = BusinessType.EXPORT)
    @PostMapping("/export/cad")
    public void exportCad(HttpServletResponse response, ConsUnitInfo consUnitInfo) {
        consUnitInfoService.exportConsUnitHole(consUnitInfo, response);
    }

    @PostMapping("/export/csv")
    public void exportCsv(HttpServletResponse response, ConsUnitInfo consUnitInfo) {
        consUnitInfoService.exportConsUnitRes(consUnitInfo, response);
    }

    /**
     * 获取施工单元详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(consUnitInfoService.selectConsUnitInfoById(id));
    }

    @GetMapping("/list/exclude/{id}")
    public AjaxResult excludeChild(@PathVariable(value = "id") Long consUnitId) {
        List<ConsUnitInfo> consUnitInfos = consUnitInfoService.selectConsUnitInfoList(new ConsUnitInfo());
        consUnitInfos.removeIf(d -> d.getId().intValue() == consUnitId
            || ArrayUtils.contains(StringMatchUtils.split(d.getAncestors(), ","), consUnitId + ""));
        return success(consUnitInfos);
    }

    /**
     * 新增施工单元
     */
    @Log(title = "施工单元", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ConsUnitMachine consUnitInfo) {
        return toAjax(consUnitInfoService.insertConsUnitInfo(consUnitInfo));
    }

    @Log(title = "施工单元分配施工机械", businessType = BusinessType.UPDATE)
    @PostMapping("/assign/machine")
    public AjaxResult assignMachine(@RequestBody ConsUnitMachine consUnitInfo) {
        consUnitInfoService.assignMachine(consUnitInfo);
        return success();
    }

    @Log(title = "施工单元分配施工机械", businessType = BusinessType.UPDATE)
    @PostMapping("/upload")
    @Anonymous
    public AjaxResult upload(@RequestParam MultipartFile file) {
        consUnitInfoService.upload(file);
        return success();
    }

    /**
     * 修改施工单元
     */
    @Log(title = "施工单元", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ConsUnitMachine consUnitInfo) {
        return toAjax(consUnitInfoService.updateConsUnitInfo(consUnitInfo));
    }

    /**
     * 删除施工单元
     */
    @Log(title = "施工单元", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        return toAjax(consUnitInfoService.deleteConsUnitInfoById(id));
    }

}
