package org.dromara.elevator.controller;

import java.io.IOException;
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.elevator.domain.bo.*;
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.elevator.domain.vo.EWorkDataVo;
import org.dromara.elevator.service.IEWorkDataService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 工作数据信息;
 *
 * @author zy
 * @date 2024-07-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/workData")
public class EWorkDataController extends BaseController {

    private final IEWorkDataService eWorkDataService;

    /**
     * 查询工作数据信息;列表
     */
    @SaCheckPermission("elevator:workData:list")
    @GetMapping("/list")
    public TableDataInfo<EWorkDataVo> list(EWorkDataBo bo, PageQuery pageQuery) {
        return eWorkDataService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询超期维保数据
     */
    @SaCheckPermission("elevator:workData:list")
    @GetMapping("/overdue/list")
    public TableDataInfo<EWorkDataVo> overdueList(EWorkDataBo bo, PageQuery pageQuery) {
        return eWorkDataService.overduePageList(bo, pageQuery);
    }

    /**
     * 导出工作数据信息;列表
     */
    @SaCheckPermission("elevator:workData:export")
    @Log(title = "工作数据信息;", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EWorkDataBo bo, HttpServletResponse response) {
        List<EWorkDataVo> list = eWorkDataService.queryList(bo);
        ExcelUtil.exportExcel(list, "工作数据信息;", EWorkDataVo.class, response);
    }

    /**
     * 导出维保数据及图片
     * @param param
     * @param resp
     */
    @Log(title = "维保数据信息;", businessType = BusinessType.EXPORT)
    @PostMapping("/exportPicture")
    public void exportPicture(EWorkDataBo param, HttpServletResponse resp) throws IOException {
        eWorkDataService.exportPicture(param, resp);
    }

    /**
     * 获取工作数据信息;详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("elevator:workData:query")
    @GetMapping("/{id}")
    public R<EWorkDataVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(eWorkDataService.queryById(id));
    }

    /**
     * 新增工作数据信息;
     */
    @SaCheckPermission("elevator:workData:add")
    @Log(title = "工作数据信息;", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EWorkDataBo bo) {
        return toAjax(eWorkDataService.insertByBo(bo));
    }

    /**
     * 修改工作数据信息;
     */
    @SaCheckPermission("elevator:workData:edit")
    @Log(title = "工作数据信息;", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EWorkDataBo bo) {
        return toAjax(eWorkDataService.updateByBo(bo));
    }

    /**
     * 删除工作数据信息;
     *
     * @param ids 主键串
     */
    @SaCheckPermission("elevator:workData:remove")
    @Log(title = "工作数据信息;", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(eWorkDataService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 派发工作数据信息;
     */
    @SaCheckPermission("elevator:workData:add")
    @Log(title = "派发工作数据信息;", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/distribution")
    public R<Void> distributionWork(@Validated(AddGroup.class) @RequestBody DistributionWorkBo bo) {
        return toAjax(eWorkDataService.distributionWork(bo));
    }

    /**
     * 工作接收
     */
    @SaCheckPermission("elevator:workData:edit")
    @Log(title = "工作接收", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/receive")
    public R<Void> receive(@Validated @RequestBody WorkOperationBo bo) {
        return toAjax(eWorkDataService.receive(bo));
    }

    /**
     * 工作到场
     */
    @SaCheckPermission("elevator:workData:edit")
    @Log(title = "工作到场", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/arrive")
    public R<Void> arrive(@Validated @RequestBody WorkOperationBo bo) {
        return toAjax(eWorkDataService.arrive(bo));
    }


    /**
     * 工作表记录
     */
    @SaCheckPermission("elevator:workData:edit")
    @Log(title = "工作表记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/workRecord")
    public R<Void> workRecord(@Validated @RequestBody WorkOperationBo bo) {
        return toAjax(eWorkDataService.workRecord(bo));
    }

    /**
     * 工作签到完成
     */
    @SaCheckPermission("elevator:workData:edit")
    @Log(title = "工作签到完成", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/complete")
    public R<Void> complete(@Validated @RequestBody WorkOperationBo bo) {
        return toAjax(eWorkDataService.complete(bo));
    }

    /**
     * 工作评价
     */
    @SaCheckPermission("elevator:workData:edit")
    @Log(title = "工作表记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/evaluate")
    public R<Void> evaluate(@Validated @RequestBody WorkOperationBo bo) {
        return toAjax(eWorkDataService.evaluate(bo));
    }

    /**
     * 修改工作期望时间
     */
    @SaCheckPermission("elevator:workData:edit")
    @Log(title = "修改工作期望时间", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/updateExpectTime")
    public R<Void> updateExpectTime(@Validated @RequestBody EWorkDataBo bo) {
        return toAjax(eWorkDataService.updateExpectTime(bo));
    }

    /**
     * 工作记录附件(图片、视频等)上传
     */
    @SaCheckPermission("elevator:workData:edit")
    @Log(title = "工作记录附件(图片、视频等)上传", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/record/upload/{workId}")
    public R<Void> uploadRecord(@PathVariable() Long workId, @RequestBody List<RecordUploadBo> recordUploadBoList) {
        return toAjax(eWorkDataService.uploadRecord(workId, recordUploadBoList));
    }

    /**
     * 维修单审核
     */
    @SaCheckPermission("elevator:workData:edit")
    @Log(title = "维修单审核;", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/repair/audit")
    public R<Void> auditRepair(@Validated(AddGroup.class) @RequestBody EWorkDataBo bo) {
        return toAjax(eWorkDataService.auditRepair(bo));
    }

    /**
     * 维保结果审核
     */
    @SaCheckPermission("elevator:workData:edit")
    @Log(title = "维保结果审核;", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/maintenance/audit")
    public R<Void> auditMaintenance(@RequestBody EWorkDataBo bo) {
        return toAjax(eWorkDataService.auditMaintenance(bo));
    }

}
