package com.ruoyi.project.ticket.controller;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.ticket.domain.TDelayPermit;
import com.ruoyi.project.ticket.domain.TDelayPermitOther;
import com.ruoyi.project.ticket.domain.TDelayPermitVo;
import com.ruoyi.project.ticket.service.ITDelayPermitOtherService;
import com.ruoyi.project.ticket.service.ITDelayPermitService;
import com.ruoyi.project.ticket.service.ITHazardWorkPermitService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 延期票主体对象Controller
 *
 * @author ruoyi
 * @date 2021-12-23
 */
@RestController
@RequestMapping("/ticket/delaypermits")
public class TDelayPermitController extends BaseController {

    @Autowired
    private ITDelayPermitService tDelayPermitService;

    @Autowired
    private ITDelayPermitOtherService tDelayPermitOtherService;

    @Autowired
    private ITHazardWorkPermitService itHazardWorkPermitService;

    /**
     * 查询延期票主体对象列表
     */
    @PreAuthorize("@ss.hasPermi('ehs:permit:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDelayPermit tDelayPermit) {
        startPage();
        List<TDelayPermit> list = tDelayPermitService.selectTDelayPermitList(tDelayPermit);
        return getDataTable(list);
    }

    /**
     * 获取延期票主体对象详细信息
     */
    @PreAuthorize("@ss.hasPermi('ehs:permit:query')")
    @GetMapping(value = "/{vId}")
    public AjaxResult getInfo(@PathVariable("vId") String vId) {
        TDelayPermit tDelayPermit = tDelayPermitService.selectTDelayPermitById(Long.parseLong(vId));
        List<TDelayPermitOther> tDelayPermitOthers = this.tDelayPermitOtherService.selectTDelayPermitOtherByIds(Long.parseLong(vId));
        tDelayPermit.setOthers(tDelayPermitOthers);
        return AjaxResult.success(tDelayPermit);
    }

    /**
     * 新增延期票主体对象
     */
    @Log(title = "延期票主体对象", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDelayPermitVo vo) {
        // 保存延期票附属数据，返回附属ID
        TDelayPermitOther other = new TDelayPermitOther();
        BeanUtils.copyProperties(vo, other);
        this.tDelayPermitOtherService.insertTDelayPermitOther(other);
        // 保存延期票主体数据，返回延期票ID
        TDelayPermit delay = new TDelayPermit();
        BeanUtils.copyProperties(vo, delay);
        delay.setOtherId(other.getId());
        if (delay.getvId() == null) {
            delay.setvId(this.tDelayPermitService.selectVid());
        }
        this.tDelayPermitService.insertTDelayPermit(delay);
        return AjaxResult.success(delay.getvId());
    }

    /**
     * 修改延期票主体对象
     */
    @PreAuthorize("@ss.hasPermi('ehs:permit:edit')")
    @Log(title = "延期票主体对象", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDelayPermit tDelayPermit) {
        return toAjax(tDelayPermitService.updateTDelayPermit(tDelayPermit));
    }

    /**
     * 删除延期票主体对象
     */
    @PreAuthorize("@ss.hasPermi('ehs:permit:remove')")
    @Log(title = "延期票主体对象", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDelayPermitService.deleteTDelayPermitByIds(ids));
    }
}
