package com.ruoyi.web.controller.nanjing;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.nanjing.domain.TBdProductinfo;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.nanjing.domain.TSyStationparameternotes;
import com.ruoyi.nanjing.service.ITSyStationparameternotesService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 工位参数定义Controller
 * 
 * @author limy
 * @date 2021-01-27
 */
@Controller
@RequestMapping("/nanjing/StationParameterNotes")
public class TSyStationparameternotesController extends BaseController
{
    private String prefix = "nanjing/StationParameterNotes";

    @Autowired
    private ITSyStationparameternotesService tSyStationparameternotesService;

    @RequiresPermissions("nanjing:StationParameterNotes:view")
    @GetMapping()
    public String StationParameterNotes()
    {
        return prefix + "/StationParameterNotes";
    }

    /**
     * 查询工位参数定义列表
     */
    @RequiresPermissions("nanjing:StationParameterNotes:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TSyStationparameternotes tSyStationparameternotes)
    {
        startPage();
        List<TSyStationparameternotes> list = tSyStationparameternotesService.selectTSyStationparameternotesList(tSyStationparameternotes);
        return getDataTable(list);
    }

    /**
     * 导出工位参数定义列表
     */
    @RequiresPermissions("nanjing:StationParameterNotes:export")
    @Log(title = "工位参数定义", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TSyStationparameternotes tSyStationparameternotes)
    {
        List<TSyStationparameternotes> list = tSyStationparameternotesService.selectTSyStationparameternotesList(tSyStationparameternotes);
        ExcelUtil<TSyStationparameternotes> util = new ExcelUtil<TSyStationparameternotes>(TSyStationparameternotes.class);
        return util.exportExcel(list, "StationParameterNotes");
    }

    /**
     * 新增工位参数定义
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存工位参数定义
     */
    @RequiresPermissions("nanjing:StationParameterNotes:add")
    @Log(title = "工位参数定义", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TSyStationparameternotes tSyStationparameternotes)
    {
        return toAjax(tSyStationparameternotesService.insertTSyStationparameternotes(tSyStationparameternotes));
    }

    /**
     * 修改工位参数定义
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TSyStationparameternotes tSyStationparameternotes = tSyStationparameternotesService.selectTSyStationparameternotesById(id);
        mmap.put("tSyStationparameternotes", tSyStationparameternotes);
        return prefix + "/edit";
    }

    /**
     * 修改保存工位参数定义
     */
    @RequiresPermissions("nanjing:StationParameterNotes:edit")
    @Log(title = "工位参数定义", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TSyStationparameternotes tSyStationparameternotes)
    {
        return toAjax(tSyStationparameternotesService.updateTSyStationparameternotes(tSyStationparameternotes));
    }

    @RequiresPermissions("nanjing:SubStation:param")
    @PostMapping("/editParmSave")
    @ResponseBody
    public AjaxResult editParmSave(HttpServletRequest request){
//        查询实体
        int res = 0;
//        结果实体列表
        List<TSyStationparameternotes> ts = new ArrayList();
        String StationID = request.getParameter("stationID");
        String StationCode = request.getParameter("stationCode");
        String TableName = request.getParameter("tableName");
        String para = request.getParameter("paras");
        String[] paras = para.split(",");
        for (int i=1;i<=paras.length;i++){
            TSyStationparameternotes tSyStationparameternotes = new TSyStationparameternotes();
            if(!(paras[i-1].equals(""))||paras[i-1]!=null){     //参数不为空时执行
                tSyStationparameternotes.setTabelName(TableName);
                tSyStationparameternotes.setParaColumn("Para"+i);
                ts = tSyStationparameternotesService.selectTSyStationparameternotesList(tSyStationparameternotes);
                if(ts.size()==0){   //判断不存在此参数记录，则新增记录
                    tSyStationparameternotes.setStationCode(StationCode);
                    tSyStationparameternotes.setStationID(StationID);
                    tSyStationparameternotes.setUsedFlag("1");
                    tSyStationparameternotes.setParaMeaning(paras[i-1]);
                    res =tSyStationparameternotesService.insertTSyStationparameternotes(tSyStationparameternotes);
                }else{      //判断存在此参数记录，则更新
                    tSyStationparameternotes.setId(ts.get(0).getId());
                    tSyStationparameternotes.setStationCode(StationCode);
                    tSyStationparameternotes.setStationID(StationID);
                    tSyStationparameternotes.setParaMeaning(paras[i-1]);
                    res=tSyStationparameternotesService.updateTSyStationparameternotes(tSyStationparameternotes);
                }
            }
        }
        return toAjax(res);
    }
    /**
     * 删除工位参数定义
     */
    @RequiresPermissions("nanjing:StationParameterNotes:remove")
    @Log(title = "工位参数定义", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tSyStationparameternotesService.deleteTSyStationparameternotesByIds(ids));
    }

    @RequiresPermissions("nanjing:SubStation:param")
    @PostMapping("/getParamData")
    @ResponseBody
    public AjaxResult getParamData(HttpServletRequest request){
        String paraMean="";
        String stationID = request.getParameter("stationID");
        String para = request.getParameter("para");
        List<TSyStationparameternotes> tSyStationparameternotes = tSyStationparameternotesService.selectByStationID(stationID);
        for (TSyStationparameternotes sp:tSyStationparameternotes)
        {
            if(sp.getParaColumn().equals(para)){
                paraMean = sp.getParaMeaning();
                break;
            }
            else {
                paraMean="";
            }
        }
        return AjaxResult.success(paraMean);
    }
}
