package com.hw.dms.controller;

import com.hw.common.core.constant.DmsConstants;
import com.hw.common.core.domain.R;
import com.hw.common.core.web.controller.BaseController;
import com.hw.common.core.web.domain.AjaxResult;
import com.hw.common.core.web.page.TableDataInfo;
import com.hw.common.log.annotation.Log;
import com.hw.common.log.enums.BusinessType;
import com.hw.dms.domain.*;
import com.hw.dms.domain.vo.DmsBillsFaultInstanceScanVo;
import com.hw.dms.domain.vo.DmsBillsInspectInstanceScanVo;
import com.hw.dms.service.*;
import com.hw.system.api.RemoteFileService;
import com.hw.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:手持PDA接口
 * @ProjectName:HwMes
 * @Author:xins
 * @Date:2024-03-13 17:08
 * @Version:1.0
 */
@RestController
@RequestMapping("/mobile")
public class DmsMobileController extends BaseController {
    @Autowired
    private IDmsBillsFaultInstanceService dmsBillsFaultInstanceService;

    @Autowired
    private IDmsBillsInspectInstanceService dmsBillsInspectInstanceService;

    @Autowired
    private IDmsBaseDeviceLedgerService baseDeviceLedgerService;

    @Autowired
    private IDmsBaseOutsrcInfoService dmsBaseOutsrcInfoService;

    @Autowired
    private IDmsBillsMaintInstanceService dmsBillsMaintInstanceService;

    @Autowired
    private IDmsBillsLubeInstanceService dmsBillsLubeInstanceService;

    @Resource
    private RemoteFileService remoteFileService;

    /**
     * 根据设备编号获取设备信息
     *
     * @return
     */
    //    @RequiresPermissions("qms:checkrule:list")
    @GetMapping("/getDeviceByDeviceCode/{deviceCode}")
    public AjaxResult getDeviceByDeviceCode(@PathVariable("deviceCode") String deviceCode) {
        DmsBaseDeviceLedger dmsBaseDeviceLedger = baseDeviceLedgerService.selectDmsBaseDeviceLedgerByDeviceCode(deviceCode);
        return success(dmsBaseDeviceLedger);
    }

    /**
     * 获取外协单位列表
     *
     * @return
     */
    //    @RequiresPermissions("qms:checkrule:list")
    @GetMapping("/getOutsrcInfos")
    public AjaxResult getOutsrcInfos() {
        DmsBaseOutsrcInfo queryBaseOutsrcInfo = new DmsBaseOutsrcInfo();
        queryBaseOutsrcInfo.setOutsrcStatus(DmsConstants.DMS_BASE_OUTSRC_INFO_STATUS_NROMAL);
        List<DmsBaseOutsrcInfo> baseOutsrcInfos = dmsBaseOutsrcInfoService.selectDmsBaseOutsrcInfoList(queryBaseOutsrcInfo);
        return success(baseOutsrcInfos);
    }


    /**
     * PDA-报修
     * <p>
     * 需传入
     * 设备id          deviceId
     * 故障类别         faultType
     * 故障描述         faultDescription
     * 涉及操作         designOperations
     * 维修类型         repairType
     * 外协id          outsrcId(可不传入)
     * 图片            files
     */
//    @RequiresPermissions("qms:checkresultdetail:add")
    @Log(title = "故障报修工单", businessType = BusinessType.INSERT)
    @PostMapping("/insertFaultInstsanceActivity")
    public AjaxResult insertFaultInstsanceActivity(DmsBillsFaultInstance dmsBillsFaultInstance,
                                                   @RequestParam(value = "files") List<MultipartFile> files) {
        List<String> fileUrls = new ArrayList<>();
        for (MultipartFile file : files) {
            R<SysFile> sysFileR = remoteFileService.upload(file);
            SysFile sysFile = sysFileR.getData();
            fileUrls.add(sysFile.getUrl());
        }
        dmsBillsFaultInstance.setFileUrls(fileUrls);
        return success(dmsBillsFaultInstanceService.insertDmsBillsFaultInstance(dmsBillsFaultInstance));
    }


