package com.bonus.sgzb.app.controller;

import cn.hutool.core.collection.CollUtil;
import com.bonus.sgzb.app.domain.LeaseApplyDetails;
import com.bonus.sgzb.app.domain.TmTask;
import com.bonus.sgzb.app.service.LeaseOutDetailsService;
import com.bonus.sgzb.base.api.domain.LeaseOutDetails;
import com.bonus.sgzb.base.api.domain.MaMachine;
import com.bonus.sgzb.common.core.web.controller.BaseController;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.common.core.web.page.TableDataInfo;
import com.bonus.sgzb.common.log.annotation.Log;
import com.bonus.sgzb.common.log.enums.BusinessType;
import com.bonus.sgzb.common.security.annotation.PreventRepeatSubmit;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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

/**
 * Description: 任务领料出库详情表控制器
 *
 * @Author 阮世耀
 * @Create 2023/12/17 15:48
 * @Version 1.0
 */
@RestController
@RequestMapping("/base/leaseOutDetails")
public class LeaseOutDetailsController extends BaseController {

    @Resource
    private LeaseOutDetailsService leaseOutDetailsService;

    @Log(title = "获取领料出库详情表", businessType = BusinessType.QUERY)
    @GetMapping("/getLeaseList")
    public TableDataInfo getLeaseList(@RequestParam(value = "taskId", required = false, defaultValue = "") String taskId) {
        return getDataTable(leaseOutDetailsService.selectListByParentId(taskId));
    }

    @Log(title = "领料出库列表", businessType = BusinessType.QUERY)
    @GetMapping("/getLeaseOutRecordList")
    public TableDataInfo getLeaseList(@RequestBody LeaseOutDetails record) {
        return getDataTable(leaseOutDetailsService.leaseOutRecordList(record));
    }

    /**
     * 绑定设备
     */
    @ApiOperation(value = "根据Rfid绑定设备-app")
    @PostMapping("bindMachineByRfid")
    public AjaxResult bindMachineByRfid(@RequestBody MaMachine maMachine) {
        int i = leaseOutDetailsService.bindMachineByRfid(maMachine);
        if (i == 0) {
            return AjaxResult.success("绑定成功");
        } else if (i == 1) {
            return AjaxResult.error("未查询到该设备");
        } else {
            return AjaxResult.error("该设备已绑定");
        }
    }

    /**
     * 根据二维码绑定设备
     */
    @ApiOperation(value = "根据二维码绑定设备-app")
    @PreventRepeatSubmit
    @PostMapping("bindMachineByQrCode")
    @Log(title = "APP机具设备标签二维码绑定", businessType = BusinessType.MATERIAL)
    public AjaxResult bindMachineByQrCode(@RequestBody MaMachine maMachine) {
        int i = leaseOutDetailsService.bindMachineByQrCode(maMachine);
        if (i == 0) {
            return AjaxResult.success("绑定成功");
        } else {
            return AjaxResult.error("二维码已绑定或编号已存在，绑定失败");
        }
    }


    /**
     * 根据code编码查询设备信息(app)
     *
     * @param maCode 机具编码
     * @return 设备信息
     */
    @Log(title = "根据code编码获取设备信息(app)", businessType = BusinessType.QUERY)
    @GetMapping("/getMaMachineByCode")
    public TableDataInfo getMaMachineByCode(@RequestParam(value = "maCode") String maCode) {
        return getDataTable(leaseOutDetailsService.getMaMachineByCode(maCode));
    }

    /**
     * 根据QrCode编码查询设备信息(app)
     *
     * @param qrCode 二维码编码
     * @return 设备信息
     */
    @Log(title = "根据QRcode编码获取设备信息(app)", businessType = BusinessType.QUERY)
    @GetMapping("/getMaMachineByQrCode")
    public TableDataInfo getMaMachineByQrCode(@RequestParam(value = "qrCode") String qrCode) {
        return getDataTable(leaseOutDetailsService.getMaMachineByQrCode(qrCode));
    }

    /**
     * 根据RFID编码查询设备信息(app)
     *
     * @param rfidCode RFID编码
     * @return 设备信息
     */
    @Log(title = "根据rfidCode编码获取设备信息(app)", businessType = BusinessType.QUERY)
    @GetMapping("/getMaMachineByRfidCode")
    public TableDataInfo getMaMachineByRfidCode(@RequestParam(value = "rfidCode") String rfidCode) {
        return getDataTable(leaseOutDetailsService.getMaMachineByRfidCode(rfidCode));
    }
    /**
     * 编码出库列表(web)
     *
     * @param
     * @return 设备信息
     */
    @Log(title = "根据TypeId获取编码出库列表(web)", businessType = BusinessType.QUERY)
    @GetMapping("/getDetailsByTypeId")
    public AjaxResult getDetailsByApplyId(TmTask typeId) {
        startPage();
        List<TmTask> detailsByApplyId = leaseOutDetailsService.getDetailsByApplyId(typeId);
        return AjaxResult.success(getDataTable(detailsByApplyId));
    }

    /**
     * 领料出库，对库存处理
     *
     * @param record 出库内容
     */
    @Log(title = "领料出库", businessType = BusinessType.MATERIAL)
    @PreventRepeatSubmit
    @PostMapping("/submitOut")
    public AjaxResult submitOut(@RequestBody LeaseOutDetails record) {
        return leaseOutDetailsService.submitOut(record);
    }

    /**
     * @param recordList
     * @return
     */
    @Log(title = "领料出库", businessType = BusinessType.MATERIAL)
    @PreventRepeatSubmit
    @PostMapping("/submitOutRfid")
    public AjaxResult submitOutRfid(@RequestBody List<LeaseOutDetails> recordList) {
        if (CollUtil.isEmpty(recordList)){
            return AjaxResult.error("请选择要出库的机具");
        }else {
            return leaseOutDetailsService.submitOutRfid(recordList);
        }
    }


    /**
     * 综合查询--领用记录查询
     *
     * @param bean
     */
    @Log(title = "综合查询--领用记录查询", businessType = BusinessType.UPDATE)
    @PostMapping("/leaseOutRecord")
    public AjaxResult leaseOutRecord(@RequestBody LeaseApplyDetails bean) {
        try {
            List<LeaseApplyDetails> list = leaseOutDetailsService.leaseOutRecord(bean);
            return success(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 综合查询--工程在用查询
     *
     * @param bean
     */
    @Log(title = "综合查询--工程在用查询", businessType = BusinessType.UPDATE)
    @PostMapping("/proUseRecord")
    public AjaxResult proUseRecord(@RequestBody LeaseApplyDetails bean) {
        try {
            List<LeaseApplyDetails> list = leaseOutDetailsService.proUseRecord(bean);
            return success(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
