package com.zehong.web.controller.system;

import java.util.List;

import com.zehong.system.domain.*;
import com.zehong.system.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zehong.common.annotation.Log;
import com.zehong.common.core.controller.BaseController;
import com.zehong.common.core.domain.AjaxResult;
import com.zehong.common.enums.BusinessType;
import com.zehong.common.utils.poi.ExcelUtil;
import com.zehong.common.core.page.TableDataInfo;

/**
 * 任务记录Controller
 *
 * @author zehong
 * @date 2023-11-06
 */
@RestController
@RequestMapping("/system/records")
public class TTaskRecordController extends BaseController
{
    @Autowired
    private ITTaskRecordService tTaskRecordService;
    @Autowired
    private ITGasBottleTrackRecordService tGasBottleTrackRecordService;
    @Autowired
    private ITGasUserInfoService tGasUserInfoService;
    @Autowired
    private ITDeliveryRecordService tDeliveryRecordService;
    @Autowired
    private ITAirChargeRecordService tAirChargeRecordService;
    @Autowired
    private ITSafeCheckRecordService tSafeCheckRecordService;
    @Autowired
    private ITVehicleUseRecordService tVehicleUseRecordService;
    /**
     * 查询任务记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TTaskRecord tTaskRecord)
    {
        startPage();
        List<TTaskRecord> list = tTaskRecordService.selectTTaskRecordList(tTaskRecord);
        for (int i=0;i<list.size();i++){
            //查询气瓶信息条码
            if ("3".equals(list.get(i).getProcessesName())){
                TGasBottleTrackRecord tGasBottleTrackRecord = tGasBottleTrackRecordService.selectTGasBottleTrackRecordById(list.get(i).getProcessesRelationId());
                if (tGasBottleTrackRecord!=null) {
                    list.get(i).setBottleCode(tGasBottleTrackRecord.getBottleCode());
                }
            }else if ("5".equals(list.get(i).getProcessesName())){
                TGasUserInfo tGasUserInfo = tGasUserInfoService.selectTGasUserInfoById(list.get(i).getProcessesRelationId());
                if (tGasUserInfo!=null) {
                    list.get(i).setGasUserName(tGasUserInfo.getGasUserName());
                }
            }else if ("1".equals(list.get(i).getProcessesName())){
                TDeliveryRecord tDeliveryRecord = tDeliveryRecordService.selectTDeliveryRecordById(list.get(i).getProcessesRelationId());
                if (tDeliveryRecord!=null) {
                    list.get(i).setGasUserName(tDeliveryRecord.getGasUserName());
                }
            }else if ("0".equals(list.get(i).getProcessesName())){
                TAirChargeRecord tAirChargeRecord = tAirChargeRecordService.selectTAirChargeRecordById(list.get(i).getProcessesRelationId());
                if (tAirChargeRecord!=null) {
                    list.get(i).setBottleCode(tAirChargeRecord.getBottleCode());
                }
            } else if ("2".equals(list.get(i).getProcessesName())){
                TSafeCheckRecord tSafeCheckRecord = tSafeCheckRecordService.selectTSafeCheckRecordById(list.get(i).getProcessesRelationId());
                if (tSafeCheckRecord!=null) {
                    list.get(i).setGasUserName(tSafeCheckRecord.getGasUserName());
                }
            }else if ("4".equals(list.get(i).getProcessesName())){
                TVehicleUseRecord tVehicleUseRecord = tVehicleUseRecordService.selectTVehicleUseRecordById(list.get(i).getProcessesRelationId());
            if (tVehicleUseRecord!=null){
                list.get(i).setVehicleCode(tVehicleUseRecord.getVehicleCode());
            }
            }

        }
        return getDataTable(list);
    }

    /**
     * 导出任务记录列表
     */
    @Log(title = "任务记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TTaskRecord tTaskRecord)
    {
        List<TTaskRecord> list = tTaskRecordService.selectTTaskRecordList(tTaskRecord);
        ExcelUtil<TTaskRecord> util = new ExcelUtil<TTaskRecord>(TTaskRecord.class);
        return util.exportExcel(list, "任务记录数据");
    }

    /**
     * 获取任务记录详细信息
     */
    @GetMapping(value = "/{taskRecordId}")
    public AjaxResult getInfo(@PathVariable("taskRecordId") Long taskRecordId)
    {
        return AjaxResult.success(tTaskRecordService.selectTTaskRecordById(taskRecordId));
    }

    /**
     * 新增任务记录
     */
    @Log(title = "任务记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTaskRecord tTaskRecord)
    {
        return toAjax(tTaskRecordService.insertTTaskRecord(tTaskRecord));
    }

    /**
     * 修改任务记录
     */
    @Log(title = "任务记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTaskRecord tTaskRecord)
    {
        return toAjax(tTaskRecordService.updateTTaskRecord(tTaskRecord));
    }

    /**
     * 删除任务记录
     */
    @Log(title = "任务记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{taskRecordIds}")
    public AjaxResult remove(@PathVariable Long[] taskRecordIds)
    {
        return toAjax(tTaskRecordService.deleteTTaskRecordByIds(taskRecordIds));
    }
}