    /**
     * 获取故障报修工单列表
     *
     * @return
     */
    //    @RequiresPermissions("qms:checkrule:list")
    @GetMapping("/getBillsFaultInstances")
    public TableDataInfo getBillsFaultInstances() {
        startPage();
        DmsBillsFaultInstance queryBillsFaultInstance = new DmsBillsFaultInstance();
        List<DmsBillsFaultInstance> billsFaultInstances = dmsBillsFaultInstanceService.selectDmsBillsFaultInstanceJoinList(queryBillsFaultInstance);
        return getDataTable(billsFaultInstances);
    }

    /**
     * 获取故障报修工单详细信息,修改工单使用
     *
     * @return
     */
    //    @RequiresPermissions("qms:checkrule:list")
    @GetMapping("/getBillsFaultInstanceByRepairInstanceId/{repairInstanceId}")
    public AjaxResult getBillsFaultInstanceByRepairInstanceId(@PathVariable("repairInstanceId") Long repairInstanceId) {
        DmsBillsFaultInstance billsFaultInstance = dmsBillsFaultInstanceService.selectDmsBillsFaultInstanceByRepairInstanceId(repairInstanceId);
        return success(billsFaultInstance);
    }

    /**
     * 修改工单提交
     */
//    @RequiresPermissions("qms:checkresultdetail:add")
    @Log(title = "故障报修工单", businessType = BusinessType.UPDATE)
    @PostMapping("/updateFaultInstanceActivity")
    public AjaxResult updateFaultInstanceActivity(DmsFaultInstanceActivity dmsFaultInstanceActivity,
                                                  @RequestParam(value = "files") List<MultipartFile> files) {
        List<String> fileUrls = new ArrayList<>();
        for (MultipartFile file : files) {
            R<SysFile> sysFileR = remoteFileService.upload(file);
            SysFile sysFile = sysFileR.getData();
            fileUrls.add(sysFile.getUrl());
        }
        dmsFaultInstanceActivity.setFileUrls(fileUrls);
        return success(dmsBillsFaultInstanceService.updateDmsFaultInstanceActivity(dmsFaultInstanceActivity));
    }

    /**
     * 获取故障报修工单详细信息,维修使用
     *
     * @return
     */
    //    @RequiresPermissions("qms:checkrule:list")
    @GetMapping("/getBillsFaultInstance4Repair/{repairInstanceId}")
    public AjaxResult getBillsFaultInstance4Repair(@PathVariable("repairInstanceId") Long repairInstanceId) {
        DmsBillsFaultInstance billsFaultInstance = dmsBillsFaultInstanceService.getDmsBillsFaultInstance4Repair(repairInstanceId);
        return success(billsFaultInstance);
    }


    /**
     * 故障报修扫描设备验证
     *
     * @return
     */
    //    @RequiresPermissions("qms:checkrule:list")
    @GetMapping("/scanDevice4Repair")
    public AjaxResult scanDevice4Repair(@Validated DmsBillsFaultInstanceScanVo dmsBillsFaultInstanceScanVo) {
        dmsBillsFaultInstanceService.scanDevice4Repair(dmsBillsFaultInstanceScanVo);
        return success();
    }


    /**
     * 开始维修
     */
//    @RequiresPermissions("qms:checkresultdetail:add")
    @Log(title = "故障报修工单", businessType = BusinessType.START)
    @PostMapping("/startRepair")
    public AjaxResult startRepair(@RequestBody DmsFaultInstanceActivity dmsFaultInstanceActivity) {
        return success(dmsBillsFaultInstanceService.startRepair(dmsFaultInstanceActivity));
    }

    /**
     * 完成维修
     */
//    @RequiresPermissions("qms:checkresultdetail:add")
    @Log(title = "故障报修工单", businessType = BusinessType.COMPLETE)
    @PostMapping("/completeRepair")
    public AjaxResult completeRepair(@RequestBody DmsFaultInstanceActivity dmsFaultInstanceActivity) {
        return success(dmsBillsFaultInstanceService.completeRepair(dmsFaultInstanceActivity));
    }


