package com.shkj.wms.controller.base;

import com.shkj.common.annotation.Log;
import com.shkj.common.core.controller.BaseController;
import com.shkj.common.core.domain.Result;
import com.shkj.common.core.page.TableDataInfo;
import com.shkj.common.enums.BusinessType;
import com.shkj.wms.bo.WcsPlcConnectAddBo;
import com.shkj.wms.bo.WcsPlcPropertyResetBo;
import com.shkj.wms.domain.WcsPlcConnect;
import com.shkj.wms.domain.WcsPlcProperty;
import com.shkj.wms.service.IBasePointService;
import com.shkj.wms.service.IWcsPlcConnectService;
import com.shkj.wms.service.IWcsPlcPropertyService;
import com.shkj.wms.vo.WcsPlcConnectVo;
import com.shkj.wms.common.SwitchStatusEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * PLC连接管理Controller
 *
 * @author zjx
 * @date 2023-04-21
 */
@RestController
@RequestMapping("/wcs/device/plcconnect")
public class WcsPlcConnectController extends BaseController
{

    @Autowired
    private IWcsPlcConnectService iWcsPlcConnectService;

    @Autowired
    private IWcsPlcPropertyService iWcsPlcPropertyService;

    /**
     * 查询PLC连接管理列表
     */
    @GetMapping("/list")
    public TableDataInfo<WcsPlcConnectVo> list(WcsPlcConnect wcsPlcConnect) {
        startPage();
        List<WcsPlcConnectVo> list = iWcsPlcConnectService.selectWcsPlcConnectList(wcsPlcConnect);
        return getDataTable(list);

    }

    /**
     * PLC复位 reset
     */
    @PostMapping("/plcReset")
    public  Result<Boolean> plcReset(WcsPlcPropertyResetBo wcsPlcPropertyResetBo) {
          return iWcsPlcConnectService.writePlcPoint(wcsPlcPropertyResetBo);
    }


    /**
     * 获取PLC连接管理详细信息
     */
    @GetMapping("/getWcsPlcConnectByid")
    public Result<WcsPlcConnect> getInfo(@RequestParam("id") Long id){
        return Result.ok().data(iWcsPlcConnectService.selectWcsPlcConnectById(id));
    }

    /**
     * 新增PLC连接管理
     */
    @Log(title = "PLC连接管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody WcsPlcConnectAddBo wcsPlcConnectAddBo) {
        return iWcsPlcConnectService.insertWcsPlcConnect(wcsPlcConnectAddBo);
    }

    /**
     * 修改PLC连接管理
     */
    @Log(title = "PLC连接管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Result<Boolean> edit(@RequestBody WcsPlcConnectAddBo wcsPlcConnect){
        return iWcsPlcConnectService.updateWcsPlcConnect(wcsPlcConnect);
    }

    /**
     * 删除PLC连接管理
     */
    @Log(title = "PLC连接管理", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    public Result<Boolean> remove(@RequestParam Long id){
        return iWcsPlcConnectService.deleteWcsPlcConnectById(id);
    }


    /**
     * 获取PLC连接属性详细信息
     */
    @GetMapping("/getWcsPlcPropertyByPlcId")
    public Result<WcsPlcProperty> getWcsPlcPropertyByPlcId(@RequestParam("plcId") Long plcId){

        return Result.ok().data(iWcsPlcPropertyService.getWcsPlcPropertyByPlcId(plcId));
    }


    /**
     * 切换状态
     */
    @Log(title = "切换状态" , businessType = BusinessType.UPDATE)
    @PostMapping("/switchStatus")
    public Result<Boolean> switchStatus(@Valid @RequestBody SwitchStatusEntity switchStatusEntity) {
        return iWcsPlcConnectService.switchStatus(switchStatusEntity.getId(), switchStatusEntity.getIsuse());
    }
}