    /**
     * 获取最新一条待巡检或巡检中的巡检或点检工单(检查类型inspectType(1巡检,2点检))
     *
     * @return
     */
    //    @RequiresPermissions("qms:checkrule:list")
    @GetMapping("/getNewestInspectInstance")
    public AjaxResult getNewestInspectInstance(DmsBillsInspectInstance dmsBillsInspectInstance) {
        DmsBillsInspectInstance newestInspectInstance = dmsBillsInspectInstanceService.getNewestBillsInspectInstance(dmsBillsInspectInstance);
        return success(newestInspectInstance);
    }


    /**
     * 扫描设备编号或者输入设备编号进行巡检验证
     * */
    //    @RequiresPermissions("qms:checkrule:list")
    @GetMapping("/scanInspectInstanceDetail")
    public AjaxResult scanInspectInstanceDetail(@Validated DmsBillsInspectInstanceScanVo dmsBillsInspectInstanceScanVo) {
        return success(dmsBillsInspectInstanceService.scanInspectInstanceDetail(dmsBillsInspectInstanceScanVo));
    }

    /**
     * 新增质检结果详情
     */
//    @RequiresPermissions("qms:checkresultdetail:add")
    @Log(title = "点巡检工单明细", businessType = BusinessType.INSERT)
    @PostMapping("/saveInspectInstanceDetail")
    public AjaxResult saveInspectInstanceDetail(@RequestBody DmsInspectInstanceDetail dmsInspectInstanceDetail) {
         return success(dmsBillsInspectInstanceService.updateDmsInspectInstanceDetail(dmsInspectInstanceDetail));

    }




    /**
     * 获取最新一条待保养或保养中的保养工单
     *
     * @return
     */
    //    @RequiresPermissions("qms:checkrule:list")
    @GetMapping("/getNewestMaintInstance")
    public AjaxResult getNewestMaintInstance(DmsBillsMaintInstance dmsBillsMaintInstance) {
        DmsBillsMaintInstance newestMaintInstance = dmsBillsMaintInstanceService.getNewestBillsMaintInstance(dmsBillsMaintInstance);
        return success(newestMaintInstance);
    }


    /**
     * 开始保养
     */
//    @RequiresPermissions("qms:checkresultdetail:add")
    @Log(title = "保养工单", businessType = BusinessType.START)
    @PostMapping("/startMaint")
    public AjaxResult startMaint(@RequestBody DmsBillsMaintDetail dmsBillsMaintDetail) {
        return success(dmsBillsMaintInstanceService.startMaint(dmsBillsMaintDetail));
    }

    /**
     * 完成保养
     */
//    @RequiresPermissions("qms:checkresultdetail:add")
    @Log(title = "保养工单", businessType = BusinessType.COMPLETE)
    @PostMapping("/completeMaint")
    public AjaxResult completeMaint(@RequestBody DmsBillsMaintDetail dmsBillsMaintDetail) {
        return success(dmsBillsMaintInstanceService.completeMaint(dmsBillsMaintDetail));
    }


    /**
     * 获取最新一条待润滑或润滑中的润滑工单
     *
     * @return
     */
    //    @RequiresPermissions("qms:checkrule:list")
    @GetMapping("/getNewestLubeInstance")
    public AjaxResult getNewestLubeInstance(DmsBillsLubeInstance dmsBillsLubeInstance) {
        DmsBillsLubeInstance newestLubeInstance = dmsBillsLubeInstanceService.getNewestBillsLubeInstance(dmsBillsLubeInstance);
        return success(newestLubeInstance);
    }

    /**
     * 开始润滑
     */
//    @RequiresPermissions("qms:checkresultdetail:add")
    @Log(title = "润滑工单", businessType = BusinessType.START)
    @PostMapping("/startLube")
    public AjaxResult startLube(@RequestBody DmsBillsLubeDetail dmsBillsLubeDetail) {

        return success(dmsBillsLubeInstanceService.startLube(dmsBillsLubeDetail));
    }

    /**
     * 完成润滑
     */
//    @RequiresPermissions("qms:checkresultdetail:add")
    @Log(title = "润滑工单", businessType = BusinessType.COMPLETE)
    @PostMapping("/completeLube")
    public AjaxResult completeLube(@RequestBody DmsBillsLubeDetail dmsBillsLubeDetail) {
        return success(dmsBillsLubeInstanceService.completeLube(dmsBillsLubeDetail));
    }
}
